blib/lib/Net/Async/HTTP.pm

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