blib/lib/LWPx/TimedHTTP.pm

filter: Statementstmt 55.7% 145 / 260 Branchbran 23.9% 35 / 146 Conditioncond 12.5% 7 / 56 MC/DCmcdc 0.0% 0 / 34 Subroutinesub 93.7% 15 / 16 Podpod 100% 1 / 1 totaltotal 39.5% 203 / 513 CC 95 SCAR 73.5
CC
95
Cov
40%
request
2069.2
_get_addr
20.0
__ANON__
7.3
CRAP 1562.7
↑ Summary
1 package LWPx::TimedHTTP;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 require LWP::Debug;
7 1 require HTTP::Response;
8 1 require HTTP::Status;
9 1 require Net::HTTP;
10 1 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use vars qw(@ISA @EXTRA_SOCK_OPTS $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 $VERSION = "1.8";
15  
16 =pod
17  
18 =head1 NAME
19  
20 LWPx::TimedHTTP - time the different stages of an HTTP request
21  
22 =head1 SYNOPSIS
23  
24 # do the work for you
25 use LWP::UserAgent;
26 use LWPx::TimedHTTP qw(:autoinstall);
27  
28 # now just continue as normal
29 my $ua = LWP::UserAgent->new;
30 my $response = $ua->get("http://thegestalt.org");
31 # ... with optional retrieving of metrics (in seconds)
32 printf "%f\n", $response->header('Client-Request-Connect-Time');
33  
34  
35 # or if you don't like magic going on in the background
36 use LWP::UserAgent;
37 use LWP::Protocol;
38 use LWPx::TimedHTTP;
39  
40 LWP::Protocol::implementor('http', 'LWPx::TimedHTTP');
41  
42 # or for https ....
43 LWP::Protocol::implementor('https', 'LWPx::TimedHTTP::https');
44  
45 my $ua = LWP::UserAgent->new;
46 my $response = $ua->get("http://thegestalt.org");
47 printf "%f\n", $response->header('Client-Request-Connect-Time');
48  
49 =head1 DESCRIPTION
50  
51 This module performs an HTTP request exactly the same
52 as B<LWP> does normally except for the fact that it
53 times each stage of the request and then inserts the
54 results as header.
55  
56 It's useful for debugging where abouts in a connection slow downs
57 are occuring.
58  
59 =head1 METRICS
60  
61 All times returned are in seconds
62  
63 =head2 Client-Request-Dns-Time
64  
65 The time it took to do a DNS lookup on the host.
66  
67 B<NOTE:> The value of this timing is NOT thread safe since it
68 has to smuggle the data back via a global variable.
69  
70 =head2 Client-Request-Connect-Time
71  
72 The time it took to connect to the remote server
73  
74 =head2 Client-Request-Transmit-Time
75  
76 The time it took to transmit the request
77  
78 =head2 Client-Response-Server-Time
79  
80 Time it took to respond to the request
81  
82 =head2 Client-Response-Receive-Time
83  
84 Time it took to get the data back
85  
86 =head1 AUTHOR
87  
88 Simon Wistow <simon@thegestalt.org>
89  
90 Based entirely on work by David Carter -
91 this module is a little light frobbing and some packaging of
92 code he posted to the libwww-perl mailing list in response to
93 one of my questions.
94  
95 His code was, in turn, based on B<LWP::Protocol::http> by
96 Gisle Aas which is distributed as part of the B<libwww> package.
97  
98 =head1 COPYING
99  
100 (c)opyright 2002, Simon Wistow
101  
102 Distributed under the same terms as Perl itself.
103  
104 This software is under no warranty and will probably ruin your life, kill your friends, burn your house and bring about the apocalypse
105  
106 =head1 BUGS
107  
108 None known
109  
110 =head1 SEE ALSO
111  
112 L<LWP::UserAgent>, L<Time::HiRes>
113  
114 =cut
115  
116  
117 1 ▶sub import {
Subroutine import: called CC=4 SCAR=14.2
118 1 my $class = shift;
119 1 ▶ my $command = shift || return;
Condition50%1/2
shift() || (return)
l!l
10
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
120  
121 1 ▶ croak "No such option '$command'\n" unless $command eq ':autoinstall';
Branch: 1 branch
BranchTrueFalseTotal
unless $command eq ":autoinstall" 0 1 1
122 1 eval { require LWP::Protocol };
123 1 ▶ croak "Requiring of LWP::Protocol failed - $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
124  
125 1 LWP::Protocol::implementor('http', __PACKAGE__);
126 1 LWP::Protocol::implementor('https', "LWPx::TimedHTTP::https");
127  
128 }
129  
130  
131 1 require LWP::Protocol::http;
132 1 @ISA = qw(LWP::Protocol::http);
133  
134 1 my $CRLF = "\015\012";
135  
136 sub request
137 2 ▶{
Subroutine request: called CC=83 SCAR=76.3
Pod documented
138 2 my($self, $request, $proxy, $arg, $size, $timeout) = @_;
139  
140 2 ▶ $size ||= 4096;
Condition50%1/2
$size ||= 4096
l!l
02
MC/DC0%0/1
$size ||= 4096
$size
Truth table50%1/2
A
A= $size
Aresult
11
00
141  
142 # check method
143 2 my $method = $request->method;
144 2 ▶ unless ($method =~ /^[A-Za-z0-9_!\#\$%&\'*+\-.^\`|~]+$/) { # HTTP token
Branch: 1 branch
BranchTrueFalseTotal
unless ($method =~ /^[A-Za-z0-9_!\#\$%&\'*+\-.^\`|~]+$/) 0 2 2
145 0 return HTTP::Response->new( &HTTP::Status::RC_BAD_REQUEST,
146 'Library does not allow method ' .
147 "$method for 'http:' URLs");
148 }
149  
150 2 my $url = $request->uri;
151 2 my($host, $port, $fullpath);
152  
153 # Check if we're proxy'ing
154 2 ▶ if (defined $proxy) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proxy) { } 0 2 2
155 # $proxy is an URL to an HTTP server which will proxy this request
156 0 $host = $proxy->host;
157 0 $port = $proxy->port;
158 0 ▶ $fullpath = $method eq "CONNECT" ?
Branch: 1 branch
BranchTrueFalseTotal
$method eq 'CONNECT' ? : 0 0 0
159 ($url->host . ":" . $url->port) :
160 $url->as_string;
161 }
162 else {
163 2 $host = $url->host;
164 2 $port = $url->port;
165 2 $fullpath = $url->path_query;
166 2 ▶ $fullpath = "/$fullpath" unless $fullpath =~ m,^/,;
Branch: 1 branch
BranchTrueFalseTotal
unless $fullpath =~ m[^/] 1 1 2
167 }
168
169 2 my $prev_time = [gettimeofday];
170 2 my $this_time;
171  
172 # connect to remote site
173 2 my $socket = $self->_new_socket($host, $port, $timeout);
174  
175 2 $this_time = [gettimeofday];
176  
177 2 my $http_version = "";
178 2 ▶ if (my $proto = $request->protocol) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $proto = $request->protocol) 0 2 2
179 0 ▶ if ($proto =~ /^(?:HTTP\/)?(1.\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto =~ m[^(?:HTTP/)?(1.\d+)$]) 0 0 0
180 0 $http_version = $1;
181 0 $socket->http_version($http_version);
182 0 ▶ $socket->send_te(0) if $http_version eq "1.0";
Branch: 1 branch
BranchTrueFalseTotal
if $http_version eq "1.0" 0 0 0
183 }
184 }
185  
186 2 $self->_check_sock($request, $socket);
187  
188 2 my @h;
189 2 my $request_headers = $request->headers->clone;
190 2 $self->_fixup_header($request_headers, $url, $proxy);
191  
192 4 ▶ $request_headers->scan(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
193 4 my($k, $v) = @_;
194 4 $k =~ s/^://;
195 4 $v =~ s/\n/ /g;
196 4 push(@h, $k, $v);
197 2 });
198  
199 2 my $content_ref = $request->content_ref;
200 2 ▶ $content_ref = $$content_ref if ref($$content_ref);
Branch: 1 branch
BranchTrueFalseTotal
if ref $$content_ref 0 2 2
201 2 my $chunked;
202 my $has_content;
203  
204 2 ▶ if (ref($content_ref) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content_ref eq 'CODE') { } 0 2 2
205 0 my $clen = $request_headers->header('Content-Length');
206 0 ▶ $has_content++ if $clen;
Branch: 1 branch
BranchTrueFalseTotal
if $clen 0 0 0
207 0 ▶ unless (defined $clen) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $clen) 0 0 0
208 0 push(@h, "Transfer-Encoding" => "chunked");
209 0 $has_content++;
210 0 $chunked++;
211 }
212 }
213 else {
214 # Set (or override) Content-Length header
215 2 my $clen = $request_headers->header('Content-Length');
216 2 ▶ if (defined($$content_ref) && length($$content_ref)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $$content_ref and length $$content_ref) { } 0 2 2
elsif ($clen) { } 0 2 2
Condition33%1/3
defined $$content_ref and length $$content_ref
!ll&&!rl&&r
020
MC/DC0%0/2
defined $$content_ref and length $$content_ref
defined $$content_ref length $$content_ref
Truth table33%1/3
A and B
A= defined $$content_refB= length $$content_ref
ABresult
0X0
100
111
217 0 $has_content = length($$content_ref);
218 0 ▶ if (!defined($clen) || $clen ne $has_content) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $clen or $clen ne $has_content) 0 0 0
Condition0%0/3
not defined $clen or $clen ne $has_content
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $clen or $clen ne $has_content
not defined $clen $clen ne $has_content
Truth table0%0/3
not(A) or B
A= not defined $clenB= $clen ne $has_content
ABresult
1X1
011
000
219 0 ▶ if (defined $clen) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $clen) 0 0 0
220 0 warn "Content-Length header value was wrong, fixed";
221 0 hlist_remove(\@h, 'Content-Length');
222 }
223 0 push(@h, 'Content-Length' => $has_content);
224 }
225 }
226 elsif ($clen) {
227 0 warn "Content-Length set when there is no content, fixed";
228 0 hlist_remove(\@h, 'Content-Length');
229 }
230 }
231  
232 2 my $write_wait = 0;
233 2 ▶ $write_wait = 2
Branch: 1 branch
BranchTrueFalseTotal
if ($request_headers->header("Expect") || "") =~ /100-continue/ 0 2 2
Condition50%1/2
$request_headers->header('Expect') || ''
l!l
02
MC/DC0%0/1
$request_headers->header('Expect') || ''
$request_headers->header('Expect')
Truth table50%1/2
A
A= $request_headers->header('Expect')
Aresult
11
00
234 if ($request_headers->header("Expect") || "") =~ /100-continue/;
235  
236 2 my $req_buf = $socket->format_request($method, $fullpath, @h);
237 #print "------\n$req_buf\n------\n";
238  
239 2 ▶ if (!$has_content || $write_wait || $has_content > 8*1024) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $has_content or $write_wait or $has_content > 8192) 2 0 2
Condition33%2/6
not $has_content or $write_wait or $has_content > 8192
l!l&&r!l&&!r
200
not $has_content or $write_wait
l!l&&r!l&&!r
200
MC/DC0%0/3
not $has_content or $write_wait or $has_content > 8192
not $has_content $write_wait $has_content > 8192
Truth table25%1/4
not(A) or B or C
A= not $has_contentB= $write_waitC= $has_content > 8192
ABCresult
1XX1
01X1
0011
0000
240 WRITE:
241 {
242 # Since this just writes out the header block it should almost
243 # always succeed to send the whole buffer in a single write call.
244 2 my $n = $socket->syswrite($req_buf, length($req_buf));
245 2 ▶ unless (defined $n) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n) 0 2 2
246 0 ▶ redo WRITE if $!{EINTR};
Subroutine BEGIN: called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EINTR"} 0 0 0
247 0 ▶ if ($!{EAGAIN}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!{"EAGAIN"}) 0 0 0
248 0 select(undef, undef, undef, 0.1);
249 0 redo WRITE;
250 }
251 0 die "write failed: $!";
252 }
253 2 ▶ if ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) { } 2 0 2
254 2 substr($req_buf, 0, $n, "");
255 }
256 else {
257 0 select(undef, undef, undef, 0.5);
258 }
259 2 ▶ redo WRITE if length $req_buf;
Branch: 1 branch
BranchTrueFalseTotal
if length $req_buf 0 2 2
260 }
261 }
262  
263 2 my($code, $mess, @junk);
264 2 my $drop_connection;
265  
266 2 ▶ if ($has_content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_content) 0 2 2
267 0 my $eof;
268 my $wbuf;
269 0 my $woffset = 0;
270 INITIAL_READ:
271 0 ▶ if ($write_wait) {
Branch: 2 branches
BranchTrueFalseTotal
if ($write_wait) { } 0 0 0
elsif (ref $content_ref eq 'CODE') { } 0 0 0
272 # skip filling $wbuf when waiting for 100-continue
273 # because if the response is a redirect or auth required
274 # the request will be cloned and there is no way
275 # to reset the input stream
276 # return here via the label after the 100-continue is read
277 }
278 elsif (ref($content_ref) eq 'CODE') {
279 0 my $buf = &$content_ref();
280 0 ▶ $buf = "" unless defined($buf);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $buf 0 0 0
281 0 ▶ $buf = sprintf "%x%s%s%s", length($buf), $CRLF, $buf, $CRLF
Branch: 1 branch
BranchTrueFalseTotal
if $chunked 0 0 0
282 if $chunked;
283 0 ▶ substr($buf, 0, 0) = $req_buf if $req_buf;
Branch: 1 branch
BranchTrueFalseTotal
if $req_buf 0 0 0
284 0 $wbuf = \$buf;
285 }
286 else {
287 0 ▶ if ($req_buf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req_buf) { } 0 0 0
288 0 my $buf = $req_buf . $$content_ref;
289 0 $wbuf = \$buf;
290 }
291 else {
292 0 $wbuf = $content_ref;
293 }
294 0 $eof = 1;
295 }
296  
297 0 my $fbits = '';
298 0 vec($fbits, fileno($socket), 1) = 1;
299  
300 WRITE:
301 0 ▶ while ($write_wait || $woffset < length($$wbuf)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($write_wait or $woffset < length $$wbuf) 0 0 0
Condition0%0/3
$write_wait or $woffset < length $$wbuf
l!l&&r!l&&!r
000
MC/DC0%0/2
$write_wait or $woffset < length $$wbuf
$write_wait $woffset < length $$wbuf
Truth table0%0/3
A or B
A= $write_waitB= $woffset < length $$wbuf
ABresult
1X1
011
000
302  
303 0 my $sel_timeout = $timeout;
304 0 ▶ if ($write_wait) {
Branch: 1 branch
BranchTrueFalseTotal
if ($write_wait) 0 0 0
305 0 ▶ $sel_timeout = $write_wait if $write_wait < $sel_timeout;
Branch: 1 branch
BranchTrueFalseTotal
if $write_wait < $sel_timeout 0 0 0
306 }
307 0 my $time_before;
308 0 ▶ $time_before = time if $sel_timeout;
Branch: 1 branch
BranchTrueFalseTotal
if $sel_timeout 0 0 0
309  
310 0 my $rbits = $fbits;
311 0 ▶ my $wbits = $write_wait ? undef : $fbits;
Branch: 1 branch
BranchTrueFalseTotal
$write_wait ? : 0 0 0
312 0 my $sel_timeout_before = $sel_timeout;
313 SELECT:
314 {
315 0 my $nfound = select($rbits, $wbits, undef, $sel_timeout);
316 0 ▶ if ($nfound < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nfound < 0) 0 0 0
317 0 ▶ if ($!{EINTR} || $!{EAGAIN}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!{"EINTR"} or $!{"EAGAIN"}) 0 0 0
Condition0%0/3
$!{"EINTR"} or $!{"EAGAIN"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$!{"EINTR"} or $!{"EAGAIN"}
$!{"EINTR"} $!{"EAGAIN"}
Truth table0%0/3
A or B
A= $!{"EINTR"}B= $!{"EAGAIN"}
ABresult
1X1
011
000
318 0 ▶ if ($time_before) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time_before) 0 0 0
319 0 $sel_timeout = $sel_timeout_before - (time - $time_before);
320 0 ▶ $sel_timeout = 0 if $sel_timeout < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $sel_timeout < 0 0 0 0
321 }
322 0 redo SELECT;
323 }
324 0 die "select failed: $!";
325 }
326 }
327  
328 0 ▶ if ($write_wait) {
Branch: 1 branch
BranchTrueFalseTotal
if ($write_wait) 0 0 0
329 0 $write_wait -= time - $time_before;
330 0 ▶ $write_wait = 0 if $write_wait < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $write_wait < 0 0 0 0
331 }
332  
333 0 ▶ if (defined($rbits) && $rbits =~ /[^\0]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rbits and $rbits =~ /[^\0]/) 0 0 0
Condition0%0/3
defined $rbits and $rbits =~ /[^\0]/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rbits and $rbits =~ /[^\0]/
defined $rbits $rbits =~ /[^\0]/
Truth table0%0/3
A and B
A= defined $rbitsB= $rbits =~ /[^\0]/
ABresult
0X0
100
111
334 # readable
335 0 my $buf = $socket->_rbuf;
336 0 my $n = $socket->sysread($buf, 1024, length($buf));
337 0 ▶ unless (defined $n) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $n) { } 0 0 0
if (do { $n == 0 }) 0 0 0
338 0 ▶ die "read failed: $!" unless $!{EINTR} || $!{EAGAIN};
Branch: 1 branch
BranchTrueFalseTotal
unless $!{"EINTR"} or $!{"EAGAIN"} 0 0 0
Condition0%0/3
$!{"EINTR"} or $!{"EAGAIN"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$!{"EINTR"} or $!{"EAGAIN"}
$!{"EINTR"} $!{"EAGAIN"}
Truth table0%0/3
A or B
A= $!{"EINTR"}B= $!{"EAGAIN"}
ABresult
1X1
011
000
339 # if we get here the rest of the block will do nothing
340 # and we will retry the read on the next round
341 }
342 0 elsif ($n == 0) {
343 # the server closed the connection before we finished
344 # writing all the request content. No need to write any more.
345 0 $drop_connection++;
346 0 last WRITE;
347 }
348 0 $socket->_rbuf($buf);
349 0 ▶ if (!$code && $buf =~ /\015?\012\015?\012/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $code and $buf =~ /\015?\012\015?\012/) 0 0 0
Condition0%0/3
not $code and $buf =~ /\015?\012\015?\012/
!ll&&!rl&&r
000
MC/DC0%0/2
not $code and $buf =~ /\015?\012\015?\012/
not $code $buf =~ /\015?\012\015?\012/
Truth table0%0/3
not(A) and B
A= not $codeB= $buf =~ /\015?\012\015?\012/
ABresult
0X0
100
111
350 # a whole response header is present, so we can read it without blocking
351 0 ($code, $mess, @h) = $socket->read_response_headers(laxed => 1,
352 junk_out => \@junk,
353 );
354 0 ▶ if ($code eq "100") {
Branch: 1 branch
BranchTrueFalseTotal
if ($code eq '100') { } 0 0 0
355 0 $write_wait = 0;
356 0 undef($code);
357 0 goto INITIAL_READ;
358 }
359 else {
360 0 $drop_connection++;
361 0 last WRITE;
362 # XXX should perhaps try to abort write in a nice way too
363 }
364 }
365 }
366 0 ▶ if (defined($wbits) && $wbits =~ /[^\0]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $wbits and $wbits =~ /[^\0]/) 0 0 0
Condition0%0/3
defined $wbits and $wbits =~ /[^\0]/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $wbits and $wbits =~ /[^\0]/
defined $wbits $wbits =~ /[^\0]/
Truth table0%0/3
A and B
A= defined $wbitsB= $wbits =~ /[^\0]/
ABresult
0X0
100
111
367 0 my $n = $socket->syswrite($$wbuf, length($$wbuf), $woffset);
368 0 ▶ unless (defined $n) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $n) { } 0 0 0
if (do { $n == 0 }) 0 0 0
369 0 ▶ die "write failed: $!" unless $!{EINTR} || $!{EAGAIN};
Branch: 1 branch
BranchTrueFalseTotal
unless $!{"EINTR"} or $!{"EAGAIN"} 0 0 0
Condition0%0/3
$!{"EINTR"} or $!{"EAGAIN"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$!{"EINTR"} or $!{"EAGAIN"}
$!{"EINTR"} $!{"EAGAIN"}
Truth table0%0/3
A or B
A= $!{"EINTR"}B= $!{"EAGAIN"}
ABresult
1X1
011
000
370 0 $n = 0; # will retry write on the next round
371 }
372 0 elsif ($n == 0) {
373 0 die "write failed: no bytes written";
374 }
375 0 $woffset += $n;
376  
377 0 ▶ if (!$eof && $woffset >= length($$wbuf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $eof and $woffset >= length $$wbuf) 0 0 0
Condition0%0/3
not $eof and $woffset >= length $$wbuf
!ll&&!rl&&r
000
MC/DC0%0/2
not $eof and $woffset >= length $$wbuf
not $eof $woffset >= length $$wbuf
Truth table0%0/3
not(A) and B
A= not $eofB= $woffset >= length $$wbuf
ABresult
0X0
100
111
378 # need to refill buffer from $content_ref code
379 0 my $buf = &$content_ref();
380 0 ▶ $buf = "" unless defined($buf);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $buf 0 0 0
381 0 ▶ $eof++ unless length($buf);
Branch: 1 branch
BranchTrueFalseTotal
unless length $buf 0 0 0
382 0 ▶ $buf = sprintf "%x%s%s%s", length($buf), $CRLF, $buf, $CRLF
Branch: 1 branch
BranchTrueFalseTotal
if $chunked 0 0 0
383 if $chunked;
384 0 $wbuf = \$buf;
385 0 $woffset = 0;
386 }
387 }
388 } # WRITE
389 }
390  
391 2 ▶ ($code, $mess, @h) = $socket->read_response_headers(laxed => 1, junk_out => \@junk)
Branch: 1 branch
BranchTrueFalseTotal
unless $code 2 0 2
392 unless $code;
393 2 ▶ ($code, $mess, @h) = $socket->read_response_headers(laxed => 1, junk_out => \@junk)
Branch: 1 branch
BranchTrueFalseTotal
if $code eq 100 0 2 2
394 if $code eq "100";
395  
396 2 my $response = HTTP::Response->new($code, $mess);
397 2 my $peer_http_version = $socket->peer_http_version;
398 2 $response->protocol("HTTP/$peer_http_version");
399 {
400 2 local $HTTP::Headers::TRANSLATE_UNDERSCORE;
401 2 $response->push_header(@h);
402 }
403 2 ▶ $response->push_header("Client-Junk" => \@junk) if @junk;
Branch: 1 branch
BranchTrueFalseTotal
if @junk 0 2 2
404
405 # store the leftover info from the connect (had to wait until we had a response. . .)
406 2 $response->push_header($_, $LWPx::TimedHTTP::Socket::timings{$_}) for keys %LWPx::TimedHTTP::Socket::timings;
407 2 $response->push_header('Client-Request-Connect-Time', tv_interval($prev_time, $this_time));
408 2 $prev_time = $this_time;
409 2 $this_time = [gettimeofday];
410 2 $response->push_header('Client-Request-Transmit-Time', tv_interval($prev_time, $this_time));
411 2 $prev_time = $this_time;
412  
413 2 $response->request($request);
414 2 $self->_get_sock_info($response, $socket);
415  
416 2 ▶ if ($method eq "CONNECT") {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "CONNECT") 0 2 2
417 0 $response->{client_socket} = $socket; # so it can be picked up
418 0 return $response;
419 }
420  
421 2 ▶ if (my @te = $response->remove_header('Transfer-Encoding')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@te) = $response->remove_header("Transfer-Encoding")) 1 1 2
422 1 $response->push_header('Client-Transfer-Encoding', \@te);
423 }
424 2 $response->push_header('Client-Response-Num', scalar $socket->increment_response_count);
425  
426 2 my $complete;
427 70 ▶ $response = $self->collect($arg, $response, sub {
Subroutine __ANON__: called CC=7 SCAR=19.9
428 70 my $buf = ""; #prevent use of uninitialized value in SSLeay.xs
429 70 my $n;
430 READ:
431 {
432 70 $n = $socket->read_entity_body($buf, $size);
433 72 ▶ unless (defined $n) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n) 2 70 72
434 2 ▶ redo READ if $!{EINTR} || $!{EAGAIN};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EINTR"} or $!{"EAGAIN"} 2 0 2
Condition33%1/3
$!{"EINTR"} or $!{"EAGAIN"}
l!l&&r!l&&!r
020
MC/DC0%0/2
$!{"EINTR"} or $!{"EAGAIN"}
$!{"EINTR"} $!{"EAGAIN"}
Truth table33%1/3
A or B
A= $!{"EINTR"}B= $!{"EAGAIN"}
ABresult
1X1
011
000
435 0 die "read failed: $!";
436 }
437 70 ▶ if (! defined $response->header('Client-Response-Server-Time') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $response->header("Client-Response-Server-Time")) 2 68 70
438 2 $this_time = [gettimeofday];
439 2 $response->push_header('Client-Response-Server-Time', tv_interval($prev_time, $this_time));
440 2 $prev_time = $this_time;
441 }
442 70 ▶ redo READ if $n == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $n == -1 0 70 70
443 }
444 70 ▶ $complete++ if !$n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 2 68 70
445 70 return \$buf;
446 2 } );
447  
448 2 $this_time = [gettimeofday];
449 2 $response->push_header('Client-Response-Receive-Time', tv_interval($prev_time, $this_time));
450 2 ▶ $drop_connection++ unless $complete;
Branch: 1 branch
BranchTrueFalseTotal
unless $complete 0 2 2
451
452 2 @h = $socket->get_trailers;
453 2 ▶ if (@h) {
Branch: 1 branch
BranchTrueFalseTotal
if (@h) 0 2 2
454 0 local $HTTP::Headers::TRANSLATE_UNDERSCORE;
455 0 $response->push_header(@h);
456 }
457  
458 # keep-alive support
459 2 ▶ unless ($drop_connection) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($drop_connection) 2 0 2
460 2 ▶ if (my $conn_cache = $self->{ua}{conn_cache}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $conn_cache = $self->{"ua"}{"conn_cache"}) 0 2 2
461 0 ▶ my %connection = map { (lc($_) => 1) }
Condition0%0/2
$response->header('Connection') || ''
l!l
00
MC/DC0%0/1
$response->header('Connection') || ''
$response->header('Connection')
Truth table0%0/2
A
A= $response->header('Connection')
Aresult
11
00
462 split(/\s*,\s*/, ($response->header("Connection") || ""));
463 0 ▶ if (($peer_http_version eq "1.1" && !$connection{close}) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($peer_http_version eq "1.1" and not $connection{"close"} or $connection{"keep-alive"}) 0 0 0
Condition0%0/6
$peer_http_version eq "1.1" and not $connection{"close"} or $connection{"keep-alive"}
l!l&&r!l&&!r
000
$peer_http_version eq "1.1" and not $connection{"close"}
!ll&&!rl&&r
000
MC/DC0%0/3
$peer_http_version eq "1.1" and not $connection{"close"} or $connection{"keep-alive"}
$peer_http_version eq "1.1" not $connection{"close"} $connection{"keep-alive"}
Truth table0%0/5
A and not(B) or C
A= $peer_http_version eq "1.1"B= not $connection{"close"}C= $connection{"keep-alive"}
ABCresult
11X1
0X11
1011
0X00
1000
464 $connection{"keep-alive"})
465 {
466 0 $conn_cache->deposit($self->socket_type, "$host:$port", $socket);
467 }
468 }
469 }
470  
471 2 $response;
472 }
473  
474 #-----------------------------------------------------------
475 package LWPx::TimedHTTP::Socket;
476 1 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
477 1 @ISA = qw(LWP::Protocol::http::SocketMethods Net::HTTP);
478 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
479 1 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
480 1 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
481  
482 1 our %timings;
483  
484 0 ▶sub _get_addr {
Subroutine _get_addr: not called CC=4 SCAR=30.0
485 0 my($sock,$addr_str, $multi) = @_;
486 0 my @addr;
487 0 my $prev_time = [gettimeofday];
488 0 ▶ if ($multi && $addr_str !~ /^\d+(?:\.\d+){3}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multi and not $addr_str =~ /^\d+(?:\.\d+){3}$/) { } 0 0 0
Condition0%0/3
$multi and not $addr_str =~ /^\d+(?:\.\d+){3}$/
!ll&&!rl&&r
000
MC/DC0%0/2
$multi and not $addr_str =~ /^\d+(?:\.\d+){3}$/
$multi not $addr_str =~ /^\d+(?:\.\d+){3}$/
Truth table0%0/3
A and not(B)
A= $multiB= not $addr_str =~ /^\d+(?:\.\d+){3}$/
ABresult
0X0
100
111
489 0 (undef, undef, undef, undef, @addr) = gethostbyname($addr_str);
490 } else {
491 0 my $h = inet_aton($addr_str);
492 0 ▶ push(@addr, $h) if defined $h;
Branch: 1 branch
BranchTrueFalseTotal
if defined $h 0 0 0
493 }
494 0 my $this_time = [gettimeofday];
495 0 $timings{'Client-Request-Dns-Time'} = tv_interval($prev_time, $this_time);
496 0 @addr;
497 }
498  
499 package LWPx::TimedHTTP::https;
500 1 eval { require LWP::Protocol::https };
501 1 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
502 1 @ISA = qw(LWPx::TimedHTTP);
503  
504 package LWPx::TimedHTTP::https::Socket;
505 1 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
506 1 @ISA = qw(LWP::Protocol::https::Socket);
507  
508  
509  
510 1 1;
↑ Summary