blib/lib/Net/Async/HTTP/Connection.pm

filter: Statementstmt 93.7% 331 / 353 Branchbran 77.3% 150 / 194 Conditioncond 76.3% 71 / 93 MC/DCmcdc 52.7% 29 / 55 Subroutinesub 97.5% 39 / 40 Podpod 44.4% 4 / 9 totaltotal 83.8% 624 / 744 SCAR 51.7
CC
131
Cov
86%
__ANON__
38.5
request
27.3
__ANON__
19.4
CRAP 175.6
« blib/lib/Net/Async/HTTP.pm ↑ Summary Build »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2008-2019 -- leonerd@leonerd.org.uk
5  
6 package Net::Async::HTTP::Connection;
7  
8 37 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 37 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 37 our $VERSION = '0.48';
12  
13 37 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 37 ▶use base qw( IO::Async::Stream );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 37 IO::Async::Stream->VERSION( '0.59' ); # ->write( ..., on_write )
17  
18 37 ▶use Net::Async::HTTP::StallTimer;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 37 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 37 my $CRLF = "\x0d\x0a"; # More portable than \r\n
23  
24 37 ▶use Struct::Dumb;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 37 struct RequestContext => [qw( req on_read stall_timer resp_header resp_bytes on_done is_done f )],
26 named_constructor => 1;
27  
28 # Detect whether HTTP::Message properly trims whitespace in header values. If
29 # it doesn't, we have to deploy a workaround to fix them up.
30 # https://rt.cpan.org/Ticket/Display.html?id=75224
31 37 ▶use constant HTTP_MESSAGE_TRIMS_LWS => HTTP::Message->parse( "Name: value " )->header("Name") eq "value";
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 =head1 NAME
34  
35 C<Net::Async::HTTP::Connection> - HTTP client protocol handler
36  
37 =head1 DESCRIPTION
38  
39 This class provides a connection to a single HTTP server, and is used
40 internally by L<Net::Async::HTTP>. It is not intended for general use.
41  
42 =cut
43  
44 sub _init
45 {
46 100 ▶ my $self = shift;
Subroutine _init: called CC=1 SCAR=0.0
47 100 $self->SUPER::_init( @_ );
48  
49 100 $self->{requests_in_flight} = 0;
50 }
51  
52 sub configure
53 {
54 286 ▶ my $self = shift;
Subroutine configure: called CC=5 SCAR=16.1
Pod documented
55 286 my %params = @_;
56  
57 286 foreach (qw( pipeline max_in_flight ready_queue decode_content is_proxy )) {
58 1430 ▶ $self->{$_} = delete $params{$_} if exists $params{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{$_} 500 930 1430
59 }
60  
61 286 ▶ if( my $on_closed = $params{on_closed} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $on_closed = $params{"on_closed"}) 100 186 286
62 $params{on_closed} = sub {
63 67 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
64  
65 67 $self->debug_printf( "CLOSED in-flight=$self->{requests_in_flight}" );
66  
67 67 $self->error_all( "Connection closed" );
68  
69 67 undef $self->{ready_queue};
70 67 $on_closed->( $self );
71 100 };
72 }
73  
74 286 ▶ croak "max_in_flight parameter required, may be zero" unless defined $self->{max_in_flight};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"max_in_flight"} 0 286 286
75  
76 286 $self->SUPER::configure( %params );
77 }
78  
79 sub should_pipeline
80 {
81 205 ▶ my $self = shift;
Subroutine should_pipeline: called CC=4 SCAR=13.9
Pod undocumented
82 return $self->{pipeline} &&
83 $self->{can_pipeline} &&
84 205 ▶ ( !$self->{max_in_flight} || $self->{requests_in_flight} < $self->{max_in_flight} );
Condition88%8/9
$self->{'pipeline'} && $self->{'can_pipeline'} && (!$self->{'max_in_flight'} || $self->{'requests_in_flight'} < $self->{'max_in_flight'})
!ll&&!rl&&r
107197
$self->{"pipeline"} and $self->{"can_pipeline"}
!ll&&!rl&&r
208798
not $self->{"max_in_flight"} or $self->{"requests_in_flight"} < $self->{"max_in_flight"}
l!l&&r!l&&!r
0971
MC/DC75%3/4
$self->{'pipeline'} && $self->{'can_pipeline'} && (!$self->{'max_in_flight'} || $self->{'requests_in_flight'} < $self->{'max_in_flight'})
$self->{"pipeline"} $self->{"can_pipeline"} not $self->{"max_in_flight"} $self->{"requests_in_flight"} < $self->{"max_in_flight"}
Truth table80%4/5
A and B && not(C) or D
A= $self->{"pipeline"}B= $self->{"can_pipeline"}C= not $self->{"max_in_flight"}D= $self->{"requests_in_flight"} < $self->{"max_in_flight"}
ABCDresult
0XXX0
10XX0
11000
111X1
11011
85 }
86  
87 sub connect
88 {
89 100 ▶ my $self = shift;
Subroutine connect: called CC=2 SCAR=7.0
Pod documented
90 100 my %args = @_;
91  
92 100 $self->debug_printf( "CONNECT $args{host}:$args{service}" );
93  
94 100 ▶ defined wantarray or die "VOID ->connect";
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray or die "VOID ->connect" 0 100 100
95  
96 $self->SUPER::connect(
97 socktype => "stream",
98 %args
99 )->on_done( sub {
100 92 ▶ $self->debug_printf( "CONNECTED" );
Subroutine __ANON__: called CC=1 SCAR=0.0
101 100 });
102 }
103  
104 sub ready
105 {
106 207 ▶ my $self = shift;
Subroutine ready: called CC=13 SCAR=25.7
Pod undocumented
107  
108 207 ▶ my $queue = $self->{ready_queue} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $queue = $self->{"ready_queue"} or return 21 186 207
109  
110 186 ▶ if( $self->should_pipeline ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->should_pipeline) { } 79 107 186
elsif (@$queue and $self->is_idle) { } 99 8 107
111 79 $self->debug_printf( "READY pipelined" );
112 79 ▶ while( @$queue && $self->should_pipeline ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$queue and $self->should_pipeline) 18 79 97
Condition100%3/3
@$queue and $self->should_pipeline
!ll&&!rl&&r
78118
MC/DC100%2/2
@$queue and $self->should_pipeline
@$queue $self->should_pipeline
Truth table100%3/3
A and B
A= @$queueB= $self->should_pipeline
ABresult
0X0
100
111
113 18 my $ready = shift @$queue;
114 18 my $f = $ready->future;
115 18 ▶ next if $f->is_cancelled;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_cancelled 1 17 18
116  
117 17 ▶ $ready->connecting and $ready->connecting->cancel;
Branch: 1 branch
BranchTrueFalseTotal
$ready->connecting and $ready->connecting->cancel 1 16 17
118  
119 17 $f->done( $self );
120 }
121 }
122 ▶ elsif( @$queue and $self->is_idle ) {
Condition66%2/3
@$queue and $self->is_idle
!ll&&!rl&&r
8099
MC/DC50%1/2
@$queue and $self->is_idle
@$queue $self->is_idle
Truth table66%2/3
A and B
A= @$queueB= $self->is_idle
ABresult
0X0
100
111
123 99 $self->debug_printf( "READY non-pipelined" );
124 99 ▶ while( @$queue ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$queue) 99 0 99
125 99 my $ready = shift @$queue;
126 99 my $f = $ready->future;
127 99 ▶ next if $f->is_cancelled;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_cancelled 0 99 99
128  
129 99 ▶ $ready->connecting and $ready->connecting->cancel;
Branch: 1 branch
BranchTrueFalseTotal
$ready->connecting and $ready->connecting->cancel 10 89 99
130  
131 99 $f->done( $self );
132 99 last;
133 }
134 }
135 else {
136 8 ▶ $self->debug_printf( "READY cannot-run queue=%d idle=%s",
Branch: 1 branch
BranchTrueFalseTotal
$self->is_idle ? : 8 0 8
137 scalar @$queue, $self->is_idle ? "Y" : "N");
138 }
139 }
140  
141 sub is_idle
142 {
143 158 ▶ my $self = shift;
Subroutine is_idle: called CC=1 SCAR=0.0
Pod undocumented
144 158 return $self->{requests_in_flight} == 0;
145 }
146  
147 sub on_read
148 {
149 246 ▶ my $self = shift;
Subroutine on_read: called CC=12 SCAR=24.9
Pod documented
150 246 my ( $buffref, $closed ) = @_;
151  
152 246 ▶ while( my $head = $self->{request_queue}[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $head = $self->{"request_queue"}[0]) 216 31 247
153 216 ▶ shift @{ $self->{request_queue} } and next if $head->is_done;
Branch: 1 branch
BranchTrueFalseTotal
if $head->is_done 1 215 216
Condition60%3/5
$head->is_done && (shift @{$$self{'request_queue'};} && (next))
!ll&&!rl&&r
21510
shift @{$$self{"request_queue"};} and next
!ll
01
MC/DC0%0/2
$head->is_done && (shift @{$$self{'request_queue'};} && (next))
$head->is_done shift @{$$self{"request_queue"};}
Truth table33%1/3
A && B
A= $head->is_doneB= shift @{$$self{"request_queue"};}
ABresult
0X0
100
111
154  
155 215 ▶ $head->stall_timer->reset if $head->stall_timer;
Branch: 1 branch
BranchTrueFalseTotal
if $head->stall_timer 3 212 215
156  
157 215 my $ret = $head->on_read->( $self, $buffref, $closed, $head );
158  
159 214 ▶ if( defined $ret ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ret) 100 114 214
160 100 ▶ return $ret if !ref $ret;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ret 31 69 100
161  
162 69 $head->on_read = $ret;
163 69 return 1;
164 }
165  
166 114 ▶ $head->is_done or die "ARGH: undef return without being marked done";
Branch: 1 branch
BranchTrueFalseTotal
$head->is_done or die "ARGH: undef return without being marked done" 0 114 114
167  
168 114 shift @{ $self->{request_queue} };
169 114 ▶ return 1 if !$closed and length $$buffref;
Branch: 1 branch
BranchTrueFalseTotal
if not $closed and length $$buffref 2 112 114
Condition100%3/3
not $closed and length $$buffref
!ll&&!rl&&r
111012
MC/DC100%2/2
not $closed and length $$buffref
not $closed length $$buffref
Truth table100%3/3
not(A) and B
A= not $closedB= length $$buffref
ABresult
0X0
100
111
170 112 return;
171 }
172  
173 # Reinvoked after switch back to baseline, but may be idle again
174 31 ▶ return if $closed or !length $$buffref;
Branch: 1 branch
BranchTrueFalseTotal
if $closed or not length $$buffref 30 1 31
Condition66%2/3
$closed or not length $$buffref
l!l&&r!l&&!r
3001
MC/DC50%1/2
$closed or not length $$buffref
$closed not length $$buffref
Truth table66%2/3
A or not(B)
A= $closedB= not length $$buffref
ABresult
1X1
011
000
175  
176 1 $self->invoke_error( "Spurious on_read of connection while idle",
177 http_connection => read => $$buffref );
178 1 $$buffref = "";
179 }
180  
181 sub on_write_eof
182 {
183 0 ▶ my $self = shift;
Subroutine on_write_eof: not called CC=1 SCAR=6.9
Pod documented
184 0 $self->error_all( "Connection closed", http => undef, undef );
185 }
186  
187 sub error_all
188 {
189 67 ▶ my $self = shift;
Subroutine error_all: called CC=4 SCAR=13.9
Pod undocumented
190  
191 67 ▶ while( my $head = shift @{ $self->{request_queue} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $head = shift @{$$self{"request_queue"};}) 34 67 101
192 34 ▶ $head->f->fail( @_ ) unless $head->is_done or $head->f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $head->is_done or $head->f->is_ready 6 28 34
Condition100%3/3
$head->is_done or $head->f->is_ready
l!l&&r!l&&!r
2176
MC/DC100%2/2
$head->is_done or $head->f->is_ready
$head->is_done $head->f->is_ready
Truth table100%3/3
A or B
A= $head->is_doneB= $head->f->is_ready
ABresult
1X1
011
000
193 }
194 }
195  
196 sub request
197 {
198 129 ▶ my $self = shift;
Subroutine request: called CC=27 SCAR=33.1
Pod undocumented
199 129 my %args = @_;
200  
201 129 ▶ my $on_header = $args{on_header} or croak "Expected 'on_header' as a CODE ref";
Branch: 1 branch
BranchTrueFalseTotal
my $on_header = $args{"on_header"} or croak("Expected 'on_header' as a CODE ref") 0 129 129
202  
203 129 my $req = $args{request};
204 129 ▶ ref $req and $req->isa( "HTTP::Request" ) or croak "Expected 'request' as a HTTP::Request reference";
Branch: 1 branch
BranchTrueFalseTotal
ref $req and $req->isa("HTTP::Request") or croak("Expected 'request' as a HTTP::Request reference") 0 129 129
Condition33%1/3
ref $req and $req->isa("HTTP::Request")
!ll&&!rl&&r
00129
MC/DC0%0/2
ref $req and $req->isa("HTTP::Request")
ref $req $req->isa("HTTP::Request")
Truth table33%1/3
A and B
A= ref $reqB= $req->isa("HTTP::Request")
ABresult
0X0
100
111
205  
206 129 $self->debug_printf( "REQUEST %s %s", $req->method, $req->uri );
207  
208 129 my $request_body = $args{request_body};
209 129 my $expect_continue = !!$args{expect_continue};
210  
211 129 my $method = $req->method;
212  
213 129 ▶ if( $method eq "POST" or $method eq "PUT" or length $req->content ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "POST" or $method eq "PUT" or length $req->content) 9 116 125
Condition100%6/6
$method eq "POST" or $method eq "PUT" or length $req->content
l!l&&r!l&&!r
112116
$method eq "POST" or $method eq "PUT"
l!l&&r!l&&!r
47118
MC/DC100%3/3
$method eq "POST" or $method eq "PUT" or length $req->content
$method eq "POST" $method eq "PUT" length $req->content
Truth table100%4/4
A or B or C
A= $method eq "POST"B= $method eq "PUT"C= length $req->content
ABCresult
1XX1
01X1
0011
0000
214 13 $req->init_header( "Content-Length", length $req->content );
215 }
216  
217 129 ▶ if( $expect_continue ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expect_continue) 1 128 129
218 1 $req->init_header( "Expect", "100-continue" );
219 }
220  
221 129 ▶ if( $self->{decode_content} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"decode_content"}) 2 127 129
222 #$req->init_header( "Accept-Encoding", Net::Async::HTTP->can_decode )
223 2 $req->init_header( "Accept-Encoding", "gzip" );
224 }
225  
226 129 my $f = $self->loop->new_future
227 ->set_label( "$method " . $req->uri );
228  
229 # TODO: Cancelling a request Future shouldn't necessarily close the socket
230 # if we haven't even started writing the request yet. But we can't know
231 # that currently.
232 $f->on_cancel( sub {
233 5 ▶ $self->debug_printf( "CLOSE on_cancel" );
Subroutine __ANON__: called CC=1 SCAR=0.0
234 5 $self->close_now;
235 129 });
236  
237 129 my $stall_timer;
238 129 ▶ if( $args{stall_timeout} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"stall_timeout"}) 4 125 129
239 $stall_timer = Net::Async::HTTP::StallTimer->new(
240 delay => $args{stall_timeout},
241 4 future => $f,
242 );
243 4 $self->add_child( $stall_timer );
244 # Don't start it yet
245  
246 my $remove_timer = sub {
247 4 ▶ $self->remove_child( $stall_timer ) if $stall_timer;
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
if $stall_timer 4 0 4
248 4 undef $stall_timer;
249 4 };
250  
251 4 $f->on_ready( $remove_timer );
252 }
253  
254 129 push @{ $self->{request_queue} }, my $ctx = RequestContext(
255 req => $req,
256 on_read => undef, # will be set later
257 stall_timer => $stall_timer,
258 resp_header => undef, # will be set later
259 resp_bytes => 0,
260 on_done => $args{on_done},
261 129 is_done => 0,
262 f => $f,
263 );
264  
265 129 my $on_body_write;
266 129 ▶ if( $stall_timer or $args{on_body_write} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stall_timer or $args{"on_body_write"}) 5 124 129
Condition100%3/3
$stall_timer or $args{"on_body_write"}
l!l&&r!l&&!r
41124
MC/DC100%2/2
$stall_timer or $args{"on_body_write"}
$stall_timer $args{"on_body_write"}
Truth table100%3/3
A or B
A= $stall_timerB= $args{"on_body_write"}
ABresult
1X1
011
000
267 5 my $inner_on_body_write = $args{on_body_write};
268 5 my $written = 0;
269 $on_body_write = sub {
270 10 ▶ $stall_timer->reset if $stall_timer;
Subroutine __ANON__: called CC=3 SCAR=12.0
Branch: 1 branch
BranchTrueFalseTotal
if $stall_timer 0 10 10
271 10 ▶ $inner_on_body_write->( $written += $_[1] ) if $inner_on_body_write;
Branch: 1 branch
BranchTrueFalseTotal
if $inner_on_body_write 10 0 10
272 5 };
273 }
274  
275 my $write_request_body = defined $request_body ? sub {
276 4 ▶ my ( $self ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
277 4 $self->write( $request_body,
278 on_write => $on_body_write
279 );
280 129 ▶ } : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $request_body ? : 4 125 129
281  
282 # Unless the request method is CONNECT, or we are connecting to a
283 # non-transparent proxy, the URL is not allowed to contain
284 # an authority; only path
285 # Take a copy of the headers since we'll be hacking them up
286 129 my $headers = $req->headers->clone;
287 129 my $path;
288 129 ▶ if( $method eq "CONNECT" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'CONNECT') { } 0 129 129
289 0 $path = $req->uri->as_string;
290 }
291 else {
292 129 my $uri = $req->uri;
293 129 ▶ if ( $self->{is_proxy} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'is_proxy'}) { } 1 128 129
294 1 $path = $uri->as_string;
295 }
296 else {
297 128 $path = $uri->path_query;
298 128 ▶ $path = "/$path" unless $path =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ m[^/] 1 127 128
299 }
300 129 my $authority = $uri->authority;
301 129 ▶ if( defined $authority and
Branch: 1 branch
BranchTrueFalseTotal
if (defined $authority and () = $authority =~ /^(.*?):(.*)@(.*)$/) { } 2 127 129
Condition100%3/3
defined $authority and () = $authority =~ /^(.*?):(.*)@(.*)$/
!ll&&!rl&&r
41862
MC/DC100%2/2
defined $authority and () = $authority =~ /^(.*?):(.*)@(.*)$/
defined $authority () = $authority =~ /^(.*?):(.*)@(.*)$/
Truth table100%3/3
A and B
A= defined $authorityB= () = $authority =~ /^(.*?):(.*)@(.*)$/
ABresult
0X0
100
111
302 my ( $user, $pass, $host ) = $authority =~ m/^(.*?):(.*)@(.*)$/ ) {
303 2 $headers->init_header( Host => $host );
304 2 $headers->authorization_basic( $user, $pass );
305 }
306 else {
307 127 $headers->init_header( Host => $authority );
308 }
309 }
310  
311 129 ▶ my $protocol = $req->protocol || "HTTP/1.1";
Condition100%2/2
$req->protocol || 'HTTP/1.1'
l!l
8346
MC/DC100%1/1
$req->protocol || 'HTTP/1.1'
$req->protocol
Truth table100%2/2
A
A= $req->protocol
Aresult
11
00
312 129 my @headers = ( "$method $path $protocol" );
313 $headers->scan( sub {
314 310 ▶ my ( $name, $value ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
315 310 $name =~ s/^://; # non-canonical header
316 310 push @headers, "$name: $value";
317 129 } );
318  
319 129 ▶ $stall_timer->start if $stall_timer;
Branch: 1 branch
BranchTrueFalseTotal
if $stall_timer 4 125 129
320 129 ▶ $stall_timer->reason = "writing request" if $stall_timer;
Branch: 1 branch
BranchTrueFalseTotal
if $stall_timer 4 125 129
321  
322 129 ▶ my $on_header_write = $stall_timer ? sub { $stall_timer->reset } : undef;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$stall_timer ? : 4 125 129
323  
324 129 $self->write( join( $CRLF, @headers ) .
325 $CRLF . $CRLF,
326 on_write => $on_header_write );
327  
328 129 ▶ $self->write( $req->content,
Branch: 1 branch
BranchTrueFalseTotal
if length $req->content 8 121 129
329 on_write => $on_body_write ) if length $req->content;
330 129 ▶ $write_request_body->( $self ) if $write_request_body and !$expect_continue;
Branch: 1 branch
BranchTrueFalseTotal
if $write_request_body and not $expect_continue 3 126 129
Condition100%3/3
$write_request_body and not $expect_continue
!ll&&!rl&&r
12513
MC/DC100%2/2
$write_request_body and not $expect_continue
$write_request_body not $expect_continue
Truth table100%3/3
A and not(B)
A= $write_request_bodyB= not $expect_continue
ABresult
0X0
100
111
331  
332 $self->write( "", on_flush => sub {
333 3 ▶ return unless $stall_timer; # test again in case it was cancelled in the meantime
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
unless $stall_timer 0 3 3
334 3 $stall_timer->reset;
335 3 $stall_timer->reason = "waiting for response";
336 129 ▶ }) if $stall_timer;
Branch: 1 branch
BranchTrueFalseTotal
if $stall_timer 4 125 129
337  
338 129 $self->{requests_in_flight}++;
339  
340 $ctx->on_read = $self->_mk_on_read_header(
341 129 ▶ $args{previous_response}, $expect_continue ? $write_request_body : undef, $on_header
Branch: 1 branch
BranchTrueFalseTotal
$expect_continue ? : 1 128 129
342 );
343  
344 129 return $f;
345 }
346  
347 sub _mk_on_read_header
348 {
349 129 ▶ shift; # $self
Subroutine _mk_on_read_header: called CC=1 SCAR=0.0
350 129 my ( $previous_response, $write_request_body, $on_header ) = @_;
351  
352 sub {
353 121 ▶ my ( $self, $buffref, $closed, $ctx ) = @_;
Subroutine __ANON__: called CC=34 SCAR=36.5
354  
355 121 my $req = $ctx->req;
356 121 my $stall_timer = $ctx->stall_timer;
357 121 my $f = $ctx->f;
358  
359 121 ▶ if( $stall_timer ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stall_timer) 2 119 121
360 2 $stall_timer->reason = "receiving response header";
361 2 $stall_timer->reset;
362 }
363  
364 121 ▶ if( length $$buffref >= 4 and $$buffref !~ m/^HTTP/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $$buffref >= 4 and not $$buffref =~ /^HTTP/) 1 120 121
Condition100%3/3
length $$buffref >= 4 and not $$buffref =~ /^HTTP/
!ll&&!rl&&r
11191
MC/DC100%2/2
length $$buffref >= 4 and not $$buffref =~ /^HTTP/
length $$buffref >= 4 not $$buffref =~ /^HTTP/
Truth table100%3/3
A and not(B)
A= length $$buffref >= 4B= not $$buffref =~ /^HTTP/
ABresult
0X0
100
111
365 1 $self->debug_printf( "ERROR fail" );
366 1 ▶ $f->fail( "Did not receive HTTP response from server", http => undef, $req ) unless $f->is_cancelled;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_cancelled 1 0 1
367 1 $self->close_now;
368 }
369  
370 121 ▶ unless( $$buffref =~ s/^(.*?$CRLF$CRLF)//s ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$buffref =~ s/^(.*?$CRLF$CRLF)//s) 3 118 121
371 3 ▶ if( $closed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($closed) 1 2 3
372 1 $self->debug_printf( "ERROR closed" );
373 1 ▶ $f->fail( "Connection closed while awaiting header", http => undef, $req ) unless $f->is_cancelled;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_cancelled 1 0 1
374 1 $self->close_now;
375 }
376 3 return 0;
377 }
378  
379 118 $ctx->resp_bytes += $+[0];
380  
381 118 my $header = HTTP::Response->parse( $1 );
382 # HTTP::Response doesn't strip the \rs from this
383 118 ( my $status_line = $header->status_line ) =~ s/\r$//;
384  
385 118 $ctx->resp_header = $header;
386  
387 118 unless( HTTP_MESSAGE_TRIMS_LWS ) {
388 118 my @headers;
389 $header->scan( sub {
390 240 ▶ my ( $name, $value ) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
391 240 s/^\s+//, s/\s+$// for $value;
392 240 push @headers, $name => $value;
393 118 } );
394 118 ▶ $header->header( @headers ) if @headers;
Branch: 1 branch
BranchTrueFalseTotal
if @headers 115 3 118
395 }
396  
397 118 my $protocol = $header->protocol;
398 118 ▶ if( $protocol =~ m{^HTTP/1\.(\d+)$} and $1 >= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($protocol =~ m[^HTTP/1\.(\d+)$] and $1 >= 1) 111 7 118
Condition66%2/3
$protocol =~ m[^HTTP/1\.(\d+)$] and $1 >= 1
!ll&&!rl&&r
07111
MC/DC50%1/2
$protocol =~ m[^HTTP/1\.(\d+)$] and $1 >= 1
$protocol =~ m[^HTTP/1\.(\d+)$] $1 >= 1
Truth table66%2/3
A and B
A= $protocol =~ m[^HTTP/1\.(\d+)$]B= $1 >= 1
ABresult
0X0
100
111
399 111 $self->{can_pipeline} = 1;
400 }
401  
402 118 ▶ if( $header->code =~ m/^1/ ) { # 1xx is not a final response
Branch: 1 branch
BranchTrueFalseTotal
if ($header->code =~ /^1/) 1 117 118
403 1 $self->debug_printf( "HEADER [provisional] %s", $status_line );
404 1 ▶ $write_request_body->( $self ) if $write_request_body;
Branch: 1 branch
BranchTrueFalseTotal
if $write_request_body 1 0 1
405 1 return 1;
406 }
407  
408 117 $header->request( $req );
409 117 ▶ $header->previous( $previous_response ) if $previous_response;
Branch: 1 branch
BranchTrueFalseTotal
if $previous_response 6 111 117
410  
411 117 $self->debug_printf( "HEADER %s", $status_line );
412  
413 117 my $on_body_chunk = $on_header->( $header );
414  
415 117 my $code = $header->code;
416  
417 117 my $content_encoding = $header->header( "Content-Encoding" );
418  
419 117 my $decoder;
420 117 ▶ if( $content_encoding and
Branch: 1 branch
BranchTrueFalseTotal
if ($content_encoding and $decoder = "Net::Async::HTTP"->can_decode($content_encoding)) 2 115 117
Condition66%2/3
$content_encoding and $decoder = "Net::Async::HTTP"->can_decode($content_encoding)
!ll&&!rl&&r
11502
MC/DC50%1/2
$content_encoding and $decoder = "Net::Async::HTTP"->can_decode($content_encoding)
$content_encoding $decoder = "Net::Async::HTTP"->can_decode($content_encoding)
Truth table66%2/3
A and B
A= $content_encodingB= $decoder = "Net::Async::HTTP"->can_decode($content_encoding)
ABresult
0X0
100
111
421 $decoder = Net::Async::HTTP->can_decode( $content_encoding ) ) {
422 2 $header->init_header( "X-Original-Content-Encoding" => $header->remove_header( "Content-Encoding" ) );
423 }
424  
425 # can_pipeline is set for HTTP/1.1 or above; presume it can keep-alive if set
426 117 ▶ my $connection_close = lc( $header->header( "Connection" ) || ( $self->{can_pipeline} ? "keep-alive" : "close" ) )
Branch: 1 branch
BranchTrueFalseTotal
$self->{'can_pipeline'} ? : 51 3 54
Condition66%2/3
$header->header('Connection') || ($self->{'can_pipeline'} ? 'keep-alive' : 'close')
l!l&&r!l&&!r
63540
MC/DC0%0/2
$header->header('Connection') || ($self->{'can_pipeline'} ? 'keep-alive' : 'close')
$header->header('Connection') ($self->{'can_pipeline'} ? 'keep-alive' : 'close')
Truth table66%2/3
A || B
A= $header->header('Connection')B= ($self->{'can_pipeline'} ? 'keep-alive' : 'close')
ABresult
1X1
011
000
427 eq "close";
428  
429 117 ▶ if( $connection_close ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($connection_close) { } 21 96 117
elsif (defined(my $keep_alive = lc($header->header('Keep-Alive') || ''))) { } 96 0 96
430 21 $self->{max_in_flight} = 1;
431 }
432 ▶ elsif( defined( my $keep_alive = lc( $header->header("Keep-Alive") || "" ) ) ) {
Condition50%1/2
$header->header('Keep-Alive') || ''
l!l
096
MC/DC0%0/1
$header->header('Keep-Alive') || ''
$header->header('Keep-Alive')
Truth table50%1/2
A
A= $header->header('Keep-Alive')
Aresult
11
00
433 96 my ( $max ) = ( $keep_alive =~ m/max=(\d+)/ );
434 96 ▶ $self->{max_in_flight} = $max if $max && $max < $self->{max_in_flight};
Branch: 1 branch
BranchTrueFalseTotal
if $max and $max < $self->{"max_in_flight"} 0 96 96
Condition33%1/3
$max and $max < $self->{"max_in_flight"}
!ll&&!rl&&r
9600
MC/DC0%0/2
$max and $max < $self->{"max_in_flight"}
$max $max < $self->{"max_in_flight"}
Truth table33%1/3
A and B
A= $maxB= $max < $self->{"max_in_flight"}
ABresult
0X0
100
111
435 }
436  
437 my $on_more = sub {
438 81 ▶ my ( $chunk ) = @_;
Subroutine __ANON__: called CC=3 SCAR=12.7
439  
440 81 ▶ if( $decoder and not eval { $chunk = $decoder->( $chunk ); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decoder and not eval { do { $chunk = &$decoder($chunk); 1 } }) 0 81 81
Condition66%2/3
$decoder and not eval { do { $chunk = &$decoder($chunk); 1 } }
!ll&&!rl&&r
7920
MC/DC0%0/2
$decoder and not eval { do { $chunk = &$decoder($chunk); 1 } }
$decoder not eval { do { $chunk = &$decoder($chunk); 1 } }
Truth table66%2/3
A and not(B)
A= $decoderB= not eval { do { $chunk = &$decoder($chunk); 1 } }
ABresult
0X0
100
111
441 0 $self->debug_printf( "ERROR decode failed" );
442 0 $f->fail( "Decode error $@", http => undef, $req );
443 0 $self->close;
444 0 return undef;
445 }
446  
447 81 $on_body_chunk->( $chunk );
448  
449 81 return 1;
450 117 };
451 my $on_done = sub {
452 115 ▶ my ( $ctx ) = @_;
Subroutine __ANON__: called CC=10 SCAR=24.1
453  
454 115 $ctx->is_done++;
455  
456 # TODO: IO::Async probably ought to do this. We need to fire the
457 # on_closed event _before_ calling on_body_chunk, to clear the
458 # connection cache in case another request comes - e.g. HEAD->GET
459 115 ▶ $self->close if $connection_close;
Branch: 1 branch
BranchTrueFalseTotal
if $connection_close 21 94 115
460  
461 115 my $final;
462 115 ▶ if( $decoder and not eval { $final = $decoder->(); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decoder and not eval { do { $final = &$decoder(); 1 } }) 0 115 115
Condition66%2/3
$decoder and not eval { do { $final = &$decoder(); 1 } }
!ll&&!rl&&r
11320
MC/DC0%0/2
$decoder and not eval { do { $final = &$decoder(); 1 } }
$decoder not eval { do { $final = &$decoder(); 1 } }
Truth table66%2/3
A and not(B)
A= $decoderB= not eval { do { $final = &$decoder(); 1 } }
ABresult
0X0
100
111
463 0 $self->debug_printf( "ERROR decode failed" );
464 0 $f->fail( "Decode error $@", http => undef, $req );
465 0 $self->close;
466 0 return undef;
467 }
468  
469 115 ▶ $on_body_chunk->( $final ) if defined $final and length $final;
Branch: 1 branch
BranchTrueFalseTotal
if defined $final and length $final 0 115 115
Condition66%2/3
defined $final and length $final
!ll&&!rl&&r
11320
MC/DC0%0/2
defined $final and length $final
defined $final length $final
Truth table66%2/3
A and B
A= defined $finalB= length $final
ABresult
0X0
100
111
470  
471 115 my $response = $on_body_chunk->();
472 115 ▶ my $e = eval { $f->done( $response ) unless $f->is_cancelled; 1 } ? undef : $@;
Branch: 2 branches
BranchTrueFalseTotal
eval { do { $f->done($response) unless $f->is_cancelled; 1 } } ? : 113 2 115
unless $f->is_cancelled 115 0 115
473  
474 115 ▶ $ctx->on_done->( $ctx ) if $ctx->on_done;
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->on_done 115 0 115
475  
476 115 $self->{requests_in_flight}--;
477 115 $self->debug_printf( "DONE remaining in-flight=$self->{requests_in_flight}" );
478 115 $self->ready;
479  
480 115 ▶ if( defined $e ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $e) 2 113 115
481 2 chomp $e;
482 2 $self->invoke_error( $e, perl => );
483 # This might not return, if it top-level croaks
484 }
485  
486 114 return undef; # Finished
487 117 };
488  
489 # RFC 2616 says "HEAD" does not have a body, nor do any 1xx codes, nor
490 # 204 (No Content) nor 304 (Not Modified)
491 117 ▶ if( $req->method eq "HEAD" or $code =~ m/^1..$/ or $code eq "204" or $code eq "304" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->method eq "HEAD" or $code =~ /^1..$/ or $code eq 204 or $code eq 304) 1 114 115
Condition77%7/9
$req->method eq "HEAD" or $code =~ /^1..$/ or $code eq 204 or $code eq 304
l!l&&r!l&&!r
21114
$req->method eq "HEAD" or $code =~ /^1..$/ or $code eq 204
l!l&&r!l&&!r
20115
$req->method eq "HEAD" or $code =~ /^1..$/
l!l&&r!l&&!r
20115
MC/DC50%2/4
$req->method eq "HEAD" or $code =~ /^1..$/ or $code eq 204 or $code eq 304
$req->method eq "HEAD" $code =~ /^1..$/ $code eq 204 $code eq 304
Truth table60%3/5
A or B or C or D
A= $req->method eq "HEAD"B= $code =~ /^1..$/C= $code eq 204D= $code eq 304
ABCDresult
1XXX1
01XX1
001X1
00011
00000
492 3 $self->debug_printf( "BODY done [none]" );
493 3 return $on_done->( $ctx );
494 }
495  
496 114 my $transfer_encoding = $header->header( "Transfer-Encoding" );
497 114 my $content_length = $header->content_length;
498  
499 114 ▶ if( defined $transfer_encoding and $transfer_encoding eq "chunked" ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $transfer_encoding and $transfer_encoding eq 'chunked') { } 4 110 114
elsif (defined $content_length) { } 99 11 110
Condition66%2/3
defined $transfer_encoding and $transfer_encoding eq "chunked"
!ll&&!rl&&r
11004
MC/DC50%1/2
defined $transfer_encoding and $transfer_encoding eq "chunked"
defined $transfer_encoding $transfer_encoding eq "chunked"
Truth table66%2/3
A and B
A= defined $transfer_encodingB= $transfer_encoding eq "chunked"
ABresult
0X0
100
111
500 4 $self->debug_printf( "BODY chunks" );
501  
502 4 ▶ $stall_timer->reason = "receiving body chunks" if $stall_timer;
Branch: 1 branch
BranchTrueFalseTotal
if $stall_timer 0 4 4
503 4 return $self->_mk_on_read_chunked( $on_more, $on_done );
504 }
505 elsif( defined $content_length ) {
506 99 $self->debug_printf( "BODY length $content_length" );
507  
508 99 ▶ if( $content_length == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_length == 0) 48 51 99
509 48 $self->debug_printf( "BODY done [length=0]" );
510 48 return $on_done->( $ctx );
511 }
512  
513 51 ▶ $stall_timer->reason = "receiving body" if $stall_timer;
Branch: 1 branch
BranchTrueFalseTotal
if $stall_timer 1 50 51
514 51 return $self->_mk_on_read_length( $content_length, $on_more, $on_done );
515 }
516 else {
517 11 $self->debug_printf( "BODY until EOF" );
518  
519 11 ▶ $stall_timer->reason = "receiving body until EOF" if $stall_timer;
Branch: 1 branch
BranchTrueFalseTotal
if $stall_timer 0 11 11
520 11 return $self->_mk_on_read_until_eof( $on_more, $on_done );
521 }
522 129 };
523 }
524  
525 sub _mk_on_read_chunked
526 {
527 4 ▶ shift; # $self
Subroutine _mk_on_read_chunked: called CC=1 SCAR=0.4
528 4 my ( $on_more, $on_done ) = @_;
529  
530 4 my $chunk_length;
531  
532 sub {
533 16 ▶ my ( $self, $buffref, $closed, $ctx ) = @_;
Subroutine __ANON__: called CC=12 SCAR=29.7
534  
535 16 my $req = $ctx->req;
536 16 my $f = $ctx->f;
537  
538 16 ▶ if( !defined $chunk_length and $$buffref =~ s/^(.*?)$CRLF// ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $chunk_length and $$buffref =~ s/^(.*?)$CRLF//) 10 6 16
Condition66%2/3
not defined $chunk_length and $$buffref =~ s/^(.*?)$CRLF//
!ll&&!rl&&r
6010
MC/DC50%1/2
not defined $chunk_length and $$buffref =~ s/^(.*?)$CRLF//
not defined $chunk_length $$buffref =~ s/^(.*?)$CRLF//
Truth table66%2/3
not(A) and B
A= not defined $chunk_lengthB= $$buffref =~ s/^(.*?)$CRLF//
ABresult
0X0
100
111
539 10 my $header = $1;
540 10 $ctx->resp_bytes += $+[0];
541  
542 # Chunk header
543 10 ▶ unless( $header =~ s/^([A-Fa-f0-9]+).*// ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($header =~ s/^([A-Fa-f0-9]+).*//) 1 9 10
544 1 ▶ $f->fail( "Corrupted chunk header", http => undef, $req ) unless $f->is_cancelled;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_cancelled 1 0 1
545 1 $self->close_now;
546 1 return 0;
547 }
548  
549 9 $chunk_length = hex( $1 );
550 9 ▶ return 1 if $chunk_length;
Branch: 1 branch
BranchTrueFalseTotal
if $chunk_length 6 3 9
551  
552 3 return $self->_mk_on_read_chunk_trailer( $req, $on_more, $on_done, $f );
553 }
554  
555 # Chunk is followed by a CRLF, which isn't counted in the length;
556 6 ▶ if( defined $chunk_length and length( $$buffref ) >= $chunk_length + 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $chunk_length and length $$buffref >= $chunk_length + 2) 6 0 6
Condition33%1/3
defined $chunk_length and length $$buffref >= $chunk_length + 2
!ll&&!rl&&r
006
MC/DC0%0/2
defined $chunk_length and length $$buffref >= $chunk_length + 2
defined $chunk_length length $$buffref >= $chunk_length + 2
Truth table33%1/3
A and B
A= defined $chunk_lengthB= length $$buffref >= $chunk_length + 2
ABresult
0X0
100
111
557 # Chunk body
558 6 my $chunk = substr( $$buffref, 0, $chunk_length, "" );
559 6 $ctx->resp_bytes += length $chunk;
560  
561 6 ▶ unless( $$buffref =~ s/^$CRLF// ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$buffref =~ s/^$CRLF//) 0 6 6
562 0 $self->debug_printf( "ERROR chunk without CRLF" );
563 0 ▶ $f->fail( "Chunk of size $chunk_length wasn't followed by CRLF", http => undef, $req ) unless $f->is_cancelled;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_cancelled 0 0 0
564 0 $self->close;
565 }
566  
567 6 $ctx->resp_bytes += $+[0];
568  
569 6 undef $chunk_length;
570  
571 6 return $on_more->( $chunk );
572 }
573  
574 0 ▶ if( $closed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($closed) 0 0 0
575 0 $self->debug_printf( "ERROR closed" );
576 0 ▶ $f->fail( "Connection closed while awaiting chunk", http => undef, $req ) unless $f->is_cancelled;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_cancelled 0 0 0
577 }
578 0 return 0;
579 4 };
580 }
581  
582 sub _mk_on_read_chunk_trailer
583 {
584 3 ▶ shift; # $self
Subroutine _mk_on_read_chunk_trailer: called CC=1 SCAR=0.2
585 3 my ( undef, $on_more, $on_done ) = @_;
586  
587 3 my $trailer = "";
588  
589 sub {
590 3 ▶ my ( $self, $buffref, $closed, $ctx ) = @_;
Subroutine __ANON__: called CC=5 SCAR=18.0
591  
592 3 my $req = $ctx->req;
593 3 my $f = $ctx->f;
594  
595 3 ▶ if( $closed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($closed) 0 3 3
596 0 $self->debug_printf( "ERROR closed" );
597 0 ▶ $f->fail( "Connection closed while awaiting chunk trailer", http => undef, $req ) unless $f->is_cancelled;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_cancelled 0 0 0
598 }
599  
600 3 ▶ $$buffref =~ s/^(.*)$CRLF// or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$$buffref =~ s/^(.*)$CRLF// or return 0 0 3 3
601 3 $trailer .= $1;
602 3 $ctx->resp_bytes += $+[0];
603  
604 3 ▶ return 1 if length $1;
Branch: 1 branch
BranchTrueFalseTotal
if length $1 0 3 3
605  
606 # TODO: Actually use the trailer
607  
608 3 $self->debug_printf( "BODY done [chunked]" );
609 3 return $on_done->( $ctx );
610 3 };
611 }
612  
613 sub _mk_on_read_length
614 {
615 51 ▶ shift; # $self
Subroutine _mk_on_read_length: called CC=1 SCAR=0.1
616 51 my ( $content_length, $on_more, $on_done ) = @_;
617  
618 sub {
619 52 ▶ my ( $self, $buffref, $closed, $ctx ) = @_;
Subroutine __ANON__: called CC=5 SCAR=17.1
620  
621 52 my $req = $ctx->req;
622 52 my $f = $ctx->f;
623  
624 # This will truncate it if the server provided too much
625 52 my $content = substr( $$buffref, 0, $content_length, "" );
626 52 $content_length -= length $content;
627 52 $ctx->resp_bytes += length $content;
628  
629 52 ▶ return undef unless $on_more->( $content );
Branch: 1 branch
BranchTrueFalseTotal
unless &$on_more($content) 0 52 52
630  
631 52 ▶ if( $content_length == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_length == 0) 50 2 52
632 50 $self->debug_printf( "BODY done [length]" );
633 50 return $on_done->( $ctx );
634 }
635  
636 2 ▶ if( $closed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($closed) 0 2 2
637 0 $self->debug_printf( "ERROR closed" );
638 0 ▶ $f->fail( "Connection closed while awaiting body", http => undef, $req ) unless $f->is_cancelled;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_cancelled 0 0 0
639 }
640 2 return 0;
641 51 };
642 }
643  
644 sub _mk_on_read_until_eof
645 {
646 11 ▶ shift; # $self
Subroutine _mk_on_read_until_eof: called CC=1 SCAR=0.0
647 11 my ( $on_more, $on_done ) = @_;
648  
649 sub {
650 23 ▶ my ( $self, $buffref, $closed, $ctx ) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
651  
652 23 my $content = $$buffref;
653 23 $$buffref = "";
654 23 $ctx->resp_bytes += length $content;
655  
656 23 ▶ return undef unless $on_more->( $content );
Branch: 1 branch
BranchTrueFalseTotal
unless &$on_more($content) 0 23 23
657  
658 23 ▶ return 0 unless $closed;
Branch: 1 branch
BranchTrueFalseTotal
unless $closed 12 11 23
659  
660 11 $self->debug_printf( "BODY done [eof]" );
661 11 return $on_done->( $ctx );
662 11 };
663 }
664  
665 =head1 AUTHOR
666  
667 Paul Evans <leonerd@leonerd.org.uk>
668  
669 =cut
670  
671 37 0x55AA;
« blib/lib/Net/Async/HTTP.pm ↑ Summary Build »