Lambda/HTTP/Client.pm

filter: Statementstmt 34.9% 111 / 318 Branchbran 15.1% 30 / 198 Conditioncond 14.9% 13 / 87 MC/DCmcdc 0.0% 0 / 51 Subroutinesub 43.4% 20 / 46 Podpod 11.7% 2 / 17 totaltotal 24.5% 176 / 717 CC 135 SCAR 43.5
CC
135
Cov
26%
get_authenticator
306.0
__ANON__
180.7
__ANON__
150.1
CRAP 77.2
« Lambda/Loop/AnyEvent.pm ↑ Summary Lambda/HTTP/Server.pm »
1 package IO::Lambda::HTTP::Client;
2 2 ▶use vars qw(@ISA @EXPORT_OK $DEBUG);
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 @ISA = qw(Exporter);
4 2 @EXPORT_OK = qw(http_request);
5
6 2 ▶our $DEBUG = $IO::Lambda::DEBUG{http} || 0;
Condition50%1/2
$IO::Lambda::DEBUG{'http'} || 0
l!l
02
MC/DC0%0/1
$IO::Lambda::DEBUG{'http'} || 0
$IO::Lambda::DEBUG{'http'}
Truth table50%1/2
A
A= $IO::Lambda::DEBUG{'http'}
Aresult
11
00
7
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Errno;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use IO::Lambda qw(:lambda :stream);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use IO::Lambda::Socket qw(connect);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
18
19 sub http_request(&)
20 {
21 0 ▶ __PACKAGE__-> new(context)->
Subroutine http_request: not called CC=1 SCAR=6.9
Pod documented
22 condition(shift, \&http_request, 'http_request')
23 }
24
25 sub new
26 {
27 5 ▶ my ( $class, $req, %options) = @_;
Subroutine new: called CC=14 SCAR=32.7
Pod documented
28
29 5 my $self = bless {}, $class;
30
31 5 ▶ $self-> {deadline} = $options{timeout} + time if defined $options{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"timeout"} 0 5 5
32 5 ▶ $self-> {deadline} = $options{deadline} if defined $options{deadline};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"deadline"} 0 5 5
33 5 ▶ $self-> {max_redirect} = defined($options{max_redirect}) ? $options{max_redirect} : 7;
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'max_redirect'} ? : 0 5 5
34
35 5 delete @options{qw(deadline timeout max_redirect)};
36 5 $self-> {$_} = $options{$_} for keys %options;
37
38 5 my %headers;
39 5 $headers{'User-Agent'} = "perl/IO-Lambda-HTTP v$IO::Lambda::VERSION";
40
41 5 ▶ if ( $self-> {keep_alive}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"keep_alive"}) 0 5 5
42 0 ▶ unless ( $self-> {conn_cache}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"conn_cache"}) 0 0 0
43 0 require LWP::ConnCache;
44 0 $self-> {conn_cache} = LWP::ConnCache-> new;
45 }
46 0 ▶ unless ( $req-> protocol) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($req->protocol) 0 0 0
47 0 $req-> protocol('HTTP/1.1');
48 }
49 0 $headers{Host} = $req-> uri-> host;
50 0 $headers{Connection} = 'Keep-Alive';
51 0 $headers{'Keep-Alive'} = 300;
52 }
53
54 5 ▶ require IO::Lambda::DNS if $self-> {async_dns};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"async_dns"} 0 5 5
55
56 5 my $h = $req-> headers;
57 5 ▶ while ( my ($k, $v) = each %headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %headers) 5 5 10
58 5 ▶ $h-> header($k, $v) unless defined $h-> header($k);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h->header($k) 5 0 5
59 }
60
61 5 ▶ if ( $options{cookie_jar} //= {} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"cookie_jar"} //= {}) 5 0 5
Condition50%1/2
$options{"cookie_jar"} //= {}
l!l
05
MC/DC0%0/1
$options{"cookie_jar"} //= {}
$options{"cookie_jar"}
Truth table50%1/2
A
A= $options{"cookie_jar"}
Aresult
11
00
62 5 ▶ if ( ref($options{cookie_jar}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $options{"cookie_jar"} eq "HASH") 5 0 5
63 5 require HTTP::Cookies;
64 5 $options{cookie_jar} = HTTP::Cookies->new(%{$options{cookie_jar}});
65 }
66 5 $self->{cookie_jar} = $options{cookie_jar};
67 }
68
69 5 return $self-> handle_redirect( $req);
70 }
71
72 # HTTP::Response features methods base() and request() that we need to set as well
73 sub finalize_response
74 {
75 0 ▶ my ( $self, $req, $response) = @_;
Subroutine finalize_response: not called CC=1 SCAR=6.9
Pod undocumented
76 0 return $response;
77 }
78
79 sub redirect
80 {
81 0 ▶ my ( $self, $location, $req) = @_;
Subroutine redirect: not called CC=1 SCAR=6.9
Pod undocumented
82 0 return URI-> new_abs( $location, $req-> uri);
83 }
84
85 # reissue the request, if necessary, because of 30X or 401 errors
86 sub handle_redirect
87 {
88 5 ▶ my ( $self, $req) = @_;
Subroutine handle_redirect: called CC=1 SCAR=4.0
Pod undocumented
89
90 5 my $was_redirected = 0;
91 5 my $was_failed_auth = 0;
92
93 5 my $auth = $self-> {auth};
94
95 lambda {
96 5 ▶ my $method;
Subroutine __ANON__: called CC=5 SCAR=29.6
97 5 ▶ if ( $auth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth) 0 5 5
98 # create fake response for protocol initiation, -- but just once
99 0 my $x = HTTP::Response-> new;
100 0 $x-> headers-> header('WWW-Authenticate', split(',', $auth));
101 0 $method = $self-> get_authenticator( $req, $x);
102 0 undef $auth;
103 }
104 $self->{cookie_jar}->add_cookie_header($req) if
105 $self->{cookie_jar} &&
106 5 ▶ !$req->headers->header('Cookie');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cookie_jar"} and not $req->headers->header("Cookie") 5 0 5
Condition33%1/3
$self->{"cookie_jar"} and not $req->headers->header("Cookie")
!ll&&!rl&&r
005
MC/DC0%0/2
$self->{"cookie_jar"} and not $req->headers->header("Cookie")
$self->{"cookie_jar"} not $req->headers->header("Cookie")
Truth table33%1/3
A and not(B)
A= $self->{"cookie_jar"}B= not $req->headers->header("Cookie")
ABresult
0X0
100
111
107 5 ▶ context $method || $self-> handle_connection( $req);
Condition33%1/3
$method || $self->handle_connection($req)
l!l&&r!l&&!r
050
MC/DC0%0/2
$method || $self->handle_connection($req)
$method $self->handle_connection($req)
Truth table33%1/3
A || B
A= $methodB= $self->handle_connection($req)
ABresult
1X1
011
000
108 tail {
109 # request is finished
110 5 ▶ my $response = shift;
Subroutine __ANON__: called CC=14 SCAR=52.0
111 5 ▶ return $response unless ref($response);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $response 5 0 5
112
113 0 $response-> request($req);
114 0 ▶ $self->{cookie_jar}->extract_cookies($response) if $self->{cookie_jar};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cookie_jar"} 0 0 0
115
116 0 ▶ if ( $response-> code =~ /^30[12378]$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($response->code =~ /^30[12378]$/) { } 0 0 0
elsif (not $was_failed_auth and $response->code eq '401' and defined $self->{'username'} and defined $self->{'password'}) { } 0 0 0
117 return $self-> finalize_response($req, $response)
118 0 ▶ if $self->{max_redirect} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"max_redirect"} == 0 0 0 0
119
120 0 $was_failed_auth = 0;
121 return 'too many redirects'
122 0 ▶ if ++$was_redirected > $self-> {max_redirect};
Branch: 1 branch
BranchTrueFalseTotal
if ++$was_redirected > $self->{"max_redirect"} 0 0 0
123
124 0 my $location = $response-> header('Location');
125 0 ▶ return $response unless defined $location;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $location 0 0 0
126
127 0 my $uri = $self->redirect( $location, $req );
128 0 ▶ return $response if $uri->scheme !~ /^https?$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri->scheme =~ /^https?$/ 0 0 0
129
130 0 $req-> uri($uri);
131 0 $req-> headers-> header( Host => $req-> uri-> host);
132 0 $req-> remove_header('Cookie');
133 0 $req-> method('GET');
134
135 0 ▶ warn "redirect to " . $req-> uri . "\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::HTTP::Client::DEBUG 0 0 0
136
137 0 this-> start;
138 ▶ } elsif (
Condition0%0/9
not $was_failed_auth and $response->code eq 401 and defined $self->{"username"} and defined $self->{"password"}
!ll&&!rl&&r
000
not $was_failed_auth and $response->code eq 401 and defined $self->{"username"}
!ll&&!rl&&r
000
not $was_failed_auth and $response->code eq 401
!ll&&!rl&&r
000
MC/DC0%0/4
not $was_failed_auth and $response->code eq 401 and defined $self->{"username"} and defined $self->{"password"}
not $was_failed_auth $response->code eq 401 defined $self->{"username"} defined $self->{"password"}
Truth table0%0/5
not(A) and B and C and D
A= not $was_failed_authB= $response->code eq 401C= defined $self->{"username"}D= defined $self->{"password"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
139 not($was_failed_auth) and
140 $response-> code eq '401' and
141 defined($self-> {username}) and
142 defined($self-> {password})
143 ) {
144 0 $was_failed_auth++;
145 0 $method = $self-> get_authenticator( $req, $response);
146 0 context $method;
147 return $method ? tail {
148 0 ▶ my $r = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
149 0 ▶ return $r unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 0 0 0
150
151 # start from beginning, from handle_connection;
152 0 this-> start;
153 0 ▶ } : $self-> finalize_response($req, $response);
Branch: 1 branch
BranchTrueFalseTotal
$method ? : 0 0 0
154 } else {
155 0 return $self-> finalize_response($req, $response);
156 }
157 5 }};
158 }
159
160 # if request needs authentication, and we can do something about it, create
161 # a lambda that handles the authentication
162 sub get_authenticator
163 {
164 0 ▶ my ( $self, $req, $response) = @_;
Subroutine get_authenticator: not called CC=17 SCAR=57.2
Pod undocumented
165
166 # supports authentication?
167 0 my %auth;
168 0 for my $auth ( $response-> header('WWW-Authenticate')) {
169 0 $auth =~ s/\s.*$//;
170 0 $auth{$auth}++;
171 }
172
173 my %preferred = defined($self-> {preferred_auth}) ? (
174 ref($self-> {preferred_auth}) ?
175 0 %{ $self-> {preferred_auth} } :
176 0 ▶ ( $self-> {preferred_auth} => 1 )
Branch: 2 branches
BranchTrueFalseTotal
defined $self->{'preferred_auth'} ? : 0 0 0
ref $self->{'preferred_auth'} ? : 0 0 0
177 ) : ();
178
179 my @auth = sort {
180 0 ▶ ($preferred{$b} || 0) <=> ($preferred{$a} || 0)
Condition0%0/4
$preferred{$b} || 0
l!l
00
$preferred{$a} || 0
l!l
00
MC/DC0%0/1
$preferred{$b} || 0
$preferred{$b}
MC/DC0%0/1
$preferred{$a} || 0
$preferred{$a}
Truth table0%0/2
A
A= $preferred{$b}
Aresult
11
00
Truth table0%0/2
A
A= $preferred{$a}
Aresult
11
00
181 } grep {
182 0 ▶ not exists($preferred{$_}) or $preferred{$_} >= 0;
Branch: 1 branch
BranchTrueFalseTotal
not exists $preferred{$_} or $preferred{$_} >= 0 0 0 0
183 } keys %auth;
184
185 0 my $compilation_errors = '';
186 0 for my $auth ( @auth) {
187 0 ▶ if ( $auth eq 'Basic') {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth eq "Basic") 0 0 0
188 # always
189 0 ▶ warn "trying basic authentication\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::HTTP::Client::DEBUG 0 0 0
190 0 $req-> authorization_basic( $self-> {username}, $self-> {password});
191 0 return $self-> handle_connection( $req);
192 }
193
194 0 eval { require "IO/Lambda/HTTP/Authen/$auth.pm" };
195 0 ▶ $compilation_errors .= "$@\n"
Branch: 1 branch
BranchTrueFalseTotal
if $@ and not $@ =~ m[^Can't locate IO/Lambda/HTTP/Authen/$auth] 0 0 0
Condition0%0/3
$@ and not $@ =~ m[^Can't locate IO/Lambda/HTTP/Authen/$auth]
!ll&&!rl&&r
000
MC/DC0%0/2
$@ and not $@ =~ m[^Can't locate IO/Lambda/HTTP/Authen/$auth]
$@ not $@ =~ m[^Can't locate IO/Lambda/HTTP/Authen/$auth]
Truth table0%0/3
A and not(B)
A= $@B= not $@ =~ m[^Can't locate IO/Lambda/HTTP/Authen/$auth]
ABresult
0X0
100
111
196 if $@ and ($@ !~ m{^Can't locate IO/Lambda/HTTP/Authen/$auth});
197 0 ▶ next if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
198
199 0 my $lambda = "IO::Lambda::HTTP::Authen::$auth"->
200 authenticate( $self, $req, $response);
201 0 ▶ warn "trying authentication with '$auth'\n" if $DEBUG and $lambda;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::HTTP::Client::DEBUG and $lambda 0 0 0
Condition0%0/3
$IO::Lambda::HTTP::Client::DEBUG and $lambda
!ll&&!rl&&r
000
MC/DC0%0/2
$IO::Lambda::HTTP::Client::DEBUG and $lambda
$IO::Lambda::HTTP::Client::DEBUG $lambda
Truth table0%0/3
A and B
A= $IO::Lambda::HTTP::Client::DEBUGB= $lambda
ABresult
0X0
100
111
202 0 ▶ return $lambda if $lambda;
Branch: 1 branch
BranchTrueFalseTotal
if $lambda 0 0 0
203 }
204
205 # XXX Propagate compilation errors as http errors. Doubtful.
206 0 ▶ return lambda { $compilation_errors } if length $compilation_errors;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if length $compilation_errors 0 0 0
207
208 0 return undef;
209 }
210
211
212 # get scheme and eventually load module
213 2 my $got_https;
214 sub prepare_transport
215 {
216 5 ▶ my ( $self, $req) = @_;
Subroutine prepare_transport: called CC=7 SCAR=31.1
Pod undocumented
217 5 my $scheme = $req-> uri-> scheme;
218
219 5 ▶ unless ( defined $scheme) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $scheme) { } 5 0 5
if (do { $scheme eq 'https' }) { } 0 5 5
elsif ($scheme ne 'http') { } 0 5 5
220 0 return "bad URI: " . $req-> uri-> as_string;
221 } elsif ( $scheme eq 'https') {
222 0 ▶ unless ( $got_https) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($got_https) 0 0 0
223 0 eval { require IO::Lambda::HTTP::HTTPS; };
224 0 ▶ return "https not supported: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
225 0 $got_https++;
226 }
227 0 $self-> {reader} = IO::Lambda::HTTP::HTTPS::https_reader();
228 0 $self-> {writer} = \&IO::Lambda::HTTP::HTTPS::https_writer;
229 0 ▶ warn "https enabled\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::HTTP::Client::DEBUG 0 0 0
230 } elsif ( $scheme ne 'http') {
231 0 return "bad URI scheme: $scheme";
232 } else {
233 5 $self-> {reader} = undef;
234 5 $self-> {writer} = undef;
235 }
236
237 5 return;
238 }
239
240 # returns static lambda that reads from socket until a condition (see sysreader) is satisfied
241 sub http_read
242 {
243 0 ▶ my ( $self, $cond) = @_;
Subroutine http_read: not called CC=1 SCAR=6.9
Pod undocumented
244 0 return $self-> {reader}, $self-> {socket}, \ $self-> {buf}, $cond, $self-> {deadline};
245 }
246
247 # read from socket until a condition (see sysreader) is satisfied
248 # after this call no communication should happen
249 sub http_tail
250 {
251 0 ▶ my ( $self, $cond) = @_;
Subroutine http_tail: not called CC=1 SCAR=6.9
Pod undocumented
252 0 context $self-> http_read($cond);
253 0 &tail();
254 }
255
256 my $tcp_proto;
257 sub socket
258 {
259 5 ▶ my ( $self, $host, $port) = @_;
Subroutine socket: called CC=7 SCAR=33.2
Pod undocumented
260
261 5 my ($iaddr,$paddr,$sock,$error);
262 5 ▶ $tcp_proto //= getprotobyname('tcp');
Condition33%1/3
$tcp_proto //= getprotobyname "tcp"
l!l&&r!l&&!r
005
MC/DC0%0/2
$tcp_proto //= getprotobyname "tcp"
$tcp_proto getprotobyname "tcp"
Truth table33%1/3
A //= B
A= $tcp_protoB= getprotobyname "tcp"
ABresult
1X1
011
000
263 5 ▶ return undef, "gethostbyname(tcp) error:$!" unless $tcp_proto;
Branch: 1 branch
BranchTrueFalseTotal
unless $tcp_proto 5 0 5
264 0 ▶ return undef, "cannot resolve $host" unless $iaddr = inet_aton($host);
Branch: 1 branch
BranchTrueFalseTotal
unless $iaddr = inet_aton($host) 0 0 0
265 0 $paddr = sockaddr_in($port,$iaddr);
266 0 ▶ return undef, "error creating socket:$!" unless socket($sock,PF_INET,SOCK_STREAM,$tcp_proto);
Branch: 1 branch
BranchTrueFalseTotal
unless CORE::socket $sock, 2, 1, $tcp_proto 0 0 0
267 0 $sock->blocking(0);
268 0 ▶ return undef, "connect($host,$port) error:$!" unless CORE::connect($sock,$paddr) or $!{EINPROGRESS};
Branch: 1 branch
BranchTrueFalseTotal
unless CORE::connect $sock, $paddr or $!{"EINPROGRESS"} 0 0 0
Condition0%0/3
CORE::connect $sock, $paddr or $!{"EINPROGRESS"}
l!l&&r!l&&!r
000
MC/DC0%0/2
CORE::connect $sock, $paddr or $!{"EINPROGRESS"}
CORE::connect $sock, $paddr $!{"EINPROGRESS"}
Truth table0%0/3
A or B
A= CORE::connect $sock, $paddrB= $!{"EINPROGRESS"}
ABresult
1X1
011
000
269 0 return $sock;
270 }
271
272 sub parse_proxy
273 {
274 5 ▶ my ( $self, $req) = @_;
Subroutine parse_proxy: called CC=16 SCAR=39.8
Pod undocumented
275 5 my ( $host, $port);
276 5 ▶ if ( exists( $self-> {proxy})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'proxy'}) { } 1 4 5
277 1 ▶ if ( !defined $self->{proxy}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $self->{'proxy'}) { } 1 0 1
elsif (ref $self->{'proxy'}) { } 0 0 0
278 # nothing
279 1 ( $host, $port) = ( $req-> uri-> host, $req-> uri-> port);
280 } elsif ( ref($self->{proxy})) {
281 Carp::confess("'proxy' option must be a non-empty array") if
282 ref($self->{proxy}) ne 'ARRAY' or
283 0 ▶ not @{$self->{proxy}};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"proxy"} ne "ARRAY" or not @{$self->{"proxy"};} 0 0 0
Condition0%0/3
ref $self->{"proxy"} ne "ARRAY" or not @{$self->{"proxy"};}
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self->{"proxy"} ne "ARRAY" or not @{$self->{"proxy"};}
ref $self->{"proxy"} ne "ARRAY" not @{$self->{"proxy"};}
Truth table0%0/3
A or not(B)
A= ref $self->{"proxy"} ne "ARRAY"B= not @{$self->{"proxy"};}
ABresult
1X1
011
000
284 0 ($host, $port) = @{$self->{proxy}};
285 } else {
286 0 $host = $self-> {proxy};
287 }
288 1 ▶ $port ||= $req-> uri-> port;
Condition33%1/3
$port ||= $req->uri->port
l!l&&r!l&&!r
100
MC/DC0%0/2
$port ||= $req->uri->port
$port $req->uri->port
Truth table33%1/3
A ||= B
A= $portB= $req->uri->port
ABresult
1X1
011
000
289 } else {
290 4 my $scheme = $req-> uri-> scheme;
291 4 ( $host, $port) = ( $req-> uri-> host, $req-> uri-> port);
292 4 my %proxy;
293 4 for my $id ($scheme, qw(all no)) {
294 12 ▶ if ( exists $ENV{"${id}_proxy"}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $ENV{"${id}_proxy"}) { } 0 12 12
elsif (exists $ENV{uc "${id}_proxy"}) { } 0 12 12
295 0 $proxy{$id} = $ENV{"${id}_proxy"};
296 } elsif ( exists $ENV{uc "${id}_proxy"}) {
297 0 $proxy{$id} = $ENV{uc "${id}_proxy"};
298 }
299 }
300 4 ▶ my $proxy = $proxy{all} // $proxy{$scheme};
Condition33%1/3
$proxy{"all"} // $proxy{$scheme}
l!l&&r!l&&!r
004
MC/DC0%0/2
$proxy{"all"} // $proxy{$scheme}
$proxy{"all"} $proxy{$scheme}
Truth table33%1/3
A // B
A= $proxy{"all"}B= $proxy{$scheme}
ABresult
1X1
011
000
301 4 ▶ $proxy{no} //= '';
Condition50%1/2
$proxy{"no"} //= ""
l!l
04
MC/DC0%0/1
$proxy{"no"} //= ""
$proxy{"no"}
Truth table50%1/2
A
A= $proxy{"no"}
Aresult
11
00
302 ($host, $port) = ( $proxy =~ /^(.+?)\:(\d+)$/) ? ($1, $2) : ($proxy, 80)
303 4 ▶ if defined($proxy) and $proxy{no} ne '*' and $proxy{no} !~ /\b\Q$host\E\b/;
Branch: 2 branches
BranchTrueFalseTotal
if defined $proxy and $proxy{"no"} ne "*" and not $proxy{"no"} =~ /\b\Q$host\E\b/ 0 4 4
$proxy =~ /^(.+?)\:(\d+)$/ ? : 0 0 0
Condition33%2/6
defined $proxy and $proxy{"no"} ne "*" and not $proxy{"no"} =~ /\b\Q$host\E\b/
!ll&&!rl&&r
400
defined $proxy and $proxy{"no"} ne "*"
!ll&&!rl&&r
400
MC/DC0%0/3
defined $proxy and $proxy{"no"} ne "*" and not $proxy{"no"} =~ /\b\Q$host\E\b/
defined $proxy $proxy{"no"} ne "*" not $proxy{"no"} =~ /\b\Q$host\E\b/
Truth table25%1/4
A and B and not(C)
A= defined $proxyB= $proxy{"no"} ne "*"C= not $proxy{"no"} =~ /\b\Q$host\E\b/
ABCresult
0XX0
10X0
1100
1111
304 }
305 5 return ( $host, $port);
306
307 }
308
309 # Connect to the remote, wait for protocol to finish, and
310 # close the connection if needed. Returns HTTP::Response object on success
311 sub handle_connection
312 {
313 5 ▶ my ( $self, $req) = @_;
Subroutine handle_connection: called CC=2 SCAR=13.3
Pod undocumented
314
315 5 my ( $host, $port) = $self->parse_proxy($req);
316
317 # have a chance to load eventual modules early
318 5 my $err = $self-> prepare_transport( $req);
319 5 ▶ return lambda { $err } if defined $err;
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 0 5 5
320
321 lambda {
322 # resolve hostname
323 5 ▶ if (
Subroutine __ANON__: called CC=16 SCAR=50.1
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"async_dns"} and not $host =~ /^(\d{1,3}\.){3}(\d{1,3})$/) 0 5 5
Condition33%1/3
$self->{"async_dns"} and not $host =~ /^(\d{1,3}\.){3}(\d{1,3})$/
!ll&&!rl&&r
500
MC/DC0%0/2
$self->{"async_dns"} and not $host =~ /^(\d{1,3}\.){3}(\d{1,3})$/
$self->{"async_dns"} not $host =~ /^(\d{1,3}\.){3}(\d{1,3})$/
Truth table33%1/3
A and not(B)
A= $self->{"async_dns"}B= not $host =~ /^(\d{1,3}\.){3}(\d{1,3})$/
ABresult
0X0
100
111
324 $self-> {async_dns} and
325 $host !~ /^(\d{1,3}\.){3}(\d{1,3})$/
326 ) {
327 context $host,
328 0 ▶ timeout => ($self-> {deadline} || $IO::Lambda::DNS::TIMEOUT);
Condition0%0/3
$self->{'deadline'} || $IO::Lambda::DNS::TIMEOUT
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'deadline'} || $IO::Lambda::DNS::TIMEOUT
$self->{'deadline'} $IO::Lambda::DNS::TIMEOUT
Truth table0%0/3
A || B
A= $self->{'deadline'}B= $IO::Lambda::DNS::TIMEOUT
ABresult
1X1
011
000
329 0 ▶ warn "resolving $host\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::HTTP::Client::DEBUG 0 0 0
330 return IO::Lambda::DNS::dns( sub {
331 0 ▶ $host = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
332 0 ▶ return $host unless $host =~ /^\d/; # error
Branch: 1 branch
BranchTrueFalseTotal
unless $host =~ /^\d/ 0 0 0
333 0 ▶ warn "resolved to $host\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::HTTP::Client::DEBUG 0 0 0
334 0 return this-> start; # restart the lambda with different $host
335 0 });
336 }
337
338 5 delete $self-> {close_connection};
339 5 ▶ $self-> {close_connection}++
Branch: 1 branch
BranchTrueFalseTotal
if ($req->header("Connection") || "") =~ /^close/i 0 5 5
Condition50%1/2
$req->header('Connection') || ''
l!l
05
MC/DC0%0/1
$req->header('Connection') || ''
$req->header('Connection')
Truth table50%1/2
A
A= $req->header('Connection')
Aresult
11
00
340 if ( $req-> header('Connection') || '') =~ /^close/i;
341
342 # got cached socket?
343 5 my ( $sock, $cached);
344 5 my $cc = $self-> {conn_cache};
345
346 5 ▶ if ( $cc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cc) 0 5 5
347 0 $sock = $cc-> withdraw( __PACKAGE__, "$host:$port");
348 0 ▶ if ( $sock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sock) 0 0 0
349 0 my $err = unpack('i', getsockopt( $sock, SOL_SOCKET, SO_ERROR));
350 0 ▶ $err ? undef $sock : $cached++;
Branch: 1 branch
BranchTrueFalseTotal
$err ? : 0 0 0
351 0 ▶ warn "reused socket is ".($err ? "bad" : "ok")."\n" if $DEBUG;
Branch: 2 branches
BranchTrueFalseTotal
if $IO::Lambda::HTTP::Client::DEBUG 0 0 0
$err ? : 0 0 0
352 }
353 }
354
355 # connect
356 5 my $err;
357 5 ▶ warn "connecting\n" if $DEBUG and not($sock);
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::HTTP::Client::DEBUG and not $sock 0 5 5
Condition33%1/3
$IO::Lambda::HTTP::Client::DEBUG and not $sock
!ll&&!rl&&r
500
MC/DC0%0/2
$IO::Lambda::HTTP::Client::DEBUG and not $sock
$IO::Lambda::HTTP::Client::DEBUG not $sock
Truth table33%1/3
A and not(B)
A= $IO::Lambda::HTTP::Client::DEBUGB= not $sock
ABresult
0X0
100
111
358 5 ▶ ( $sock, $err) = $self-> socket( $host, $port) unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 5 0 5
359 5 ▶ return $err unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 5 0 5
360 0 context( $sock, $self-> {deadline});
361
362 connect {
363 0 ▶ return shift if @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
364 # connected
365
366 0 $self-> {socket} = $sock;
367 0 $self-> {reader} = readbuf ( $self-> {reader});
368 0 ▶ $self-> {writer} = $self-> {writer}-> ($cached, $host, $port) if $self-> {writer};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"writer"} 0 0 0
369 0 $self-> {writer} = writebuf( $self-> {writer});
370
371 0 context $self-> handle_request( $req);
372 autocatch tail {
373 0 ▶ my $response = shift;
Subroutine __ANON__: not called CC=8 SCAR=42.8
374
375 # put back the connection, if possible
376 0 ▶ if ( $cc and not $self-> {close_connection}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cc and not $self->{"close_connection"}) 0 0 0
Condition0%0/3
$cc and not $self->{"close_connection"}
!ll&&!rl&&r
000
MC/DC0%0/2
$cc and not $self->{"close_connection"}
$cc not $self->{"close_connection"}
Truth table0%0/3
A and not(B)
A= $ccB= not $self->{"close_connection"}
ABresult
0X0
100
111
377 0 my $err = unpack('i', getsockopt( $sock, SOL_SOCKET, SO_ERROR));
378 0 ▶ warn "deposited socket back\n" if $DEBUG and not($err);
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::HTTP::Client::DEBUG and not $err 0 0 0
Condition0%0/3
$IO::Lambda::HTTP::Client::DEBUG and not $err
!ll&&!rl&&r
000
MC/DC0%0/2
$IO::Lambda::HTTP::Client::DEBUG and not $err
$IO::Lambda::HTTP::Client::DEBUG not $err
Truth table0%0/3
A and not(B)
A= $IO::Lambda::HTTP::Client::DEBUGB= not $err
ABresult
0X0
100
111
379 0 ▶ $cc-> deposit( __PACKAGE__, "$host:$port", $sock)
Branch: 1 branch
BranchTrueFalseTotal
unless $err 0 0 0
380 unless $err;
381 }
382 0 ▶ warn "connection:close\n" if $DEBUG and $self-> {close_connection};
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::HTTP::Client::DEBUG and $self->{"close_connection"} 0 0 0
Condition0%0/3
$IO::Lambda::HTTP::Client::DEBUG and $self->{"close_connection"}
!ll&&!rl&&r
000
MC/DC0%0/2
$IO::Lambda::HTTP::Client::DEBUG and $self->{"close_connection"}
$IO::Lambda::HTTP::Client::DEBUG $self->{"close_connection"}
Truth table0%0/3
A and B
A= $IO::Lambda::HTTP::Client::DEBUGB= $self->{"close_connection"}
ABresult
0X0
100
111
383 0 delete @{$self}{qw(close_connection socket buf writer reader)};
384 0 return $response;
385 0 }}}
386 5 }
387
388 # Execute single http request over an established connection.
389 # Returns either a HTTP::Response object, or an error string
390 sub handle_request
391 {
392 0 ▶ my ( $self, $req) = @_;
Subroutine handle_request: not called CC=1 SCAR=6.9
Pod undocumented
393
394 lambda {
395 0 ▶ $self-> {buf} = '';
Subroutine __ANON__: not called CC=3 SCAR=24.8
396
397 0 context $self-> handle_request_in_buffer( $req);
398
399 0 ▶ if ( $DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IO::Lambda::HTTP::Client::DEBUG) 0 0 0
400 0 warn "request sent\n";
401 0 ▶ warn $req-> as_string . "\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::HTTP::Client::DEBUG > 1 0 0 0
402 }
403 tail {
404 0 ▶ my ( undef, $error) = @_; # readbuf style
Subroutine __ANON__: not called CC=5 SCAR=34.0
405 0 ▶ if ( $DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IO::Lambda::HTTP::Client::DEBUG) 0 0 0
406 0 warn "got response\n";
407 0 ▶ warn (( $error ? $error : $self-> {buf}) . "\n") if $DEBUG > 1;
Branch: 2 branches
BranchTrueFalseTotal
if $IO::Lambda::HTTP::Client::DEBUG > 1 0 0 0
$error ? : 0 0 0
408 }
409 0 ▶ return defined($error) ? $error : $self-> parse( \ $self-> {buf} );
Branch: 1 branch
BranchTrueFalseTotal
defined $error ? : 0 0 0
410 0 }}
411 0 }
412
413 # Execute single http request over an established connection.
414 # Returns 2 parameters, readbuf-style, where actually only the 2nd matters,
415 # and signals error if defined. 2 parameters are there for readbuf() compatibility,
416 # so that the protocol handler can easily fall back to readbuf() itself.
417 sub handle_request_in_buffer
418 {
419 0 ▶ my ( $self, $req) = @_;
Subroutine handle_request_in_buffer: not called CC=6 SCAR=37.4
Pod undocumented
420
421 0 my $method = $req-> method;
422
423 # fixup path - otherwise LWP generates request as GET http://hostname/uri HTTP/1.1
424 # which not all servers understand
425 0 my ($req_line, $save_uri);
426 0 ▶ if (!$self-> {proxy} && ( $req-> protocol || '') =~ /http\/1.\d/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"proxy"} and ($req->protocol || "") =~ m[http/1.\d]i) 0 0 0
Condition0%0/5
not $self->{"proxy"} and ($req->protocol || "") =~ m[http/1.\d]i
!ll&&!rl&&r
000
$req->protocol || ''
l!l
00
MC/DC0%0/2
not $self->{"proxy"} and ($req->protocol || "") =~ m[http/1.\d]i
not $self->{"proxy"} $req->protocol
Truth table0%0/3
not(A) and B
A= not $self->{"proxy"}B= $req->protocol
ABresult
0X0
100
111
427 0 $save_uri = $req-> uri;
428
429 0 my $fullpath = $save_uri-> path_query;
430 0 ▶ $fullpath = "/$fullpath" unless $fullpath =~ m[^/];
Branch: 1 branch
BranchTrueFalseTotal
unless $fullpath =~ m[^/] 0 0 0
431 0 $req-> uri( $fullpath);
432 }
433 0 $req_line = $req-> as_string("\x0d\x0a");
434 0 ▶ $req-> uri($save_uri) if defined $save_uri;
Branch: 1 branch
BranchTrueFalseTotal
if defined $save_uri 0 0 0
435
436 lambda {
437 # send request
438 context
439 $self-> {writer},
440 $self-> {socket}, \ $req_line,
441 0 ▶ undef, 0, $self-> {deadline};
Subroutine __ANON__: not called CC=1 SCAR=6.9
442 state write => tail {
443 0 ▶ my ( $bytes_written, $error) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
444 0 ▶ return undef, $error if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
445
446 0 context $self-> {socket}, $self-> {deadline};
447 readable {
448 # request sent, now wait for data
449 0 ▶ return undef, 'timeout' unless shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless shift() 0 0 0
450
451 # read first line
452 0 context $self-> http_read(qr/^.*?\n/);
453 state head => tail {
454 0 ▶ my $line = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
455 0 ▶ unless ( defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 0 0 0
456 0 my $error = shift;
457 # remote closed connection and content is single-line HTTP/1.0
458 0 ▶ return (undef, $error) if $error ne 'eof';
Branch: 1 branch
BranchTrueFalseTotal
if $error ne "eof" 0 0 0
459 0 return (undef, undef);
460 }
461
462 # no headers?
463 0 ▶ return $self-> http_tail
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ m[^HTTP/[\.\d]+\s+\d{3}\s+]i 0 0 0
464 unless $line =~ /^HTTP\/[\.\d]+\s+\d{3}\s+/i;
465
466 # got some headers
467 0 context $self-> http_read( qr/^.*?\r?\n\r?\n/s);
468 state body => tail {
469 0 ▶ $line = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
470 0 ▶ return undef, shift unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 0 0
471
472 0 my $headers = HTTP::Response-> parse( $line);
473
474 # Connection: close
475 0 ▶ my $c = lc( $headers-> header('Connection') || '');
Condition0%0/2
$headers->header('Connection') || ''
l!l
00
MC/DC0%0/1
$headers->header('Connection') || ''
$headers->header('Connection')
Truth table0%0/2
A
A= $headers->header('Connection')
Aresult
11
00
476 0 $self-> {close_connection} = $c =~ /^close\s*$/i;
477
478 0 ▶ return 1 if $method eq 'HEAD';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "HEAD" 0 0 0
479
480 0 return $self-> http_read_body( length $line, $headers);
481 0 }}}}}
482 0 }
483
484 # have headers, read body
485 sub http_read_body
486 {
487 0 ▶ my ( $self, $offset, $headers) = @_;
Subroutine http_read_body: not called CC=8 SCAR=42.8
Pod undocumented
488
489 # have Content-Length? read that many bytes then
490 0 my $l = $headers-> header('Content-Length');
491 0 ▶ return $self-> http_tail( $1 + $offset )
Branch: 1 branch
BranchTrueFalseTotal
if defined $l and $l =~ /^(\d+)\s*$/ 0 0 0
Condition0%0/3
defined $l and $l =~ /^(\d+)\s*$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $l and $l =~ /^(\d+)\s*$/
defined $l $l =~ /^(\d+)\s*$/
Truth table0%0/3
A and B
A= defined $lB= $l =~ /^(\d+)\s*$/
ABresult
0X0
100
111
492 if defined ($l) and $l =~ /^(\d+)\s*$/;
493
494 # have 'Transfer-Encoding: chunked' ? read the chunks
495 0 ▶ my $te = lc( $headers-> header('Transfer-Encoding') || '');
Condition0%0/2
$headers->header('Transfer-Encoding') || ''
l!l
00
MC/DC0%0/1
$headers->header('Transfer-Encoding') || ''
$headers->header('Transfer-Encoding')
Truth table0%0/2
A
A= $headers->header('Transfer-Encoding')
Aresult
11
00
496 return $self-> http_read_chunked($offset)
497 0 ▶ if $self-> {chunked} = $te =~ /^chunked\s*$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"chunked"} = $te =~ /^chunked\s*$/i 0 0 0
498
499 # just read as much as possible then -- however with considerations;
500 # we can't do that if server keeps connection open, otherwise we'll hang
501
502 # http/1.0 and less doesn't implement open connections
503 0 ▶ return $self-> http_tail if
Branch: 1 branch
BranchTrueFalseTotal
if $headers->protocol =~ m[^HTTP/(\d+\.\d+)] and $1 < "1.1" 0 0 0
Condition0%0/3
$headers->protocol =~ m[^HTTP/(\d+\.\d+)] and $1 < "1.1"
!ll&&!rl&&r
000
MC/DC0%0/2
$headers->protocol =~ m[^HTTP/(\d+\.\d+)] and $1 < "1.1"
$headers->protocol =~ m[^HTTP/(\d+\.\d+)] $1 < "1.1"
Truth table0%0/3
A and B
A= $headers->protocol =~ m[^HTTP/(\d+\.\d+)]B= $1 < "1.1"
ABresult
0X0
100
111
504 $headers-> protocol =~ /^HTTP\/(\d+\.\d+)/ and
505 $1 < 1.1;
506
507 # server wants to close the connection
508 return $self-> http_tail if
509 0 ▶ $self-> {close_connection};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"close_connection"} 0 0 0
510 }
511
512 # read sequence of TE chunks
513 sub http_read_chunked
514 {
515 0 ▶ my ( $self, $offset) = @_;
Subroutine http_read_chunked: not called CC=1 SCAR=6.9
Pod undocumented
516
517 # read chunk size
518 0 pos( $self-> {buf} ) = $offset;
519 0 context $self-> http_read( qr/\G[^\r\n]+\r?\n/i);
520 state size => tail {
521 0 ▶ my $line = shift;
Subroutine __ANON__: not called CC=5 SCAR=34.0
522 0 ▶ return undef, shift unless defined $line; # got error
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 0 0
523
524 # advance
525 0 substr( $self-> {buf}, $offset, length($line), '');
526 0 pos( $self-> {buf} ) = $offset;
527 0 $line =~ s/\r?\n//;
528 0 ▶ return undef, "protocol error: chunk size error"
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^[\da-f]+$/ 0 0 0
529 unless $line =~ /^[\da-f]+$/;
530 0 my $size = hex $line;
531 0 ▶ warn "reading chunk $size bytes\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::HTTP::Client::DEBUG 0 0 0
532 0 ▶ return 1 unless $size;
Branch: 1 branch
BranchTrueFalseTotal
unless $size 0 0 0
533 0 $size += 2; # CRLF
534
535 0 my $frame = restartable;
536
537 # read the chunk itself
538 0 context $self-> http_read( $offset + $size);
539 state chunk => tail {
540 0 ▶ return undef, shift unless shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
unless shift() 0 0 0
541
542 0 $offset += $size - 2;
543 0 substr( $self->{buf}, $offset, 2, '' ); # remove CRLF
544 0 pos( $self-> {buf} ) = $offset;
545 0 ▶ warn "chunk $size bytes ok\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::HTTP::Client::DEBUG 0 0 0
546
547 0 again($frame);
548 0 }};
549 }
550
551
552 sub parse
553 {
554 0 ▶ my ( $self, $buf_ptr) = @_;
Subroutine parse: not called CC=2 SCAR=17.9
Pod undocumented
555 0 ▶ return HTTP::Response-> parse( $$buf_ptr) if $$buf_ptr =~ /^(HTTP\S+)\s+(\d{3})\s+/i;
Branch: 1 branch
BranchTrueFalseTotal
if $$buf_ptr =~ /^(HTTP\S+)\s+(\d{3})\s+/i 0 0 0
556 0 return HTTP::Response-> new( '000', '', undef, $$buf_ptr);
557 }
558
559 2 1;
560
561 __DATA__
« Lambda/Loop/AnyEvent.pm ↑ Summary Lambda/HTTP/Server.pm »