Perlbal/ClientProxy.pm

filter: Statementstmt 78.3% 497 / 634 Branchbran 63.1% 182 / 288 Conditioncond 60.6% 71 / 117 MC/DCmcdc 41.1% 30 / 73 Subroutinesub 82.8% 53 / 64 Podpod 13.5% 5 / 37 totaltotal 69.0% 838 / 1213 CC 189 SCAR 28.8
CC
189
Cov
72%
as_string
72.0
event_read
62.6
__ANON__
20.1
CRAP 17.8
« Perlbal/Plugin/MaxContentLength.pm ↑ Summary Perlbal/UploadListener.pm »
1 ######################################################################
2 # HTTP Connection from a reverse proxy client
3 #
4 # Copyright 2004, Danga Interactive, Inc.
5 # Copyright 2005-2007, Six Apart, Ltd.
6 #
7 package Perlbal::ClientProxy;
8 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 22 ▶use base "Perlbal::ClientHTTPBase";
Subroutine BEGIN: called CC=1 SCAR=0.0
11 22 ▶no warnings qw(deprecated);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 22 ▶use Perlbal::ChunkedUploadState;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 22 ▶use Perlbal::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 22 ▶use fields (
Subroutine BEGIN: called CC=1 SCAR=0.0
17 22 'backend', # Perlbal::BackendHTTP object (or undef if disconnected)
18 'backend_requested', # true if we've requested a backend for this request
19 'reconnect_count', # number of times we've tried to reconnect to backend
20 'high_priority', # boolean; 1 if we are or were in the high priority queue
21 'low_priority', # boolean; 1 if we are or were in the low priority queue
22 'reproxy_uris', # arrayref; URIs to reproxy to, in order
23 'reproxy_expected_size', # int: size of response we expect to get back for reproxy
24 'currently_reproxying', # arrayref; the host info and URI we're reproxying right now
25 'content_length_remain', # int: amount of data we're still waiting for
26 'responded', # bool: whether we've already sent a response to the user or not
27 'last_request_time', # int: time that we last received a request
28 'primary_res_hdrs', # if defined, we are doing a transparent reproxy-URI
29 # and the headers we get back aren't necessarily
30 # the ones we want. instead, get most headers
31 # from the provided res headers object here.
32 'is_buffering', # bool; if we're buffering some/all of a request to memory/disk
33 'is_writing', # bool; if on, we currently have an aio_write out
34 'start_time', # hi-res time when we started getting data to upload
35 'bufh', # buffered upload filehandle object
36 'bufilename', # string; buffered upload filename
37 'bureason', # string; if defined, the reason we're buffering to disk
38 'buoutpos', # int; buffered output position
39 'backend_stalled', # boolean: if backend has shut off its reads because we're too slow.
40 'unread_data_waiting', # boolean: if we shut off reads while we know data is yet to be read from client
41 'chunked_upload_state', # bool/obj: if processing a chunked upload, Perlbal::ChunkedUploadState object, else undef
42 'request_body_length', # integer: request's body length, either as-declared,
43 # or calculated after chunked upload is complete
44  
45 # for perlbal sending out UDP packets related to upload status (for xmlhttprequest upload bar)
46 'last_upload_packet', # unixtime we last sent a UDP upload packet
47 'upload_session', # client's self-generated upload session
48  
49 # error-retrying stuff
50 'retry_count', # number of times we've retried this request so far after getting 500 errors
51 22 );
52  
53 22 ▶use constant READ_SIZE => 131072; # 128k, ~common TCP window size?
Subroutine BEGIN: called CC=1 SCAR=0.0
54 22 ▶use constant READ_AHEAD_SIZE => 32768; # kinda arbitrary. sum of these two is max stored per connection while waiting for backend.
Subroutine BEGIN: called CC=1 SCAR=0.0
55 22 ▶use Errno qw( EPIPE ENOENT ECONNRESET EAGAIN );
Subroutine BEGIN: called CC=1 SCAR=0.0
56 22 ▶use POSIX qw( O_CREAT O_TRUNC O_RDWR O_RDONLY );
Subroutine BEGIN: called CC=1 SCAR=0.0
57 22 ▶use Time::HiRes qw( gettimeofday tv_interval );
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 22 my $udp_sock;
60  
61 # ClientProxy
62 28 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
63 28 my Perlbal::ClientProxy $self = shift;
64 28 my ($service, $sock) = @_;
65 28 ▶ $self = fields::new($self) unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 28 0 28
66 28 $self->SUPER::new($service, $sock );
67  
68 28 Perlbal::objctor($self);
69  
70 28 $self->init;
71 28 $self->watch_read(1);
72 28 return $self;
73 }
74  
75 55 ▶sub new_from_base {
Subroutine new_from_base: called CC=1 SCAR=0.0
Pod undocumented
76 55 my $class = shift;
77 55 my Perlbal::ClientHTTPBase $cb = shift;
78 55 Perlbal::Util::rebless($cb, $class);
79 55 $cb->init;
80 55 $cb->watch_read(1);
81 55 $cb->handle_request;
82 55 return $cb;
83 }
84  
85 83 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
86 83 my Perlbal::ClientProxy $self = $_[0];
87  
88 83 $self->{last_request_time} = 0;
89  
90 83 $self->{backend} = undef;
91 83 $self->{high_priority} = 0;
92 83 $self->{low_priority} = 0;
93  
94 83 $self->{responded} = 0;
95 83 $self->{unread_data_waiting} = 0;
96 83 $self->{content_length_remain} = undef;
97 83 $self->{backend_requested} = 0;
98  
99 83 $self->{is_buffering} = 0;
100 83 $self->{is_writing} = 0;
101 83 $self->{start_time} = undef;
102 83 $self->{bufh} = undef;
103 83 $self->{bufilename} = undef;
104 83 $self->{buoutpos} = 0;
105 83 $self->{bureason} = undef;
106 83 $self->{chunked_upload_state} = undef;
107 83 $self->{request_body_length} = undef;
108  
109 83 $self->{reproxy_uris} = undef;
110 83 $self->{reproxy_expected_size} = undef;
111 83 $self->{currently_reproxying} = undef;
112  
113 83 $self->{retry_count} = 0;
114 }
115  
116 # given a service name, re-request (GET/HEAD only) to that service, even though
117 # you've already done a request to your original service
118 0 ▶sub start_reproxy_service {
Subroutine start_reproxy_service: not called CC=3 SCAR=24.8
Pod undocumented
119 0 my Perlbal::ClientProxy $self = $_[0];
120 0 my Perlbal::HTTPHeaders $primary_res_hdrs = $_[1];
121 0 my $svc_name = $_[2];
122  
123 0 ▶ my $svc = $svc_name ? Perlbal->service($svc_name) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$svc_name ? : 0 0 0
124 0 ▶ unless ($svc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($svc) 0 0 0
125 0 $self->_simple_response(404, "Vhost twiddling not configured for requested pair.");
126 0 return 1;
127 }
128  
129 0 $self->{backend_requested} = 0;
130 0 $self->{backend} = undef;
131 0 $self->{res_headers} = $primary_res_hdrs;
132  
133 0 $svc->adopt_base_client($self);
134 }
135  
136 # call this with a string of space separated URIs to start a process
137 # that will fetch the item at the first and return it to the user,
138 # on failure it will try the second, then third, etc
139 21 ▶sub start_reproxy_uri {
Subroutine start_reproxy_uri: called CC=13 SCAR=28.6
Pod undocumented
140 21 my Perlbal::ClientProxy $self = $_[0];
141 21 my Perlbal::HTTPHeaders $primary_res_hdrs = $_[1];
142 21 my $urls = $_[2];
143  
144 # at this point we need to disconnect from our backend
145 21 $self->{backend} = undef;
146  
147 # failure if we have no primary response headers
148 21 ▶ return unless $self->{primary_res_hdrs} ||= $primary_res_hdrs;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"primary_res_hdrs"} ||= $primary_res_hdrs 0 21 21
Condition66%2/3
$self->{"primary_res_hdrs"} ||= $primary_res_hdrs
l!l&&r!l&&!r
2190
MC/DC0%0/2
$self->{"primary_res_hdrs"} ||= $primary_res_hdrs
$self->{"primary_res_hdrs"} $primary_res_hdrs
Truth table66%2/3
A ||= B
A= $self->{"primary_res_hdrs"}B= $primary_res_hdrs
ABresult
1X1
011
000
149  
150 # construct reproxy_uri list
151 21 ▶ if (defined $urls) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $urls) 19 2 21
152 19 my @uris = split /\s+/, $urls;
153 19 $self->{currently_reproxying} = undef;
154 19 $self->{reproxy_uris} = [];
155 19 foreach my $uri (@uris) {
156 21 ▶ next unless $uri =~ m!^http://(.+?)(?::(\d+))?(/.*)?$!;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri =~ m[^http://(.+?)(?::(\d+))?(/.*)?$] 0 21 21
157 21 ▶ push @{$self->{reproxy_uris}}, [ $1, $2 || 80, $3 || '/' ];
Condition50%2/4
$2 || 80
l!l
210
$3 || '/'
l!l
210
MC/DC0%0/1
$2 || 80
$2
MC/DC0%0/1
$3 || '/'
$3
Truth table50%1/2
A
A= $2
Aresult
11
00
Truth table50%1/2
A
A= $3
Aresult
11
00
158 }
159 }
160  
161 # if we get in here and we have currently_reproxying defined, then something
162 # happened and we want to retry that one
163 21 ▶ if ($self->{currently_reproxying}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"currently_reproxying"}) 0 21 21
164 0 unshift @{$self->{reproxy_uris}}, $self->{currently_reproxying};
165 0 $self->{currently_reproxying} = undef;
166 }
167  
168 # if we have no uris in our list now, tell the user 404
169 return $self->_simple_response(503)
170 21 ▶ unless @{$self->{reproxy_uris} || []};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"reproxy_uris"} or [];} 0 21 21
Condition50%1/2
$self->{'reproxy_uris'} || []
l!l
210
MC/DC0%0/1
$self->{'reproxy_uris'} || []
$self->{'reproxy_uris'}
Truth table50%1/2
A
A= $self->{'reproxy_uris'}
Aresult
11
00
171  
172 # set the expected size if we got a content length in our headers
173 21 ▶ if ($primary_res_hdrs && (my $expected_size = $primary_res_hdrs->header('X-REPROXY-EXPECTED-SIZE'))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($primary_res_hdrs and my $expected_size = $primary_res_hdrs->header("X-REPROXY-EXPECTED-SIZE")) 0 21 21
Condition66%2/3
$primary_res_hdrs and my $expected_size = $primary_res_hdrs->header("X-REPROXY-EXPECTED-SIZE")
!ll&&!rl&&r
2190
MC/DC0%0/2
$primary_res_hdrs and my $expected_size = $primary_res_hdrs->header("X-REPROXY-EXPECTED-SIZE")
$primary_res_hdrs my $expected_size = $primary_res_hdrs->header("X-REPROXY-EXPECTED-SIZE")
Truth table66%2/3
A and B
A= $primary_res_hdrsB= my $expected_size = $primary_res_hdrs->header("X-REPROXY-EXPECTED-SIZE")
ABresult
0X0
100
111
174 0 $self->{reproxy_expected_size} = $expected_size;
175 }
176  
177 # pass ourselves off to the reproxy manager
178 21 $self->state('wait_backend');
179 21 Perlbal::ReproxyManager::do_reproxy($self);
180 }
181  
182 # called by the reproxy manager when we can't get to our requested backend
183 2 ▶sub try_next_uri {
Subroutine try_next_uri: called CC=2 SCAR=6.9
Pod undocumented
184 2 my Perlbal::ClientProxy $self = $_[0];
185  
186 2 ▶ if ($self->{currently_reproxying}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'currently_reproxying'}) { } 1 1 2
187 # If we're currently reproxying to a backend, that means we want to try the next uri which is
188 # ->{reproxy_uris}->[0].
189 } else {
190 # Since we're not currently reproxying, that means we never got a backend in the first place,
191 # so we want to move on to the next uri which is ->{reproxy_uris}->[1] (shift one off)
192 1 shift @{$self->{reproxy_uris}};
193 }
194  
195 2 $self->{currently_reproxying} = undef;
196  
197 2 $self->start_reproxy_uri();
198 }
199  
200 # returns true if this ClientProxy is too many bytes behind the backend
201 131 ▶sub too_far_behind_backend {
Subroutine too_far_behind_backend: called CC=3 SCAR=11.4
Pod undocumented
202 131 my Perlbal::ClientProxy $self = $_[0];
203 131 ▶ my Perlbal::BackendHTTP $backend = $self->{backend} or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my Perlbal::BackendHTTP $backend = $self->{"backend"} or return 0 131 0 131
204  
205 # if a backend doesn't have a service, it's a
206 # ReproxyManager-created backend, and thus it should use the
207 # 'buffer_size_reproxy_url' parameter for acceptable buffer
208 # widths, and not the regular 'buffer_size'. this lets people
209 # tune buffers depending on the types of webservers. (assumption
210 # being that reproxied-to webservers are event-based and it's okay
211 # to tie the up longer in favor of using less buffer memory in
212 # perlbal)
213 my $max_buffer = defined $backend->{service} ?
214 $self->{service}->{buffer_size} :
215 131 ▶ $self->{service}->{buffer_size_reproxy_url};
Branch: 1 branch
BranchTrueFalseTotal
defined $backend->{'service'} ? : 131 0 131
216  
217 131 return $self->{write_buf_size} > $max_buffer;
218 }
219  
220 # this is a callback for when a backend has been created and is
221 # ready for us to do something with it
222 20 ▶sub use_reproxy_backend {
Subroutine use_reproxy_backend: called CC=5 SCAR=16.3
Pod undocumented
223 20 my Perlbal::ClientProxy $self = $_[0];
224 20 my Perlbal::BackendHTTP $be = $_[1];
225  
226 # get a URI
227 20 my $datref = $self->{currently_reproxying} = shift @{$self->{reproxy_uris}};
228 20 ▶ unless (defined $datref) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $datref) 0 20 20
229 # return error and close the backend
230 0 $be->close('invalid_uris');
231 0 return $self->_simple_response(503);
232 }
233  
234 # now send request
235 20 $self->{backend} = $be;
236 20 $be->{client} = $self;
237  
238 20 my $extra_hdr = "";
239 20 ▶ if (my $range = $self->{req_headers}->header("Range")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $range = $self->{"req_headers"}->header("Range")) 4 16 20
240 4 $extra_hdr .= "Range: $range\r\n";
241 }
242 20 ▶ if (my $host = $self->{req_headers}->header("Host")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $host = $self->{"req_headers"}->header("Host")) 0 20 20
243 0 $extra_hdr .= "Host: $host\r\n";
244 }
245  
246 20 ▶ my $req_method = $self->{req_headers}->request_method eq 'HEAD' ? 'HEAD' : 'GET';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'req_headers'}->request_method eq 'HEAD' ? : 1 19 20
247 20 my $headers = "$req_method $datref->[2] HTTP/1.0\r\nConnection: keep-alive\r\n${extra_hdr}\r\n";
248  
249 20 $be->{req_headers} = Perlbal::HTTPHeaders->new(\$headers);
250 20 $be->state('sending_req');
251 20 $self->state('backend_req_sent');
252 20 $be->write($be->{req_headers}->to_string_ref);
253 20 $be->watch_read(1);
254 20 $be->watch_write(1);
255 }
256  
257 # this is called when a transient backend getting a reproxied URI has received
258 # a response from the server and is ready for us to deal with it
259 20 ▶sub backend_response_received {
Subroutine backend_response_received: called CC=4 SCAR=14.0
Pod undocumented
260 20 my Perlbal::ClientProxy $self = $_[0];
261 20 my Perlbal::BackendHTTP $be = $_[1];
262  
263 # we fail if we got something that's NOT a 2xx code, OR, if we expected
264 # a certain size and got back something different
265 20 my $code = $be->{res_headers}->response_code + 0;
266  
267 20 ▶ my $bad_code = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
268 20 ▶ return 0 if $code >= 200 && $code <= 299;
Branch: 1 branch
BranchTrueFalseTotal
if $code >= 200 and $code <= 299 17 3 20
Condition66%2/3
$code >= 200 and $code <= 299
!ll&&!rl&&r
0317
MC/DC50%1/2
$code >= 200 and $code <= 299
$code >= 200 $code <= 299
Truth table66%2/3
A and B
A= $code >= 200B= $code <= 299
ABresult
0X0
100
111
269 3 ▶ return 0 if $code == 416;
Branch: 1 branch
BranchTrueFalseTotal
if $code == 416 2 1 3
270 1 return 1;
271 20 };
272  
273 19 ▶ my $bad_size = sub {
Subroutine __ANON__: called CC=2 SCAR=9.2
274 19 ▶ return 0 unless defined $self->{reproxy_expected_size};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"reproxy_expected_size"} 19 0 19
275 0 return $self->{reproxy_expected_size} != $be->{res_headers}->header('Content-length');
276 20 };
277  
278 20 ▶ if ($bad_code->() || $bad_size->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$bad_code() or &$bad_size()) 1 19 20
Condition66%2/3
&$bad_code() or &$bad_size()
l!l&&r!l&&!r
1019
MC/DC50%1/2
&$bad_code() or &$bad_size()
&$bad_code() &$bad_size()
Truth table66%2/3
A or B
A= &$bad_code()B= &$bad_size()
ABresult
1X1
011
000
279 # fall back to an alternate URL
280 1 $be->{client} = undef;
281 1 $be->close('non_200_reproxy');
282 1 $self->try_next_uri;
283 1 return 1;
284 }
285  
286 19 ▶ return if $self->{service}->run_hook('reproxy_response_received', $be);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"service"}->run_hook("reproxy_response_received", $be) 0 19 19
287  
288 # a response means that we are no longer currently waiting on a reproxy, and
289 # don't want to retry this URI
290 19 $self->{currently_reproxying} = undef;
291  
292 19 return 0;
293 }
294  
295 10 ▶sub start_reproxy_file {
Subroutine start_reproxy_file: called CC=3 SCAR=11.4
Pod undocumented
296 10 my Perlbal::ClientProxy $self = shift;
297 10 my $file = shift; # filename to reproxy
298 10 my Perlbal::HTTPHeaders $hd = shift; # headers from backend, in need of cleanup
299  
300 # at this point we need to disconnect from our backend
301 10 $self->{backend} = undef;
302  
303 # call hook for pre-reproxy
304 10 ▶ return if $self->{service}->run_hook("start_file_reproxy", $self, \$file);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"service"}->run_hook("start_file_reproxy", $self, \$file) 0 10 10
305  
306 # set our expected size
307 10 ▶ if (my $expected_size = $hd->header('X-REPROXY-EXPECTED-SIZE')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $expected_size = $hd->header("X-REPROXY-EXPECTED-SIZE")) 0 10 10
308 0 $self->{reproxy_expected_size} = $expected_size;
309 }
310  
311 # start an async stat on the file
312 10 $self->state('wait_stat');
313 10 ▶ Perlbal::AIO::aio_stat($file, sub {
Subroutine __ANON__: called CC=13 SCAR=27.3
314  
315 # if the client's since disconnected by the time we get the stat,
316 # just bail.
317 10 ▶ return if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 10 10
318  
319 10 my $size = -s _;
320  
321 10 ▶ unless ($size) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($size) 0 10 10
322 # FIXME: POLICY: 404 or retry request to backend w/o reproxy-file capability?
323 0 return $self->_simple_response(404);
324 }
325 10 ▶ if (defined $self->{reproxy_expected_size} && $self->{reproxy_expected_size} != $size) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"reproxy_expected_size"} and $self->{"reproxy_expected_size"} != $size) 0 10 10
Condition33%1/3
defined $self->{"reproxy_expected_size"} and $self->{"reproxy_expected_size"} != $size
!ll&&!rl&&r
1000
MC/DC0%0/2
defined $self->{"reproxy_expected_size"} and $self->{"reproxy_expected_size"} != $size
defined $self->{"reproxy_expected_size"} $self->{"reproxy_expected_size"} != $size
Truth table33%1/3
A and B
A= defined $self->{"reproxy_expected_size"}B= $self->{"reproxy_expected_size"} != $size
ABresult
0X0
100
111
326 # 404; the file size doesn't match what we expected
327 0 return $self->_simple_response(404);
328 }
329  
330 # if the thing we're reproxying is indeed a file, advertise that
331 # we support byte ranges on it
332 10 ▶ if (-f _) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f _) 10 0 10
333 10 $hd->header("Accept-Ranges", "bytes");
334 }
335  
336 10 my ($status, $range_start, $range_end) = $self->{req_headers}->range($size);
337 10 my $not_satisfiable = 0;
338  
339 10 ▶ if ($status == 416) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 416) 2 8 10
340 2 $hd = Perlbal::HTTPHeaders->new_response(416);
341 2 ▶ $hd->header("Content-Range", $size ? "bytes */$size" : "*");
Branch: 1 branch
BranchTrueFalseTotal
$size ? : 2 0 2
342 2 $not_satisfiable = 1;
343 }
344  
345 # change the status code to 200 if the backend gave us 204 No Content
346 10 ▶ $hd->code(200) if $hd->response_code == 204;
Branch: 1 branch
BranchTrueFalseTotal
if $hd->response_code == 204 0 10 10
347  
348 # fixup the Content-Length header with the correct size (application
349 # doesn't need to provide a correct value if it doesn't want to stat())
350 10 ▶ if ($status == 200) {
Branch: 2 branches
BranchTrueFalseTotal
if ($status == 200) { } 6 4 10
elsif ($status == 206) { } 2 2 4
351 6 $hd->header("Content-Length", $size);
352 } elsif ($status == 206) {
353 2 $hd->header("Content-Range", "bytes $range_start-$range_end/$size");
354 2 $hd->header("Content-Length", $range_end - $range_start + 1);
355 2 $hd->code(206);
356 }
357  
358 # don't send this internal header to the client:
359 10 $hd->header('X-REPROXY-FILE', undef);
360  
361 # rewrite some other parts of the header
362 10 $self->setup_keepalive($hd);
363  
364 # just send the header, now that we cleaned it.
365 10 $self->{res_headers} = $hd;
366 10 $self->write($hd->to_string_ref);
367  
368 10 ▶ if ($self->{req_headers}->request_method eq 'HEAD' || $not_satisfiable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"req_headers"}->request_method eq "HEAD" or $not_satisfiable) 2 8 10
Condition66%2/3
$self->{"req_headers"}->request_method eq "HEAD" or $not_satisfiable
l!l&&r!l&&!r
028
MC/DC50%1/2
$self->{"req_headers"}->request_method eq "HEAD" or $not_satisfiable
$self->{"req_headers"}->request_method eq "HEAD" $not_satisfiable
Truth table66%2/3
A or B
A= $self->{"req_headers"}->request_method eq "HEAD"B= $not_satisfiable
ABresult
1X1
011
000
369 2 ▶ $self->write(sub { $self->http_response_sent; });
Subroutine __ANON__: called CC=1 SCAR=0.0
370 2 return;
371 }
372  
373 8 $self->state('wait_open');
374 8 ▶ Perlbal::AIO::aio_open($file, 0, 0 , sub {
Subroutine __ANON__: called CC=5 SCAR=18.3
375 8 my $fh = shift;
376  
377 # if client's gone, just close filehandle and abort
378 8 ▶ if ($self->{closed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"closed"}) 0 8 8
379 0 ▶ CORE::close($fh) if $fh;
Branch: 1 branch
BranchTrueFalseTotal
if $fh 0 0 0
380 0 return;
381 }
382  
383 # handle errors
384 8 ▶ if (! $fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 8 8
385 # FIXME: do 500 vs. 404 vs whatever based on $! ?
386 0 return $self->_simple_response(500);
387 }
388  
389 # seek if partial content
390 8 ▶ if ($status == 206) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 206) 2 6 8
391 2 sysseek($fh, $range_start, &POSIX::SEEK_SET);
392 2 $size = $range_end - $range_start + 1;
393 }
394  
395 8 $self->reproxy_fh($fh, $size);
396 8 $self->watch_write(1);
397 8 });
398 10 });
399 }
400  
401 # Client
402 # get/set backend proxy connection
403 287 ▶sub backend {
Subroutine backend: called CC=3 SCAR=11.0
Pod undocumented
404 287 my Perlbal::ClientProxy $self = shift;
405 287 ▶ return $self->{backend} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 126 161 287
406  
407 161 my $backend = shift;
408 161 ▶ $self->state('draining_res') unless $backend;
Branch: 1 branch
BranchTrueFalseTotal
unless $backend 17 144 161
409 161 return $self->{backend} = $backend;
410 }
411  
412 # invoked by backend when it wants us to start watching for reads again
413 # and feeding it data (if we have any)
414 144 ▶sub backend_ready {
Subroutine backend_ready: called CC=3 SCAR=11.0
Pod undocumented
415 144 my Perlbal::ClientProxy $self = $_[0];
416 144 my Perlbal::BackendHTTP $be = $_[1];
417  
418 # if we'd turned ourselves off while we waited for a backend, turn
419 # ourselves back on, because the backend is ready for data now.
420 144 ▶ if ($self->{unread_data_waiting}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"unread_data_waiting"}) 28 116 144
421 28 $self->watch_read(1);
422 }
423  
424 # normal, not-buffered-to-disk case:
425 144 ▶ return $self->drain_read_buf_to($be) unless $self->{bureason};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"bureason"} 115 29 144
426  
427 # buffered-to-disk case.
428  
429 # tell the backend it has to go into buffered_upload_mode,
430 # which makes it inform us of its writable availability
431 29 $be->invoke_buffered_upload_mode;
432 }
433  
434 # our backend enqueues a call to this method in our write buffer, so this is called
435 # right after we've finished sending all of the results to the user. at this point,
436 # if we were doing keep-alive, we don't close and setup for the next request.
437 135 ▶sub backend_finished {
Subroutine backend_finished: called CC=2 SCAR=7.2
Pod undocumented
438 135 my Perlbal::ClientProxy $self = shift;
439 135 print "ClientProxy::backend_finished\n" if Perlbal::DEBUG >= 3;
440  
441 # mark ourselves as having responded (presumably if we're here,
442 # the backend has responded already)
443 135 $self->{responded} = 1;
444  
445 # our backend is done with us, so we disconnect ourselves from it
446 135 $self->{backend} = undef;
447  
448 # backend is done sending data to us, so we can recycle this clientproxy
449 # if we don't have any data yet to read
450 135 ▶ return $self->http_response_sent unless $self->{unread_data_waiting};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"unread_data_waiting"} 135 0 135
451  
452 # if we get here (and we do, rarely, in practice) then that means
453 # the backend read was empty/disconnected (or otherwise messed up),
454 # and the only thing we can really do is close the client down.
455 0 $self->close("backend_finished_while_unread_data");
456 }
457  
458 # Called when this client is entering a persist_wait state, but before we are returned to base.
459 128 ▶sub persist_wait {
Subroutine persist_wait: called CC=1 SCAR=0.0
Pod undocumented
460 128 my Perlbal::ClientProxy $self = $_[0];
461 # We're in keepalive, and just completed a proxy request
462 128 $self->{service}->run_hooks('end_proxy_request', $self);
463 }
464  
465 # called when we've sent a response to a user fully and we need to reset state
466 151 ▶sub http_response_sent {
Subroutine http_response_sent: called CC=3 SCAR=11.0
Pod undocumented
467 151 my Perlbal::ClientProxy $self = $_[0];
468  
469 # persistence logic is in ClientHTTPBase
470 151 ▶ return 0 unless $self->SUPER::http_response_sent;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->SUPER::http_response_sent 23 128 151
471  
472 128 print "ClientProxy::http_response_sent -- resetting state\n" if Perlbal::DEBUG >= 3;
473  
474 128 ▶ if (my $be = $self->{backend}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $be = $self->{"backend"}) 0 128 128
475 0 $self->{backend} = undef;
476 0 $be->forget_client;
477 }
478  
479 # if we get here we're being persistent, reset our state
480 128 $self->{backend_requested} = 0;
481 128 $self->{high_priority} = 0;
482 128 $self->{reproxy_uris} = undef;
483 128 $self->{reproxy_expected_size} = undef;
484 128 $self->{currently_reproxying} = undef;
485 128 $self->{content_length_remain} = undef;
486 128 $self->{primary_res_hdrs} = undef;
487 128 $self->{responded} = 0;
488 128 $self->{is_buffering} = 0;
489 128 $self->{is_writing} = 0;
490 128 $self->{start_time} = undef;
491 128 $self->{bufh} = undef;
492 128 $self->{bufilename} = undef;
493 128 $self->{buoutpos} = 0;
494 128 $self->{bureason} = undef;
495 128 $self->{upload_session} = undef;
496 128 $self->{chunked_upload_state} = undef;
497 128 $self->{request_body_length} = undef;
498 128 return 1;
499 }
500  
501 # to request a backend connection AFTER you've already done so, if you
502 # didn't like the results from the first one. (like after a 500 error)
503 0 ▶sub rerequest_backend {
Subroutine rerequest_backend: not called CC=1 SCAR=6.9
Pod undocumented
504 0 my Perlbal::ClientProxy $self = shift;
505  
506 0 $self->{backend_requested} = 0;
507 0 $self->{backend} = undef;
508 0 $self->request_backend;
509 }
510  
511 144 ▶sub request_backend {
Subroutine request_backend: called CC=2 SCAR=7.0
Pod undocumented
512 144 my Perlbal::ClientProxy $self = shift;
513 144 ▶ return if $self->{backend_requested};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"backend_requested"} 0 144 144
514 144 $self->{backend_requested} = 1;
515  
516 144 $self->state('wait_backend');
517 144 $self->{service}->request_backend_connection($self);
518 144 $self->tcp_cork(1); # cork writes to self
519 }
520  
521 # Client (overrides and calls super)
522 29 ▶sub close {
Subroutine close: called CC=4 SCAR=13.9
Pod documented
523 29 my Perlbal::ClientProxy $self = shift;
524 29 my $reason = shift;
525  
526 warn sprintf(
527 "Perlbal::ClientProxy closed %s%s.\n",
528 29 ( $self->{closed} ? "again " : "" ),
529 (defined $reason ? "saying '$reason'" : "for an unknown reason")
530 ) if Perlbal::DEBUG >= 2;
531  
532 # don't close twice
533 29 ▶ return if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 1 28 29
534  
535 # signal that we're done
536 28 $self->{service}->run_hooks('end_proxy_request', $self);
537  
538 # kill our backend if we still have one
539 28 ▶ if (my $backend = $self->{backend}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $backend = $self->{'backend'}) { } 1 27 28
540 1 print "Client ($self) closing backend ($backend)\n" if Perlbal::DEBUG >= 1;
541 1 $self->backend(undef);
542 1 ▶ $backend->close($reason ? "proxied_from_client_close:$reason" : "proxied_from_client_close");
Branch: 1 branch
BranchTrueFalseTotal
$reason ? : 1 0 1
543 } else {
544 # if no backend, tell our service that we don't care for one anymore
545 27 $self->{service}->note_client_close($self);
546 }
547  
548 # call ClientHTTPBase's close
549 28 $self->SUPER::close($reason);
550 }
551  
552 151 ▶sub setup_keepalive {
Subroutine setup_keepalive: called CC=3 SCAR=11.4
Pod undocumented
553 151 my Perlbal::ClientProxy $self = $_[0];
554 151 ▶ my $not_done_reading = defined $self->{content_length_remain} && $self->{content_length_remain} > 0;
Condition66%2/3
defined $self->{'content_length_remain'} && $self->{'content_length_remain'} > 0
!ll&&!rl&&r
65860
MC/DC0%0/2
defined $self->{'content_length_remain'} && $self->{'content_length_remain'} > 0
defined $self->{'content_length_remain'} $self->{'content_length_remain'} > 0
Truth table66%2/3
A && B
A= defined $self->{'content_length_remain'}B= $self->{'content_length_remain'} > 0
ABresult
0X0
100
111
555  
556 151 ▶ return $self->SUPER::setup_keepalive($_[1], $not_done_reading ? 0 : undef);
Branch: 1 branch
BranchTrueFalseTotal
$not_done_reading ? : 0 151 151
557 }
558  
559  
560 1 ▶sub client_disconnected { # : void
Subroutine client_disconnected: called CC=2 SCAR=7.0
Pod undocumented
561 1 my Perlbal::ClientProxy $self = shift;
562 1 print "ClientProxy::client_disconnected\n" if Perlbal::DEBUG >= 2;
563  
564 # if client disconnected, then we need to turn off watching for
565 # further reads and purge the existing upload if any. also, we
566 # should just return and do nothing else.
567  
568 1 $self->watch_read(0);
569 1 ▶ $self->purge_buffered_upload if $self->{bureason};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"bureason"} 1 0 1
570 1 return $self->close('user_disconnected');
571 }
572  
573 # Client
574 8 ▶sub event_write {
Subroutine event_write: called CC=3 SCAR=13.6
Pod documented
575 8 my Perlbal::ClientProxy $self = shift;
576 8 print "ClientProxy::event_write\n" if Perlbal::DEBUG >= 3;
577  
578 # obviously if we're writing the backend has processed our request
579 # and we are responding/have responded to the user, so mark it so
580 8 $self->{responded} = 1;
581  
582 # will eventually, finally reset the whole object on completion
583 8 $self->SUPER::event_write;
584  
585 # trigger our backend to keep reading, if it's still connected
586 8 ▶ if ($self->{backend_stalled} && (my $backend = $self->{backend})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"backend_stalled"} and my $backend = $self->{"backend"}) 0 8 8
Condition33%1/3
$self->{"backend_stalled"} and my $backend = $self->{"backend"}
!ll&&!rl&&r
800
MC/DC0%0/2
$self->{"backend_stalled"} and my $backend = $self->{"backend"}
$self->{"backend_stalled"} my $backend = $self->{"backend"}
Truth table33%1/3
A and B
A= $self->{"backend_stalled"}B= my $backend = $self->{"backend"}
ABresult
0X0
100
111
587 0 print " unstalling backend\n" if Perlbal::DEBUG >= 3;
588  
589 0 $self->{backend_stalled} = 0;
590 0 $backend->watch_read(1);
591 }
592 }
593  
594 # ClientProxy
595 253 ▶sub event_read {
Subroutine event_read: called CC=33 SCAR=41.4
Pod documented
596 253 my Perlbal::ClientProxy $self = shift;
597 253 print "ClientProxy::event_read\n" if Perlbal::DEBUG >= 3;
598  
599 # mark alive so we don't get killed for being idle
600 253 $self->{alive_time} = time;
601  
602 # if we have no headers, the only thing we can do is try to get some
603 253 ▶ if (! $self->{req_headers}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"req_headers"}) 112 141 253
604 112 print " no headers. reading.\n" if Perlbal::DEBUG >= 3;
605 112 ▶ $self->handle_request if $self->read_request_headers;
Branch: 1 branch
BranchTrueFalseTotal
if $self->read_request_headers 97 15 112
606 112 return;
607 }
608  
609 # if we're buffering to disk or haven't read too much from this client, keep reading,
610 # otherwise shut off read notifications
611 141 ▶ unless ($self->{is_buffering} || $self->{read_ahead} < READ_AHEAD_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"is_buffering"} or $self->{"read_ahead"} < 32768) 4 137 141
Condition100%3/3
$self->{"is_buffering"} or $self->{"read_ahead"} < 32768
l!l&&r!l&&!r
98394
MC/DC100%2/2
$self->{"is_buffering"} or $self->{"read_ahead"} < 32768
$self->{"is_buffering"} $self->{"read_ahead"} < 32768
Truth table100%3/3
A or B
A= $self->{"is_buffering"}B= $self->{"read_ahead"} < 32768
ABresult
1X1
011
000
612 # our buffer is full, so turn off reads for now
613 4 print " disabling reads.\n" if Perlbal::DEBUG >= 3;
614 4 $self->watch_read(0);
615 4 return;
616 }
617  
618 # deal with chunked uploads
619 137 ▶ if (my $cus = $self->{chunked_upload_state}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cus = $self->{"chunked_upload_state"}) 9 128 137
620 9 $cus->on_readable($self);
621  
622 # if we got more than 1MB not flushed to disk,
623 # stop reading for a bit until disk catches up
624 9 ▶ if ($self->{read_ahead} > 1024*1024) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"read_ahead"} > 1048576) 0 9 9
625 0 $self->watch_read(0);
626 }
627 9 return;
628 }
629  
630 # read more data if we're still buffering or if our current read buffer
631 # is not full to the max READ_AHEAD_SIZE which is how much data we will
632 # buffer in from the user before passing on to the backend
633  
634 # read the MIN(READ_SIZE, content_length_remain)
635 128 my $read_size = READ_SIZE;
636 128 my $remain = $self->{content_length_remain};
637  
638 128 ▶ $read_size = $remain if $remain && $remain < $read_size;
Branch: 1 branch
BranchTrueFalseTotal
if $remain and $remain < $read_size 87 41 128
Condition100%3/3
$remain and $remain < $read_size
!ll&&!rl&&r
14087
MC/DC100%2/2
$remain and $remain < $read_size
$remain $remain < $read_size
Truth table100%3/3
A and B
A= $remainB= $remain < $read_size
ABresult
0X0
100
111
639 128 print " reading $read_size bytes (", (defined $remain ? $remain : "(undef)"), " bytes remain)\n" if Perlbal::DEBUG >= 3;
640  
641 128 my $bref = $self->read($read_size);
642  
643 # if the read returned undef, that means the connection was closed
644 # (see: Danga::Socket::read)
645 128 ▶ return $self->client_disconnected unless defined $bref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bref 1 127 128
646  
647 # if they didn't declare a content body length and we just got a
648 # readable event that's not a disconnect, something's messed up.
649 # they're overflowing us. disconnect!
650 127 ▶ if (! $remain) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($remain) 1 126 127
651 1 $self->_simple_response(400, "Can't pipeline to HTTP/1.0");
652 1 $self->close("pipelining_to_http10");
653 1 return;
654 }
655  
656 # now that we know we have a defined value, determine how long it is, and do
657 # housekeeping to keep our tracking numbers up to date.
658 126 my $len = length($$bref);
659 126 print " read $len bytes\n" if Perlbal::DEBUG >= 3;
660  
661 # when run under the program "trickle", epoll speaks the truth to
662 # us, but then trickle interferes and steals our reads/writes, so
663 # this fails. normally this check isn't needed.
664 126 ▶ return unless $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $len 0 126 126
665  
666 126 $self->{read_size} += $len;
667 126 ▶ $self->{content_length_remain} -= $len if $remain;
Branch: 1 branch
BranchTrueFalseTotal
if $remain 126 0 126
668  
669 126 ▶ my $done_reading = defined $self->{content_length_remain} && $self->{content_length_remain} <= 0;
Condition66%2/3
defined $self->{'content_length_remain'} && $self->{'content_length_remain'} <= 0
!ll&&!rl&&r
04284
MC/DC50%1/2
defined $self->{'content_length_remain'} && $self->{'content_length_remain'} <= 0
defined $self->{'content_length_remain'} $self->{'content_length_remain'} <= 0
Truth table66%2/3
A && B
A= defined $self->{'content_length_remain'}B= $self->{'content_length_remain'} <= 0
ABresult
0X0
100
111
670 126 my $backend = $self->backend;
671 126 print(" done_reading = $done_reading, backend = ", ($backend || "<undef>"), "\n") if Perlbal::DEBUG >= 3;
672  
673 # upload tracking
674 126 ▶ if (my $session = $self->{upload_session}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $session = $self->{"upload_session"}) 0 126 126
675 0 my $cl = $self->{req_headers}->content_length;
676 0 my $remain = $self->{content_length_remain};
677 0 my $now = time(); # FIXME: more efficient?
678 0 ▶ if ($cl && $remain && ($self->{last_upload_packet} || 0) != $now) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cl and $remain and ($self->{"last_upload_packet"} || 0) != $now) 0 0 0
Condition0%0/8
$cl and $remain and ($self->{"last_upload_packet"} || 0) != $now
!ll&&!rl&&r
000
$cl and $remain
!ll&&!rl&&r
000
$self->{'last_upload_packet'} || 0
l!l
00
MC/DC0%0/3
$cl and $remain and ($self->{"last_upload_packet"} || 0) != $now
$cl $remain $self->{'last_upload_packet'}
Truth table0%0/4
A and B and C
A= $clB= $remainC= $self->{'last_upload_packet'}
ABCresult
0XX0
10X0
1100
1111
679 0 my $done = $cl - $remain;
680 0 $self->{last_upload_packet} = $now;
681 0 ▶ $udp_sock ||= IO::Socket::INET->new(Proto => 'udp');
Condition0%0/3
$udp_sock ||= "IO::Socket::INET"->new("Proto", "udp")
l!l&&r!l&&!r
000
MC/DC0%0/2
$udp_sock ||= "IO::Socket::INET"->new("Proto", "udp")
$udp_sock "IO::Socket::INET"->new("Proto", "udp")
Truth table0%0/3
A ||= B
A= $udp_sockB= "IO::Socket::INET"->new("Proto", "udp")
ABresult
1X1
011
000
682 0 my $since = $self->{last_request_time};
683 0 my $send = "UPLOAD:$session:$done:$cl:$since:$now";
684 0 ▶ if ($udp_sock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($udp_sock) 0 0 0
685 0 foreach my $ep (@{ $self->{service}{upload_status_listeners_sockaddr} }) {
686 0 my $rv = $udp_sock->send($send, 0, $ep);
687 }
688 }
689 }
690 }
691  
692 # just dump the read into the nether if we're dangling. that is
693 # the case when we send the headers to the backend and it responds
694 # before we're done reading from the client; therefore further
695 # reads from the client just need to be sent nowhere, because the
696 # RFC2616 section 8.2.3 says: "the server SHOULD NOT close the
697 # transport connection until it has read the entire request"
698 126 ▶ if ($self->{responded}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"responded"}) 0 126 126
699 0 print " already responded.\n" if Perlbal::DEBUG >= 3;
700 # in addition, if we're now out of data (clr == 0), then we should
701 # either close ourselves or get ready for another request
702 0 ▶ return $self->http_response_sent if $done_reading;
Branch: 1 branch
BranchTrueFalseTotal
if $done_reading 0 0 0
703  
704 0 print " already responded [2].\n" if Perlbal::DEBUG >= 3;
705 # at this point, if the backend has responded then we just return
706 # as we don't want to send it on to them or buffer it up, which is
707 # what the code below does
708 0 return;
709 }
710  
711 # if we have no data left to read, stop reading. all that can
712 # come later is an extra \r\n which we handle later when parsing
713 # new request headers. and if it's something else, we'll bail on
714 # the next request, not this one.
715 126 ▶ if ($done_reading) {
Branch: 1 branch
BranchTrueFalseTotal
if ($done_reading) 84 42 126
716 Carp::confess("content_length_remain less than zero: self->{content_length_remain}")
717 84 ▶ if $self->{content_length_remain} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"content_length_remain"} < 0 0 84 84
718 84 $self->{unread_data_waiting} = 0;
719 84 $self->watch_read(0);
720 }
721  
722 # now, if we have a backend, then we should be writing it to the backend
723 # and not doing anything else
724 126 ▶ if ($backend) {
Branch: 1 branch
BranchTrueFalseTotal
if ($backend) 38 88 126
725 38 print " got a backend. sending write to it.\n" if Perlbal::DEBUG >= 3;
726 38 $backend->write($bref);
727 # TODO: monitor the backend's write buffer depth?
728 38 return;
729 }
730  
731 # now, we know we don't have a backend, so we have to push this data onto our
732 # read buffer... it's not going anywhere yet
733 88 push @{$self->{read_buf}}, $bref;
734 88 $self->{read_ahead} += $len;
735 88 print " no backend. read_ahead = $self->{read_ahead}.\n" if Perlbal::DEBUG >= 3;
736  
737 # if we know we've already started spooling a file to disk, then continue
738 # to do that.
739 88 print " bureason = $self->{bureason}\n" if Perlbal::DEBUG >= 3 && $self->{bureason};
740 88 ▶ return $self->buffered_upload_update if $self->{bureason};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"bureason"} 46 42 88
741  
742 # if we are under our buffer-to-memory size, just continue buffering here and
743 # don't fall through to the backend request call below
744 return if
745 ! $done_reading &&
746 42 ▶ $self->{read_ahead} < $self->{service}->{buffer_backend_connect};
Branch: 1 branch
BranchTrueFalseTotal
if not $done_reading and $self->{"read_ahead"} < $self->{"service"}{"buffer_backend_connect"} 7 35 42
Condition100%3/3
not $done_reading and $self->{"read_ahead"} < $self->{"service"}{"buffer_backend_connect"}
!ll&&!rl&&r
2967
MC/DC100%2/2
not $done_reading and $self->{"read_ahead"} < $self->{"service"}{"buffer_backend_connect"}
not $done_reading $self->{"read_ahead"} < $self->{"service"}{"buffer_backend_connect"}
Truth table100%3/3
not(A) and B
A= not $done_readingB= $self->{"read_ahead"} < $self->{"service"}{"buffer_backend_connect"}
ABresult
0X0
100
111
747  
748 # over the buffer-to-memory size, see if we should start spooling to disk.
749 35 ▶ return if $self->{service}->{buffer_uploads} && $self->decide_to_buffer_to_disk;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"service"}{"buffer_uploads"} and $self->decide_to_buffer_to_disk 2 33 35
Condition100%3/3
$self->{"service"}{"buffer_uploads"} and $self->decide_to_buffer_to_disk
!ll&&!rl&&r
2942
MC/DC100%2/2
$self->{"service"}{"buffer_uploads"} and $self->decide_to_buffer_to_disk
$self->{"service"}{"buffer_uploads"} $self->decide_to_buffer_to_disk
Truth table100%3/3
A and B
A= $self->{"service"}{"buffer_uploads"}B= $self->decide_to_buffer_to_disk
ABresult
0X0
100
111
750  
751 # give plugins a chance to act on the request before we request a backend
752 # (added by Chris Hondl <chris@imvu.com>, March 2006)
753 33 my $svc = $self->{service};
754 33 ▶ return if $svc->run_hook('proxy_read_request', $self);
Branch: 1 branch
BranchTrueFalseTotal
if $svc->run_hook("proxy_read_request", $self) 0 33 33
755  
756 # if we fall through to here, we need to ensure that a backend is on the
757 # way, because no specialized handling took over above
758 33 print " finally requesting a backend\n" if Perlbal::DEBUG >= 3;
759 33 return $self->request_backend;
760 }
761  
762 152 ▶sub handle_request {
Subroutine handle_request: called CC=15 SCAR=27.4
Pod undocumented
763 152 my Perlbal::ClientProxy $self = shift;
764 152 my $req_hd = $self->{req_headers};
765  
766 152 ▶ unless ($req_hd) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($req_hd) 0 152 152
767 0 $self->close("handle_request without headers");
768 0 return;
769 }
770  
771 152 $self->check_req_headers;
772  
773 152 my $svc = $self->{service};
774 # give plugins a chance to force us to bail
775 152 ▶ return if $svc->run_hook('start_proxy_request', $self);
Branch: 1 branch
BranchTrueFalseTotal
if $svc->run_hook("start_proxy_request", $self) 0 152 152
776 152 ▶ return if $svc->run_hook('start_http_request', $self);
Branch: 1 branch
BranchTrueFalseTotal
if $svc->run_hook("start_http_request", $self) 0 152 152
777  
778 152 ▶ if ($self->handle_chunked_upload) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->handle_chunked_upload) { } 2 150 152
779 # handled in method.
780 } else {
781 # if defined we're waiting on some amount of data. also, we have to
782 # subtract out read_size, which is the amount of data that was
783 # extra in the packet with the header that's part of the body.
784 my $length = $self->{request_body_length} =
785 $self->{content_length_remain} =
786 150 $req_hd->content_length;
787  
788 150 ▶ if (defined $length && $length < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $length and $length < 0) 1 149 150
Condition100%3/3
defined $length and $length < 0
!ll&&!rl&&r
63861
MC/DC100%2/2
defined $length and $length < 0
defined $length $length < 0
Truth table100%3/3
A and B
A= defined $lengthB= $length < 0
ABresult
0X0
100
111
789 1 $self->_simple_response(400, "Invalid request: Content-Length < 0");
790 1 $self->close("negative_content_length");
791 1 return;
792 }
793  
794 149 ▶ $self->{unread_data_waiting} = 1 if $self->{content_length_remain};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"content_length_remain"} 85 64 149
795 }
796  
797 # upload-tracking stuff. both starting a new upload track session,
798 # and checking on status of ongoing one
799 151 ▶ return if $svc->{upload_status_listeners} && $self->handle_upload_tracking;
Branch: 1 branch
BranchTrueFalseTotal
if $svc->{"upload_status_listeners"} and $self->handle_upload_tracking 0 151 151
Condition33%1/3
$svc->{"upload_status_listeners"} and $self->handle_upload_tracking
!ll&&!rl&&r
15100
MC/DC0%0/2
$svc->{"upload_status_listeners"} and $self->handle_upload_tracking
$svc->{"upload_status_listeners"} $self->handle_upload_tracking
Truth table33%1/3
A and B
A= $svc->{"upload_status_listeners"}B= $self->handle_upload_tracking
ABresult
0X0
100
111
800  
801 # note that we've gotten a request
802 151 $self->{requests}++;
803 151 $self->{last_request_time} = $self->{alive_time};
804  
805 # either start buffering some of the request to memory, or
806 # immediately request a backend connection.
807 151 ▶ if ($self->{chunked_upload_state}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'chunked_upload_state'}) { } 2 149 151
elsif ($self->{'content_length_remain'} and $self->{'service'}{'buffer_backend_connect'}) { } 61 88 149
808 2 $self->{request_body_length} = 0;
809 2 $self->{is_buffering} = 1;
810 2 $self->{bureason} = 'chunked';
811 2 $self->buffered_upload_update;
812 ▶ } elsif ($self->{content_length_remain} && $self->{service}->{buffer_backend_connect}) {
Condition100%3/3
$self->{"content_length_remain"} and $self->{"service"}{"buffer_backend_connect"}
!ll&&!rl&&r
642461
MC/DC100%2/2
$self->{"content_length_remain"} and $self->{"service"}{"buffer_backend_connect"}
$self->{"content_length_remain"} $self->{"service"}{"buffer_backend_connect"}
Truth table100%3/3
A and B
A= $self->{"content_length_remain"}B= $self->{"service"}{"buffer_backend_connect"}
ABresult
0X0
100
111
813 # the deeper path
814 61 $self->start_buffering_request;
815 } else {
816 # get the backend request process moving, since we aren't buffering
817 88 $self->{is_buffering} = 0;
818  
819 # if reproxy-caching is enabled, we can often bypass needing to allocate a BackendHTTP connection:
820 88 ▶ return if $svc->{reproxy_cache} && $self->satisfy_request_from_cache;
Branch: 1 branch
BranchTrueFalseTotal
if $svc->{"reproxy_cache"} and $self->satisfy_request_from_cache 6 82 88
Condition100%3/3
$svc->{"reproxy_cache"} and $self->satisfy_request_from_cache
!ll&&!rl&&r
58246
MC/DC100%2/2
$svc->{"reproxy_cache"} and $self->satisfy_request_from_cache
$svc->{"reproxy_cache"} $self->satisfy_request_from_cache
Truth table100%3/3
A and B
A= $svc->{"reproxy_cache"}B= $self->satisfy_request_from_cache
ABresult
0X0
100
111
821  
822 82 $self->request_backend;
823 }
824 }
825  
826 152 ▶sub handle_chunked_upload {
Subroutine handle_chunked_upload: called CC=6 SCAR=18.6
Pod undocumented
827 152 my Perlbal::ClientProxy $self = shift;
828 152 my $req_hd = $self->{req_headers};
829 152 my $te = $req_hd->header("Transfer-Encoding");
830 152 ▶ return unless $te && $te eq "chunked";
Branch: 1 branch
BranchTrueFalseTotal
unless $te and $te eq "chunked" 150 2 152
Condition66%2/3
$te and $te eq "chunked"
!ll&&!rl&&r
15002
MC/DC50%1/2
$te and $te eq "chunked"
$te $te eq "chunked"
Truth table66%2/3
A and B
A= $teB= $te eq "chunked"
ABresult
0X0
100
111
831 2 ▶ return unless $self->{service}->{buffer_uploads};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"service"}{"buffer_uploads"} 0 2 2
832  
833 2 $req_hd->header("Transfer-Encoding", undef); # remove it (won't go to backend)
834  
835 2 my $eh = $req_hd->header("Expect");
836 2 ▶ if ($eh && $eh =~ /\b100-continue\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eh and $eh =~ /\b100-continue\b/) 1 1 2
Condition66%2/3
$eh and $eh =~ /\b100-continue\b/
!ll&&!rl&&r
101
MC/DC50%1/2
$eh and $eh =~ /\b100-continue\b/
$eh $eh =~ /\b100-continue\b/
Truth table66%2/3
A and B
A= $ehB= $eh =~ /\b100-continue\b/
ABresult
0X0
100
111
837 1 $self->write(\ "HTTP/1.1 100 Continue\r\n\r\n");
838 1 $req_hd->header("Expect", undef); # remove it (won't go to backend)
839 }
840  
841 2 my $max_size = $self->{service}{max_chunked_request_size};
842  
843 my $args = {
844 12 ▶ on_new_chunk => sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
845 12 my $cref = shift;
846 12 my $len = length($$cref);
847 12 push @{$self->{read_buf}}, $cref;
848 12 $self->{read_ahead} += $len;
849 12 $self->{request_body_length} += $len;
850  
851 # if too large, disconnect them...
852 12 ▶ if ($max_size && $self->{request_body_length} > $max_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_size and $self->{"request_body_length"} > $max_size) 0 12 12
Condition33%1/3
$max_size and $self->{"request_body_length"} > $max_size
!ll&&!rl&&r
0120
MC/DC0%0/2
$max_size and $self->{"request_body_length"} > $max_size
$max_size $self->{"request_body_length"} > $max_size
Truth table33%1/3
A and B
A= $max_sizeB= $self->{"request_body_length"} > $max_size
ABresult
0X0
100
111
853 0 $self->purge_buffered_upload;
854 0 $self->close;
855 0 return;
856 }
857 12 $self->buffered_upload_update;
858 },
859 0 ▶ on_disconnect => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
860 0 $self->client_disconnected;
861 },
862 2 ▶ on_zero_chunk => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
863 2 $self->send_buffered_upload;
864 },
865 2 };
866  
867 2 $self->{chunked_upload_state} = Perlbal::ChunkedUploadState->new(%$args);
868 2 return 1;
869 }
870  
871 30 ▶sub satisfy_request_from_cache {
Subroutine satisfy_request_from_cache: called CC=11 SCAR=24.1
Pod undocumented
872 30 my Perlbal::ClientProxy $self = shift;
873  
874 30 my $req_hd = $self->{req_headers};
875 30 my $svc = $self->{service};
876 30 my $cache = $svc->{reproxy_cache};
877 30 $svc->{_stat_requests}++;
878  
879 30 ▶ my $requri = $req_hd->request_uri || '';
Condition50%1/2
$req_hd->request_uri || ''
l!l
300
MC/DC0%0/1
$req_hd->request_uri || ''
$req_hd->request_uri
Truth table50%1/2
A
A= $req_hd->request_uri
Aresult
11
00
880 30 ▶ my $hostname = $req_hd->header("Host") || '';
Condition50%1/2
$req_hd->header('Host') || ''
l!l
030
MC/DC0%0/1
$req_hd->header('Host') || ''
$req_hd->header('Host')
Truth table50%1/2
A
A= $req_hd->header('Host')
Aresult
11
00
881  
882 30 my $key = "$hostname|$requri";
883  
884 30 ▶ my $reproxy = $cache->get($key) or
Branch: 1 branch
BranchTrueFalseTotal
my $reproxy = $cache->get($key) or return 0 7 23 30
885 return 0;
886  
887 7 my ($timeout, $headers, $urls) = @$reproxy;
888 7 ▶ return 0 if time() > $timeout;
Branch: 1 branch
BranchTrueFalseTotal
if time > $timeout 1 6 7
889  
890 6 $svc->{_stat_cache_hits}++;
891 6 ▶ my %headers = map { ref $_ eq 'SCALAR' ? $$_ : $_ } @{$headers || []};
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'SCALAR' ? : 18 6 24
Condition50%1/2
$headers || []
l!l
60
MC/DC0%0/1
$headers || []
$headers
Truth table50%1/2
A
A= $headers
Aresult
11
00
892  
893 6 ▶ if (my $ims = $req_hd->header("If-Modified-Since")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ims = $req_hd->header("If-Modified-Since")) 4 2 6
894 4 my ($lm_key) = grep { uc($_) eq "LAST-MODIFIED" } keys %headers;
895 4 ▶ my $lm = $headers{$lm_key} || "";
Condition50%1/2
$headers{$lm_key} || ''
l!l
40
MC/DC0%0/1
$headers{$lm_key} || ''
$headers{$lm_key}
Truth table50%1/2
A
A= $headers{$lm_key}
Aresult
11
00
896  
897 # remove the IE length suffix
898 4 $ims =~ s/; length=(\d+)//;
899  
900 # If 'Last-Modified' is same as 'If-Modified-Since', send a 304
901 4 ▶ if ($ims eq $lm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ims eq $lm) 4 0 4
902 4 my $res_hd = $self->{res_headers} = Perlbal::HTTPHeaders->new_response(304);
903 4 $res_hd->header("Content-Length", "0");
904 4 $self->setup_keepalive($res_hd);
905 4 $self->tcp_cork(1);
906 4 $self->state('xfer_resp');
907 4 $self->write($res_hd->to_string_ref);
908 4 ▶ $self->write(sub { $self->http_response_sent; });
Subroutine __ANON__: called CC=1 SCAR=0.0
909 4 return 1;
910 }
911 }
912  
913 2 my $res_hd = Perlbal::HTTPHeaders->new_response(200);
914 2 $res_hd->header("Date", HTTP::Date::time2str(time()));
915 2 ▶ while (my ($key, $value) = each %headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %headers) 4 2 6
916 4 $res_hd->header($key, $value);
917 }
918  
919 2 $self->start_reproxy_uri($res_hd, $urls);
920 2 return 1;
921 }
922  
923 # return 1 to steal this connection (when they're asking status of an
924 # upload session), return 0 to return it to handle_request's control.
925 0 ▶sub handle_upload_tracking {
Subroutine handle_upload_tracking: not called CC=4 SCAR=30.0
Pod undocumented
926 0 my Perlbal::ClientProxy $self = shift;
927 0 my $req_hd = $self->{req_headers};
928  
929 0 ▶ return 0 unless
Branch: 1 branch
BranchTrueFalseTotal
unless $req_hd->request_uri =~ /[\?&]client_up_sess=(\w{5,50})\b/ 0 0 0
930 $req_hd->request_uri =~ /[\?&]client_up_sess=(\w{5,50})\b/;
931  
932 0 my $sess = $1;
933  
934 # getting status?
935 0 ▶ if ($req_hd->request_uri =~ m!^/__upload_status\?!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req_hd->request_uri =~ m[^/__upload_status\?]) 0 0 0
936 0 my $status = Perlbal::UploadListener::get_status($sess);
937 0 my $now = time();
938 0 ▶ my $body = $status ?
Branch: 1 branch
BranchTrueFalseTotal
$status ? : 0 0 0
939 "{done:$status->{done},total:$status->{total},starttime:$status->{starttime},nowtime:$now}" :
940 "{}";
941  
942 0 my $res = $self->{res_headers} = Perlbal::HTTPHeaders->new_response(200);
943 0 $res->header("Content-Type", "text/plain");
944 0 $res->header('Content-Length', length $body);
945 0 $self->setup_keepalive($res);
946 0 $self->tcp_cork(1); # cork writes to self
947 0 $self->write($res->to_string_ref);
948 0 $self->write(\ $body);
949 0 ▶ $self->write(sub { $self->http_response_sent; });
Subroutine __ANON__: not called CC=1 SCAR=6.9
950 0 return 1;
951 }
952  
953 # otherwise just tagging this upload as a new upload session
954 0 $self->{upload_session} = $sess;
955 0 return 0;
956 }
957  
958 # continuation of handle_request, in the case where we need to start buffering
959 # a bit of the request body to memory, either hoping that's all of it, or to
960 # make a determination of whether or not we should save it all to disk first
961 61 ▶sub start_buffering_request {
Subroutine start_buffering_request: called CC=6 SCAR=17.9
Pod undocumented
962 61 my Perlbal::ClientProxy $self = shift;
963  
964 # buffering case:
965 61 $self->{is_buffering} = 1;
966  
967 # shortcut: if we know that we're buffering by size, and the size
968 # of this upload is bigger than that value, we can just turn on spool
969 # to disk right now...
970 61 ▶ if ($self->{service}->{buffer_uploads} && $self->{service}->{buffer_upload_threshold_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"service"}{"buffer_uploads"} and $self->{"service"}{"buffer_upload_threshold_size"}) 27 34 61
Condition100%3/3
$self->{"service"}{"buffer_uploads"} and $self->{"service"}{"buffer_upload_threshold_size"}
!ll&&!rl&&r
29527
MC/DC100%2/2
$self->{"service"}{"buffer_uploads"} and $self->{"service"}{"buffer_upload_threshold_size"}
$self->{"service"}{"buffer_uploads"} $self->{"service"}{"buffer_upload_threshold_size"}
Truth table100%3/3
A and B
A= $self->{"service"}{"buffer_uploads"}B= $self->{"service"}{"buffer_upload_threshold_size"}
ABresult
0X0
100
111
971 27 my $req_hd = $self->{req_headers};
972 27 ▶ if ($req_hd->content_length >= $self->{service}->{buffer_upload_threshold_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req_hd->content_length >= $self->{"service"}{"buffer_upload_threshold_size"}) 26 1 27
973 26 $self->{bureason} = 'size';
974 26 ▶ if ($ENV{PERLBAL_DEBUG_BUFFERED_UPLOADS}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"PERLBAL_DEBUG_BUFFERED_UPLOADS"}) 2 24 26
975 2 $self->{req_headers}->header('X-PERLBAL-BUFFERED-UPLOAD-REASON', 'size');
976 }
977 26 $self->state('buffering_upload');
978 26 $self->buffered_upload_update;
979 26 return;
980 }
981 }
982  
983 # well, we're buffering, but we're not going to disk just yet (but still might)
984 35 $self->state('buffering_request');
985  
986 # only need time if we are using the buffer to disk functionality
987 $self->{start_time} = [ gettimeofday() ]
988 35 ▶ if $self->{service}->{buffer_uploads};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"service"}{"buffer_uploads"} 6 29 35
989 }
990  
991 # looks at our states and decides if we should start writing to disk
992 # or should just go ahead and blast this to the backend. returns 1
993 # if the decision was made to buffer to disk
994 6 ▶sub decide_to_buffer_to_disk {
Subroutine decide_to_buffer_to_disk: called CC=10 SCAR=23.1
Pod undocumented
995 6 my Perlbal::ClientProxy $self = shift;
996 6 ▶ return unless $self->{is_buffering};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"is_buffering"} 0 6 6
997 6 ▶ return $self->{bureason} if defined $self->{bureason};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"bureason"} 0 6 6
998  
999 # this is called when we have enough data to determine whether or not to
1000 # start buffering to disk
1001 6 ▶ my $dur = tv_interval($self->{start_time}) || 1;
Condition50%1/2
tv_interval($self->{'start_time'}) || 1
l!l
60
MC/DC0%0/1
tv_interval($self->{'start_time'}) || 1
tv_interval($self->{'start_time'})
Truth table50%1/2
A
A= tv_interval($self->{'start_time'})
Aresult
11
00
1002 6 my $rate = $self->{read_ahead} / $dur;
1003 6 my $etime = $self->{content_length_remain} / $rate;
1004  
1005 # see if we have enough data to make the determination
1006 6 my $reason = undef;
1007  
1008 # see if we blow the rate away
1009 6 ▶ if ($self->{service}->{buffer_upload_threshold_rate} > 0 &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"service"}{"buffer_upload_threshold_rate"} > 0 and $rate < $self->{"service"}{"buffer_upload_threshold_rate"}) 1 5 6
Condition100%3/3
$self->{"service"}{"buffer_upload_threshold_rate"} > 0 and $rate < $self->{"service"}{"buffer_upload_threshold_rate"}
!ll&&!rl&&r
411
MC/DC100%2/2
$self->{"service"}{"buffer_upload_threshold_rate"} > 0 and $rate < $self->{"service"}{"buffer_upload_threshold_rate"}
$self->{"service"}{"buffer_upload_threshold_rate"} > 0 $rate < $self->{"service"}{"buffer_upload_threshold_rate"}
Truth table100%3/3
A and B
A= $self->{"service"}{"buffer_upload_threshold_rate"} > 0B= $rate < $self->{"service"}{"buffer_upload_threshold_rate"}
ABresult
0X0
100
111
1010 $rate < $self->{service}->{buffer_upload_threshold_rate}) {
1011 # they are slower than the minimum rate
1012 1 $reason = 'rate';
1013 }
1014  
1015 # and finally check estimated time exceeding
1016 6 ▶ if ($self->{service}->{buffer_upload_threshold_time} > 0 &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"service"}{"buffer_upload_threshold_time"} > 0 and $etime > $self->{"service"}{"buffer_upload_threshold_time"}) 1 5 6
Condition100%3/3
$self->{"service"}{"buffer_upload_threshold_time"} > 0 and $etime > $self->{"service"}{"buffer_upload_threshold_time"}
!ll&&!rl&&r
411
MC/DC100%2/2
$self->{"service"}{"buffer_upload_threshold_time"} > 0 and $etime > $self->{"service"}{"buffer_upload_threshold_time"}
$self->{"service"}{"buffer_upload_threshold_time"} > 0 $etime > $self->{"service"}{"buffer_upload_threshold_time"}
Truth table100%3/3
A and B
A= $self->{"service"}{"buffer_upload_threshold_time"} > 0B= $etime > $self->{"service"}{"buffer_upload_threshold_time"}
ABresult
0X0
100
111
1017 $etime > $self->{service}->{buffer_upload_threshold_time}) {
1018 # exceeds
1019 1 $reason = 'time';
1020 }
1021  
1022 6 ▶ unless ($reason) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reason) 4 2 6
1023 4 $self->{is_buffering} = 0;
1024 4 return 0;
1025 }
1026  
1027 # start saving it to disk
1028 2 $self->state('buffering_upload');
1029 2 $self->buffered_upload_update;
1030 2 $self->{bureason} = $reason;
1031  
1032 2 ▶ if ($ENV{PERLBAL_DEBUG_BUFFERED_UPLOADS}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"PERLBAL_DEBUG_BUFFERED_UPLOADS"}) 2 0 2
1033 2 $self->{req_headers}->header('X-PERLBAL-BUFFERED-UPLOAD-REASON', $reason);
1034 }
1035  
1036 2 return 1;
1037 }
1038  
1039 # take ourselves and send along our buffered data to the backend
1040 29 ▶sub send_buffered_upload {
Subroutine send_buffered_upload: called CC=5 SCAR=16.6
Pod undocumented
1041 29 my Perlbal::ClientProxy $self = shift;
1042  
1043 # make sure our buoutpos is the same as the content length...
1044 29 ▶ return if $self->{is_writing};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"is_writing"} 0 29 29
1045  
1046 # set the content-length that goes to the backend...
1047 29 ▶ if ($self->{chunked_upload_state}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"chunked_upload_state"}) 2 27 29
1048 2 $self->{req_headers}->header("Content-Length", $self->{request_body_length});
1049 }
1050  
1051 29 my $clen = $self->{req_headers}->content_length;
1052 29 ▶ if ($clen != $self->{buoutpos}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clen != $self->{"buoutpos"}) 0 29 29
1053 0 Perlbal::log('crit', "Content length of $clen declared but $self->{buoutpos} bytes written to disk");
1054 0 return $self->_simple_response(500);
1055 }
1056  
1057 # reset our position so we start reading from the right spot
1058 29 $self->{buoutpos} = 0;
1059 29 ▶ sysseek($self->{bufh}, 0, 0) if ($self->{bufh}); # But only if it exists at all
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"bufh"} 28 1 29
1060  
1061 # notify that we want the backend so we get the ball rolling
1062 29 $self->request_backend;
1063 }
1064  
1065 29 ▶sub continue_buffered_upload {
Subroutine continue_buffered_upload: called CC=8 SCAR=25.4
Pod undocumented
1066 29 my Perlbal::ClientProxy $self = shift;
1067 29 my Perlbal::BackendHTTP $be = shift;
1068 29 ▶ return unless $self && $be;
Branch: 1 branch
BranchTrueFalseTotal
unless $self and $be 0 29 29
Condition33%1/3
$self and $be
!ll&&!rl&&r
0029
MC/DC0%0/2
$self and $be
$self $be
Truth table33%1/3
A and B
A= $selfB= $be
ABresult
0X0
100
111
1069  
1070 # now send the data
1071 29 my $clen = $self->{request_body_length};
1072  
1073 29 ▶ if ($self->{buoutpos} < $clen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"buoutpos"} < $clen) 28 1 29
1074 28 my $sent = Perlbal::Socket::sendfile($be->{fd}, fileno($self->{bufh}), $clen - $self->{buoutpos});
1075 28 ▶ if ($sent < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sent < 0) 0 28 28
1076 0 ▶ return $self->close("epipe") if $! == EPIPE;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 32 0 0 0
1077 0 ▶ return $self->close("connreset") if $! == ECONNRESET;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 104 0 0 0
1078 0 print STDERR "Error w/ sendfile: $!\n";
1079 0 return $self->close('sendfile_error');
1080 }
1081 28 $self->{buoutpos} += $sent;
1082 }
1083  
1084 # if we're done, purge the file and move on
1085 29 ▶ if ($self->{buoutpos} >= $clen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"buoutpos"} >= $clen) 29 0 29
1086 29 $be->{buffered_upload_mode} = 0;
1087 29 $self->purge_buffered_upload;
1088 29 return;
1089 }
1090  
1091 # we will be called again by the backend since buffered_upload_mode is on
1092 }
1093  
1094 # write data to disk
1095 150 ▶sub buffered_upload_update {
Subroutine buffered_upload_update: called CC=9 SCAR=24.5
Pod undocumented
1096 150 my Perlbal::ClientProxy $self = shift;
1097 # Reading too far ahead of our AIO subsystem will cause us to buffer it in memory.
1098 150 ▶ $self->watch_read(0) if $self->{read_ahead} >= 1024 * 1024; # arbitrary
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"read_ahead"} >= 1048576 0 150 150
1099 150 ▶ return if $self->{is_writing};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"is_writing"} 0 150 150
1100 150 ▶ return unless $self->{is_buffering} && $self->{read_ahead};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"is_buffering"} and $self->{"read_ahead"} 61 89 150
Condition66%2/3
$self->{"is_buffering"} and $self->{"read_ahead"}
!ll&&!rl&&r
06189
MC/DC50%1/2
$self->{"is_buffering"} and $self->{"read_ahead"}
$self->{"is_buffering"} $self->{"read_ahead"}
Truth table66%2/3
A and B
A= $self->{"is_buffering"}B= $self->{"read_ahead"}
ABresult
0X0
100
111
1101  
1102 # so we're not writing now and we have data to write...
1103 89 ▶ unless ($self->{bufilename}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"bufilename"}) 29 60 89
1104 # create a filename and see if it exists or not
1105 29 $self->{is_writing} = 1;
1106 29 my $fn = join('-', $self->{service}->name, $self->{service}->listenaddr, "client", $self->{fd}, int(rand(0xffffffff)));
1107 29 $fn = $self->{service}->{buffer_uploads_path} . '/' . $fn;
1108  
1109 # good, now we need to create the file
1110 29 ▶ Perlbal::AIO::aio_open($fn, O_CREAT | O_TRUNC | O_RDWR, 0644, sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
1111 29 $self->{is_writing} = 0;
1112 29 $self->{bufh} = shift;
1113  
1114 # throw errors back to the user
1115 29 ▶ if (! $self->{bufh}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"bufh"}) 0 29 29
1116 0 Perlbal::log('crit', "Failure to open $fn for buffered upload output");
1117 0 return $self->_simple_response(500);
1118 }
1119  
1120 # save state and info and bounce it back to write data
1121 29 $self->{bufilename} = $fn;
1122 29 $self->buffered_upload_update;
1123 29 });
1124  
1125 29 return;
1126 }
1127  
1128 # can't proceed if we have no disk file to async write to
1129 # people reported seeing this crash rarely in production...
1130 # must be a race between previously in-flight's write
1131 # re-invoking a write immediately after something triggered
1132 # a buffered upload purge.
1133 60 ▶ unless ($self->{bufh}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"bufh"}) 0 60 60
1134 0 $self->close;
1135 0 return;
1136 }
1137  
1138 # at this point, we want to do some writing
1139 60 my $bref = \join("", map { $$_ } @{$self->{read_buf}});
1140 60 $self->{read_buf} = []; # clear these out
1141 60 $self->{read_ahead} = 0;
1142 60 my $len = length $$bref;
1143  
1144 # After copying out and clearing the buffer, turn reads back on again to fill up another buffer.
1145 60 ▶ $self->watch_read(1) if $self->{content_length_remain} || $self->{chunked_upload_state};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"content_length_remain"} or $self->{"chunked_upload_state"} 33 27 60
Condition100%3/3
$self->{"content_length_remain"} or $self->{"chunked_upload_state"}
l!l&&r!l&&!r
211227
MC/DC100%2/2
$self->{"content_length_remain"} or $self->{"chunked_upload_state"}
$self->{"content_length_remain"} $self->{"chunked_upload_state"}
Truth table100%3/3
A or B
A= $self->{"content_length_remain"}B= $self->{"chunked_upload_state"}
ABresult
1X1
011
000
1146  
1147 # so at this point we have a valid filename and file handle and should write out
1148 # the buffer that we have
1149 60 $self->{is_writing} = 1;
1150 60 ▶ Perlbal::AIO::aio_write($self->{bufh}, $self->{buoutpos}, $len, $$bref, sub {
Subroutine __ANON__: called CC=10 SCAR=30.0
1151 60 my $bytes = shift;
1152 60 $self->{is_writing} = 0;
1153  
1154 # check for error
1155 60 ▶ unless ($bytes > 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bytes > 0) 0 60 60
1156 0 Perlbal::log('crit', "Error writing buffered upload: $!. Tried to do $len bytes at $self->{buoutpos}.");
1157 0 return $self->_simple_response(500);
1158 }
1159  
1160 # update our count of data written
1161 60 $self->{buoutpos} += $bytes;
1162  
1163 # now check if we wrote less than we had in this chunk of buffer. if that's
1164 # the case then we need to re-enqueue the part of the chunk that wasn't
1165 # written out and update as appropriate.
1166 60 ▶ if ($bytes < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes < $len) 0 60 60
1167 0 my $diff = $len - $bytes;
1168 0 unshift @{$self->{read_buf}}, \ substr($$bref, $bytes, $diff);
1169 0 $self->{read_ahead} += $diff;
1170 }
1171  
1172 # if we're processing a chunked upload, ...
1173 60 ▶ if ($self->{chunked_upload_state}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'chunked_upload_state'}) { } 12 48 60
elsif ($self->{'read_ahead'} <= 0 and $self->{'content_length_remain'} <= 0) { } 27 21 48
1174 # turn reads back on, if we haven't hit the end yet.
1175 12 ▶ if ($self->{unread_data_waiting} && $self->{read_ahead} < 1024*1024) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"unread_data_waiting"} and $self->{"read_ahead"} < 1048576) 0 12 12
Condition33%1/3
$self->{"unread_data_waiting"} and $self->{"read_ahead"} < 1048576
!ll&&!rl&&r
1200
MC/DC0%0/2
$self->{"unread_data_waiting"} and $self->{"read_ahead"} < 1048576
$self->{"unread_data_waiting"} $self->{"read_ahead"} < 1048576
Truth table33%1/3
A and B
A= $self->{"unread_data_waiting"}B= $self->{"read_ahead"} < 1048576
ABresult
0X0
100
111
1176 0 $self->watch_read(1);
1177 0 $self->{unread_data_waiting} = 0;
1178 }
1179  
1180 12 ▶ if ($self->{read_ahead} == 0 && $self->{chunked_upload_state}->hit_zero_chunk) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"read_ahead"} == 0 and $self->{"chunked_upload_state"}->hit_zero_chunk) 0 12 12
Condition33%1/3
$self->{"read_ahead"} == 0 and $self->{"chunked_upload_state"}->hit_zero_chunk
!ll&&!rl&&r
0120
MC/DC0%0/2
$self->{"read_ahead"} == 0 and $self->{"chunked_upload_state"}->hit_zero_chunk
$self->{"read_ahead"} == 0 $self->{"chunked_upload_state"}->hit_zero_chunk
Truth table33%1/3
A and B
A= $self->{"read_ahead"} == 0B= $self->{"chunked_upload_state"}->hit_zero_chunk
ABresult
0X0
100
111
1181 0 $self->watch_read(0);
1182 0 $self->send_buffered_upload;
1183 0 return;
1184 }
1185 }
1186  
1187 # if we're done (no clr and no read ahead!) then send it
1188 ▶ elsif ($self->{read_ahead} <= 0 && $self->{content_length_remain} <= 0) {
Condition66%2/3
$self->{"read_ahead"} <= 0 and $self->{"content_length_remain"} <= 0
!ll&&!rl&&r
02127
MC/DC50%1/2
$self->{"read_ahead"} <= 0 and $self->{"content_length_remain"} <= 0
$self->{"read_ahead"} <= 0 $self->{"content_length_remain"} <= 0
Truth table66%2/3
A and B
A= $self->{"read_ahead"} <= 0B= $self->{"content_length_remain"} <= 0
ABresult
0X0
100
111
1189 27 $self->send_buffered_upload;
1190 27 return;
1191 }
1192  
1193 # spawn another writer!
1194 33 $self->buffered_upload_update;
1195 60 });
1196 }
1197  
1198 # destroy any files we've created
1199 30 ▶sub purge_buffered_upload {
Subroutine purge_buffered_upload: called CC=4 SCAR=15.2
Pod undocumented
1200 30 my Perlbal::ClientProxy $self = shift;
1201  
1202 # Main reason for failure below is a 0-length chunked upload, where the file is never created.
1203 30 ▶ return unless $self->{bufh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"bufh"} 1 29 30
1204  
1205 # FIXME: it's reported that sometimes the two now-in-eval blocks
1206 # fail, hence the eval blocks and warnings. the FIXME is to
1207 # figure this out, why it happens sometimes.
1208  
1209 # first close our filehandle... not async
1210 29 eval {
1211 29 CORE::close($self->{bufh});
1212 };
1213 0 ▶ if ($@) { warn "Error closing file in ClientProxy::purge_buffered_upload: $@\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 29 29
1214  
1215 29 $self->{bufh} = undef;
1216  
1217 29 eval {
1218 # now asynchronously unlink the file
1219 29 ▶ Perlbal::AIO::aio_unlink($self->{bufilename}, sub {
Subroutine __ANON__: called CC=3 SCAR=15.4
1220 29 ▶ if ($_[0] != 0 && $!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] != 0 and $!) 0 29 29
Condition33%1/3
$_[0] != 0 and $!
!ll&&!rl&&r
2900
MC/DC0%0/2
$_[0] != 0 and $!
$_[0] != 0 $!
Truth table33%1/3
A and B
A= $_[0] != 0B= $!
ABresult
0X0
100
111
1221 # note an error, but whatever, we'll either overwrite the file later (O_TRUNC | O_CREAT)
1222 # or a cleaner will come through and do it for us someday (if the user runs one)
1223 0 Perlbal::log('warning', "Unable to link $self->{bufilename}: $!");
1224 }
1225 29 });
1226 };
1227 0 ▶ if ($@) { warn "Error unlinking file in ClientProxy::purge_buffered_upload: $@\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 29 29
1228 }
1229  
1230 # returns bool; whether backend should hide the 500 error from the client
1231 # and have us try a new backend. return true to retry, false to get a 500 error.
1232 0 ▶sub should_retry_after_500 {
Subroutine should_retry_after_500: not called CC=3 SCAR=24.8
Pod undocumented
1233 0 my Perlbal::ClientProxy $self = shift;
1234 0 my Perlbal::BackendHTTP $be = shift;
1235 0 my $svc = $be->{service};
1236 0 ▶ return 0 unless $svc->{enable_error_retries};
Branch: 1 branch
BranchTrueFalseTotal
unless $svc->{"enable_error_retries"} 0 0 0
1237 0 my @sched = split(/\s*,\s*/, $svc->{error_retry_schedule});
1238 0 ▶ return 0 if ++$self->{retry_count} > @sched;
Branch: 1 branch
BranchTrueFalseTotal
if ++$self->{"retry_count"} > @sched 0 0 0
1239 0 return 1;
1240 }
1241  
1242 # called by Backend to tell us it got a 500 error and we should retry another backend.
1243 0 ▶sub retry_after_500 {
Subroutine retry_after_500: not called CC=2 SCAR=17.9
Pod undocumented
1244 0 my Perlbal::ClientProxy $self = shift;
1245 0 my Perlbal::Service $svc = shift;
1246  
1247 0 my @sched = split(/\s*,\s*/, $svc->{error_retry_schedule});
1248 0 my $delay = $sched[$self->{retry_count} - 1];
1249  
1250 0 ▶ if ($delay) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delay) { } 0 0 0
1251 0 ▶ Danga::Socket->AddTimer($delay, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1252 0 ▶ return if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 0 0
1253 0 $self->rerequest_backend;
1254 0 });
1255 } else {
1256 0 $self->rerequest_backend;
1257 }
1258  
1259 }
1260  
1261 0 ▶sub as_string {
Subroutine as_string: not called CC=8 SCAR=42.8
Pod documented
1262 0 my Perlbal::ClientProxy $self = shift;
1263  
1264 0 my $ret = $self->SUPER::as_string;
1265 0 ▶ if ($self->{backend}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'backend'}) { } 0 0 0
1266 0 my $ipport = $self->{backend}->{ipport};
1267 0 $ret .= "; backend=$ipport";
1268 } else {
1269 $ret .= "; write_buf_size=$self->{write_buf_size}"
1270 0 ▶ if $self->{write_buf_size} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"write_buf_size"} > 0 0 0 0
1271 }
1272 0 ▶ $ret .= "; highpri" if $self->{high_priority};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"high_priority"} 0 0 0
1273 0 ▶ $ret .= "; lowpri" if $self->{low_priority};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"low_priority"} 0 0 0
1274 0 ▶ $ret .= "; responded" if $self->{responded};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"responded"} 0 0 0
1275 $ret .= "; waiting_for=" . $self->{content_length_remain}
1276 0 ▶ if defined $self->{content_length_remain};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"content_length_remain"} 0 0 0
1277 0 ▶ $ret .= "; reproxying" if $self->{currently_reproxying};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"currently_reproxying"} 0 0 0
1278  
1279 0 return $ret;
1280 }
1281  
1282 0 ▶sub set_queue_low {
Subroutine set_queue_low: not called CC=1 SCAR=6.9
Pod undocumented
1283 0 my Perlbal::ClientProxy $self = shift;
1284 0 $self->{low_priority} = 1;
1285 0 return;
1286 }
1287  
1288 0 ▶sub set_queue_high {
Subroutine set_queue_high: not called CC=1 SCAR=6.9
Pod undocumented
1289 0 my Perlbal::ClientProxy $self = shift;
1290 0 $self->{high_priority} = 1;
1291 0 return;
1292 }
1293  
1294  
1295 81 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
1296 81 Perlbal::objdtor($_[0]);
1297 81 $_[0]->SUPER::DESTROY;
1298 }
1299  
1300 22 1;
1301  
1302  
1303 # Local Variables:
1304 # mode: perl
1305 # c-basic-indent: 4
1306 # indent-tabs-mode: nil
1307 # End:
« Perlbal/Plugin/MaxContentLength.pm ↑ Summary Perlbal/UploadListener.pm »