Perlbal/ClientHTTPBase.pm

filter: Statementstmt 82.3% 407 / 494 Branchbran 60.6% 137 / 226 Conditioncond 61.9% 65 / 105 MC/DCmcdc 33.8% 21 / 62 Subroutinesub 86.5% 45 / 52 Podpod 33.3% 7 / 21 totaltotal 71.0% 682 / 960 CC 156 SCAR 26.9
CC
156
Cov
74%
as_string
56.0
__ANON__
32.1
event_write_reproxy_fh
28.9
CRAP 14.7
« Perlbal/TCPListener.pm ↑ Summary Perlbal/Pool.pm »
1 ######################################################################
2 # Common HTTP functionality for ClientProxy and ClientHTTP
3 # possible states:
4 # reading_headers (initial state, then follows one of two paths)
5 # wait_backend, backend_req_sent, wait_res, xfer_res, draining_res
6 # wait_stat, wait_open, xfer_disk
7 # both paths can then go into persist_wait, which means they're waiting
8 # for another request from the user
9 #
10 # Copyright 2004, Danga Interactive, Inc.
11 # Copyright 2005-2007, Six Apart, Ltd.
12  
13 package Perlbal::ClientHTTPBase;
14 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 22 ▶no warnings qw(deprecated);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 22 ▶use Sys::Syscall;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 22 ▶use base "Perlbal::Socket";
Subroutine BEGIN: called CC=1 SCAR=0.0
20 22 ▶use HTTP::Date ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 22 ▶use fields ('service', # Perlbal::Service object
Subroutine BEGIN: called CC=1 SCAR=0.0
22 'replacement_uri', # URI to send instead of the one requested; this is used
23 # to instruct _serve_request to send an index file instead
24 # of trying to serve a directory and failing
25 'scratch', # extra storage; plugins can use it if they want
26  
27 # reproxy support
28 'reproxy_file', # filename the backend told us to start opening
29 'reproxy_file_size', # size of file, once we stat() it
30 'reproxy_fh', # if needed, IO::Handle of fd
31 'reproxy_file_offset', # how much we've sent from the file.
32  
33 'post_sendfile_cb', # subref to run after we're done sendfile'ing the current file
34  
35 'requests', # number of requests this object has performed for the user
36  
37 # service selector parent
38 'selector_svc', # the original service from which we came
39 'is_ssl', # Is this socket SSL attached (restricted operations)
40 22 );
41  
42 22 ▶use Fcntl ':mode';
Subroutine BEGIN: called CC=1 SCAR=0.0
43 22 ▶use Errno qw(EPIPE ECONNRESET);
Subroutine BEGIN: called CC=1 SCAR=0.0
44 22 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 # hard-code defaults can be changed with MIME management command
47 22 our $MimeType = {qw(
48 css text/css
49 doc application/msword
50 gif image/gif
51 htm text/html
52 html text/html
53 jpg image/jpeg
54 js application/x-javascript
55 mp3 audio/mpeg
56 mpg video/mpeg
57 pdf application/pdf
58 png image/png
59 tif image/tiff
60 tiff image/tiff
61 torrent application/x-bittorrent
62 txt text/plain
63 zip application/zip
64 )};
65  
66 # ClientHTTPBase
67 77 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
68  
69 77 my Perlbal::ClientHTTPBase $self = shift;
70 77 my ($service, $sock, $selector_svc) = @_;
71 77 ▶ $self = fields::new($self) unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 5 72 77
72 77 $self->SUPER::new($sock); # init base fields
73  
74 77 $self->{service} = $service;
75 77 $self->{replacement_uri} = undef;
76 77 $self->{headers_string} = '';
77 77 $self->{requests} = 0;
78 77 $self->{scratch} = {};
79 77 $self->{selector_svc} = $selector_svc;
80 77 $self->{is_ssl} = 0;
81  
82 77 $self->state('reading_headers');
83  
84 77 $self->watch_read(1);
85 77 return $self;
86 }
87  
88 24 ▶sub new_from_base {
Subroutine new_from_base: called CC=1 SCAR=0.0
Pod undocumented
89 24 my $class = shift;
90 24 my Perlbal::ClientHTTPBase $cb = shift; # base object
91 24 Perlbal::Util::rebless($cb, $class);
92  
93 24 $cb->handle_request;
94 24 return $cb;
95 }
96  
97 73 ▶sub close {
Subroutine close: called CC=3 SCAR=11.3
Pod documented
98 73 my Perlbal::ClientHTTPBase $self = shift;
99  
100 # don't close twice
101 73 ▶ return if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 73 73
102  
103 # could contain a closure with circular reference
104 73 $self->{post_sendfile_cb} = undef;
105  
106 # close the file we were reproxying, if any
107 73 ▶ CORE::close($self->{reproxy_fh}) if $self->{reproxy_fh};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"reproxy_fh"} 0 73 73
108  
109 # now pass up the line
110 73 $self->SUPER::close(@_);
111 }
112  
113 # given the response headers we just got, and considering our request
114 # headers, determine if we should be sending keep-alive header
115 # information back to the client
116 226 ▶sub setup_keepalive {
Subroutine setup_keepalive: called CC=7 SCAR=19.5
Pod undocumented
117 226 my Perlbal::ClientHTTPBase $self = $_[0];
118 226 print "ClientHTTPBase::setup_keepalive($self)\n" if Perlbal::DEBUG >= 2;
119  
120 # now get the headers we're using
121 226 my Perlbal::HTTPHeaders $reshd = $_[1];
122 226 my Perlbal::HTTPHeaders $rqhd = $self->{req_headers};
123 226 my $override_value = $_[2];
124  
125 # for now, we enforce outgoing HTTP 1.0
126 226 $reshd->set_version("1.0");
127  
128 # if we came in via a selector service, that's whose settings
129 # we respect for persist_client
130 226 ▶ my $svc = $self->{selector_svc} || $self->{service};
Condition66%2/3
$self->{'selector_svc'} || $self->{'service'}
l!l&&r!l&&!r
651610
MC/DC0%0/2
$self->{'selector_svc'} || $self->{'service'}
$self->{'selector_svc'} $self->{'service'}
Truth table66%2/3
A || B
A= $self->{'selector_svc'}B= $self->{'service'}
ABresult
1X1
011
000
131 226 ▶ my $persist_client = $svc->{persist_client} || 0;
Condition100%2/2
$svc->{'persist_client'} || 0
l!l
2224
MC/DC100%1/1
$svc->{'persist_client'} || 0
$svc->{'persist_client'}
Truth table100%2/2
A
A= $svc->{'persist_client'}
Aresult
11
00
132 226 ▶ $persist_client = $override_value if defined $override_value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $override_value 1 225 226
133 226 print " service's persist_client = $persist_client\n" if Perlbal::DEBUG >= 3;
134  
135 # do keep alive if they sent content-length or it's a head request
136 226 ▶ my $do_keepalive = $persist_client && $rqhd->req_keep_alive($reshd);
Condition100%3/3
$persist_client && $rqhd->req_keep_alive($reshd)
!ll&&!rl&&r
556165
MC/DC100%2/2
$persist_client && $rqhd->req_keep_alive($reshd)
$persist_client $rqhd->req_keep_alive($reshd)
Truth table100%3/3
A && B
A= $persist_clientB= $rqhd->req_keep_alive($reshd)
ABresult
0X0
100
111
137 226 ▶ if ($do_keepalive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_keepalive) { } 165 61 226
138 165 print " doing keep-alive to client\n" if Perlbal::DEBUG >= 3;
139 165 my $timeout = $self->{service}->{persist_client_idle_timeout};
140 165 $reshd->header('Connection', 'keep-alive');
141 165 ▶ $reshd->header('Keep-Alive', $timeout ? "timeout=$timeout, max=100" : undef);
Branch: 1 branch
BranchTrueFalseTotal
$timeout ? : 165 0 165
142 } else {
143 61 print " doing connection: close\n" if Perlbal::DEBUG >= 3;
144 # FIXME: we don't necessarily want to set connection to close,
145 # but really set a space-separated list of tokens which are
146 # specific to the connection. "close" and "keep-alive" are
147 # just special.
148 61 $reshd->header('Connection', 'close');
149 61 $reshd->header('Keep-Alive', undef);
150 }
151 }
152  
153 # overridden here from Perlbal::Socket to use the service value
154 5 ▶sub max_idle_time {
Subroutine max_idle_time: called CC=2 SCAR=7.6
Pod undocumented
155 5 my Perlbal::ClientHTTPBase $self = shift;
156 5 ▶ if ($self->state eq 'persist_wait') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state eq 'persist_wait') { } 0 5 5
157 0 return $self->{service}->{persist_client_idle_timeout};
158 } else {
159 5 return $self->{service}->{idle_timeout};
160 }
161 }
162  
163 # Called when this client is entering a persist_wait state, but before we are returned to base.
164 37 ▶sub persist_wait {
Subroutine persist_wait: called CC=1 SCAR=0.0
Pod undocumented
165
166 }
167  
168 # called when we've finished writing everything to a client and we need
169 # to reset our state for another request. returns 1 to mean that we should
170 # support persistence, 0 means we're discarding this connection.
171 226 ▶sub http_response_sent {
Subroutine http_response_sent: called CC=7 SCAR=19.5
Pod undocumented
172 226 my Perlbal::ClientHTTPBase $self = $_[0];
173  
174 # close if we're supposed to
175 226 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"res_headers"} && $self->{"res_headers"}->res_keep_alive($self->{"req_headers"}) or $self->{"do_die"}) 61 165 226
Condition66%4/6
not defined $self->{"res_headers"} && $self->{"res_headers"}->res_keep_alive($self->{"req_headers"}) or $self->{"do_die"}
l!l&&r!l&&!r
610165
defined $self->{'res_headers'} && $self->{'res_headers'}->res_keep_alive($self->{'req_headers'})
!ll&&!rl&&r
061165
MC/DC33%1/3
not defined $self->{"res_headers"} && $self->{"res_headers"}->res_keep_alive($self->{"req_headers"}) or $self->{"do_die"}
defined $self->{'res_headers'} $self->{'res_headers'}->res_keep_alive($self->{'req_headers'}) $self->{"do_die"}
Truth table50%2/4
not(A && B) or C
A= defined $self->{'res_headers'}B= $self->{'res_headers'}->res_keep_alive($self->{'req_headers'})C= $self->{"do_die"}
ABCresult
0XX1
10X1
1111
1100
176 ! defined $self->{res_headers} ||
177 ! $self->{res_headers}->res_keep_alive($self->{req_headers}) ||
178 $self->{do_die}
179 )
180 {
181 # do a final read so we don't have unread_data_waiting and RST
182 # the connection. IE and others send an extra \r\n after POSTs
183 61 my $dummy = $self->read(5);
184  
185 # close if we have no response headers or they say to close
186 61 $self->close("no_keep_alive");
187 61 return 0;
188 }
189  
190 # if they just did a POST, set the flag that says we might expect
191 # an unadvertised \r\n coming from some browsers. Old Netscape
192 # 4.x did this on all POSTs, and Firefox/Safari do it on
193 # XmlHttpRequest POSTs.
194 165 ▶ if ($self->{req_headers}->request_method eq "POST") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"req_headers"}->request_method eq "POST") 75 90 165
195 75 $self->{ditch_leading_rn} = 1;
196 }
197  
198 # now since we're doing persistence, uncork so the last packet goes.
199 # we will recork when we're processing a new request.
200 165 $self->tcp_cork(0);
201  
202 # reset state
203 165 $self->{replacement_uri} = undef;
204 165 $self->{headers_string} = '';
205 165 $self->{req_headers} = undef;
206 165 $self->{res_headers} = undef;
207 165 $self->{reproxy_fh} = undef;
208 165 $self->{reproxy_file} = undef;
209 165 $self->{reproxy_file_size} = 0;
210 165 $self->{reproxy_file_offset} = 0;
211 165 $self->{read_buf} = [];
212 165 $self->{read_ahead} = 0;
213 165 $self->{read_size} = 0;
214 165 $self->{scratch} = {};
215 165 $self->{post_sendfile_cb} = undef;
216 165 $self->state('persist_wait');
217  
218 165 $self->persist_wait;
219  
220 165 ▶ if (my $selector_svc = $self->{selector_svc}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $selector_svc = $self->{"selector_svc"}) 64 101 165
221 64 ▶ if (! $selector_svc->run_hook('return_to_base', $self)){
Branch: 1 branch
BranchTrueFalseTotal
unless ($selector_svc->run_hook("return_to_base", $self)) 64 0 64
222 64 $selector_svc->return_to_base($self);
223 }
224 }
225  
226 # NOTE: because we only speak 1.0 to clients they can't have
227 # pipeline in a read that we haven't read yet.
228 165 $self->watch_read(1);
229 165 $self->watch_write(0);
230 165 return 1;
231 }
232  
233 46 ▶sub reproxy_fh {
Subroutine reproxy_fh: called CC=6 SCAR=25.6
Pod undocumented
234 46 my Perlbal::ClientHTTPBase $self = shift;
235  
236 # setter
237 46 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 46 0 46
238 46 my ($fh, $size) = @_;
239 46 $self->state('xfer_disk');
240 46 $self->{reproxy_fh} = $fh;
241 46 $self->{reproxy_file_offset} = 0;
242 46 $self->{reproxy_file_size} = $size;
243  
244 my $is_ssl_webserver = ( $self->{service}->{listener}->{sslopts} &&
245 46 ▶ ( $self->{service}->{role} eq 'web_server') );
Condition33%1/3
$self->{'service'}{'listener'}{'sslopts'} && $self->{'service'}{'role'} eq 'web_server'
!ll&&!rl&&r
4600
MC/DC0%0/2
$self->{'service'}{'listener'}{'sslopts'} && $self->{'service'}{'role'} eq 'web_server'
$self->{'service'}{'listener'}{'sslopts'} $self->{'service'}{'role'} eq 'web_server'
Truth table33%1/3
A && B
A= $self->{'service'}{'listener'}{'sslopts'}B= $self->{'service'}{'role'} eq 'web_server'
ABresult
0X0
100
111
246  
247 46 ▶ unless ($is_ssl_webserver) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_ssl_webserver) { } 0 46 46
248 # call hook that we're reproxying a file
249 46 ▶ return $fh if $self->{service}->run_hook("start_send_file", $self);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"service"}->run_hook("start_send_file", $self) 0 46 46
250 # turn on writes (the hook might not have wanted us to)
251 46 $self->watch_write(1);
252 46 return $fh;
253 } else { # use aio_read for ssl webserver instead of sendfile
254  
255 0 ▶ print "webserver in ssl mode, sendfile disabled!\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Perlbal::DEBUG >= 3 0 0 0
256 if $Perlbal::DEBUG >= 3;
257  
258 # turn off writes
259 0 $self->watch_write(0);
260 #create filehandle for reading
261 0 my $data = '';
262 0 ▶ Perlbal::AIO::aio_read($self->reproxy_fh, 0, 2048, $data, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
263 # got data? undef is error
264 0 ▶ return $self->_simple_response(500) unless $_[0] > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] > 0 0 0 0
265  
266 # seek into the file now so sendfile starts further in
267 0 my $ld = length $data;
268 0 sysseek($self->{reproxy_fh}, $ld, &POSIX::SEEK_SET);
269 0 $self->{reproxy_file_offset} = $ld;
270 # reenable writes after we get data
271 0 $self->tcp_cork(1); # by setting reproxy_file_offset above,
272 # it won't cork, so we cork it
273 0 $self->write($data);
274 0 $self->watch_write(1);
275 0 });
276 0 return 1;
277 }
278 }
279  
280 0 return $self->{reproxy_fh};
281 }
282  
283 90 ▶sub event_read {
Subroutine event_read: called CC=2 SCAR=7.0
Pod documented
284 90 my Perlbal::ClientHTTPBase $self = shift;
285  
286 90 $self->{alive_time} = $Perlbal::tick_time;
287  
288 # see if we have headers?
289 die "Shouldn't get here! This is an abstract base class, pretty much, except in the case of the 'selector' role."
290 90 ▶ if $self->{req_headers};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"req_headers"} 0 90 90
291  
292 90 my $hd = $self->read_request_headers;
293 90 $self->handle_request;
294 }
295  
296 114 ▶sub handle_request {
Subroutine handle_request: called CC=4 SCAR=14.1
Pod undocumented
297 114 my Perlbal::ClientHTTPBase $self = shift;
298 114 my Perlbal::HTTPHeaders $hd = $self->{req_headers};
299  
300 114 ▶ return unless $hd;
Branch: 1 branch
BranchTrueFalseTotal
unless $hd 25 89 114
301  
302 89 $self->check_req_headers;
303  
304 89 ▶ return if $self->{service}->run_hook('start_http_request', $self);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"service"}->run_hook("start_http_request", $self) 0 89 89
305  
306 # we must stop watching for events now, otherwise if there's
307 # PUT/POST overflow, it'll be sent to ClientHTTPBase, which can't
308 # handle it yet. must wait for the selector (which has as much
309 # time as it wants) to route as to our subclass, which can then
310 # re-enable reads.
311 89 $self->watch_read(0);
312  
313 89 ▶ my $select = sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
314 # now that we have headers, it's time to tell the selector
315 # plugin that it's time for it to select which real service to
316 # use
317 89 my $selector = $self->{'service'}->selector();
318 89 ▶ return $self->_simple_response(500, "No service selector configured.")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $selector eq "CODE" 0 89 89
319 unless ref $selector eq "CODE";
320 89 $selector->($self);
321 89 };
322  
323 89 my $svc = $self->{'service'};
324 89 ▶ if ($svc->{latency}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($svc->{'latency'}) { } 0 89 89
325 0 Danga::Socket->AddTimer($svc->{latency} / 1000, $select);
326 } else {
327 89 $select->();
328 }
329 }
330  
331 46 ▶sub reproxy_file_done {
Subroutine reproxy_file_done: called CC=3 SCAR=11.0
Pod undocumented
332 46 my Perlbal::ClientHTTPBase $self = shift;
333 46 ▶ return if $self->{service}->run_hook('reproxy_fh_finished', $self);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"service"}->run_hook("reproxy_fh_finished", $self) 0 46 46
334 # close the sendfile fd
335 46 CORE::close($self->{reproxy_fh});
336 46 $self->{reproxy_fh} = undef;
337 46 ▶ if (my $cb = $self->{post_sendfile_cb}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $self->{'post_sendfile_cb'}) { } 8 38 46
338 8 $cb->();
339 } else {
340 38 $self->http_response_sent;
341 }
342 }
343  
344 # client is ready for more of its file. so sendfile some more to it.
345 # (called by event_write when we're actually in this mode)
346 46 ▶sub event_write_reproxy_fh {
Subroutine event_write_reproxy_fh: called CC=8 SCAR=33.6
Pod undocumented
347 46 my Perlbal::ClientHTTPBase $self = shift;
348  
349 46 my $remain = $self->{reproxy_file_size} - $self->{reproxy_file_offset};
350 46 ▶ $self->tcp_cork(1) if $self->{reproxy_file_offset} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"reproxy_file_offset"} == 0 46 0 46
351 46 $self->watch_write(0);
352  
353 46 ▶ if ($self->{is_ssl}) { # SSL (sendfile does not do SSL)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"is_ssl"}) 0 46 46
354 0 ▶ return if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 0 0
355 0 ▶ if ($remain <= 0) { #done
Branch: 1 branch
BranchTrueFalseTotal
if ($remain <= 0) 0 0 0
356 0 print "REPROXY SSL done\n" if Perlbal::DEBUG >= 2;
357 0 $self->reproxy_file_done;
358 0 return;
359 }
360 # queue up next read
361 0 Perlbal::AIO::set_file_for_channel($self->{reproxy_file});
362 0 ▶ my $len = $remain > 4096 ? 4096 : $remain; # buffer size
Branch: 1 branch
BranchTrueFalseTotal
$remain > 4096 ? : 0 0 0
363 0 my $buffer = '';
364 Perlbal::AIO::aio_read(
365 $self->{reproxy_fh},
366 $self->{reproxy_file_offset},
367 $len,
368 $buffer,
369 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
370 0 ▶ return if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 0 0
371 # we have buffer to send
372 0 my $rv = $_[0]; # arg is result of sysread
373 0 ▶ if (!defined($rv) || $rv <= 0) { # read error
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rv or $rv <= 0) 0 0 0
Condition0%0/3
not defined $rv or $rv <= 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $rv or $rv <= 0
not defined $rv $rv <= 0
Truth table0%0/3
not(A) or B
A= not defined $rvB= $rv <= 0
ABresult
1X1
011
000
374 # sysseek is called after sysread so $! not valid
375 0 $self->close('sysread_error');
376 0 print STDERR "Error w/ reproxy sysread\n";
377 0 return;
378 }
379 0 $self->{reproxy_file_offset} += $rv;
380 0 $self->tcp_cork(1); # by setting reproxy_file_offset above,
381 # it won't cork, so we cork it
382 0 $self->write($buffer); # start socket send
383 0 $self->watch_write(1);
384 }
385 0 );
386 0 return;
387 }
388  
389 # cap at 128k sendfiles
390 46 ▶ my $to_send = $remain > 128 * 1024 ? 128 * 1024 : $remain;
Branch: 1 branch
BranchTrueFalseTotal
$remain > 131072 ? : 0 46 46
391  
392 46 ▶ my $postread = sub {
Subroutine __ANON__: called CC=6 SCAR=25.3
393 46 ▶ return if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 46 46
394  
395 my $sent = Perlbal::Socket::sendfile($self->{fd},
396 46 fileno($self->{reproxy_fh}),
397 $to_send);
398 #warn "to_send = $to_send, sent = $sent\n";
399 46 print "REPROXY Sent: $sent\n" if Perlbal::DEBUG >= 2;
400  
401 46 ▶ if ($sent < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sent < 0) 0 46 46
402 0 ▶ return $self->close("epipe") if $! == EPIPE;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 32 0 0 0
403 0 ▶ return $self->close("connreset") if $! == ECONNRESET;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 104 0 0 0
404 0 print STDERR "Error w/ sendfile: $!\n";
405 0 $self->close('sendfile_error');
406 0 return;
407 }
408 46 $self->{reproxy_file_offset} += $sent;
409  
410 46 ▶ if ($sent >= $remain) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sent >= $remain) { } 46 0 46
411 46 $self->reproxy_file_done;
412 } else {
413 0 $self->watch_write(1);
414 }
415 46 };
416  
417 # TODO: way to bypass readahead and go straight to sendfile for common/hot/recent files.
418 # something like:
419 # if ($hot) { $postread->(); return ; }
420  
421 46 ▶ if ($to_send < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to_send < 0) 0 46 46
422 0 Perlbal::log('warning', "tried to readahead negative bytes. filesize=$self->{reproxy_file_size}, offset=$self->{reproxy_file_offset}");
423 # this code, doing sendfile, will fail gracefully with return
424 # code, not 'die', and we'll close with sendfile_error:
425 0 $postread->();
426 0 return;
427 }
428  
429 46 Perlbal::AIO::set_file_for_channel($self->{reproxy_file});
430 Perlbal::AIO::aio_readahead($self->{reproxy_fh},
431 $self->{reproxy_file_offset},
432 46 $to_send, $postread);
433 }
434  
435 46 ▶sub event_write {
Subroutine event_write: called CC=3 SCAR=14.2
Pod documented
436 46 my Perlbal::ClientHTTPBase $self = shift;
437  
438 # Any HTTP client is considered alive if it's writable.
439 # if it's not writable for 30 seconds, we kill it.
440 # subclasses can decide what's appropriate for timeout.
441 46 $self->{alive_time} = $Perlbal::tick_time;
442  
443 # if we're sending a filehandle, go do some more sendfile:
444 46 ▶ if ($self->{reproxy_fh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"reproxy_fh"}) 46 0 46
445 46 $self->event_write_reproxy_fh;
446 46 return;
447 }
448  
449 # otherwise just kick-start our write buffer.
450 0 ▶ if ($self->write(undef)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->write(undef)) 0 0 0
451 # we've written all data in the queue, so stop waiting for
452 # write notifications:
453 0 print "All writing done to $self\n" if Perlbal::DEBUG >= 2;
454 0 $self->watch_write(0);
455 }
456 }
457  
458 # this gets called when a "web" service is serving a file locally.
459 55 ▶sub _serve_request {
Subroutine _serve_request: called CC=9 SCAR=22.5
460 55 my Perlbal::ClientHTTPBase $self = shift;
461 55 my Perlbal::HTTPHeaders $hd = shift;
462  
463 55 my $rm = $hd->request_method;
464 55 ▶ unless ($rm eq "HEAD" || $rm eq "GET") {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rm eq "HEAD" or $rm eq "GET") 0 55 55
Condition66%2/3
$rm eq "HEAD" or $rm eq "GET"
l!l&&r!l&&!r
1540
MC/DC0%0/2
$rm eq "HEAD" or $rm eq "GET"
$rm eq "HEAD" $rm eq "GET"
Truth table66%2/3
A or B
A= $rm eq "HEAD"B= $rm eq "GET"
ABresult
1X1
011
000
465 0 return $self->_simple_response(403, "Unimplemented method");
466 }
467  
468 55 ▶ my $uri = Perlbal::Util::durl($self->{replacement_uri} || $hd->request_uri);
Condition66%2/3
$self->{'replacement_uri'} || $hd->request_uri
l!l&&r!l&&!r
1540
MC/DC0%0/2
$self->{'replacement_uri'} || $hd->request_uri
$self->{'replacement_uri'} $hd->request_uri
Truth table66%2/3
A || B
A= $self->{'replacement_uri'}B= $hd->request_uri
ABresult
1X1
011
000
469 55 my Perlbal::Service $svc = $self->{service};
470  
471 # start_serve_request hook
472 55 ▶ return 1 if $self->{service}->run_hook('start_serve_request', $self, \$uri);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"service"}->run_hook("start_serve_request", $self, \$uri) 0 55 55
473  
474 # don't allow directory traversal
475 55 ▶ if ($uri =~ m!/\.\./! || $uri !~ m!^/!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ m[/\.\./] or not $uri =~ m[^/]) 1 54 55
Condition66%2/3
$uri =~ m[/\.\./] or not $uri =~ m[^/]
l!l&&r!l&&!r
1054
MC/DC50%1/2
$uri =~ m[/\.\./] or not $uri =~ m[^/]
$uri =~ m[/\.\./] not $uri =~ m[^/]
Truth table66%2/3
A or not(B)
A= $uri =~ m[/\.\./]B= not $uri =~ m[^/]
ABresult
1X1
011
000
476 1 return $self->_simple_response(403, "Bogus URL");
477 }
478  
479 # double question mark means to serve multiple files, comma separated after the
480 # questions. the uri part before the question mark is the relative base directory
481 # TODO: only do this if $uri has ?? and the service also allows it. otherwise
482 # we don't want to mess with anybody's meaning of '??' on the backend service
483 54 ▶ return $self->_serve_request_multiple($hd, $uri) if $uri =~ /\?\?/;
Branch: 1 branch
BranchTrueFalseTotal
if $uri =~ /\?\?/ 9 45 54
484  
485 # chop off the query string
486 45 $uri =~ s/\?.*//;
487  
488 return $self->_simple_response(500, "Docroot unconfigured")
489 45 ▶ unless $svc->{docroot};
Branch: 1 branch
BranchTrueFalseTotal
unless $svc->{"docroot"} 0 45 45
490  
491 45 my $file = $svc->{docroot} . $uri;
492  
493 # update state, since we're now waiting on stat
494 45 $self->state('wait_stat');
495  
496 45 ▶ Perlbal::AIO::aio_stat($file, sub {
Subroutine __ANON__: called CC=28 SCAR=34.7
497 # client's gone anyway
498 45 ▶ return if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 45 45
499 45 ▶ unless (-e _) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e _) 2 43 45
500 2 ▶ return if $self->{service}->run_hook('static_get_poststat_file_missing', $self);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"service"}->run_hook("static_get_poststat_file_missing", $self) 0 2 2
501 2 return $self->_simple_response(404);
502 }
503  
504 43 my $mtime = (stat(_))[9];
505 43 my $lastmod = HTTP::Date::time2str($mtime);
506 43 ▶ my $ims = $hd->header("If-Modified-Since") || "";
Condition100%2/2
$hd->header('If-Modified-Since') || ''
l!l
142
MC/DC100%1/1
$hd->header('If-Modified-Since') || ''
$hd->header('If-Modified-Since')
Truth table100%2/2
A
A= $hd->header('If-Modified-Since')
Aresult
11
00
507  
508 # IE sends a request header like "If-Modified-Since: <DATE>; length=<length>"
509 # so we have to remove the length bit before comparing it with our date.
510 # then we save the length to compare later.
511 43 my $ims_len;
512 43 ▶ if ($ims && $ims =~ s/; length=(\d+)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ims and $ims =~ s/; length=(\d+)//) 0 43 43
Condition66%2/3
$ims and $ims =~ s/; length=(\d+)//
!ll&&!rl&&r
4210
MC/DC0%0/2
$ims and $ims =~ s/; length=(\d+)//
$ims $ims =~ s/; length=(\d+)//
Truth table66%2/3
A and B
A= $imsB= $ims =~ s/; length=(\d+)//
ABresult
0X0
100
111
513 0 $ims_len = $1;
514 }
515  
516 43 ▶ my $not_mod = $ims eq $lastmod && -f _;
Condition66%2/3
$ims eq $lastmod && -f _
!ll&&!rl&&r
4201
MC/DC50%1/2
$ims eq $lastmod && -f _
$ims eq $lastmod -f _
Truth table66%2/3
A && B
A= $ims eq $lastmodB= -f _
ABresult
0X0
100
111
517  
518 43 my $res;
519 43 my $not_satisfiable = 0;
520 43 ▶ my $size = -s _ if -f _;
Branch: 1 branch
BranchTrueFalseTotal
if -f _ 36 7 43
521  
522 # extra protection for IE, since it's offering the info anyway. (see above)
523 43 ▶ $not_mod = 0 if $ims_len && $ims_len != $size;
Branch: 1 branch
BranchTrueFalseTotal
if $ims_len and $ims_len != $size 0 43 43
Condition33%1/3
$ims_len and $ims_len != $size
!ll&&!rl&&r
4300
MC/DC0%0/2
$ims_len and $ims_len != $size
$ims_len $ims_len != $size
Truth table33%1/3
A and B
A= $ims_lenB= $ims_len != $size
ABresult
0X0
100
111
524  
525 43 my ($status, $range_start, $range_end) = $hd->range($size);
526  
527 43 ▶ if ($not_mod) {
Branch: 3 branches
BranchTrueFalseTotal
if ($not_mod) { } 1 42 43
elsif ($status == 416) { } 4 38 42
elsif ($status == 206) { } 4 34 38
528 1 $res = $self->{res_headers} = Perlbal::HTTPHeaders->new_response(304);
529 } elsif ($status == 416) {
530 4 $res = $self->{res_headers} = Perlbal::HTTPHeaders->new_response(416);
531 4 ▶ $res->header("Content-Range", $size ? "bytes */$size" : "*");
Branch: 1 branch
BranchTrueFalseTotal
$size ? : 4 0 4
532 4 $res->header("Content-Length", 0);
533 4 $not_satisfiable = 1;
534 } elsif ($status == 206) {
535 # partial content
536 4 $res = $self->{res_headers} = Perlbal::HTTPHeaders->new_response(206);
537 } else {
538 34 ▶ return if $self->{service}->run_hook('static_get_poststat_pre_send', $self, $mtime);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"service"}->run_hook("static_get_poststat_pre_send", $self, $mtime) 0 34 34
539 34 $res = $self->{res_headers} = Perlbal::HTTPHeaders->new_response(200);
540 }
541  
542 # now set whether this is keep-alive or not
543 43 $res->header("Date", HTTP::Date::time2str());
544 43 ▶ $res->header("Server", "Perlbal") if $self->{service}{server_tokens};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"service"}{"server_tokens"} 42 1 43
545 43 $res->header("Last-Modified", $lastmod);
546  
547 43 ▶ if (-f _) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f _) { } 36 7 43
elsif (-d _) { } 7 0 7
548 # advertise that we support byte range requests
549 36 $res->header("Accept-Ranges", "bytes");
550  
551 36 ▶ unless ($not_mod || $not_satisfiable) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($not_mod or $not_satisfiable) 31 5 36
Condition100%3/3
$not_mod or $not_satisfiable
l!l&&r!l&&!r
1431
MC/DC100%2/2
$not_mod or $not_satisfiable
$not_mod $not_satisfiable
Truth table100%3/3
A or B
A= $not_modB= $not_satisfiable
ABresult
1X1
011
000
552 31 my ($ext) = ($file =~ /\.(\w+)$/);
553 $res->header("Content-Type",
554 31 ▶ (defined $ext && exists $MimeType->{$ext}) ? $MimeType->{$ext} : "text/plain");
Branch: 1 branch
BranchTrueFalseTotal
defined $ext && exists $Perlbal::ClientHTTPBase::MimeType->{$ext} ? : 31 0 31
Condition33%1/3
defined $ext and exists $Perlbal::ClientHTTPBase::MimeType->{$ext}
!ll&&!rl&&r
0031
MC/DC0%0/2
defined $ext and exists $Perlbal::ClientHTTPBase::MimeType->{$ext}
defined $ext exists $Perlbal::ClientHTTPBase::MimeType->{$ext}
Truth table33%1/3
A and B
A= defined $extB= exists $Perlbal::ClientHTTPBase::MimeType->{$ext}
ABresult
0X0
100
111
555  
556 31 ▶ unless ($status == 206) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 206) { } 4 27 31
557 27 $res->header("Content-Length", $size);
558 } else {
559 4 $res->header("Content-Range", "bytes $range_start-$range_end/$size");
560 4 $res->header("Content-Length", $range_end - $range_start + 1);
561 }
562 }
563  
564 # has to happen after content-length is set to work:
565 36 $self->setup_keepalive($res);
566  
567 36 ▶ return if $self->{service}->run_hook('modify_response_headers', $self);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"service"}->run_hook("modify_response_headers", $self) 0 36 36
568  
569 36 ▶ if ($rm eq "HEAD" || $not_mod || $not_satisfiable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rm eq "HEAD" or $not_mod or $not_satisfiable) 6 30 36
Condition100%6/6
$rm eq "HEAD" or $not_mod or $not_satisfiable
l!l&&r!l&&!r
2430
$rm eq "HEAD" or $not_mod
l!l&&r!l&&!r
1134
MC/DC100%3/3
$rm eq "HEAD" or $not_mod or $not_satisfiable
$rm eq "HEAD" $not_mod $not_satisfiable
Truth table100%4/4
A or B or C
A= $rm eq "HEAD"B= $not_modC= $not_satisfiable
ABCresult
1XX1
01X1
0011
0000
570 # we can return already, since we know the size
571 6 $self->tcp_cork(1);
572 6 $self->state('xfer_resp');
573 6 $self->write($res->to_string_ref);
574 6 ▶ $self->write(sub { $self->http_response_sent; });
Subroutine __ANON__: called CC=1 SCAR=0.0
575 6 return;
576 }
577  
578 # state update
579 30 $self->state('wait_open');
580  
581 30 ▶ Perlbal::AIO::aio_open($file, 0, 0, sub {
Subroutine __ANON__: called CC=5 SCAR=17.6
582 30 my $rp_fh = shift;
583  
584 # if client's gone, just close filehandle and abort
585 30 ▶ if ($self->{closed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"closed"}) 0 30 30
586 0 ▶ CORE::close($rp_fh) if $rp_fh;
Branch: 1 branch
BranchTrueFalseTotal
if $rp_fh 0 0 0
587 0 return;
588 }
589  
590 # handle errors
591 30 ▶ if (! $rp_fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rp_fh) 0 30 30
592 # couldn't open the file we had already successfully stat'ed.
593 # FIXME: do 500 vs. 404 vs whatever based on $!
594 0 return $self->close('aio_open_failure');
595 }
596  
597 30 $self->state('xfer_disk');
598 30 $self->tcp_cork(1); # cork writes to self
599 30 $self->write($res->to_string_ref);
600  
601 # seek if partial content
602 30 ▶ if ($status == 206) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 206) 4 26 30
603 4 sysseek($rp_fh, $range_start, &POSIX::SEEK_SET);
604 4 $size = $range_end - $range_start + 1;
605 }
606  
607 30 $self->{reproxy_file} = $file;
608 30 $self->reproxy_fh($rp_fh, $size);
609 30 });
610  
611 } elsif (-d _) {
612 7 $self->try_index_files($hd, $res, $uri);
613 }
614 45 });
615 }
616  
617 9 ▶sub _serve_request_multiple {
Subroutine _serve_request_multiple: called CC=7 SCAR=19.5
618 9 my Perlbal::ClientHTTPBase $self = shift;
619 9 my ($hd, $uri) = @_;
620  
621 9 my @multiple_files;
622 my %statinfo; # file -> [ stat fields ]
623  
624 # double question mark means to serve multiple files, comma
625 # separated after the questions. the uri part before the question
626 # mark is the relative base directory
627 9 my ($base, $list) = ($uri =~ /(.+)\?\?(.+)/);
628  
629 9 ▶ unless ($base =~ m!/$!) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($base =~ m[/$]) 1 8 9
630 1 return $self->_simple_response(500, "Base directory (before ??) must end in slash.")
631 }
632  
633 # and remove any trailing ?.+ on the list, so you can do things like cache busting
634 # with a ?v=<modtime> at the end of a list of files.
635 8 $list =~ s/\?.+//;
636  
637 8 my Perlbal::Service $svc = $self->{service};
638 return $self->_simple_response(500, "Docroot unconfigured")
639 8 ▶ unless $svc->{docroot};
Branch: 1 branch
BranchTrueFalseTotal
unless $svc->{"docroot"} 0 8 8
640  
641 8 @multiple_files = split(/,/, $list);
642  
643 8 ▶ return $self->_simple_response(403, "Multiple file serving isn't enabled") unless $svc->{enable_concatenate_get};
Branch: 1 branch
BranchTrueFalseTotal
unless $svc->{"enable_concatenate_get"} 1 7 8
644 7 ▶ return $self->_simple_response(403, "Too many files requested") if @multiple_files > 100;
Branch: 1 branch
BranchTrueFalseTotal
if @multiple_files > 100 0 7 7
645 7 ▶ return $self->_simple_response(403, "Bogus filenames") if grep { m!(?:\A|/)\.\./! } @multiple_files;
Branch: 1 branch
BranchTrueFalseTotal
if grep {m[(?:\A|/)\.\./];} @multiple_files 0 7 7
646  
647 7 my $remain = @multiple_files + 1; # 1 for the base directory
648 7 my $dirbase = $svc->{docroot} . $base;
649 7 foreach my $file ('', @multiple_files) {
650 21 ▶ Perlbal::AIO::aio_stat("$dirbase$file", sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
651 21 $remain--;
652 21 ▶ $statinfo{$file} = $! ? [] : [ stat(_) ];
Branch: 1 branch
BranchTrueFalseTotal
$! ? : 4 17 21
653 21 ▶ return if $remain || $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $remain or $self->{"closed"} 14 7 21
Condition66%2/3
$remain or $self->{"closed"}
l!l&&r!l&&!r
1407
MC/DC50%1/2
$remain or $self->{"closed"}
$remain $self->{"closed"}
Truth table66%2/3
A or B
A= $remainB= $self->{"closed"}
ABresult
1X1
011
000
654 7 $self->_serve_request_multiple_poststat($hd, $dirbase, \@multiple_files, \%statinfo);
655 21 });
656 }
657 }
658  
659 7 ▶sub _serve_request_multiple_poststat {
Subroutine _serve_request_multiple_poststat: called CC=22 SCAR=31.8
660 7 my Perlbal::ClientHTTPBase $self = shift;
661 7 my ($hd, $basedir, $filelist, $stats) = @_;
662  
663 # base directory must be a directory
664 7 ▶ unless (S_ISDIR($stats->{''}[2] || 0)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (S_ISDIR($stats->{""}[2] || 0)) 1 6 7
Condition100%2/2
$stats->{''}[2] || 0
l!l
61
MC/DC100%1/1
$stats->{''}[2] || 0
$stats->{''}[2]
Truth table100%2/2
A
A= $stats->{''}[2]
Aresult
11
00
665 1 return $self->_simple_response(404, "Base directory not a directory");
666 }
667  
668 # files must all exist
669 6 my $sum_length = 0;
670 6 my $most_recent_mod = 0;
671 6 my $mime; # undef until set, or defaults to text/plain later
672 6 foreach my $f (@$filelist) {
673 12 my $stat = $stats->{$f};
674 12 ▶ unless (S_ISREG($stat->[2] || 0)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (S_ISREG($stat->[2] || 0)) 1 11 12
Condition100%2/2
$stat->[2] || 0
l!l
111
MC/DC100%1/1
$stat->[2] || 0
$stat->[2]
Truth table100%2/2
A
A= $stat->[2]
Aresult
11
00
675 1 ▶ return if $self->{service}->run_hook('concat_get_poststat_file_missing', $self);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"service"}->run_hook("concat_get_poststat_file_missing", $self) 0 1 1
676 1 return $self->_simple_response(404, "One or more file does not exist");
677 }
678 11 ▶ if (!$mime && $f =~ /\.(\w+)$/ && $MimeType->{$1}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $mime and $f =~ /\.(\w+)$/ and $Perlbal::ClientHTTPBase::MimeType->{$1}) 6 5 11
Condition66%4/6
not $mime and $f =~ /\.(\w+)$/ and $Perlbal::ClientHTTPBase::MimeType->{$1}
!ll&&!rl&&r
506
not $mime and $f =~ /\.(\w+)$/
!ll&&!rl&&r
506
MC/DC33%1/3
not $mime and $f =~ /\.(\w+)$/ and $Perlbal::ClientHTTPBase::MimeType->{$1}
not $mime $f =~ /\.(\w+)$/ $Perlbal::ClientHTTPBase::MimeType->{$1}
Truth table50%2/4
not(A) and B and C
A= not $mimeB= $f =~ /\.(\w+)$/C= $Perlbal::ClientHTTPBase::MimeType->{$1}
ABCresult
0XX0
10X0
1100
1111
679 6 $mime = $MimeType->{$1};
680 }
681 11 $sum_length += $stat->[7];
682 11 ▶ $most_recent_mod = $stat->[9] if
Branch: 1 branch
BranchTrueFalseTotal
if $stat->[9] > $most_recent_mod 6 5 11
683 $stat->[9] >$most_recent_mod;
684 }
685 5 ▶ $mime ||= 'text/plain';
Condition50%1/2
$mime ||= "text/plain"
l!l
50
MC/DC0%0/1
$mime ||= "text/plain"
$mime
Truth table50%1/2
A
A= $mime
Aresult
11
00
686  
687 5 my $lastmod = HTTP::Date::time2str($most_recent_mod);
688 5 ▶ my $ims = $hd->header("If-Modified-Since") || "";
Condition100%2/2
$hd->header('If-Modified-Since') || ''
l!l
14
MC/DC100%1/1
$hd->header('If-Modified-Since') || ''
$hd->header('If-Modified-Since')
Truth table100%2/2
A
A= $hd->header('If-Modified-Since')
Aresult
11
00
689  
690 # IE sends a request header like "If-Modified-Since: <DATE>; length=<length>"
691 # so we have to remove the length bit before comparing it with our date.
692 # then we save the length to compare later.
693 5 my $ims_len;
694 5 ▶ if ($ims && $ims =~ s/; length=(\d+)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ims and $ims =~ s/; length=(\d+)//) 0 5 5
Condition66%2/3
$ims and $ims =~ s/; length=(\d+)//
!ll&&!rl&&r
410
MC/DC0%0/2
$ims and $ims =~ s/; length=(\d+)//
$ims $ims =~ s/; length=(\d+)//
Truth table66%2/3
A and B
A= $imsB= $ims =~ s/; length=(\d+)//
ABresult
0X0
100
111
695 0 $ims_len = $1;
696 }
697  
698 # What is -f _ doing here? don't we detect the existence of all files above in the loop?
699 5 ▶ my $not_mod = $ims eq $lastmod && -f _;
Condition66%2/3
$ims eq $lastmod && -f _
!ll&&!rl&&r
401
MC/DC50%1/2
$ims eq $lastmod && -f _
$ims eq $lastmod -f _
Truth table66%2/3
A && B
A= $ims eq $lastmodB= -f _
ABresult
0X0
100
111
700  
701 5 my $res;
702 5 ▶ if ($not_mod) {
Branch: 1 branch
BranchTrueFalseTotal
if ($not_mod) { } 1 4 5
703 1 $res = $self->{res_headers} = Perlbal::HTTPHeaders->new_response(304);
704 } else {
705 4 ▶ return if $self->{service}->run_hook('concat_get_poststat_pre_send', $self, $most_recent_mod);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"service"}->run_hook("concat_get_poststat_pre_send", $self, $most_recent_mod) 0 4 4
706 4 $res = $self->{res_headers} = Perlbal::HTTPHeaders->new_response(200);
707 4 $res->header("Content-Length", $sum_length);
708 }
709  
710 5 $res->header("Date", HTTP::Date::time2str());
711 5 ▶ $res->header("Server", "Perlbal") if $self->{service}{server_tokens};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"service"}{"server_tokens"} 5 0 5
712 5 $res->header("Last-Modified", $lastmod);
713 5 $res->header("Content-Type", $mime);
714 # has to happen after content-length is set to work:
715 5 $self->setup_keepalive($res);
716 5 ▶ return if $self->{service}->run_hook('modify_response_headers', $self);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"service"}->run_hook("modify_response_headers", $self) 0 5 5
717  
718 5 ▶ if ($hd->request_method eq "HEAD" || $not_mod) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hd->request_method eq "HEAD" or $not_mod) 1 4 5
Condition66%2/3
$hd->request_method eq "HEAD" or $not_mod
l!l&&r!l&&!r
014
MC/DC50%1/2
$hd->request_method eq "HEAD" or $not_mod
$hd->request_method eq "HEAD" $not_mod
Truth table66%2/3
A or B
A= $hd->request_method eq "HEAD"B= $not_mod
ABresult
1X1
011
000
719 # we can return already, since we know the size
720 1 $self->tcp_cork(1);
721 1 $self->state('xfer_resp');
722 1 $self->write($res->to_string_ref);
723 1 ▶ $self->write(sub { $self->http_response_sent; });
Subroutine __ANON__: called CC=1 SCAR=0.0
724 1 return;
725 }
726  
727 4 $self->tcp_cork(1); # cork writes to self
728 4 $self->write($res->to_string_ref);
729 4 $self->state('wait_open');
730  
731 # gotta send all files, one by one...
732 4 my @remain = @$filelist;
733 12 ▶ $self->{post_sendfile_cb} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
734 12 ▶ unless (@remain) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@remain) 4 8 12
735 4 ▶ $self->write(sub { $self->http_response_sent; });
Subroutine __ANON__: called CC=1 SCAR=0.0
736 4 return;
737 }
738  
739 8 my $file = shift @remain;
740 8 my $fullfile = "$basedir$file";
741 8 my $size = $stats->{$file}[7];
742  
743 8 ▶ Perlbal::AIO::aio_open($fullfile, 0, 0, sub {
Subroutine __ANON__: called CC=4 SCAR=17.9
744 8 my $rp_fh = shift;
745  
746 # if client's gone, just close filehandle and abort
747 8 ▶ if ($self->{closed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"closed"}) 0 8 8
748 0 ▶ CORE::close($rp_fh) if $rp_fh;
Branch: 1 branch
BranchTrueFalseTotal
if $rp_fh 0 0 0
749 0 return;
750 }
751  
752 # handle errors
753 8 ▶ if (! $rp_fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rp_fh) 0 8 8
754 # couldn't open the file we had already successfully stat'ed.
755 # FIXME: do 500 vs. 404 vs whatever based on $!
756 0 return $self->close('aio_open_failure');
757 }
758  
759 8 $self->{reproxy_file} = $file;
760 8 $self->reproxy_fh($rp_fh, $size);
761 8 });
762 4 };
763 4 $self->{post_sendfile_cb}->();
764 }
765  
766 313 ▶sub check_req_headers {
Subroutine check_req_headers: called CC=3 SCAR=14.2
Pod undocumented
767 313 my Perlbal::ClientHTTPBase $self = shift;
768 313 my Perlbal::HTTPHeaders $hds = $self->{req_headers};
769  
770 313 ▶ if ($self->{service}->trusted_ip($self->peer_ip_string)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"service"}->trusted_ip($self->peer_ip_string)) 0 313 313
771 0 ▶ my @ips = split /,\s*/, ($hds->header("X-Forwarded-For") || '');
Condition0%0/2
$hds->header('X-Forwarded-For') || ''
l!l
00
MC/DC0%0/1
$hds->header('X-Forwarded-For') || ''
$hds->header('X-Forwarded-For')
Truth table0%0/2
A
A= $hds->header('X-Forwarded-For')
Aresult
11
00
772  
773 # This list may be empty, and that's OK, in that case we should unset the
774 # observed_ip_string, so no matter what we'll use the 0th element, whether
775 # it happens to be an ip string, or undef.
776 0 $self->observed_ip_string($ips[0]);
777 }
778  
779 313 return;
780 }
781  
782 15 ▶sub try_index_files {
Subroutine try_index_files: called CC=7 SCAR=19.5
Pod undocumented
783 15 my Perlbal::ClientHTTPBase $self = shift;
784 15 my ($hd, $res, $uri, $filepos) = @_;
785  
786 # make sure this starts at 0 initially, and fail if it's past the end
787 15 ▶ $filepos ||= 0;
Condition100%2/2
$filepos ||= 0
l!l
87
MC/DC100%1/1
$filepos ||= 0
$filepos
Truth table100%2/2
A
A= $filepos
Aresult
11
00
788 15 ▶ if ($filepos >= scalar(@{$self->{service}->{index_files} || []})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filepos >= scalar @{$self->{"service"}{"index_files"} or [];}) 6 9 15
Condition50%1/2
$self->{'service'}{'index_files'} || []
l!l
150
MC/DC0%0/1
$self->{'service'}{'index_files'} || []
$self->{'service'}{'index_files'}
Truth table50%1/2
A
A= $self->{'service'}{'index_files'}
Aresult
11
00
789 6 ▶ unless ($self->{service}->{dirindexing}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"service"}{"dirindexing"}) 5 1 6
790 # just inform them that listing is disabled
791 5 $self->_simple_response(200, "Directory listing disabled");
792 5 return;
793 }
794  
795 # ensure uri has one and only one trailing slash for better URLs
796 1 $uri =~ s!/*$!/!;
797  
798 # open the directory and create an index
799 1 my $body = "<html><body>";
800 1 my $file = $self->{service}->{docroot} . $uri;
801  
802 1 $res->header("Content-Type", "text/html");
803 1 opendir(D, $file);
804 1 foreach my $de (sort readdir(D)) {
805 4 ▶ if (-d "$file/$de") {
Branch: 1 branch
BranchTrueFalseTotal
if (-d "$file/$de") { } 2 2 4
806 2 $body .= "<b><a href='$uri$de/'>$de</a></b><br />\n";
807 } else {
808 2 $body .= "<a href='$uri$de'>$de</a><br />\n";
809 }
810 }
811 1 closedir(D);
812  
813 1 $body .= "</body></html>";
814 1 $res->header("Content-Length", length($body));
815 1 $self->setup_keepalive($res);
816  
817 1 $self->state('xfer_resp');
818 1 $self->tcp_cork(1); # cork writes to self
819 1 $self->write($res->to_string_ref);
820 1 $self->write(\$body);
821 1 ▶ $self->write(sub { $self->http_response_sent; });
Subroutine __ANON__: called CC=1 SCAR=0.0
822 1 return;
823 }
824  
825 # construct the file path we need to check
826 9 my $file = $self->{service}->{index_files}->[$filepos];
827 9 my $fullpath = $self->{service}->{docroot} . $uri . '/' . $file;
828  
829 # now see if it exists
830 9 ▶ Perlbal::AIO::aio_stat($fullpath, sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
831 9 ▶ return if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 9 9
832 9 ▶ return $self->try_index_files($hd, $res, $uri, $filepos + 1) unless -f _;
Branch: 1 branch
BranchTrueFalseTotal
unless -f _ 8 1 9
833  
834 # at this point the file exists, so we just want to serve it
835 1 $self->{replacement_uri} = $uri . '/' . $file;
836 1 return $self->_serve_request($hd);
837 9 });
838 }
839  
840 34 ▶sub _simple_response {
Subroutine _simple_response: called CC=9 SCAR=22.1
841 34 my Perlbal::ClientHTTPBase $self = shift;
842 34 my ($code, $msg) = @_; # or bodyref
843  
844 34 my $res = $self->{res_headers} = Perlbal::HTTPHeaders->new_response($code);
845  
846 34 my $body;
847 34 ▶ if ($code != 204 && $code != 304) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code != 204 and $code != 304) 33 1 34
Condition66%2/3
$code != 204 and $code != 304
!ll&&!rl&&r
1033
MC/DC50%1/2
$code != 204 and $code != 304
$code != 204 $code != 304
Truth table66%2/3
A and B
A= $code != 204B= $code != 304
ABresult
0X0
100
111
848 33 $res->header("Content-Type", "text/html");
849 33 my $en = $res->http_code_english;
850 33 ▶ $body = "<h1>$code" . ($en ? " - $en" : "") . "</h1>\n";
Branch: 1 branch
BranchTrueFalseTotal
$en ? : 32 1 33
851 33 ▶ $body .= $msg if $msg;
Branch: 1 branch
BranchTrueFalseTotal
if $msg 17 16 33
852 33 $res->header('Content-Length', length($body));
853 }
854  
855 34 ▶ $res->header('Server', 'Perlbal') if $self->{service}{server_tokens};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"service"}{"server_tokens"} 33 1 34
856  
857 34 $self->setup_keepalive($res);
858  
859 34 $self->state('xfer_resp');
860 34 $self->tcp_cork(1); # cork writes to self
861 34 $self->write($res->to_string_ref);
862 34 ▶ if (defined $body) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body) 33 1 34
863 33 ▶ unless ($self->{req_headers} && $self->{req_headers}->request_method eq 'HEAD') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"req_headers"} and $self->{"req_headers"}->request_method eq "HEAD") 33 0 33
Condition33%1/3
$self->{"req_headers"} and $self->{"req_headers"}->request_method eq "HEAD"
!ll&&!rl&&r
0330
MC/DC0%0/2
$self->{"req_headers"} and $self->{"req_headers"}->request_method eq "HEAD"
$self->{"req_headers"} $self->{"req_headers"}->request_method eq "HEAD"
Truth table33%1/3
A and B
A= $self->{"req_headers"}B= $self->{"req_headers"}->request_method eq "HEAD"
ABresult
0X0
100
111
864 # don't write body for head requests
865 33 $self->write(\$body);
866 }
867 }
868 34 ▶ $self->write(sub { $self->http_response_sent; });
Subroutine __ANON__: called CC=1 SCAR=0.0
869 34 return 1;
870 }
871  
872  
873 17 ▶sub send_response {
Subroutine send_response: called CC=1 SCAR=0.0
Pod undocumented
874 17 my Perlbal::ClientHTTPBase $self = shift;
875  
876 17 $self->watch_read(0);
877 17 $self->watch_write(1);
878 17 return $self->_simple_response(@_);
879 }
880  
881 1 ▶sub send_full_response {
Subroutine send_full_response: called CC=12 SCAR=28.0
Pod undocumented
882 1 my Perlbal::ClientHTTPBase $self = shift;
883 1 my $code = shift;
884 1 ▶ my $headers = shift || [];
Condition50%1/2
shift() || []
l!l
10
MC/DC0%0/1
shift() || []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
885 1 ▶ my $bref = ref($_[0]) eq 'SCALAR' ? shift : \shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'SCALAR' ? : 0 1 1
886 1 ▶ my $options = shift || {};
Condition50%1/2
shift() || {}
l!l
01
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
887  
888 1 my $res = $self->{res_headers} = Perlbal::HTTPHeaders->new_response($code);
889  
890 1 ▶ while (@$headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$headers) 2 1 3
891 2 my ($name, $value) = splice @$headers, 0, 2;
892 2 $res->header($name, $value);
893 }
894  
895 1 ▶ if ($code == 204 || $code == 304) {
Branch: 2 branches
BranchTrueFalseTotal
if ($code == 204 or $code == 304) { } 0 1 1
elsif (defined $$bref) { } 1 0 1
Condition33%1/3
$code == 204 or $code == 304
l!l&&r!l&&!r
001
MC/DC0%0/2
$code == 204 or $code == 304
$code == 204 $code == 304
Truth table33%1/3
A or B
A= $code == 204B= $code == 304
ABresult
1X1
011
000
896 0 $res->header('Content-Length', undef);
897 0 $bref = \undef;
898 } elsif (defined $$bref) {
899 1 $res->header('Content-Length', length($$bref));
900 }
901  
902 1 ▶ $res->header('Server', 'Perlbal') if $self->{service}{server_tokens};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"service"}{"server_tokens"} 1 0 1
903 # $res->header('Date', # We should do this
904  
905 1 $self->setup_keepalive($res, $options->{persist_client});
906  
907 1 $self->state('xfer_resp');
908 1 $self->tcp_cork(1); # cork writes to self
909 1 $self->write($res->to_string_ref);
910  
911 1 ▶ if (defined $$bref && $self->{req_headers} && $self->{req_headers}->request_method ne 'HEAD') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$bref and $self->{"req_headers"} and $self->{"req_headers"}->request_method ne "HEAD") 1 0 1
Condition33%2/6
defined $$bref and $self->{"req_headers"} and $self->{"req_headers"}->request_method ne "HEAD"
!ll&&!rl&&r
001
defined $$bref and $self->{"req_headers"}
!ll&&!rl&&r
001
MC/DC0%0/3
defined $$bref and $self->{"req_headers"} and $self->{"req_headers"}->request_method ne "HEAD"
defined $$bref $self->{"req_headers"} $self->{"req_headers"}->request_method ne "HEAD"
Truth table25%1/4
A and B and C
A= defined $$brefB= $self->{"req_headers"}C= $self->{"req_headers"}->request_method ne "HEAD"
ABCresult
0XX0
10X0
1100
1111
912 # don't write body for head requests
913 1 $self->write($bref);
914 }
915  
916 1 ▶ $self->write(sub { $self->http_response_sent; });
Subroutine __ANON__: called CC=1 SCAR=0.0
917 1 return 1;
918 }
919  
920 # method that sends a 500 to the user but logs it and any extra information
921 # we have about the error in question
922 0 ▶sub system_error {
Subroutine system_error: not called CC=1 SCAR=6.9
Pod undocumented
923 0 my Perlbal::ClientHTTPBase $self = shift;
924 0 my ($msg, $info) = @_;
925  
926 # log to syslog
927 0 Perlbal::log('warning', "system error: $msg ($info)");
928  
929 # and return a 500
930 0 return $self->send_response(500, $msg);
931 }
932  
933 0 ▶sub event_err { my $self = shift; $self->close('error'); }
Subroutine event_err: not called CC=1 SCAR=6.9
Pod documented
934 0 ▶sub event_hup { my $self = shift; $self->close('hup'); }
Subroutine event_hup: not called CC=1 SCAR=6.9
Pod documented
935  
936 0 ▶sub _sock_port {
Subroutine _sock_port: not called CC=2 SCAR=17.9
937 0 my $name = $_[0];
938 0 my $port = eval { (Socket::sockaddr_in($name))[0] };
939 0 ▶ return $port unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
940 # fallback to IPv6:
941 0 return (Socket6::unpack_sockaddr_in($name))[0];
942 }
943  
944 0 ▶sub as_string {
Subroutine as_string: not called CC=7 SCAR=40.3
Pod documented
945 0 my Perlbal::ClientHTTPBase $self = shift;
946  
947 0 my $ret = $self->SUPER::as_string;
948 0 ▶ my $name = $self->{sock} ? getsockname($self->{sock}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'sock'} ? : 0 0 0
949 0 ▶ my $lport = $name ? _sock_port($name) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 0 0 0
950 0 my $observed = $self->observed_ip_string;
951 0 ▶ $ret .= ": localport=$lport" if $lport;
Branch: 1 branch
BranchTrueFalseTotal
if $lport 0 0 0
952 0 ▶ $ret .= "; observed_ip=$observed" if defined $observed;
Branch: 1 branch
BranchTrueFalseTotal
if defined $observed 0 0 0
953 0 $ret .= "; reqs=$self->{requests}";
954 0 $ret .= "; $self->{state}";
955  
956 0 my $hd = $self->{req_headers};
957 0 ▶ if (defined $hd) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hd) 0 0 0
958 0 ▶ my $host = $hd->header('Host') || 'unknown';
Condition0%0/2
$hd->header('Host') || 'unknown'
l!l
00
MC/DC0%0/1
$hd->header('Host') || 'unknown'
$hd->header('Host')
Truth table0%0/2
A
A= $hd->header('Host')
Aresult
11
00
959 0 $ret .= "; http://$host" . $hd->request_uri;
960 }
961  
962 0 return $ret;
963 }
964  
965 22 1;
966  
967 # Local Variables:
968 # mode: perl
969 # c-basic-indent: 4
970 # indent-tabs-mode: nil
971 # End:
« Perlbal/TCPListener.pm ↑ Summary Perlbal/Pool.pm »