HTTP.pm

filter: Statementstmt 87.8% 370 / 421 Branchbran 76.5% 147 / 192 Conditioncond 59.2% 64 / 108 MC/DCmcdc 39.7% 27 / 68 Subroutinesub 90.2% 65 / 72 Podpod 61.5% 8 / 13 totaltotal 77.9% 681 / 874 CC 139 SCAR 28.3
CC
139
Cov
81%
configure
73.1
_make_request_for_uri
25.7
_do_one_request
20.3
CRAP 16.9
↑ Summary HTTP/Connection.pm »
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-2024 -- leonerd@leonerd.org.uk
5  
6 package Net::Async::HTTP 0.50;
7  
8 38 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 38 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 38 ▶use base qw( IO::Async::Notifier );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 38 our $DEFAULT_UA = "Perl + " . __PACKAGE__ . "/$Net::Async::HTTP::VERSION";
13 38 our $DEFAULT_MAXREDIR = 3;
14 38 our $DEFAULT_MAX_IN_FLIGHT = 4;
15 38 ▶our $DEFAULT_MAX_CONNS_PER_HOST = $ENV{NET_ASYNC_HTTP_MAXCONNS} // 1;
Condition50%1/2
$ENV{"NET_ASYNC_HTTP_MAXCONNS"} // 1
l!l
038
MC/DC0%0/1
$ENV{"NET_ASYNC_HTTP_MAXCONNS"} // 1
$ENV{"NET_ASYNC_HTTP_MAXCONNS"}
Truth table50%1/2
A
A= $ENV{"NET_ASYNC_HTTP_MAXCONNS"}
Aresult
11
00
16  
17 38 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 38 ▶use Net::Async::HTTP::Connection;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 38 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 38 ▶use HTTP::Request::Common qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 38 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 38 ▶use IO::Async::Stream 0.59;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 38 ▶use IO::Async::Loop 0.59; # ->connect( handle ) ==> $stream
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 38 ▶use Future 0.28; # ->set_label
Subroutine BEGIN: called CC=1 SCAR=0.0
29 38 ▶use Future::Utils 0.16 qw( repeat );
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 38 ▶use Metrics::Any 0.05 '$metrics',
Subroutine BEGIN: called CC=1 SCAR=0.0
32 strict => 1,
33 38 name_prefix => [qw( http client )];
34  
35 38 ▶use Scalar::Util qw( blessed reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
36 38 ▶use Time::HiRes qw( time );
Subroutine BEGIN: called CC=1 SCAR=0.0
37 38 ▶use List::Util 1.29 qw( first pairs pairgrep );
Subroutine BEGIN: called CC=1 SCAR=0.0
38 38 ▶use Socket 2.010 qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
39 SOCK_STREAM IPPROTO_IP IP_TOS
40 IPTOS_LOWDELAY IPTOS_THROUGHPUT IPTOS_RELIABILITY IPTOS_MINCOST
41 38 );
42  
43 38 ▶use constant HTTP_PORT => 80;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 38 ▶use constant HTTPS_PORT => 443;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 38 ▶use constant READ_LEN => 64*1024; # 64 KiB
Subroutine BEGIN: called CC=1 SCAR=0.0
47 38 ▶use constant WRITE_LEN => 64*1024; # 64 KiB
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 38 ▶use Struct::Dumb 0.07; # equallity operator overloading
Subroutine BEGIN: called CC=1 SCAR=0.0
50 38 struct Ready => [qw( future connecting )];
51  
52 =head1 NAME
53  
54 C<Net::Async::HTTP> - use HTTP with C<IO::Async>
55  
56 =head1 SYNOPSIS
57  
58 =for highlighter language=perl
59  
60 use Future::AsyncAwait;
61  
62 use IO::Async::Loop;
63 use Net::Async::HTTP;
64 use URI;
65  
66 my $loop = IO::Async::Loop->new();
67  
68 my $http = Net::Async::HTTP->new();
69  
70 $loop->add( $http );
71  
72 my $response = await $http->do_request(
73 uri => URI->new( "http://www.cpan.org/" ),
74 );
75  
76 print "Front page of http://www.cpan.org/ is:\n";
77 print $response->as_string;
78  
79 =head1 DESCRIPTION
80  
81 This object class implements an asynchronous HTTP user agent. It sends
82 requests to servers, returning L<Future> instances to yield responses when
83 they are received. The object supports multiple concurrent connections to
84 servers, and allows multiple requests in the pipeline to any one connection.
85 Normally, only one such object will be needed per program to support any
86 number of requests.
87  
88 As well as using futures the module also supports a callback-based interface.
89  
90 This module optionally supports SSL connections, if L<IO::Async::SSL> is
91 installed. If so, SSL can be requested either by passing a URI with the
92 C<https> scheme, or by passing a true value as the C<SSL> parameter.
93  
94 =head2 Connection Pooling
95  
96 There are three ways in which connections to HTTP server hosts are managed by
97 this object, controlled by the value of C<max_connections_per_host>. This
98 controls when new connections are established to servers, as compared to
99 waiting for existing connections to be free, as new requests are made to them.
100  
101 They are:
102  
103 =over 2
104  
105 =item max_connections_per_host = 1
106  
107 This is the default setting. In this mode, there will be one connection per
108 host on which there are active or pending requests. If new requests are made
109 while an existing one is outstanding, they will be queued to wait for it.
110  
111 If pipelining is active on the connection (because both the C<pipeline> option
112 is true and the connection is known to be an HTTP/1.1 server), then requests
113 will be pipelined into the connection awaiting their response. If not, they
114 will be queued awaiting a response to the previous before sending the next.
115  
116 =item max_connections_per_host > 1
117  
118 In this mode, there can be more than one connection per host. If a new request
119 is made, it will try to re-use idle connections if there are any, or if they
120 are all busy it will create a new connection to the host, up to the configured
121 limit.
122  
123 =item max_connections_per_host = 0
124  
125 In this mode, there is no upper limit to the number of connections per host.
126 Every new request will try to reuse an idle connection, or else create a new
127 one if all the existing ones are busy.
128  
129 =back
130  
131 These modes all apply per hostname / server port pair; they do not affect the
132 behaviour of connections made to differing hostnames, or differing ports on
133 the same hostname.
134  
135 =cut
136  
137 38 $metrics->make_gauge( requests_in_flight =>
138 description => "Count of the number of requests sent that have not yet been completed",
139 # no labels
140 );
141 38 $metrics->make_counter( requests =>
142 description => "Number of HTTP requests sent",
143 labels => [qw( method )],
144 );
145 38 $metrics->make_counter( responses =>
146 description => "Number of HTTP responses received",
147 labels => [qw( method code )],
148 );
149 38 $metrics->make_timer( request_duration =>
150 description => "Duration of time spent waiting for responses",
151 # no labels
152 );
153 38 $metrics->make_distribution( response_bytes =>
154 name => [qw( response bytes )],
155 description => "The size in bytes of responses received",
156 units => "bytes",
157 # no labels
158 );
159  
160 sub _init
161 36 ▶{
Subroutine _init: called CC=1 SCAR=0.0
162 36 my $self = shift;
163  
164 36 $self->{connections} = {}; # { "$host:$port" } -> [ @connections ]
165  
166 36 $self->{read_len} = READ_LEN;
167 36 $self->{write_len} = WRITE_LEN;
168  
169 36 $self->{max_connections_per_host} = $DEFAULT_MAX_CONNS_PER_HOST;
170  
171 36 $self->{ssl_params} = {};
172 }
173  
174 sub _remove_from_loop
175 5 ▶{
Subroutine _remove_from_loop: called CC=2 SCAR=6.9
176 5 my $self = shift;
177  
178 5 foreach my $conn ( map { @$_ } values %{ $self->{connections} } ) {
179 4 $conn->close;
180 }
181  
182 5 $self->SUPER::_remove_from_loop( @_ );
183 }
184  
185 =head1 PARAMETERS
186  
187 The following named parameters may be passed to C<new> or C<configure>:
188  
189 =head2 user_agent => STRING
190  
191 A string to set in the C<User-Agent> HTTP header. If not supplied, one will
192 be constructed that declares C<Net::Async::HTTP> and the version number.
193  
194 =head2 headers => ARRAY or HASH
195  
196 I<Since version 0.45.>
197  
198 A set of extra headers to apply to every outgoing request. May be specified
199 either as an even-sized array containing key/value pairs, or a hash.
200  
201 Individual header values may be added or changed without replacing the entire
202 set by using the L<configure> method and passing a key called C<+headers>:
203  
204 $http->configure( +headers => { One_More => "Key" } );
205  
206 =head2 max_redirects => INT
207  
208 Optional. How many levels of redirection to follow. If not supplied, will
209 default to 3. Give 0 to disable redirection entirely.
210  
211 =head2 max_in_flight => INT
212  
213 Optional. The maximum number of in-flight requests to allow per host when
214 pipelining is enabled and supported on that host. If more requests are made
215 over this limit they will be queued internally by the object and not sent to
216 the server until responses are received. If not supplied, will default to 4.
217 Give 0 to disable the limit entirely.
218  
219 =head2 max_connections_per_host => INT
220  
221 Optional. Controls the maximum number of connections per hostname/server port
222 pair, before requests will be queued awaiting one to be free. Give 0 to
223 disable the limit entirely. See also the L</Connection Pooling> section
224 documented above.
225  
226 Currently, if not supplied it will default to 1. However, it has been found in
227 practice that most programs will raise this limit to something higher, perhaps
228 3 or 4. Therefore, a future version of this module may set a higher value.
229  
230 To test if your application will handle this correctly, you can set a
231 different default by setting an environment variable:
232  
233 $ NET_ASYNC_HTTP_MAXCONNS=3 perl ...
234  
235 =head2 timeout => NUM
236  
237 Optional. How long in seconds to wait before giving up on a request. If not
238 supplied then no default will be applied, and no timeout will take place.
239  
240 =head2 stall_timeout => NUM
241  
242 Optional. How long in seconds to wait after each write or read of data on a
243 socket, before giving up on a request. This may be more useful than
244 C<timeout> on large-file operations, as it will not time out provided that
245 regular progress is still being made.
246  
247 =head2 proxy_host => STRING
248  
249 =head2 proxy_port => INT
250  
251 I<Since version 0.10.>
252  
253 =head2 proxy_path => PATH
254  
255 I<Since version 0.49.>
256  
257 Optional. Default values to apply to each C<request> method.
258  
259 =head2 cookie_jar => HTTP::Cookies
260  
261 Optional. A reference to a L<HTTP::Cookies> object. Will be used to set
262 cookies in requests and store them from responses.
263  
264 =head2 pipeline => BOOL
265  
266 Optional. If false, disables HTTP/1.1-style request pipelining.
267  
268 =head2 close_after_request => BOOL
269  
270 I<Since version 0.45.>
271  
272 Optional. If true, will set the C<Connection: close> header on outgoing
273 requests and disable pipelining, thus making every request use a new
274 connection.
275  
276 =head2 family => INT
277  
278 =head2 local_host => STRING
279  
280 =head2 local_port => INT
281  
282 =head2 local_addrs => ARRAY
283  
284 =head2 local_addr => HASH or ARRAY
285  
286 Optional. Parameters to pass on to the C<connect> method used to connect
287 sockets to HTTP servers. Sets the socket family and local socket address to
288 C<bind()> to. For more detail, see the documentation in
289 L<IO::Async::Connector>.
290  
291 =head2 fail_on_error => BOOL
292  
293 Optional. Affects the behaviour of response handling when a C<4xx> or C<5xx>
294 response code is received. When false, these responses will be processed as
295 other responses and yielded as the result of the future, or passed to the
296 C<on_response> callback. When true, such an error response causes the future
297 to fail, or the C<on_error> callback to be invoked.
298  
299 The HTTP response and request objects will be passed as well as the code and
300 message, and the failure name will be C<http>.
301  
302 ( $code_message, "http", $response, $request ) = $f->failure
303  
304 $on_error->( "$code $message", $response, $request )
305  
306 =head2 read_len => INT
307  
308 =head2 write_len => INT
309  
310 Optional. Used to set the reading and writing buffer lengths on the underlying
311 C<IO::Async::Stream> objects that represent connections to the server. If not
312 define, a default of 64 KiB will be used.
313  
314 =head2 ip_tos => INT or STRING
315  
316 Optional. Used to set the C<IP_TOS> socket option on client sockets. If given,
317 should either be a C<IPTOS_*> constant, or one of the string names
318 C<lowdelay>, C<throughput>, C<reliability> or C<mincost>. If undefined or left
319 absent, no option will be set.
320  
321 =head2 decode_content => BOOL
322  
323 Optional. If true, incoming responses that have a recognised
324 C<Content-Encoding> are handled by the module, and decompressed content is
325 passed to the body handling callback or returned in the C<HTTP::Response>. See
326 L</CONTENT DECODING> below for details of which encoding types are recognised.
327 When this option is enabled, outgoing requests also have the
328 C<Accept-Encoding> header added to them if it does not already exist.
329  
330 Currently the default is false, because this behaviour is new, but it may
331 default to true in a later version. Applications which care which behaviour
332 applies should set this to a defined value to ensure it doesn't change.
333  
334 =head2 SSL_*
335  
336 Additionally, any parameters whose names start with C<SSL_> will be stored and
337 passed on requests to perform SSL requests. This simplifies configuration of
338 common SSL parameters.
339  
340 =head2 require_SSL => BOOL
341  
342 Optional. If true, then any attempt to make a request that does not use SSL
343 (either by calling C<request>, or as a result of a redirection) will
344 immediately fail.
345  
346 =head2 SOCKS_*
347  
348 I<Since version 0.42.>
349  
350 Additionally, any parameters whose names start with C<SOCKS_> will be stored
351 and used by L<Net::Async::SOCKS> to establish connections via a configured
352 proxy.
353  
354 =cut
355  
356 sub configure
357 48 ▶{
Subroutine configure: called CC=24 SCAR=42.9
Pod documented
358 48 my $self = shift;
359 48 my %params = @_;
360  
361 48 foreach (qw( user_agent max_redirects max_in_flight max_connections_per_host
362 timeout stall_timeout proxy_host proxy_port cookie_jar pipeline
363 close_after_request family local_host local_port local_addrs local_addr
364 fail_on_error read_len write_len decode_content require_SSL ))
365 {
366 1008 ▶ $self->{$_} = delete $params{$_} if exists $params{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{$_} 49 959 1008
367 }
368  
369 # Always store internally as ARRAyref
370 48 ▶ if( my $headers = delete $params{headers} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $headers = delete $params{"headers"}) 1 47 48
371 1 ▶ @{ $self->{headers} } =
Branch: 2 branches
BranchTrueFalseTotal
ref $headers eq 'ARRAY' ? : 0 1 1
ref $headers eq 'HASH' ? : 1 0 1
372 ( ref $headers eq "ARRAY" ) ? @$headers :
373 ( ref $headers eq "HASH" ) ? %$headers :
374 croak "Expected 'headers' to be either ARRAY or HASH reference";
375 }
376  
377 48 ▶ if( my $more = delete $params{"+headers"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $more = delete $params{"+headers"}) 1 47 48
378 1 ▶ my @more =
Branch: 2 branches
BranchTrueFalseTotal
ref $more eq 'ARRAY' ? : 0 1 1
ref $more eq 'HASH' ? : 1 0 1
379 ( ref $more eq "ARRAY" ) ? @$more :
380 ( ref $more eq "HASH" ) ? %$more :
381 croak "Expected '+headers' to be either ARRAY or HASH reference";
382 1 my %to_remove = @more;
383  
384 1 my $headers = $self->{headers};
385 1 ▶ @$headers = ( ( pairgrep { !exists $to_remove{$a} } @$headers ), @more );
Subroutine __ANON__: called CC=1 SCAR=0.0
386 }
387  
388 48 foreach ( grep { m/^SSL_/ } keys %params ) {
389 0 $self->{ssl_params}{$_} = delete $params{$_};
390 }
391  
392 48 foreach ( grep { m/^SOCKS_/ } keys %params ) {
393 0 $self->{socks_params}{$_} = delete $params{$_};
394 }
395  
396 48 ▶ if( exists $params{ip_tos} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"ip_tos"}) 0 48 48
397 # TODO: This conversion should live in IO::Async somewhere
398 0 my $ip_tos = delete $params{ip_tos};
399 0 ▶ $ip_tos = IPTOS_LOWDELAY if defined $ip_tos and $ip_tos eq "lowdelay";
Branch: 1 branch
BranchTrueFalseTotal
if defined $ip_tos and $ip_tos eq "lowdelay" 0 0 0
Condition0%0/3
defined $ip_tos and $ip_tos eq "lowdelay"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ip_tos and $ip_tos eq "lowdelay"
defined $ip_tos $ip_tos eq "lowdelay"
Truth table0%0/3
A and B
A= defined $ip_tosB= $ip_tos eq "lowdelay"
ABresult
0X0
100
111
400 0 ▶ $ip_tos = IPTOS_THROUGHPUT if defined $ip_tos and $ip_tos eq "throughput";
Branch: 1 branch
BranchTrueFalseTotal
if defined $ip_tos and $ip_tos eq "throughput" 0 0 0
Condition0%0/3
defined $ip_tos and $ip_tos eq "throughput"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ip_tos and $ip_tos eq "throughput"
defined $ip_tos $ip_tos eq "throughput"
Truth table0%0/3
A and B
A= defined $ip_tosB= $ip_tos eq "throughput"
ABresult
0X0
100
111
401 0 ▶ $ip_tos = IPTOS_RELIABILITY if defined $ip_tos and $ip_tos eq "reliability";
Branch: 1 branch
BranchTrueFalseTotal
if defined $ip_tos and $ip_tos eq "reliability" 0 0 0
Condition0%0/3
defined $ip_tos and $ip_tos eq "reliability"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ip_tos and $ip_tos eq "reliability"
defined $ip_tos $ip_tos eq "reliability"
Truth table0%0/3
A and B
A= defined $ip_tosB= $ip_tos eq "reliability"
ABresult
0X0
100
111
402 0 ▶ $ip_tos = IPTOS_MINCOST if defined $ip_tos and $ip_tos eq "mincost";
Branch: 1 branch
BranchTrueFalseTotal
if defined $ip_tos and $ip_tos eq "mincost" 0 0 0
Condition0%0/3
defined $ip_tos and $ip_tos eq "mincost"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ip_tos and $ip_tos eq "mincost"
defined $ip_tos $ip_tos eq "mincost"
Truth table0%0/3
A and B
A= defined $ip_tosB= $ip_tos eq "mincost"
ABresult
0X0
100
111
403 0 $self->{ip_tos} = $ip_tos;
404 }
405  
406 48 $self->SUPER::configure( %params );
407  
408 48 ▶ defined $self->{user_agent} or $self->{user_agent} = $DEFAULT_UA;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"user_agent"} or $self->{"user_agent"} = $Net::Async::HTTP::DEFAULT_UA 38 10 48
409 48 ▶ defined $self->{max_redirects} or $self->{max_redirects} = $DEFAULT_MAXREDIR;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"max_redirects"} or $self->{"max_redirects"} = $Net::Async::HTTP::DEFAULT_MAXREDIR 12 36 48
410 48 ▶ defined $self->{max_in_flight} or $self->{max_in_flight} = $DEFAULT_MAX_IN_FLIGHT;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"max_in_flight"} or $self->{"max_in_flight"} = $Net::Async::HTTP::DEFAULT_MAX_IN_FLIGHT 13 35 48
411 48 ▶ defined $self->{pipeline} or $self->{pipeline} = 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"pipeline"} or $self->{"pipeline"} = 1 17 31 48
412 }
413  
414 =head1 METHODS
415  
416 The following methods documented in an C<await> expression return L<Future>
417 instances.
418  
419 When returning a Future, the following methods all indicate HTTP-level errors
420 using the Future failure name of C<http>. If the error relates to a specific
421 response it will be included. The original request is also included.
422  
423 $f->fail( $message, "http", $response, $request )
424  
425 =cut
426  
427 sub connect_connection
428 101 ▶{
Subroutine connect_connection: called CC=11 SCAR=26.0
Pod undocumented
429 101 my $self = shift;
430 101 my %args = @_;
431  
432 101 my $conn = delete $args{conn};
433 101 ▶ my $key = defined $args{path} ? "unix:$args{path}" : "$args{host}:$args{port}";
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'path'} ? : 1 100 101
434  
435 101 my $on_error = $args{on_error};
436  
437 101 ▶ if( my $socks_params = $self->{socks_params} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $socks_params = $self->{"socks_params"}) 0 101 101
438 0 require Net::Async::SOCKS;
439 0 Net::Async::SOCKS->VERSION( '0.003' );
440  
441 0 unshift @{ $args{extensions} }, "SOCKS";
442 0 $args{$_} = $socks_params->{$_} for keys %$socks_params;
443 }
444  
445 101 ▶ if( $args{SSL} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"SSL"}) 0 101 101
446 0 require IO::Async::SSL;
447 0 IO::Async::SSL->VERSION( '0.12' ); # 0.12 has ->connect(handle) bugfix
448  
449 0 unshift @{ $args{extensions} }, "SSL";
450 }
451  
452 101 ▶ if( exists $args{port} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"port"}) 100 1 101
453 100 $args{service} = delete $args{port};
454 }
455  
456 101 ▶ unless( exists $args{host} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"host"}) 1 100 101
457 1 $args{addr} = { family => $args{family}, path => $args{path} };
458 }
459  
460 my $f = $conn->connect(
461 family => ( $args{family} || $self->{family} || 0 ),
462 404 ▶ ( map { defined $self->{$_} ? ( $_ => $self->{$_} ) : () }
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{$_} ? : 0 404 404
463 qw( local_host local_port local_addrs local_addr ) ),
464 %args,
465 93 ▶ )->on_done( sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
466 93 my ( $stream ) = @_;
467 93 $stream->configure(
468 notifier_name => "$key,fd=" . $stream->read_handle->fileno,
469 );
470  
471 # Defend against ->setsockopt doing silly things like detecting SvPOK()
472 93 ▶ $stream->read_handle->setsockopt( IPPROTO_IP, IP_TOS, $self->{ip_tos}+0 ) if defined $self->{ip_tos};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"ip_tos"} 0 93 93
473  
474 93 $stream->ready;
475 7 ▶ })->on_fail( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
476 7 $on_error->( $conn, "$key - $_[0] failed [$_[-1]]" );
477 101 ▶ });
Condition80%4/5
$args{'family'} || $self->{'family'} || 0
l!l
1100
$args{"family"} or $self->{"family"}
l!l&&r!l&&!r
10100
MC/DC50%1/2
$args{'family'} || $self->{'family'} || 0
$args{"family"} $self->{"family"}
Truth table66%2/3
A or B
A= $args{"family"}B= $self->{"family"}
ABresult
1X1
011
000
478  
479 101 ▶ $f->on_ready( sub { undef $f } ) unless $f->is_ready; # intentionally cycle
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 18 83 101
480  
481 101 return $f;
482 }
483  
484 sub get_connection
485 142 ▶{
Subroutine get_connection: called CC=12 SCAR=24.9
Pod undocumented
486 142 my $self = shift;
487 142 my %args = @_;
488  
489 142 ▶ my $loop = $self->get_loop or croak "Cannot ->get_connection without a Loop";
Branch: 1 branch
BranchTrueFalseTotal
my $loop = $self->get_loop or croak("Cannot ->get_connection without a Loop") 142 0 142
490  
491 142 ▶ my $key = defined $args{path} ? "unix:$args{path}" : "$args{host}:$args{port}";
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'path'} ? : 1 141 142
492 142 ▶ my $conns = $self->{connections}{$key} ||= [];
Condition100%2/2
$self->{"connections"}{$key} ||= []
l!l
6577
MC/DC100%1/1
$self->{"connections"}{$key} ||= []
$self->{"connections"}{$key}
Truth table100%2/2
A
A= $self->{"connections"}{$key}
Aresult
11
00
493 142 ▶ my $ready_queue = $self->{ready_queue}{$key} ||= [];
Condition100%2/2
$self->{"ready_queue"}{$key} ||= []
l!l
6775
MC/DC100%1/1
$self->{"ready_queue"}{$key} ||= []
$self->{"ready_queue"}{$key}
Truth table100%2/2
A
A= $self->{"ready_queue"}{$key}
Aresult
11
00
494  
495 # Have a look to see if there are any idle connected ones first
496 142 foreach my $conn ( @$conns ) {
497 51 ▶ $conn->is_idle and $conn->read_handle and return Future->done( $conn );
Branch: 1 branch
BranchTrueFalseTotal
$conn->is_idle and $conn->read_handle and return "Future"->done($conn) 13 38 51
Condition100%3/3
$conn->is_idle and $conn->read_handle
!ll&&!rl&&r
31713
MC/DC100%2/2
$conn->is_idle and $conn->read_handle
$conn->is_idle $conn->read_handle
Truth table100%3/3
A and B
A= $conn->is_idleB= $conn->read_handle
ABresult
0X0
100
111
498 }
499  
500 129 my $ready = $args{ready};
501 129 ▶ $ready or push @$ready_queue, $ready =
Branch: 1 branch
BranchTrueFalseTotal
$ready or push @$ready_queue, $ready = Ready($self->loop->new_future->set_label("[ready $key]"), 0) 4 125 129
502 Ready( $self->loop->new_future->set_label( "[ready $key]" ), 0 );
503  
504 129 my $f = $ready->future;
505  
506 129 my $max = $self->{max_connections_per_host};
507 129 ▶ if( $max and @$conns >= $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max and @$conns >= $max) 28 101 129
Condition100%3/3
$max and @$conns >= $max
!ll&&!rl&&r
39828
MC/DC100%2/2
$max and @$conns >= $max
$max @$conns >= $max
Truth table100%3/3
A and B
A= $maxB= @$conns >= $max
ABresult
0X0
100
111
508 28 return $f;
509 }
510  
511 my $conn = Net::Async::HTTP::Connection->new(
512 notifier_name => "$key,connecting",
513 ready_queue => $ready_queue,
514 404 ▶ ( map { $_ => $self->{$_} }
Condition100%3/3
$self->{'pipeline'} && !$self->{'close_after_request'}
!ll&&!rl&&r
7193
MC/DC100%2/2
$self->{'pipeline'} && !$self->{'close_after_request'}
$self->{'pipeline'} !$self->{'close_after_request'}
Truth table100%3/3
A && not(B)
A= $self->{'pipeline'}B= !$self->{'close_after_request'}
ABresult
0X0
100
111
515 qw( max_in_flight read_len write_len decode_content ) ),
516 pipeline => ( $self->{pipeline} && !$self->{close_after_request} ),
517 is_proxy => $args{is_proxy},
518  
519 68 ▶ on_closed => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
520 68 my $conn = shift;
521 68 my $http = $conn->parent;
522  
523 68 $conn->remove_from_parent;
524 68 @$conns = grep { $_ != $conn } @$conns;
525  
526 68 ▶ if( my $next = first { !$_->connecting } @$ready_queue ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = &first(sub { not $_->connecting; } , @$ready_queue)) 2 66 68
527 # Requeue another connection attempt as there's still more to do
528 2 $http->get_connection( %args, ready => $next );
529 }
530 },
531 101 );
532  
533 101 $self->add_child( $conn );
534 101 push @$conns, $conn;
535  
536 $ready->connecting = $self->connect_connection( %args,
537 conn => $conn,
538 7 ▶ on_error => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
539 7 my $conn = shift;
540  
541 7 ▶ $f->fail( @_ ) unless $f->is_cancelled;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_cancelled 7 0 7
542  
543 7 $conn->remove_from_parent;
544 7 @$conns = grep { $_ != $conn } @$conns;
545 7 @$ready_queue = grep { $_ != $ready } @$ready_queue;
546  
547 7 ▶ if( my $next = first { !$_->connecting } @$ready_queue ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = &first(sub { not $_->connecting; } , @$ready_queue)) 2 5 7
548 # Requeue another connection attempt as there's still more to do
549 2 $self->get_connection( %args, ready => $next );
550 }
551 },
552 1 ▶ )->on_cancel( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
553 1 $conn->remove_from_parent;
554 1 @$conns = grep { $_ != $conn } @$conns;
555 101 });
556  
557 101 return $f;
558 }
559  
560 =head2 do_request
561  
562 $response = await $http->do_request( %args );
563  
564 Send an HTTP request to a server, returning a L<Future> that will yield the
565 response. The request may be represented by an L<HTTP::Request> object, or a
566 L<URI> object, depending on the arguments passed.
567  
568 The following named arguments are used for C<HTTP::Request>s:
569  
570 =over 8
571  
572 =item request => HTTP::Request
573  
574 A reference to an C<HTTP::Request> object
575  
576 =item host => STRING
577  
578 Hostname of the server to connect to
579  
580 =item port => INT or STRING
581  
582 Optional. Port number or service of the server to connect to. If not defined,
583 will default to C<http> or C<https> depending on whether SSL is being used.
584  
585 =item family => INT or STRING
586  
587 Optional. Restricts the socket family for connecting. If not defined, will
588 default to the globally-configured value in the object. The value may either
589 be a C<PF_*> constant directly, or the lowercase name of one such as C<inet>.
590  
591 =item SSL => BOOL
592  
593 Optional. If true, an SSL connection will be used.
594  
595 =back
596  
597 The following named arguments are used for C<URI> requests:
598  
599 =over 8
600  
601 =item uri => URI or STRING
602  
603 A reference to a C<URI> object, or a plain string giving the request URI. If
604 the scheme is C<https> then an SSL connection will be used.
605  
606 =item method => STRING
607  
608 Optional. The HTTP method name. If missing, C<GET> is used.
609  
610 =item content => STRING or ARRAY ref
611  
612 Optional. The body content to use for C<PUT> or C<POST> requests.
613  
614 If this is a plain scalar it will be used directly, and a C<content_type>
615 field must also be supplied to describe it.
616  
617 If this is an ARRAY ref and the request method is C<POST>, it will be form
618 encoded. It should contain an even-sized list of field names and values. For
619 more detail see L<HTTP::Request::Common/POST>.
620  
621 =item content_type => STRING
622  
623 The type of non-form data C<content>.
624  
625 =item user => STRING
626  
627 =item pass => STRING
628  
629 Optional. If both are given, the HTTP Basic Authorization header will be sent
630 with these details.
631  
632 =item headers => ARRAY|HASH
633  
634 Optional. If provided, contains additional HTTP headers to set on the
635 constructed request object. If provided as an ARRAY reference, it should
636 contain an even-sized list of name/value pairs.
637  
638 =item proxy_host => STRING
639  
640 =item proxy_port => INT
641  
642 I<Since version 0.10.>
643  
644 Optional. Override the hostname or port number implied by the URI.
645  
646 =item proxy_path => PATH
647  
648 I<Since version 0.49.>
649  
650 Optional. Set a UNIX socket path to use as a proxy. To make use of this, also
651 set the C<family> argument to C<unix>.
652  
653 =back
654  
655 For either request type, it takes the following arguments:
656  
657 =over 8
658  
659 =item request_body => STRING | CODE | Future
660  
661 Optional. Allows request body content to be generated by a future or
662 callback, rather than being provided as part of the C<request> object. This
663 can either be a plain string, a C<CODE> reference to a generator function, or
664 a future.
665  
666 As this is passed to the underlying L<IO::Async::Stream> C<write> method, the
667 usual semantics apply here. If passed a C<CODE> reference, it will be called
668 repeatedly whenever it's safe to write. The code should should return C<undef>
669 to indicate completion. If passed a C<Future> it is expected to eventually
670 yield the body value.
671  
672 As with the C<content> parameter, the C<content_type> field should be
673 specified explicitly in the request header, as should the content length
674 (typically via the L<HTTP::Request> C<content_length> method). See also
675 F<examples/PUT.pl>.
676  
677 =item expect_continue => BOOL
678  
679 Optional. If true, sets the C<Expect> request header to the value
680 C<100-continue> and does not send the C<request_body> parameter until a
681 C<100 Continue> response is received from the server. If an error response is
682 received then the C<request_body> code, if present, will not be invoked.
683  
684 =item on_ready => CODE
685  
686 Optional. A callback that is invoked once a socket connection is established
687 with the HTTP server, but before the request is actually sent over it. This
688 may be used by the client code to inspect the socket, or perform any other
689 operations on it. This code is expected to return a C<Future>; only once that
690 has completed will the request cycle continue. If it fails, that failure is
691 propagated to the caller.
692  
693 $f = $on_ready->( $connection );
694  
695 =item on_redirect => CODE
696  
697 Optional. A callback that is invoked if a redirect response is received,
698 before the new location is fetched. It will be passed the response and the new
699 URL.
700  
701 $on_redirect->( $response, $location );
702  
703 =item on_body_write => CODE
704  
705 Optional. A callback that is invoked after each successful C<syswrite> of the
706 body content. This may be used to implement an upload progress indicator or
707 similar. It will be passed the total number of bytes of body content written
708 so far (i.e. excluding bytes consumed in the header).
709  
710 $on_body_write->( $written );
711  
712 =item max_redirects => INT
713  
714 Optional. How many levels of redirection to follow. If not supplied, will
715 default to the value given in the constructor.
716  
717 =item timeout => NUM
718  
719 =item stall_timeout => NUM
720  
721 Optional. Overrides the object's configured timeout values for this one
722 request. If not specified, will use the configured defaults.
723  
724 On a timeout, the returned future will fail with either C<timeout> or
725 C<stall_timeout> as the operation name.
726  
727 ( $message, "timeout" ) = $f->failure;
728  
729 =back
730  
731 =head2 do_request (void)
732  
733 $http->do_request( %args );
734  
735 When not returning a future, the following extra arguments are used as
736 callbacks instead:
737  
738 =over 8
739  
740 =item on_response => CODE
741  
742 A callback that is invoked when a response to this request has been received.
743 It will be passed an L<HTTP::Response> object containing the response the
744 server sent.
745  
746 $on_response->( $response );
747  
748 =item on_header => CODE
749  
750 Alternative to C<on_response>. A callback that is invoked when the header of a
751 response has been received. It is expected to return a C<CODE> reference for
752 handling chunks of body content. This C<CODE> reference will be invoked with
753 no arguments once the end of the request has been reached, and whatever it
754 returns will be used as the result of the returned C<Future>, if there is one.
755  
756 $on_body_chunk = $on_header->( $header );
757  
758 $on_body_chunk->( $data );
759 $response = $on_body_chunk->();
760  
761 =item on_error => CODE
762  
763 A callback that is invoked if an error occurs while trying to send the request
764 or obtain the response. It will be passed an error message.
765  
766 $on_error->( $message );
767  
768 If this is invoked because of a received C<4xx> or C<5xx> error code in an
769 HTTP response, it will be invoked with the response and request objects as
770 well.
771  
772 $on_error->( $message, $response, $request );
773  
774 =back
775  
776 =cut
777  
778 sub _do_one_request
779 138 ▶{
Subroutine _do_one_request: called CC=16 SCAR=30.1
780 138 my $self = shift;
781 138 my %args = @_;
782  
783 138 my $host = delete $args{host};
784 138 my $port = delete $args{port};
785 138 my $request = delete $args{request};
786 138 my $SSL = delete $args{SSL};
787  
788 138 my $start_time = time;
789 138 ▶ my $stall_timeout = $args{stall_timeout} // $self->{stall_timeout};
Condition66%2/3
$args{"stall_timeout"} // $self->{"stall_timeout"}
l!l&&r!l&&!r
40134
MC/DC50%1/2
$args{"stall_timeout"} // $self->{"stall_timeout"}
$args{"stall_timeout"} $self->{"stall_timeout"}
Truth table66%2/3
A // B
A= $args{"stall_timeout"}B= $self->{"stall_timeout"}
ABresult
1X1
011
000
790  
791 138 $self->prepare_request( $request );
792  
793 138 ▶ if( $self->{require_SSL} and not $SSL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"require_SSL"} and not $SSL) 0 138 138
Condition33%1/3
$self->{"require_SSL"} and not $SSL
!ll&&!rl&&r
13800
MC/DC0%0/2
$self->{"require_SSL"} and not $SSL
$self->{"require_SSL"} not $SSL
Truth table33%1/3
A and not(B)
A= $self->{"require_SSL"}B= not $SSL
ABresult
0X0
100
111
794 0 return Future->fail( "Non-SSL request is not allowed with 'require_SSL' set",
795 http => undef, $request );
796 }
797  
798 138 ▶ if( $metrics ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Net::Async::HTTP::metrics) 34 104 138
799 34 $metrics->inc_gauge( requests_in_flight => );
800 34 $metrics->inc_counter( requests => [ method => $request->method ] );
801 }
802  
803 138 my %conn_target;
804 my $is_proxy;
805  
806 138 ▶ if( defined $args{proxy_host} or ( defined $self->{proxy_host} and not defined $args{proxy_path} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args{'proxy_host'} or defined $self->{'proxy_host'} and not defined $args{'proxy_path'}) { } 1 137 138
elsif (defined $args{'proxy_path'} or defined $self->{'proxy_path'}) { } 1 136 137
Condition66%4/6
defined $args{"proxy_host"} or defined $self->{"proxy_host"} and not defined $args{"proxy_path"}
l!l&&r!l&&!r
01137
defined $self->{"proxy_host"} and not defined $args{"proxy_path"}
!ll&&!rl&&r
13701
MC/DC33%1/3
defined $args{"proxy_host"} or defined $self->{"proxy_host"} and not defined $args{"proxy_path"}
defined $args{"proxy_host"} defined $self->{"proxy_host"} not defined $args{"proxy_path"}
Truth table50%2/4
A or B and not(C)
A= defined $args{"proxy_host"}B= defined $self->{"proxy_host"}C= not defined $args{"proxy_path"}
ABCresult
1XX1
0111
00X0
0100
807 %conn_target = (
808 host => $args{proxy_host} || $self->{proxy_host},
809 port => $args{proxy_port} || $self->{proxy_port},
810 1 ▶ );
Condition33%2/6
$args{'proxy_host'} || $self->{'proxy_host'}
l!l&&r!l&&!r
010
$args{'proxy_port'} || $self->{'proxy_port'}
l!l&&r!l&&!r
010
MC/DC0%0/2
$args{'proxy_host'} || $self->{'proxy_host'}
$args{'proxy_host'} $self->{'proxy_host'}
MC/DC0%0/2
$args{'proxy_port'} || $self->{'proxy_port'}
$args{'proxy_port'} $self->{'proxy_port'}
Truth table33%1/3
A || B
A= $args{'proxy_host'}B= $self->{'proxy_host'}
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $args{'proxy_port'}B= $self->{'proxy_port'}
ABresult
1X1
011
000
811 1 $is_proxy = 1;
812 }
813 ▶ elsif( defined $args{proxy_path} or defined $self->{proxy_path} ) {
Condition66%2/3
defined $args{"proxy_path"} or defined $self->{"proxy_path"}
l!l&&r!l&&!r
10136
MC/DC50%1/2
defined $args{"proxy_path"} or defined $self->{"proxy_path"}
defined $args{"proxy_path"} defined $self->{"proxy_path"}
Truth table66%2/3
A or B
A= defined $args{"proxy_path"}B= defined $self->{"proxy_path"}
ABresult
1X1
011
000
814 %conn_target = (
815 path => $args{proxy_path} || $self->{proxy_path},
816 1 ▶ );
Condition33%1/3
$args{'proxy_path'} || $self->{'proxy_path'}
l!l&&r!l&&!r
100
MC/DC0%0/2
$args{'proxy_path'} || $self->{'proxy_path'}
$args{'proxy_path'} $self->{'proxy_path'}
Truth table33%1/3
A || B
A= $args{'proxy_path'}B= $self->{'proxy_path'}
ABresult
1X1
011
000
817 1 $is_proxy = 1;
818 }
819 else {
820 136 %conn_target = (
821 host => $host,
822 port => $port,
823 );
824 }
825  
826 return $self->get_connection(
827 %conn_target,
828 is_proxy => $is_proxy,
829 ( defined $args{family} ? ( family => $args{family} ) : () ),
830 $SSL ? (
831 SSL => 1,
832 SSL_hostname => $host,
833 0 %{ $self->{ssl_params} },
834 0 ▶ ( map { m/^SSL_/ ? ( $_ => $args{$_} ) : () } keys %args ),
Branch: 1 branch
BranchTrueFalseTotal
/^SSL_/u ? : 0 0 0
835 ) : (),
836 130 ▶ )->then( sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
837 130 my ( $conn ) = @_;
838 130 ▶ $args{on_ready} ? $args{on_ready}->( $conn )->then_done( $conn )
Branch: 1 branch
BranchTrueFalseTotal
$args{'on_ready'} ? : 1 129 130
839 : Future->done( $conn )
840 130 ▶ })->then( sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
841 130 my ( $conn ) = @_;
842  
843 return $conn->request(
844 request => $request,
845 stall_timeout => $stall_timeout,
846 %args,
847 $SSL ? ( SSL => 1 ) : (),
848 116 ▶ on_done => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
849 116 my ( $ctx ) = @_;
850  
851 116 ▶ if( $metrics ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Net::Async::HTTP::metrics) 1 115 116
852 1 $metrics->dec_gauge( requests_in_flight => );
853 # TODO: Some sort of error counter instead for errors?
854 1 $metrics->inc_counter( responses => [ method => $request->method, code => $ctx->resp_header->code ] );
855 1 $metrics->report_timer( request_duration => time - $start_time );
856 1 $metrics->report_distribution( response_bytes => $ctx->resp_bytes );
857 }
858 },
859 130 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$SSL ? : 0 130 130
860 138 ▶ } );
Branch: 2 branches
BranchTrueFalseTotal
defined $args{'family'} ? : 1 137 138
$SSL ? : 0 138 138
861 }
862  
863 sub _should_redirect
864 116 ▶{
Subroutine _should_redirect: called CC=4 SCAR=13.9
865 116 my ( $response ) = @_;
866  
867 # Should only redirect if we actually have a Location header
868 116 ▶ return 0 unless $response->is_redirect and defined $response->header( "Location" );
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_redirect and defined $response->header("Location") 107 9 116
Condition100%3/3
$response->is_redirect and defined $response->header("Location")
!ll&&!rl&&r
10619
MC/DC100%2/2
$response->is_redirect and defined $response->header("Location")
$response->is_redirect defined $response->header("Location")
Truth table100%3/3
A and B
A= $response->is_redirectB= defined $response->header("Location")
ABresult
0X0
100
111
869  
870 9 my $req_method = $response->request->method;
871 # Should only redirect GET or HEAD requests
872 9 ▶ return $req_method eq "GET" || $req_method eq "HEAD";
Condition66%2/3
$req_method eq 'GET' || $req_method eq 'HEAD'
l!l&&r!l&&!r
801
MC/DC50%1/2
$req_method eq 'GET' || $req_method eq 'HEAD'
$req_method eq 'GET' $req_method eq 'HEAD'
Truth table66%2/3
A || B
A= $req_method eq 'GET'B= $req_method eq 'HEAD'
ABresult
1X1
011
000
873 }
874  
875 sub _do_request
876 131 ▶{
Subroutine _do_request: called CC=3 SCAR=11.0
877 131 my $self = shift;
878 131 my %args = @_;
879  
880 131 my $host = $args{host};
881 131 my $port = $args{port};
882 131 my $ssl = $args{SSL};
883  
884 131 my $on_header = delete $args{on_header};
885  
886 131 ▶ my $redirects = defined $args{max_redirects} ? $args{max_redirects} : $self->{max_redirects};
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'max_redirects'} ? : 1 130 131
887  
888 131 my $request = $args{request};
889 131 my $response;
890 my $reqf;
891 # Defeat prototype
892 138 ▶ my $future = &repeat( $self->_capture_weakself( sub {
Subroutine __ANON__: called CC=14 SCAR=26.7
893 138 my $self = shift;
894 138 my ( $previous_f ) = @_;
895  
896 138 ▶ if( $previous_f ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($previous_f) 7 131 138
897 7 my $previous_response = $previous_f->get;
898 7 $args{previous_response} = $previous_response;
899  
900 7 my $location = $previous_response->header( "Location" );
901  
902 7 ▶ if( $location =~ m{^http(?:s?)://} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($location =~ m[^http(?:s?)://]u) { } 4 3 7
elsif ($location =~ m[^/]u) { } 3 0 3
903 # skip
904 }
905 elsif( $location =~ m{^/} ) {
906 3 ▶ my $hostport = ( $port != HTTP_PORT ) ? "$host:$port" : $host;
Branch: 1 branch
BranchTrueFalseTotal
$port != 80 ? : 0 3 3
907 3 $location = "http://$hostport" . $location;
908 }
909 else {
910 0 return Future->fail( "Unrecognised Location: $location", http => $previous_response, $request );
911 }
912  
913 7 my $loc_uri = URI->new( $location );
914 7 ▶ unless( $loc_uri ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($loc_uri) 0 7 7
915 0 return Future->fail( "Unable to parse '$location' as a URI", http => $previous_response, $request );
916 }
917  
918 7 $self->debug_printf( "REDIRECT $loc_uri" );
919  
920 7 ▶ $args{on_redirect}->( $previous_response, $location ) if $args{on_redirect};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"on_redirect"} 2 5 7
921  
922 7 %args = $self->_make_request_for_uri( $loc_uri, %args );
923 7 $request = $args{request};
924  
925 7 undef $host; undef $port; undef $ssl;
926 }
927  
928 138 my $uri = $request->uri;
929 138 ▶ if( defined $uri->scheme and $uri->scheme =~ m/^http(s?)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $uri->scheme and $uri->scheme =~ /^http(s?)$/u) 97 41 138
Condition66%2/3
defined $uri->scheme and $uri->scheme =~ /^http(s?)$/u
!ll&&!rl&&r
41097
MC/DC50%1/2
defined $uri->scheme and $uri->scheme =~ /^http(s?)$/u
defined $uri->scheme $uri->scheme =~ /^http(s?)$/u
Truth table66%2/3
A and B
A= defined $uri->schemeB= $uri->scheme =~ /^http(s?)$/u
ABresult
0X0
100
111
930 97 ▶ $host = $uri->host if !defined $host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 10 87 97
931 97 ▶ $port = $uri->port if !defined $port;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $port 14 83 97
932 97 $ssl = ( $uri->scheme eq "https" );
933 }
934  
935 138 ▶ defined $host or croak "Expected 'host'";
Branch: 1 branch
BranchTrueFalseTotal
defined $host or croak("Expected 'host'") 138 0 138
936 138 ▶ defined $port or $port = ( $ssl ? HTTPS_PORT : HTTP_PORT );
Branch: 2 branches
BranchTrueFalseTotal
defined $port or $port = $ssl ? 443 : 80 98 40 138
$ssl ? : 0 40 40
937  
938 return $reqf = $self->_do_one_request(
939 host => $host,
940 port => $port,
941 SSL => $ssl,
942 %args,
943 118 ▶ on_header => $self->_capture_weakself( sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
944 118 my $self = shift;
945 118 ( $response ) = @_;
946  
947 # Consume and discard the entire body of a redirect
948 9 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
949 9 ▶ return if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 9 9
950 9 return $response;
951 118 ▶ } if $redirects and $response->is_redirect;
Branch: 1 branch
BranchTrueFalseTotal
if $redirects and $response->is_redirect 9 109 118
Condition100%3/3
$redirects and $response->is_redirect
!ll&&!rl&&r
11089
MC/DC100%2/2
$redirects and $response->is_redirect
$redirects $response->is_redirect
Truth table100%3/3
A and B
A= $redirectsB= $response->is_redirect
ABresult
0X0
100
111
952  
953 109 return $on_header->( $response );
954 138 } ),
955 );
956 } ),
957 131 ▶ while => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
958 131 my $f = shift;
959 131 ▶ return 0 if $f->failure or $f->is_cancelled;
Branch: 1 branch
BranchTrueFalseTotal
if $f->failure or $f->is_cancelled 15 116 131
Condition66%2/3
$f->failure or $f->is_cancelled
l!l&&r!l&&!r
150116
MC/DC50%1/2
$f->failure or $f->is_cancelled
$f->failure $f->is_cancelled
Truth table66%2/3
A or B
A= $f->failureB= $f->is_cancelled
ABresult
1X1
011
000
960 116 ▶ return _should_redirect( $response ) && $redirects--;
Condition100%3/3
_should_redirect($response) && $redirects--
!ll&&!rl&&r
10817
MC/DC100%2/2
_should_redirect($response) && $redirects--
_should_redirect($response) $redirects--
Truth table100%3/3
A && B
A= _should_redirect($response)B= $redirects--
ABresult
0X0
100
111
961 131 } );
962  
963 131 ▶ if( $self->{fail_on_error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"fail_on_error"}) 3 128 131
964 3 ▶ $future = $future->then_with_f( sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
965 3 my ( $f, $resp ) = @_;
966 3 my $code = $resp->code;
967  
968 3 ▶ if( $code =~ m/^[45]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /^[45]/u) 2 1 3
969 2 my $message = $resp->message;
970 2 $message =~ s/\r$//; # HTTP::Message bug
971  
972 2 return Future->fail( "$code $message", http => $resp, $request );
973 }
974  
975 1 return $f;
976 3 });
977 }
978  
979 131 return $future;
980 }
981  
982 sub do_request
983 131 ▶{
Subroutine do_request: called CC=11 SCAR=24.1
Pod documented
984 131 my $self = shift;
985 131 my %args = @_;
986  
987 131 ▶ if( my $uri = delete $args{uri} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $uri = delete $args{'uri'}) { } 83 48 131
elsif (not defined $args{'request'}) { } 0 48 48
988 83 %args = $self->_make_request_for_uri( $uri, %args );
989 }
990 elsif( !defined $args{request} ) {
991 0 croak "Require either 'uri' or 'request' argument";
992 }
993  
994 131 ▶ if( $args{on_header} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'on_header'}) { } 5 126 131
elsif ($args{'on_response'} or defined wantarray) { } 126 0 126
995 # ok
996 }
997 ▶ elsif( $args{on_response} or defined wantarray ) {
Condition66%2/3
$args{"on_response"} or defined wantarray
l!l&&r!l&&!r
79470
MC/DC0%0/2
$args{"on_response"} or defined wantarray
$args{"on_response"} defined wantarray
Truth table66%2/3
A or B
A= $args{"on_response"}B= defined wantarray
ABresult
1X1
011
000
998 104 ▶ $args{on_header} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
999 104 my ( $response ) = @_;
1000 177 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1001 177 ▶ if( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 75 102 177
1002 75 $response->add_content( @_ );
1003 }
1004 else {
1005 102 return $response;
1006 }
1007 104 };
1008 }
1009 126 }
1010 else {
1011 0 croak "Expected 'on_response' or 'on_header' as CODE ref or to return a Future";
1012 }
1013  
1014 131 my $on_error = delete $args{on_error};
1015 131 ▶ my $timeout = defined $args{timeout} ? $args{timeout} : $self->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'timeout'} ? : 34 97 131
1016  
1017 131 my $future = $self->_do_request( %args );
1018  
1019 131 ▶ if( defined $timeout ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timeout) 34 97 131
1020 $future = Future->wait_any(
1021 $future,
1022 $self->loop->timeout_future( after => $timeout )
1023 34 ▶ ->transform( fail => sub { "Timed out", timeout => } ),
Subroutine __ANON__: called CC=1 SCAR=0.0
1024 );
1025 }
1026  
1027 107 ▶ $future->on_done( $self->_capture_weakself( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1028 107 my $self = shift;
1029 107 my $response = shift;
1030 107 $self->process_response( $response );
1031 131 } ) );
1032  
1033 8 ▶ $future->on_fail( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1034 8 my ( $message, $name, @rest ) = @_;
1035 8 $on_error->( $message, @rest );
1036 131 ▶ }) if $on_error;
Branch: 1 branch
BranchTrueFalseTotal
if $on_error 87 44 131
1037  
1038 131 ▶ if( my $on_response = delete $args{on_response} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $on_response = delete $args{"on_response"}) 79 52 131
1039 72 ▶ $future->on_done( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1040 72 my ( $response ) = @_;
1041 72 $on_response->( $response );
1042 79 });
1043 }
1044  
1045 # DODGY HACK:
1046 # In void context we'll lose reference on the ->wait_any Future, so the
1047 # timeout logic will never happen. So lets purposely create a cycle by
1048 # capturing the $future in on_done/on_fail closures within itself. This
1049 # conveniently clears them out to drop the ref when done.
1050 131 ▶ return $future if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 79 52 131
1051  
1052 52 ▶ $future->on_ready( sub { undef $future } );
Subroutine __ANON__: called CC=1 SCAR=0.0
1053 }
1054  
1055 sub _make_request_for_uri
1056 90 ▶{
Subroutine _make_request_for_uri: called CC=24 SCAR=32.5
1057 90 my $self = shift;
1058 90 my ( $uri, %args ) = @_;
1059  
1060 90 ▶ if( !ref $uri ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $uri) { } 15 75 90
elsif (builtin::blessed($uri) and not $uri->isa('URI')) { } 0 75 75
1061 15 $uri = URI->new( $uri );
1062 }
1063 ▶ elsif( blessed $uri and !$uri->isa( "URI" ) ) {
Condition33%1/3
builtin::blessed($uri) and not $uri->isa("URI")
!ll&&!rl&&r
0750
MC/DC0%0/2
builtin::blessed($uri) and not $uri->isa("URI")
builtin::blessed($uri) not $uri->isa("URI")
Truth table33%1/3
A and not(B)
A= builtin::blessed($uri)B= not $uri->isa("URI")
ABresult
0X0
100
111
1064 0 croak "Expected 'uri' as a URI reference";
1065 }
1066  
1067 90 ▶ my $method = delete $args{method} || "GET";
Condition100%2/2
delete $args{'method'} || 'GET'
l!l
4545
MC/DC100%1/1
delete $args{'method'} || 'GET'
delete $args{'method'}
Truth table100%2/2
A
A= delete $args{'method'}
Aresult
11
00
1068  
1069 90 $args{host} = $uri->host;
1070 90 $args{port} = $uri->port;
1071  
1072 90 my $request;
1073  
1074 90 ▶ if( $method eq "POST" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'POST') { } 2 88 90
1075 2 ▶ defined $args{content} or croak "Expected 'content' with POST method";
Branch: 1 branch
BranchTrueFalseTotal
defined $args{"content"} or croak("Expected 'content' with POST method") 2 0 2
1076  
1077 # Lack of content_type didn't used to be a failure condition:
1078 ref $args{content} or defined $args{content_type} or
1079 2 ▶ carp "No 'content_type' was given with 'content'";
Branch: 1 branch
BranchTrueFalseTotal
ref $args{"content"} or defined $args{"content_type"} or carp("No 'content_type' was given with 'content'") 2 0 2
Condition66%2/3
ref $args{"content"} or defined $args{"content_type"}
l!l&&r!l&&!r
110
MC/DC0%0/2
ref $args{"content"} or defined $args{"content_type"}
ref $args{"content"} defined $args{"content_type"}
Truth table66%2/3
A or B
A= ref $args{"content"}B= defined $args{"content_type"}
ABresult
1X1
011
000
1080  
1081 # This will automatically encode a form for us
1082 2 $request = HTTP::Request::Common::POST( $uri, Content => $args{content}, Content_Type => $args{content_type} );
1083 }
1084 else {
1085 88 $request = HTTP::Request->new( $method, $uri );
1086 88 ▶ if( defined $args{content} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"content"}) 3 85 88
1087 3 ▶ defined $args{content_type} or carp "No 'content_type' was given with 'content'";
Branch: 1 branch
BranchTrueFalseTotal
defined $args{"content_type"} or carp("No 'content_type' was given with 'content'") 3 0 3
1088  
1089 3 $request->content( $args{content} );
1090 3 ▶ $request->content_type( $args{content_type} // "" );
Condition50%1/2
$args{"content_type"} // ""
l!l
30
MC/DC0%0/1
$args{"content_type"} // ""
$args{"content_type"}
Truth table50%1/2
A
A= $args{"content_type"}
Aresult
11
00
1091 }
1092 }
1093  
1094 90 $request->protocol( "HTTP/1.1" );
1095 90 ▶ if( $args{port} != $uri->default_port ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'port'} != $uri->default_port) { } 16 74 90
1096 16 $request->header( Host => "$args{host}:$args{port}" );
1097 }
1098 else {
1099 74 $request->header( Host => "$args{host}" );
1100 }
1101  
1102 90 my $headers = $args{headers};
1103 90 ▶ if( $headers and reftype $headers eq "ARRAY" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($headers and builtin::reftype($headers) eq 'ARRAY') { } 1 89 90
elsif ($headers and builtin::reftype($headers) eq 'HASH') { } 1 176 177
Condition100%3/3
$headers and builtin::reftype($headers) eq "ARRAY"
!ll&&!rl&&r
8811
MC/DC100%2/2
$headers and builtin::reftype($headers) eq "ARRAY"
$headers builtin::reftype($headers) eq "ARRAY"
Truth table100%3/3
A and B
A= $headersB= builtin::reftype($headers) eq "ARRAY"
ABresult
0X0
100
111
1104 1 $request->header( @$_ ) for pairs @$headers;
1105 }
1106 ▶ elsif( $headers and reftype $headers eq "HASH" ) {
Condition66%2/3
$headers and builtin::reftype($headers) eq "HASH"
!ll&&!rl&&r
8801
MC/DC50%1/2
$headers and builtin::reftype($headers) eq "HASH"
$headers builtin::reftype($headers) eq "HASH"
Truth table66%2/3
A and B
A= $headersB= builtin::reftype($headers) eq "HASH"
ABresult
0X0
100
111
1107 1 $request->header( $_, $headers->{$_} ) for keys %$headers;
1108 }
1109  
1110 90 my ( $user, $pass );
1111  
1112 90 ▶ if( defined $uri->userinfo ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $uri->userinfo) { } 1 89 90
elsif (defined $args{'user'} and defined $args{'pass'}) { } 1 176 177
1113 1 ( $user, $pass ) = split( m/:/, $uri->userinfo, 2 );
1114 }
1115 ▶ elsif( defined $args{user} and defined $args{pass} ) {
Condition66%2/3
defined $args{"user"} and defined $args{"pass"}
!ll&&!rl&&r
8801
MC/DC50%1/2
defined $args{"user"} and defined $args{"pass"}
defined $args{"user"} defined $args{"pass"}
Truth table66%2/3
A and B
A= defined $args{"user"}B= defined $args{"pass"}
ABresult
0X0
100
111
1116 1 $user = $args{user};
1117 1 $pass = $args{pass};
1118 }
1119  
1120 90 ▶ if( defined $user and defined $pass ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $user and defined $pass) 2 88 90
Condition66%2/3
defined $user and defined $pass
!ll&&!rl&&r
8802
MC/DC50%1/2
defined $user and defined $pass
defined $user defined $pass
Truth table66%2/3
A and B
A= defined $userB= defined $pass
ABresult
0X0
100
111
1121 2 $request->authorization_basic( $user, $pass );
1122 }
1123  
1124 90 $args{request} = $request;
1125  
1126 90 return %args;
1127 }
1128  
1129 =head2 GET, HEAD, PUT, ...
1130  
1131 $response = await $http->GET( $uri, %args );
1132  
1133 $response = await $http->HEAD( $uri, %args );
1134  
1135 $response = await $http->PUT( $uri, $content, %args );
1136  
1137 $response = await $http->POST( $uri, $content, %args );
1138  
1139 I<Since version 0.36.>
1140  
1141 $response = await $http->PATCH( $uri, $content, %args );
1142  
1143 I<Since version 0.48.>
1144  
1145 $response = await $http->DELETE( $uri, %args );
1146  
1147 I<Since version 0.49.>
1148  
1149 Convenient wrappers for performing C<GET>, C<HEAD>, C<PUT>, C<POST>, C<PATCH>
1150 or C<DELETE> requests with a C<URI> object and few if any other arguments,
1151 returning a C<Future>.
1152  
1153 Remember that C<POST> with non-form data (as indicated by a plain scalar
1154 instead of an C<ARRAY> reference of form data name/value pairs) needs a
1155 C<content_type> key in C<%args>.
1156  
1157 =cut
1158  
1159 sub GET
1160 8 ▶{
Subroutine GET: called CC=1 SCAR=0.0
Pod documented
1161 8 my $self = shift;
1162 8 my ( $uri, @args ) = @_;
1163 8 return $self->do_request( method => "GET", uri => $uri, @args );
1164 }
1165  
1166 sub HEAD
1167 0 ▶{
Subroutine HEAD: not called CC=1 SCAR=6.9
Pod documented
1168 0 my $self = shift;
1169 0 my ( $uri, @args ) = @_;
1170 0 return $self->do_request( method => "HEAD", uri => $uri, @args );
1171 }
1172  
1173 sub PUT
1174 0 ▶{
Subroutine PUT: not called CC=1 SCAR=6.9
Pod documented
1175 0 my $self = shift;
1176 0 my ( $uri, $content, @args ) = @_;
1177 0 return $self->do_request( method => "PUT", uri => $uri, content => $content, @args );
1178 }
1179  
1180 sub POST
1181 0 ▶{
Subroutine POST: not called CC=1 SCAR=6.9
Pod undocumented
1182 0 my $self = shift;
1183 0 my ( $uri, $content, @args ) = @_;
1184 0 return $self->do_request( method => "POST", uri => $uri, content => $content, @args );
1185 }
1186  
1187 sub PATCH
1188 0 ▶{
Subroutine PATCH: not called CC=1 SCAR=6.9
Pod undocumented
1189 0 my $self = shift;
1190 0 my ( $uri, $content, @args ) = @_;
1191 0 return $self->do_request( method => "PATCH", uri => $uri, content => $content, @args );
1192 }
1193  
1194 sub DELETE
1195 0 ▶{
Subroutine DELETE: not called CC=1 SCAR=6.9
1196 0 my $self = shift;
1197 0 my ( $uri, @args ) = @_;
1198 0 return $self->do_request( method => "DELETE", uri => $uri, @args );
1199 }
1200  
1201 =head1 SUBCLASS METHODS
1202  
1203 The following methods are intended as points for subclasses to override, to
1204 add extra functionallity.
1205  
1206 =cut
1207  
1208 =head2 prepare_request
1209  
1210 $http->prepare_request( $request );
1211  
1212 Called just before the C<HTTP::Request> object is sent to the server.
1213  
1214 =cut
1215  
1216 sub prepare_request
1217 138 ▶{
Subroutine prepare_request: called CC=5 SCAR=16.1
Pod documented
1218 138 my $self = shift;
1219 138 my ( $request ) = @_;
1220  
1221 138 ▶ $request->init_header( 'User-Agent' => $self->{user_agent} ) if length $self->{user_agent};
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"user_agent"} 34 104 138
1222 138 ▶ if( $self->{close_after_request} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'close_after_request'}) { } 1 137 138
1223 1 $request->header( "Connection" => "close" );
1224 }
1225 else {
1226 137 $request->init_header( "Connection" => "keep-alive" );
1227 }
1228  
1229 138 foreach ( pairs @{ $self->{headers} } ) {
1230 3 $request->init_header( $_->key, $_->value );
1231 }
1232  
1233 138 ▶ $self->{cookie_jar}->add_cookie_header( $request ) if $self->{cookie_jar};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cookie_jar"} 2 136 138
1234 }
1235  
1236 =head2 process_response
1237  
1238 $http->process_response( $response );
1239  
1240 Called after a non-redirect C<HTTP::Response> has been received from a server.
1241 The originating request will be set in the object.
1242  
1243 =cut
1244  
1245 sub process_response
1246 107 ▶{
Subroutine process_response: called CC=2 SCAR=6.9
Pod documented
1247 107 my $self = shift;
1248 107 my ( $response ) = @_;
1249  
1250 107 ▶ $self->{cookie_jar}->extract_cookies( $response ) if $self->{cookie_jar};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cookie_jar"} 2 105 107
1251 }
1252  
1253 =head1 CONTENT DECODING
1254  
1255 If the required decompression modules are installed and available, compressed
1256 content can be decoded. If the received C<Content-Encoding> is recognised and
1257 the required module is available, the content is transparently decoded and the
1258 decoded content is returned in the resulting response object, or passed to the
1259 data chunk handler. In this case, the original C<Content-Encoding> header will
1260 be deleted from the response, and its value will be available instead as
1261 C<X-Original-Content-Encoding>.
1262  
1263 The following content encoding types are recognised by these modules:
1264  
1265 =over 4
1266  
1267 =cut
1268  
1269 =item * gzip (q=0.7) and deflate (q=0.5)
1270  
1271 Recognised if L<Compress::Raw::Zlib> version 2.057 or newer is installed.
1272  
1273 =cut
1274  
1275 38 ▶if( eval { require Compress::Raw::Zlib and $Compress::Raw::Zlib::VERSION >= 2.057 } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Compress::Raw::Zlib and $Compress::Raw::Zlib::VERSION >= "2.057" } }) 38 0 38
Condition33%1/3
require Compress::Raw::Zlib && $Compress::Raw::Zlib::VERSION >= 2.057
!ll&&!rl&&r
0038
MC/DC0%0/2
require Compress::Raw::Zlib && $Compress::Raw::Zlib::VERSION >= 2.057
require Compress::Raw::Zlib $Compress::Raw::Zlib::VERSION >= 2.057
Truth table33%1/3
A && B
A= require Compress::Raw::ZlibB= $Compress::Raw::Zlib::VERSION >= 2.057
ABresult
0X0
100
111
1276 2 ▶ my $make_zlib_decoder = sub {
Subroutine __ANON__: called CC=1 SCAR=0.1
1277 2 my ( $bits ) = @_;
1278 2 my $inflator = Compress::Raw::Zlib::Inflate->new(
1279 -ConsumeInput => 0,
1280 -WindowBits => $bits,
1281 );
1282 4 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=14.6
1283 4 my $output;
1284 4 ▶ my $status = @_ ? $inflator->inflate( $_[0], $output )
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2 2 4
1285 : $inflator->inflate( "", $output, 1 );
1286 4 ▶ die "$status\n" if $status && $status != Compress::Raw::Zlib::Z_STREAM_END();
Branch: 1 branch
BranchTrueFalseTotal
if $status and $status != Compress::Raw::Zlib::Z_STREAM_END() 0 4 4
Condition33%1/3
$status and $status != Compress::Raw::Zlib::Z_STREAM_END()
!ll&&!rl&&r
040
MC/DC0%0/2
$status and $status != Compress::Raw::Zlib::Z_STREAM_END()
$status $status != Compress::Raw::Zlib::Z_STREAM_END()
Truth table33%1/3
A and B
A= $statusB= $status != Compress::Raw::Zlib::Z_STREAM_END()
ABresult
0X0
100
111
1287 4 return $output;
1288 2 };
1289 38 };
1290  
1291 # RFC1950
1292 __PACKAGE__->register_decoder(
1293 1 ▶ deflate => 0.5, sub { $make_zlib_decoder->( 15 ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
1294 38 );
1295  
1296 # RFC1952
1297 __PACKAGE__->register_decoder(
1298 1 ▶ gzip => 0.7, sub { $make_zlib_decoder->( Compress::Raw::Zlib::WANT_GZIP() ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
1299 38 );
1300 }
1301  
1302 =item * bzip2 (q=0.8)
1303  
1304 Recognised if L<Compress::Bzip2> version 2.10 or newer is installed.
1305  
1306 =cut
1307  
1308 38 ▶if( eval { require Compress::Bzip2 and $Compress::Bzip2::VERSION >= 2.10 } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Compress::Bzip2 and $Compress::Bzip2::VERSION >= "2.1" } }) 0 38 38
Condition0%0/3
require Compress::Bzip2 && $Compress::Bzip2::VERSION >= 2.1
!ll&&!rl&&r
000
MC/DC0%0/2
require Compress::Bzip2 && $Compress::Bzip2::VERSION >= 2.1
require Compress::Bzip2 $Compress::Bzip2::VERSION >= 2.1
Truth table0%0/3
A && B
A= require Compress::Bzip2B= $Compress::Bzip2::VERSION >= 2.1
ABresult
0X0
100
111
1309 __PACKAGE__->register_decoder(
1310 0 ▶ bzip2 => 0.8, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1311 0 my $inflator = Compress::Bzip2::inflateInit();
1312 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1313 0 ▶ return unless my ( $in ) = @_;
Branch: 1 branch
BranchTrueFalseTotal
unless my($in) = 0 0 0
1314 0 my $out = $inflator->bzinflate( \$in );
1315 0 ▶ die $inflator->bzerror."\n" if !defined $out;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $out 0 0 0
1316 0 return $out;
1317 0 };
1318 }
1319 0 );
1320 }
1321  
1322 =back
1323  
1324 Other content encoding types can be registered by calling the following method
1325  
1326 =head2 register_decoder
1327  
1328 Net::Async::HTTP->register_decoder( $name, $q, $make_decoder )
1329  
1330 Registers an encoding type called C<$name>, at the quality value C<$q>. In
1331 order to decode this encoding type, C<$make_decoder> will be invoked with no
1332 paramters, and expected to return a CODE reference to perform one instance of
1333 decoding.
1334  
1335 $decoder = $make_decoder->()
1336  
1337 This decoder will be invoked on string buffers to decode them until
1338 the end of stream is reached, when it will be invoked with no arguments.
1339  
1340 $content = $decoder->( $encoded_content )
1341 $content = $decoder->() # EOS
1342  
1343 =cut
1344  
1345 {
1346 38 my %DECODERS; # {$name} = [$q, $make_decoder]
1347  
1348 sub register_decoder
1349 76 ▶ {
Subroutine register_decoder: called CC=1 SCAR=0.0
Pod documented
1350 76 shift;
1351 76 my ( $name, $q, $make_decoder ) = @_;
1352  
1353 76 $DECODERS{$name} = [ $q, $make_decoder ];
1354 }
1355  
1356 sub can_decode
1357 2 ▶ {
Subroutine can_decode: called CC=3 SCAR=13.6
Pod undocumented
1358 2 shift;
1359 2 ▶ if( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 2 0 2
1360 2 my ( $name ) = @_;
1361  
1362 2 ▶ return unless my $d = $DECODERS{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless my $d = $DECODERS{$name} 0 2 2
1363 2 return $d->[1]->();
1364 }
1365 else {
1366 0 my @ds = sort { $DECODERS{$b}[0] <=> $DECODERS{$a}[0] } keys %DECODERS;
1367 0 return join ", ", map { "$_;q=$DECODERS{$_}[0]" } @ds;
1368 }
1369 }
1370 }
1371  
1372 =head1 EXAMPLES
1373  
1374 =head2 Concurrent GET
1375  
1376 The C<Future>-returning C<GET> method makes it easy to await multiple URLs at
1377 once, by using the L<Future::Utils> C<fmap_void> utility
1378  
1379 use Future::AsyncAwait;
1380 use Future::Utils qw( fmap_void );
1381  
1382 my @URLs = ( ... );
1383  
1384 my $http = Net::Async::HTTP->new( ... );
1385 $loop->add( $http );
1386  
1387 my $future = fmap_void {
1388 my ( $url ) = @_;
1389 $http->GET( $url )
1390 ->on_done( sub {
1391 my $response = shift;
1392 say "$url succeeded: ", $response->code;
1393 say " Content-Type:", $response->content_type;
1394 } )
1395 ->on_fail( sub {
1396 my $failure = shift;
1397 say "$url failed: $failure";
1398 } );
1399 } foreach => \@URLs,
1400 concurrent => 5;
1401  
1402 await $future;
1403  
1404 =cut
1405  
1406 =head1 SEE ALSO
1407  
1408 =over 4
1409  
1410 =item *
1411  
1412 L<http://tools.ietf.org/html/rfc2616> - Hypertext Transfer Protocol -- HTTP/1.1
1413  
1414 =back
1415  
1416 =head1 SPONSORS
1417  
1418 Parts of this code, or bugfixes to it were paid for by
1419  
1420 =over 2
1421  
1422 =item *
1423  
1424 SocialFlow L<http://www.socialflow.com>
1425  
1426 =item *
1427  
1428 Shadowcat Systems L<http://www.shadow.cat>
1429  
1430 =item *
1431  
1432 NET-A-PORTER L<http://www.net-a-porter.com>
1433  
1434 =item *
1435  
1436 Cisco L<http://www.cisco.com>
1437  
1438 =back
1439  
1440 =head1 AUTHOR
1441  
1442 Paul Evans <leonerd@leonerd.org.uk>
1443  
1444 =cut
1445  
1446 38 0x55AA;
↑ Summary HTTP/Connection.pm »