Furl/HTTP.pm

filter: Statementstmt 68.4% 353 / 516 Branchbran 54.9% 177 / 322 Conditioncond 52.3% 55 / 105 MC/DCmcdc 33.9% 19 / 56 Subroutinesub 73.2% 41 / 56 Podpod 50.0% 9 / 18 totaltotal 60.9% 654 / 1073 CC 203 SCAR 54.9
CC
203
Cov
62%
request
577.4
connect_ssl_over_proxy
90.0
_ssl_opts
42.0
CRAP 242.0
↑ Summary Furl/Response.pm »
1 package Furl::HTTP;
2 48 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 48 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 48 ▶use base qw/Exporter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 48 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 48 our $VERSION = '3.16';
8  
9 48 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 48 ▶use Furl::ConnectionCache;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 48 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 48 ▶use Errno qw(EAGAIN ECONNRESET EINPROGRESS EINTR EWOULDBLOCK ECONNABORTED EISCONN);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 48 ▶use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK SEEK_SET SEEK_END);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 48 ▶use Socket qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
16 PF_INET SOCK_STREAM
17 IPPROTO_TCP
18 TCP_NODELAY
19 pack_sockaddr_in
20 48 );
21 48 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 48 ▶use constant WIN32 => $^O eq 'MSWin32';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 48 ▶use HTTP::Parser::XS qw/HEADERS_NONE HEADERS_AS_ARRAYREF HEADERS_AS_HASHREF/;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 48 our @EXPORT_OK = qw/HEADERS_NONE HEADERS_AS_ARRAYREF HEADERS_AS_HASHREF/;
27  
28  
29 # ref. RFC 2616, 3.5 Content Codings:
30 # For compatibility with previous implementations of HTTP,
31 # applications SHOULD consider "x-gzip" and "x-compress" to be
32 # equivalent to "gzip" and "compress" respectively.
33 # ("compress" is not supported, though)
34 48 my %COMPRESSED = map { $_ => undef } qw(gzip x-gzip deflate);
35  
36 48 my $HTTP_TOKEN = '[^\x00-\x31\x7F]+';
37 48 my $HTTP_QUOTED_STRING = q{"([^"]+|\\.)*"};
38  
39 # Default for max_decompressed_size when the body is held in memory.
40 48 our $DEFAULT_MAX_DECOMPRESSED_SIZE = 100 * 1024 * 1024;
41  
42 36 ▶sub new {
Subroutine new: called CC=7 SCAR=25.7
Pod documented
43 36 my $class = shift;
44 0 ▶ my %args = @_ == 1 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 36 36
45  
46 my @headers = (
47 36 ▶ 'User-Agent' => (delete($args{agent}) || __PACKAGE__ . '/' . $Furl::HTTP::VERSION),
Condition100%2/2
delete $args{'agent'} || 'Furl::HTTP/' . $Furl::HTTP::VERSION
l!l
135
MC/DC100%1/1
delete $args{'agent'} || 'Furl::HTTP/' . $Furl::HTTP::VERSION
delete $args{'agent'}
Truth table100%2/2
A
A= delete $args{'agent'}
Aresult
11
00
48 );
49 36 my $connection_header = 'keep-alive';
50 36 ▶ if(defined $args{headers}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"headers"}) 0 36 36
51 0 my $in_headers = delete $args{headers};
52 0 ▶ for (my $i = 0; $i < @$in_headers; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$in_headers) 0 0 0
53 0 my $name = $in_headers->[$i];
54 0 ▶ if (lc($name) eq 'connection') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $name eq 'connection') { } 0 0 0
55 0 $connection_header = $in_headers->[$i + 1];
56 } else {
57 0 push @headers, $name, $in_headers->[$i + 1];
58 }
59 }
60 }
61 bless {
62 timeout => 10,
63 max_redirects => 7,
64 bufsize => 10*1024, # no mmap
65 headers => \@headers,
66 connection_header => $connection_header,
67 proxy => '',
68 no_proxy => '',
69 connection_pool => Furl::ConnectionCache->new(),
70 header_format => HEADERS_AS_ARRAYREF,
71 0 ▶ stop_if => sub {},
Subroutine __ANON__: not called CC=1 SCAR=0.0
72 48 ▶ inet_aton => sub { Socket::inet_aton($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
73 ssl_opts => {},
74 36 ▶ capture_request => $args{capture_request} || 0,
Condition100%2/2
$args{'capture_request'} || 0
l!l
234
MC/DC100%1/1
$args{'capture_request'} || 0
$args{'capture_request'}
Truth table100%2/2
A
A= $args{'capture_request'}
Aresult
11
00
75 inactivity_timeout => 600,
76 %args
77 }, $class;
78 }
79  
80 0 ▶sub get {
Subroutine get: not called CC=1 SCAR=6.9
Pod documented
81 0 my ( $self, $url, $headers ) = @_;
82 0 $self->request(
83 method => 'GET',
84 url => $url,
85 headers => $headers
86 );
87 }
88  
89 0 ▶sub head {
Subroutine head: not called CC=1 SCAR=6.9
Pod documented
90 0 my ( $self, $url, $headers ) = @_;
91 0 $self->request(
92 method => 'HEAD',
93 url => $url,
94 headers => $headers
95 );
96 }
97  
98 0 ▶sub post {
Subroutine post: not called CC=1 SCAR=6.9
Pod documented
99 0 my ( $self, $url, $headers, $content ) = @_;
100 0 $self->request(
101 method => 'POST',
102 url => $url,
103 headers => $headers,
104 content => $content
105 );
106 }
107  
108 0 ▶sub put {
Subroutine put: not called CC=1 SCAR=6.9
Pod documented
109 0 my ( $self, $url, $headers, $content ) = @_;
110 0 $self->request(
111 method => 'PUT',
112 url => $url,
113 headers => $headers,
114 content => $content
115 );
116 }
117  
118 0 ▶sub delete {
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
119 0 my ( $self, $url, $headers, $content ) = @_;
120 0 $self->request(
121 method => 'DELETE',
122 url => $url,
123 headers => $headers,
124 content => $content
125 );
126 }
127  
128 5 ▶sub agent {
Subroutine agent: called CC=2 SCAR=6.9
Pod documented
129 5 ▶ if ( @_ == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 3 2 5
130 3 _header_set(shift->{headers}, 'User-Agent', shift);
131 } else {
132 2 return _header_get(shift->{headers}, 'User-Agent');
133 }
134 }
135  
136 3 ▶sub _header_set {
Subroutine _header_set: called CC=3 SCAR=11.8
137 3 my ($headers, $key, $value) = (shift, lc shift, shift);
138 3 ▶ for (my $i=0; $i<@$headers; $i+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$headers) 3 0 3
139 3 ▶ if (lc($headers->[$i]) eq $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $headers->[$i] eq $key) 3 0 3
140 3 $headers->[$i+1] = $value;
141 3 return;
142 }
143 }
144 0 push @$headers, $key, $value;
145 }
146  
147 23 ▶sub _header_get {
Subroutine _header_get: called CC=3 SCAR=11.0
148 23 my ($headers, $key) = (shift, lc shift);
149 23 ▶ for (my $i=0; $i<@$headers; $i+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$headers) 29 3 32
150 29 ▶ return $headers->[$i+1] if lc($headers->[$i]) eq $key;
Branch: 1 branch
BranchTrueFalseTotal
if lc $headers->[$i] eq $key 20 9 29
151 }
152 3 return undef;
153 }
154  
155 23 ▶sub _requires {
Subroutine _requires: called CC=5 SCAR=25.2
156 23 my($file, $feature, $library) = @_;
157 23 ▶ return if exists $INC{$file};
Branch: 1 branch
BranchTrueFalseTotal
if exists $INC{$file} 21 2 23
158 2 ▶ unless(eval { require $file }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require $file } }) 0 2 2
159 0 ▶ if ($@ =~ /^Can't locate/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /^Can't locate/) { } 0 0 0
160 0 ▶ $library ||= do {
Condition0%0/3
$library ||= do { local $_ = $file; s/ \.pm \z//msx; s[/][::]g; $_ }
l!l&&r!l&&!r
000
MC/DC0%0/2
$library ||= do { local $_ = $file; s/ \.pm \z//msx; s[/][::]g; $_ }
$library do { local $_ = $file; s/ \.pm \z//msx; s[/][::]g; $_ }
Truth table0%0/3
A ||= B
A= $libraryB= do { local $_ = $file; s/ \.pm \z//msx; s[/][::]g; $_ }
ABresult
1X1
011
000
161 0 local $_ = $file;
162 0 s/ \.pm \z//xms;
163 0 s{/}{::}g;
164 0 $_;
165 };
166 0 Carp::croak(
167 "$feature requires $library, but it is not available."
168 . " Please install $library using your prefer CPAN client"
169 );
170 } else {
171 0 die $@;
172 }
173 }
174 }
175  
176 # returns $scheme, $host, $port, $path_query
177 46 ▶sub _parse_url {
Subroutine _parse_url: called CC=2 SCAR=6.9
178 46 my($self, $url) = @_;
179 46 ▶ $url =~ m{\A
Branch: 1 branch
BranchTrueFalseTotal
$url =~ m[\A ([a-z]+) # scheme :// (?: ([^/:\@?]+) # user : ([^/:\@?]+) # password @ )? ([^/:?]+) # host (?: : (\d+) )? # port (?: ( /? \? .* | / .*) )? # path_query \z]msx or Carp::croak("Passed malformed URL: $url") 45 1 46
180 ([a-z]+) # scheme
181 ://
182 (?:
183 ([^/:@?]+) # user
184 :
185 ([^/:@?]+) # password
186 @
187 )?
188 ([^/:?]+) # host
189 (?: : (\d+) )? # port
190 (?: ( /? \? .* | / .*) )? # path_query
191 \z}xms or Carp::croak("Passed malformed URL: $url");
192 45 return( $1, $2, $3, $4, $5, $6 );
193 }
194  
195 1 ▶sub make_x_www_form_urlencoded {
Subroutine make_x_www_form_urlencoded: called CC=5 SCAR=16.6
Pod undocumented
196 1 my($self, $content) = @_;
197 1 my @params;
198 0 ▶ my @p = ref($content) eq 'HASH' ? %{$content}
Branch: 1 branch
BranchTrueFalseTotal
ref $content eq 'ARRAY' ? : 1 0 1
199 1 ▶ : ref($content) eq 'ARRAY' ? @{$content}
Branch: 1 branch
BranchTrueFalseTotal
ref $content eq 'HASH' ? : 0 1 1
200 : Carp::croak("Cannot coerce $content to x-www-form-urlencoded");
201 1 ▶ while ( my ( $k, $v ) = splice @p, 0, 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@p, 0, 2)) 1 1 2
202 1 foreach my $s($k, $v) {
203 2 utf8::downgrade($s); # will die in wide characters
204 # escape unsafe chars (defined by RFC 3986)
205 0 $s =~ s/ ([^A-Za-z0-9\-\._~]) / sprintf '%%%02X', ord $1 /xmsge;
206 }
207 1 push @params, "$k=$v";
208 }
209 1 return join( "&", @params );
210 }
211  
212 8 ▶sub env_proxy {
Subroutine env_proxy: called CC=5 SCAR=16.2
Pod documented
213 8 my $self = shift;
214 # Under CGI, bypass HTTP_PROXY as request sets it from Proxy header
215 # Note: This doesn't work on windows correctly.
216 8 ▶ local $ENV{HTTP_PROXY} if $ENV{REQUEST_METHOD};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"REQUEST_METHOD"} 4 4 8
217 8 ▶ $self->{proxy} = $ENV{http_proxy} || $ENV{HTTP_PROXY} || $self->{proxy};
Condition83%5/6
$ENV{'http_proxy'} || $ENV{'HTTP_PROXY'} || $self->{'proxy'}
l!l&&r!l&&!r
503
$ENV{"http_proxy"} or $ENV{"HTTP_PROXY"}
l!l&&r!l&&!r
413
MC/DC66%2/3
$ENV{'http_proxy'} || $ENV{'HTTP_PROXY'} || $self->{'proxy'}
$ENV{"http_proxy"} $ENV{"HTTP_PROXY"} $self->{'proxy'}
Truth table75%3/4
A or B || C
A= $ENV{"http_proxy"}B= $ENV{"HTTP_PROXY"}C= $self->{'proxy'}
ABCresult
1XX1
01X1
0011
0000
218 8 ▶ $self->{no_proxy} = $ENV{NO_PROXY} || '';
Condition50%1/2
$ENV{'NO_PROXY'} || ''
l!l
08
MC/DC0%0/1
$ENV{'NO_PROXY'} || ''
$ENV{'NO_PROXY'}
Truth table50%1/2
A
A= $ENV{'NO_PROXY'}
Aresult
11
00
219 8 $self;
220 }
221  
222 79 ▶sub request {
Subroutine request: called CC=104 SCAR=63.6
Pod documented
223 79 my $self = shift;
224 79 my %args = @_;
225  
226 79 my $timeout_at = time + $self->{timeout};
227  
228 79 my ($scheme, $username, $password, $host, $port, $path_query);
229 79 ▶ if (defined(my $url = $args{url})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $url = $args{'url'})) { } 34 45 79
230 34 ($scheme, $username, $password, $host, $port, $path_query) = $self->_parse_url($url);
231 }
232 else {
233 45 ($scheme, $host, $port, $path_query) = @args{qw/scheme host port path_query/};
234 45 ▶ if (not defined $host) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $host) 0 45 45
235 0 Carp::croak("Missing host name in arguments");
236 }
237 }
238  
239 79 ▶ if (not defined $scheme) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $scheme) { } 45 34 79
elsif ($scheme ne 'http' and $scheme ne 'https') { } 6 56 62
240 45 $scheme = 'http';
241 ▶ } elsif($scheme ne 'http' && $scheme ne 'https') {
Condition66%2/3
$scheme ne "http" and $scheme ne "https"
!ll&&!rl&&r
2806
MC/DC50%1/2
$scheme ne "http" and $scheme ne "https"
$scheme ne "http" $scheme ne "https"
Truth table66%2/3
A and B
A= $scheme ne "http"B= $scheme ne "https"
ABresult
0X0
100
111
242 6 Carp::croak("Unsupported scheme: $scheme");
243 }
244  
245 73 ▶ my $default_port = $scheme eq 'http'
Branch: 1 branch
BranchTrueFalseTotal
$scheme eq 'http' ? : 73 0 73
246 ? 80
247 : 443;
248 73 ▶ if(not defined $port) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $port) 1 72 73
249 1 $port = $default_port;
250 }
251 73 ▶ if(not defined $path_query) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path_query) 14 59 73
252 14 $path_query = '/';
253 }
254  
255 73 ▶ unless (substr($path_query, 0, 1) eq '/') {
Branch: 1 branch
BranchTrueFalseTotal
unless (substr($path_query, 0, 1) eq "/") 3 70 73
256 3 $path_query = "/$path_query"; # Compensate for slash (?foo=bar => /?foo=bar)
257 }
258  
259 # Note. '_' is a invalid character for URI, but some servers using fucking underscore for domain name. Then, I accept the '_' character for domain name.
260 73 ▶ if ($host =~ /[^A-Za-z0-9._-]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /[^A-Za-z0-9._-]/) 0 73 73
261 0 _requires('Net/IDN/Encode.pm',
262 'Internationalized Domain Name (IDN)');
263 0 $host = Net::IDN::Encode::domain_to_ascii($host);
264 }
265  
266 73 my $proxy = $self->{proxy};
267 73 my $no_proxy = $self->{no_proxy};
268 73 ▶ if ($proxy && $no_proxy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proxy and $no_proxy) 0 73 73
Condition33%1/3
$proxy and $no_proxy
!ll&&!rl&&r
7300
MC/DC0%0/2
$proxy and $no_proxy
$proxy $no_proxy
Truth table33%1/3
A and B
A= $proxyB= $no_proxy
ABresult
0X0
100
111
269 0 ▶ if ($self->match_no_proxy($no_proxy, $host)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->match_no_proxy($no_proxy, $host)) 0 0 0
270 0 undef $proxy;
271 }
272 }
273  
274 73 local $SIG{PIPE} = 'IGNORE';
275 73 my $sock = $self->{connection_pool}->steal($host, $port);
276 73 my $in_keepalive;
277 73 ▶ if (defined $sock) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sock) 26 47 73
278 26 ▶ if ($self->_do_select(0, $sock, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_do_select(0, $sock, 0)) { } 2 24 26
279 2 close $sock;
280 2 undef $sock;
281 } else {
282 24 $in_keepalive = 1;
283 }
284 }
285 73 ▶ if(!$in_keepalive) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_keepalive) 49 24 73
286 49 my $err_reason;
287 49 ▶ if ($proxy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proxy) { } 0 49 49
288 0 my (undef, $proxy_user, $proxy_pass, $proxy_host, $proxy_port, undef)
289 = $self->_parse_url($proxy);
290 0 my $proxy_authorization;
291 0 ▶ if (defined $proxy_user) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proxy_user) 0 0 0
292 0 _requires('URI/Escape.pm',
293 'Basic auth');
294 0 my($unescape_proxy_user) = URI::Escape::uri_unescape($proxy_user);
295 0 my($unescape_proxy_pass) = URI::Escape::uri_unescape($proxy_pass);
296 0 _requires('MIME/Base64.pm',
297 'Basic auth');
298 0 $proxy_authorization = 'Basic ' . MIME::Base64::encode_base64("$unescape_proxy_user:$unescape_proxy_pass","");
299 }
300 0 ▶ if ($scheme eq 'http') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme eq 'http') { } 0 0 0
301 0 ($sock, $err_reason)
302 = $self->connect($proxy_host, $proxy_port, $timeout_at);
303 0 ▶ if (defined $proxy_authorization) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proxy_authorization) 0 0 0
304 0 $self->{proxy_authorization} = $proxy_authorization;
305 }
306 } else {
307 0 ($sock, $err_reason) = $self->connect_ssl_over_proxy(
308 $proxy_host, $proxy_port, $host, $port, $timeout_at, $proxy_authorization);
309 }
310 } else {
311 49 ▶ if ($scheme eq 'http') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme eq 'http') { } 49 0 49
312 49 ($sock, $err_reason)
313 = $self->connect($host, $port, $timeout_at);
314 } else {
315 0 ($sock, $err_reason)
316 = $self->connect_ssl($host, $port, $timeout_at);
317 }
318 }
319 49 ▶ return $self->_r500($err_reason)
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 1 48 49
320 unless $sock;
321 }
322  
323 # keep request dump
324 72 my ($req_headers, $req_content) = ("", "");
325  
326 # write request
327 72 ▶ my $method = $args{method} || 'GET';
Condition100%2/2
$args{'method'} || 'GET'
l!l
1260
MC/DC100%1/1
$args{'method'} || 'GET'
$args{'method'}
Truth table100%2/2
A
A= $args{'method'}
Aresult
11
00
328 72 my $connection_header = $self->{connection_header};
329 72 my $cookie_jar = $self->{cookie_jar};
330 {
331 72 my @headers = @{$self->{headers}};
332 72 ▶ $connection_header = 'close'
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "HEAD" 1 71 72
333 if $method eq 'HEAD';
334 72 ▶ if (my $in_headers = $args{headers}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $in_headers = $args{"headers"}) 14 58 72
335 14 ▶ for (my $i = 0; $i < @$in_headers; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$in_headers) 18 14 32
336 18 my $name = $in_headers->[$i];
337 18 ▶ if (lc($name) eq 'connection') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $name eq 'connection') { } 0 18 18
338 0 $connection_header = $in_headers->[$i + 1];
339 } else {
340 18 push @headers, $name, $in_headers->[$i + 1];
341 }
342 }
343 }
344 72 unshift @headers, 'Connection', $connection_header;
345 72 ▶ if (exists $self->{proxy_authorization}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"proxy_authorization"}) 0 72 72
346 0 push @headers, 'Proxy-Authorization', $self->{proxy_authorization};
347 }
348 72 ▶ if (defined $username) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $username) 6 66 72
349 6 _requires('URI/Escape.pm', 'Basic auth');
350 6 my($unescape_username) = URI::Escape::uri_unescape($username);
351 6 my($unescape_password) = URI::Escape::uri_unescape($password);
352 6 _requires('MIME/Base64.pm', 'Basic auth');
353 6 push @headers, 'Authorization', 'Basic ' . MIME::Base64::encode_base64("${unescape_username}:${unescape_password}","");
354 }
355  
356 # set Cookie header
357 72 ▶ if (defined $cookie_jar) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cookie_jar) 0 72 72
358 0 my $url;
359 0 ▶ if ($args{url}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'url'}) { } 0 0 0
360 0 $url = $args{url};
361 } else {
362 $url = join(
363 '',
364 $args{scheme},
365 '://',
366 $args{host},
367 (exists($args{port}) ? ":$args{port}" : ()),
368 0 ▶ exists($args{path_query}) ? $args{path_query} : '/',
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'port'} ? : 0 0 0
exists $args{'path_query'} ? : 0 0 0
369 );
370 }
371 0 push @headers, 'Cookie' => $cookie_jar->cookie_header($url);
372 }
373  
374 72 my $content = $args{content};
375 72 my $content_is_fh = 0;
376 72 ▶ if(defined $content) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content) 3 69 72
377 3 $content_is_fh = Scalar::Util::openhandle($content);
378 3 ▶ if(!$content_is_fh && ref $content) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $content_is_fh and ref $content) 0 3 3
Condition33%1/3
not $content_is_fh and ref $content
!ll&&!rl&&r
030
MC/DC0%0/2
not $content_is_fh and ref $content
not $content_is_fh ref $content
Truth table33%1/3
not(A) and B
A= not $content_is_fhB= ref $content
ABresult
0X0
100
111
379 0 $content = $self->make_x_www_form_urlencoded($content);
380 0 ▶ if(!defined _header_get(\@headers, 'Content-Type')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined _header_get(\@headers, "Content-Type")) 0 0 0
381 0 push @headers, 'Content-Type'
382 => 'application/x-www-form-urlencoded';
383 }
384 }
385 3 ▶ if(!defined _header_get(\@headers, 'Content-Length')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined _header_get(\@headers, "Content-Length")) 3 0 3
386 3 my $content_length;
387 3 ▶ if($content_is_fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_is_fh) { } 0 3 3
388 0 ▶ my $assert = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
389 0 ▶ $_[0] or Carp::croak(
Branch: 1 branch
BranchTrueFalseTotal
$_[0] or Carp::croak("Failed to $_[1] for Content-Length: $!") 0 0 0
390 "Failed to $_[1] for Content-Length: $!",
391 );
392 0 };
393 0 $assert->(defined(my $cur_pos = tell($content)), 'tell');
394 0 $assert->(seek($content, 0, SEEK_END), 'seek');
395 0 $assert->(defined(my $end_pos = tell($content)), 'tell');
396 0 $assert->(seek($content, $cur_pos, SEEK_SET), 'seek');
397  
398 0 $content_length = $end_pos - $cur_pos;
399 }
400 else {
401 3 $content_length = length($content);
402 }
403 3 push @headers, 'Content-Length' => $content_length;
404 }
405 }
406  
407 # finally, set Host header
408 72 ▶ my $request_target = ($port == $default_port) ? $host : "$host:$port";
Branch: 1 branch
BranchTrueFalseTotal
$port == $default_port ? : 1 71 72
409 72 push @headers, 'Host' => $request_target;
410  
411 72 ▶ my $request_uri = $proxy && $scheme eq 'http' ? "$scheme://$request_target$path_query" : $path_query;
Branch: 1 branch
BranchTrueFalseTotal
$proxy && $scheme eq 'http' ? : 0 72 72
Condition33%1/3
$proxy and $scheme eq "http"
!ll&&!rl&&r
7200
MC/DC0%0/2
$proxy and $scheme eq "http"
$proxy $scheme eq "http"
Truth table33%1/3
A and B
A= $proxyB= $scheme eq "http"
ABresult
0X0
100
111
412  
413 72 my $p = "$method $request_uri HTTP/1.1\015\012";
414 72 ▶ for (my $i = 0; $i < @headers; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @headers) 243 72 315
415 243 my $val = $headers[ $i + 1 ];
416 # the de facto standard way to handle [\015\012](by kazuho-san)
417 243 $val =~ tr/\015\012/ /;
418 243 $p .= "$headers[$i]: $val\015\012";
419 }
420 72 $p .= "\015\012";
421 72 ▶ $self->write_all($sock, $p, $timeout_at)
Branch: 1 branch
BranchTrueFalseTotal
$self->write_all($sock, $p, $timeout_at) or return $self->_r500('Failed to send HTTP request: ' . _strerror_or_timeout()) 72 0 72
422 or return $self->_r500(
423 "Failed to send HTTP request: " . _strerror_or_timeout());
424  
425 72 ▶ if ($self->{capture_request}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"capture_request"}) 1 71 72
426 1 $req_headers = $p;
427 }
428  
429 72 ▶ if (defined $content) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content) 3 69 72
430 3 ▶ if ($content_is_fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_is_fh) { } 0 3 3
431 0 my $ret;
432 my $buf;
433 0 SENDFILE: while (1) {
434 0 $ret = read($content, $buf, $self->{bufsize});
435 0 ▶ if (not defined $ret) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $ret) { } 0 0 0
elsif ($ret == 0) { } 0 0 0
436 0 Carp::croak("Failed to read request content: $!");
437 } elsif ($ret == 0) { # EOF
438 0 last SENDFILE;
439 }
440 0 ▶ $self->write_all($sock, $buf, $timeout_at)
Branch: 1 branch
BranchTrueFalseTotal
$self->write_all($sock, $buf, $timeout_at) or return $self->_r500('Failed to send content: ' . _strerror_or_timeout()) 0 0 0
441 or return $self->_r500(
442 "Failed to send content: " . _strerror_or_timeout()
443 );
444  
445 0 ▶ if ($self->{capture_request}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"capture_request"}) 0 0 0
446 0 $req_content .= $buf;
447 }
448 }
449 } else { # simple string
450 3 ▶ if (length($content) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $content > 0) 0 3 3
451 0 ▶ $self->write_all($sock, $content, $timeout_at)
Branch: 1 branch
BranchTrueFalseTotal
$self->write_all($sock, $content, $timeout_at) or return $self->_r500('Failed to send content: ' . _strerror_or_timeout()) 0 0 0
452 or return $self->_r500(
453 "Failed to send content: " . _strerror_or_timeout()
454 );
455  
456 0 ▶ if ($self->{capture_request}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"capture_request"}) 0 0 0
457 0 $req_content = $content;
458 }
459 }
460 }
461 }
462 }
463  
464 # read response
465 72 my $buf = '';
466 72 my $rest_header;
467 my $res_minor_version;
468 72 my $res_status;
469 72 my $res_msg;
470 72 my $res_headers;
471 72 ▶ my $special_headers = $args{special_headers} || +{};
Condition50%1/2
$args{'special_headers'} || {}
l!l
072
MC/DC0%0/1
$args{'special_headers'} || {}
$args{'special_headers'}
Truth table50%1/2
A
A= $args{'special_headers'}
Aresult
11
00
472 72 $special_headers->{'connection'} = '';
473 72 $special_headers->{'content-length'} = undef;
474 72 $special_headers->{'location'} = '';
475 72 $special_headers->{'content-encoding'} = '';
476 72 $special_headers->{'transfer-encoding'} = '';
477 72 LOOP: while (1) {
478 my $n = $self->read_timeout($sock,
479 224 \$buf, $self->{bufsize}, length($buf), $timeout_at);
480 224 ▶ if(!$n) { # error or eof
Branch: 1 branch
BranchTrueFalseTotal
if (not $n) { } 3 221 224
481 3 ▶ if ($in_keepalive && length($buf) == 0
Branch: 1 branch
BranchTrueFalseTotal
if ($in_keepalive and length $buf == 0 and defined $n || $! == 104 || !!0) 0 3 3
Condition36%4/11
$in_keepalive and length $buf == 0 and defined $n || $! == 104 || !!0
!ll&&!rl&&r
030
$in_keepalive and length $buf == 0
!ll&&!rl&&r
003
defined $n or $! == 104 or !!0
l!l
03
defined $n or $! == 104
l!l&&r!l&&!r
003
MC/DC0%0/4
$in_keepalive and length $buf == 0 and defined $n || $! == 104 || !!0
$in_keepalive length $buf == 0 defined $n $! == 104
Truth table20%1/5
A and B and C or D
A= $in_keepaliveB= length $buf == 0C= defined $nD= $! == 104
ABCDresult
0XXX0
10XX0
11000
111X1
11011
482 && (defined($n) || $!==ECONNRESET || (WIN32 && $! == ECONNABORTED))) {
483 # the server closes the connection (maybe because of keep-alive timeout)
484 0 return $self->request(%args);
485 }
486 3 ▶ return $self->_r500(
Branch: 1 branch
BranchTrueFalseTotal
!defined($n) ? : 3 0 3
487 !defined($n)
488 ? "Cannot read response header: " . _strerror_or_timeout()
489 : "Unexpected EOF while reading response header"
490 );
491 }
492 else {
493 221 my $ret;
494 ( $ret, $res_minor_version, $res_status, $res_msg, $res_headers )
495 = HTTP::Parser::XS::parse_http_response( $buf,
496 221 $self->{header_format}, $special_headers );
497 221 ▶ if ( $ret == -1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ret == -1) { } 0 221 221
elsif ($ret == -2) { } 147 74 221
498 0 return $self->_r500("Invalid HTTP response");
499 }
500 elsif ( $ret == -2 ) {
501 # partial response
502 147 next LOOP;
503 }
504 else {
505 # succeeded
506 74 $rest_header = substr( $buf, $ret );
507 74 ▶ if ((int $res_status / 100) eq 1) { # Continue
Branch: 1 branch
BranchTrueFalseTotal
if (int $res_status / 100 eq 1) 5 69 74
508 # The origin server must not wait for the request body
509 # before sending the 100 (Continue) response.
510 # see http://greenbytes.de/tech/webdav/rfc2616.html#status.100
511 5 $buf = $rest_header;
512 5 next LOOP;
513 }
514 69 last LOOP;
515 }
516 }
517 }
518  
519 69 my $max_redirects = 0;
520 69 my $do_redirect = undef;
521 69 ▶ if ($special_headers->{location}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($special_headers->{"location"}) 0 69 69
522 0 ▶ $max_redirects = defined($args{max_redirects}) ? $args{max_redirects} : $self->{max_redirects};
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'max_redirects'} ? : 0 0 0
523 0 ▶ $do_redirect = $max_redirects && $res_status =~ /^30[12378]$/;
Condition0%0/3
$max_redirects && $res_status =~ /^30[12378]$/
!ll&&!rl&&r
000
MC/DC0%0/2
$max_redirects && $res_status =~ /^30[12378]$/
$max_redirects $res_status =~ /^30[12378]$/
Truth table0%0/3
A && B
A= $max_redirectsB= $res_status =~ /^30[12378]$/
ABresult
0X0
100
111
524 }
525  
526 69 my $res_content = '';
527 69 ▶ unless ($do_redirect) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($do_redirect) 69 0 69
528 69 ▶ if (my $fh = $args{write_file}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $fh = $args{'write_file'}) { } 0 69 69
elsif (my $coderef = $args{'write_code'}) { } 3 66 69
529 0 $res_content = Furl::FileStream->new( $fh );
530 } elsif (my $coderef = $args{write_code}) {
531 $res_content = Furl::CallbackStream->new(
532 31 ▶ sub { $coderef->($res_status, $res_msg, $res_headers, @_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
533 3 );
534 }
535 }
536  
537 69 ▶ if (exists $COMPRESSED{ $special_headers->{'content-encoding'} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $COMPRESSED{$special_headers->{"content-encoding"}}) 11 58 69
538 11 _requires('Furl/ZlibStream.pm', 'Content-Encoding', 'Compress::Raw::Zlib');
539  
540 # An explicit limit (per request, or given to new) always applies.
541 # Otherwise the default limit applies only when the body is held in
542 # memory; a body streamed with write_file/write_code does not grow
543 # in memory, so it is left unlimited as before.
544 my $max_decompressed_size
545 = defined($args{max_decompressed_size}) ? $args{max_decompressed_size}
546 : defined($self->{max_decompressed_size}) ? $self->{max_decompressed_size}
547 11 ▶ : ref($res_content) ? 0
Branch: 3 branches
BranchTrueFalseTotal
defined $args{'max_decompressed_size'} ? : 1 10 11
defined $self->{'max_decompressed_size'} ? : 8 2 10
ref $res_content ? : 1 1 2
548 : $DEFAULT_MAX_DECOMPRESSED_SIZE;
549 11 $res_content = Furl::ZlibStream->new($res_content, $max_decompressed_size);
550 }
551  
552 69 my $chunked = ($special_headers->{'transfer-encoding'} eq 'chunked');
553 69 my $content_length = $special_headers->{'content-length'};
554 69 ▶ if (defined($content_length) && $content_length !~ /\A[0-9]+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content_length and not $content_length =~ /\A[0-9]+\z/) 1 68 69
Condition100%3/3
defined $content_length and not $content_length =~ /\A[0-9]+\z/
!ll&&!rl&&r
11571
MC/DC100%2/2
defined $content_length and not $content_length =~ /\A[0-9]+\z/
defined $content_length not $content_length =~ /\A[0-9]+\z/
Truth table100%3/3
A and not(B)
A= defined $content_lengthB= not $content_length =~ /\A[0-9]+\z/
ABresult
0X0
100
111
555 1 return $self->_r500("Bad Content-Length: ${content_length}");
556 }
557  
558 68 ▶ unless ($method eq 'HEAD'
Branch: 1 branch
BranchTrueFalseTotal
unless ($method eq "HEAD" or $res_status < 200 and $res_status >= 100 or $res_status == 204 or $res_status == 304) 61 7 68
Condition75%9/12
$method eq "HEAD" or $res_status < 200 and $res_status >= 100 or $res_status == 204 or $res_status == 304
l!l&&r!l&&!r
4361
$method eq "HEAD" or $res_status < 200 and $res_status >= 100 or $res_status == 204
l!l&&r!l&&!r
1364
$method eq "HEAD" or $res_status < 200 and $res_status >= 100
l!l&&r!l&&!r
1067
$res_status < 200 and $res_status >= 100
!ll&&!rl&&r
6700
MC/DC60%3/5
$method eq "HEAD" or $res_status < 200 and $res_status >= 100 or $res_status == 204 or $res_status == 304
$method eq "HEAD" $res_status < 200 $res_status >= 100 $res_status == 204 $res_status == 304
Truth table50%4/8
A or B and C or D or E
A= $method eq "HEAD"B= $res_status < 200C= $res_status >= 100D= $res_status == 204E= $res_status == 304
ABCDEresult
1XXXX1
011XX1
00X1X1
0101X1
00X011
010011
00X000
010000
559 || ($res_status < 200 && $res_status >= 100)
560 || $res_status == 204
561 || $res_status == 304) {
562 61 my @err;
563 61 my $ok = eval {
564 61 ▶ if ( $chunked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunked) { } 9 52 61
565 9 @err = $self->_read_body_chunked($sock,
566 \$res_content, $rest_header, $timeout_at);
567 } else {
568 52 $res_content .= $rest_header;
569 48 ▶ if (ref $res_content || !defined($content_length)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res_content or not defined $content_length) { } 7 41 48
Condition100%3/3
ref $res_content or not defined $content_length
l!l&&r!l&&!r
5241
MC/DC100%2/2
ref $res_content or not defined $content_length
ref $res_content not defined $content_length
Truth table100%3/3
A or not(B)
A= ref $res_contentB= not defined $content_length
ABresult
1X1
011
000
570 7 @err = $self->_read_body_normal($sock,
571 \$res_content, length($rest_header),
572 $content_length, $timeout_at);
573 } else {
574 41 @err = $self->_read_body_normal_to_string_buffer($sock,
575 \$res_content, length($rest_header),
576 $content_length, $timeout_at);
577 }
578 }
579 56 1;
580 };
581 61 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 5 56 61
582 5 my $e = $@;
583 # The rest of the body is left unread, so the connection is
584 # dropped rather than returned to the pool.
585 5 ▶ return $self->_r500("$e")
Branch: 1 branch
BranchTrueFalseTotal
if ref $e and ref $e eq "Furl::ZlibStream::LimitExceeded" 4 1 5
Condition66%2/3
ref $e and ref $e eq "Furl::ZlibStream::LimitExceeded"
!ll&&!rl&&r
104
MC/DC50%1/2
ref $e and ref $e eq "Furl::ZlibStream::LimitExceeded"
ref $e ref $e eq "Furl::ZlibStream::LimitExceeded"
Truth table66%2/3
A and B
A= ref $eB= ref $e eq "Furl::ZlibStream::LimitExceeded"
ABresult
0X0
100
111
586 if ref($e) && ref($e) eq 'Furl::ZlibStream::LimitExceeded';
587 1 die $e;
588 }
589 56 ▶ if(@err) {
Branch: 1 branch
BranchTrueFalseTotal
if (@err) 2 54 56
590 2 return @err;
591 }
592 }
593  
594 # manage connection cache (i.e. keep-alive)
595 61 ▶ if (lc($connection_header) eq 'keep-alive') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $connection_header eq "keep-alive") 60 1 61
596 60 my $connection = lc $special_headers->{'connection'};
597 60 ▶ if (($res_minor_version == 0
Branch: 2 branches
BranchTrueFalseTotal
if ($res_minor_version == 0 ? $connection eq "keep-alive" : $connection ne "close" and defined $content_length || $chunked) 44 16 60
$res_minor_version == 0 ? : 10 50 60
Condition66%4/6
$res_minor_version == 0 ? $connection eq "keep-alive" : $connection ne "close" and defined $content_length || $chunked
!ll&&!rl&&r
16044
defined $content_length or $chunked
l!l&&r!l&&!r
3860
MC/DC33%1/3
$res_minor_version == 0 ? $connection eq "keep-alive" : $connection ne "close" and defined $content_length || $chunked
$res_minor_version == 0 ? $connection eq "keep-alive" : $connection ne "close" defined $content_length $chunked
Truth table75%3/4
A and B or C
A= $res_minor_version == 0 ? $connection eq "keep-alive" : $connection ne "close"B= defined $content_lengthC= $chunked
ABCresult
0XX0
1000
11X1
1011
598 ? $connection eq 'keep-alive' # HTTP/1.0 needs explicit keep-alive
599 : $connection ne 'close') # HTTP/1.1 can keep alive by default
600 && ( defined $content_length or $chunked)) {
601 44 $self->{connection_pool}->push($host, $port, $sock);
602 }
603 }
604 # explicitly close here, just after returning the socket to the pool,
605 # since it might be reused in the upcoming recursive call
606 61 undef $sock;
607  
608 # process 'Set-Cookie' header.
609 61 ▶ if (defined $cookie_jar) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cookie_jar) 0 61 61
610 0 ▶ my $req_url = join(
Branch: 1 branch
BranchTrueFalseTotal
defined $username && defined $password ? : 0 0 0
Condition0%0/3
defined $username and defined $password
!ll&&!rl&&r
000
MC/DC0%0/2
defined $username and defined $password
defined $username defined $password
Truth table0%0/3
A and B
A= defined $usernameB= defined $password
ABresult
0X0
100
111
611 '',
612 $scheme,
613 '://',
614 (defined($username) && defined($password) ? "${username}:${password}@" : ()),
615 "$host:${port}${path_query}",
616 );
617 0 my $cookies = $res_headers->{'set-cookie'};
618 0 ▶ $cookies = [$cookies] if !ref$cookies;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cookies 0 0 0
619 0 for my $cookie (@$cookies) {
620 0 $cookie_jar->add($req_url, $cookie);
621 }
622 }
623  
624 61 ▶ if ($do_redirect) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_redirect) 0 61 61
625 0 my $location = $special_headers->{location};
626 0 ▶ unless ($location =~ m{^[a-z0-9]+://}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($location =~ m[^[a-z0-9]+://]) 0 0 0
627 # RFC 2616 14.30 says Location header is absolute URI.
628 # But, a lot of servers return relative URI.
629 0 _requires("URI.pm", "redirect with relative url");
630 0 $location = URI->new_abs($location, "$scheme://$host:$port$path_query")->as_string;
631 }
632 # Note: RFC 1945 and RFC 2068 specify that the client is not allowed
633 # to change the method on the redirected request. However, most
634 # existing user agent implementations treat 302 as if it were a 303
635 # response, performing a GET on the Location field-value regardless
636 # of the original request method. The status codes 303 and 307 have
637 # been added for servers that wish to make unambiguously clear which
638 # kind of reaction is expected of the client. Also, 308 was introduced
639 # to avoid the ambiguity of 301.
640 0 ▶ return $self->request(
Branch: 1 branch
BranchTrueFalseTotal
$res_status =~ /^30[178]$/ ? : 0 0 0
641 @_,
642 method => $res_status =~ /^30[178]$/ ? $method : 'GET',
643 url => $location,
644 max_redirects => $max_redirects - 1,
645 );
646 }
647  
648 # return response.
649  
650 61 ▶ if (ref $res_content) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res_content) 6 55 61
651 6 $res_content = $res_content->get_response_string;
652 }
653  
654 return (
655 61 $res_minor_version, $res_status, $res_msg, $res_headers, $res_content,
656 $req_headers, $req_content, undef, undef, [$scheme, $username, $password, $host, $port, $path_query],
657 );
658 }
659  
660 # connects to $host:$port and returns $socket
661 49 ▶sub connect :method {
Subroutine connect: called CC=11 SCAR=31.2
Pod undocumented
662 49 my($self, $host, $port, $timeout_at) = @_;
663 49 my $sock;
664  
665 49 my $timeout = $timeout_at - time;
666 49 ▶ return (undef, "Failed to resolve host name: timeout")
Branch: 1 branch
BranchTrueFalseTotal
if $timeout <= 0 0 49 49
667 if $timeout <= 0;
668 49 my ($sock_addr, $err_reason) = $self->_get_address($host, $port, $timeout);
669 49 ▶ return (undef, "Cannot resolve host name: $host (port: $port), " . ($err_reason || $!))
Branch: 1 branch
BranchTrueFalseTotal
unless $sock_addr 1 48 49
Condition33%1/3
$err_reason || $!
l!l&&r!l&&!r
001
MC/DC0%0/2
$err_reason || $!
$err_reason $!
Truth table33%1/3
A || B
A= $err_reasonB= $!
ABresult
1X1
011
000
670 unless $sock_addr;
671  
672 48 ▶ RETRY:
Branch: 1 branch
BranchTrueFalseTotal
socket $sock, Socket::sockaddr_family($sock_addr), 1, 0 or Carp::croak("Cannot create socket: $!") 48 0 48
673 socket($sock, Socket::sockaddr_family($sock_addr), SOCK_STREAM, 0)
674 or Carp::croak("Cannot create socket: $!");
675 48 _set_sockopts($sock);
676 48 ▶ if (connect($sock, $sock_addr)) {
Branch: 2 branches
BranchTrueFalseTotal
if (CORE::connect $sock, $sock_addr) { } 0 48 48
elsif ($! == 115 or 0) { } 48 0 48
677 # connected
678 ▶ } elsif ($! == EINPROGRESS || (WIN32 && $! == EWOULDBLOCK)) {
Condition50%1/2
$! == 115 or !!0
l!l
480
MC/DC0%0/1
$! == 115 or !!0
$! == 115
Truth table50%1/2
A
A= $! == 115
Aresult
11
00
679 48 ▶ $self->do_select(1, $sock, $timeout_at)
Branch: 1 branch
BranchTrueFalseTotal
$self->do_select(1, $sock, $timeout_at) or return undef, "Cannot connect to ${host}:${port}: timeout" 48 0 48
680 or return (undef, "Cannot connect to ${host}:${port}: timeout");
681 # connected
682 } else {
683 0 ▶ if ($! == EINTR && ! $self->{stop_if}->()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! == 4 and not $self->{"stop_if"}->()) 0 0 0
Condition0%0/3
$! == 4 and not $self->{"stop_if"}->()
!ll&&!rl&&r
000
MC/DC0%0/2
$! == 4 and not $self->{"stop_if"}->()
$! == 4 not $self->{"stop_if"}->()
Truth table0%0/3
A and not(B)
A= $! == 4B= not $self->{"stop_if"}->()
ABresult
0X0
100
111
684 0 close $sock;
685 0 goto RETRY;
686 }
687 0 return (undef, "Cannot connect to ${host}:${port}: $!");
688 }
689 48 $sock;
690 }
691  
692 49 ▶sub _get_address {
Subroutine _get_address: called CC=3 SCAR=11.0
693 49 my ($self, $host, $port, $timeout) = @_;
694 49 ▶ if ($self->{get_address}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"get_address"}) 1 48 49
695 1 return $self->{get_address}->($host, $port, $timeout);
696 }
697 # default rule (TODO add support for IPv6)
698 48 ▶ my $iaddr = $self->{inet_aton}->($host, $timeout)
Branch: 1 branch
BranchTrueFalseTotal
my $iaddr = $self->{"inet_aton"}->($host, $timeout) or return undef, $! 47 1 48
699 or return (undef, $!);
700 47 pack_sockaddr_in($port, $iaddr);
701 }
702  
703 0 ▶sub _ssl_opts {
Subroutine _ssl_opts: not called CC=6 SCAR=37.4
704 0 my $self = shift;
705 0 my $ssl_opts = $self->{ssl_opts};
706 0 ▶ unless (exists $ssl_opts->{SSL_verify_mode}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ssl_opts->{"SSL_verify_mode"}) 0 0 0
707 # set SSL_VERIFY_PEER as default.
708 0 $ssl_opts->{SSL_verify_mode} = IO::Socket::SSL::SSL_VERIFY_PEER();
709 0 ▶ unless (exists $ssl_opts->{SSL_verifycn_scheme}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ssl_opts->{"SSL_verifycn_scheme"}) 0 0 0
710 0 $ssl_opts->{SSL_verifycn_scheme} = 'www'
711 }
712 }
713 0 ▶ if ($ssl_opts->{SSL_verify_mode}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ssl_opts->{"SSL_verify_mode"}) 0 0 0
714 0 ▶ unless (exists $ssl_opts->{SSL_ca_file} || exists $ssl_opts->{SSL_ca_path}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ssl_opts->{"SSL_ca_file"} or exists $ssl_opts->{"SSL_ca_path"}) 0 0 0
Condition0%0/3
exists $ssl_opts->{"SSL_ca_file"} or exists $ssl_opts->{"SSL_ca_path"}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $ssl_opts->{"SSL_ca_file"} or exists $ssl_opts->{"SSL_ca_path"}
exists $ssl_opts->{"SSL_ca_file"} exists $ssl_opts->{"SSL_ca_path"}
Truth table0%0/3
A or B
A= exists $ssl_opts->{"SSL_ca_file"}B= exists $ssl_opts->{"SSL_ca_path"}
ABresult
1X1
011
000
715 0 require Mozilla::CA;
716 0 $ssl_opts->{SSL_ca_file} = Mozilla::CA::SSL_ca_file();
717 }
718 }
719 0 $ssl_opts;
720 }
721  
722 # connect SSL socket.
723 # You can override this method in your child class, if you want to use Crypt::SSLeay or some other library.
724 # @return file handle like object
725 0 ▶sub connect_ssl {
Subroutine connect_ssl: not called CC=4 SCAR=30.0
Pod undocumented
726 0 my ($self, $host, $port, $timeout_at) = @_;
727 0 _requires('IO/Socket/SSL.pm', 'SSL');
728  
729 0 my ($sock, $err_reason) = $self->connect($host, $port, $timeout_at);
730 0 ▶ return (undef, $err_reason)
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 0 0 0
731 unless $sock;
732  
733 0 my $timeout = $timeout_at - time;
734 0 ▶ return (undef, "Cannot create SSL connection: timeout")
Branch: 1 branch
BranchTrueFalseTotal
if $timeout <= 0 0 0 0
735 if $timeout <= 0;
736  
737 0 my $ssl_opts = $self->_ssl_opts;
738 0 ▶ IO::Socket::SSL->start_SSL(
Branch: 1 branch
BranchTrueFalseTotal
"IO::Socket::SSL"->start_SSL($sock, "PeerHost", $host, "PeerPort", $port, "Timeout", $timeout, %$ssl_opts) or return undef, 'Cannot create SSL connection: ' . IO::Socket::SSL::errstr() 0 0 0
739 $sock,
740 PeerHost => $host,
741 PeerPort => $port,
742 Timeout => $timeout,
743 %$ssl_opts,
744 ) or return (undef, "Cannot create SSL connection: " . IO::Socket::SSL::errstr());
745 0 _set_sockopts($sock);
746 0 $sock;
747 }
748  
749 0 ▶sub connect_ssl_over_proxy {
Subroutine connect_ssl_over_proxy: not called CC=9 SCAR=45.0
Pod undocumented
750 0 my ($self, $proxy_host, $proxy_port, $host, $port, $timeout_at, $proxy_authorization) = @_;
751 0 _requires('IO/Socket/SSL.pm', 'SSL');
752  
753 0 my $sock = $self->connect($proxy_host, $proxy_port, $timeout_at);
754  
755 0 my $p = "CONNECT $host:$port HTTP/1.0\015\012Server: $host\015\012";
756 0 ▶ if (defined $proxy_authorization) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proxy_authorization) 0 0 0
757 0 $p .= "Proxy-Authorization: $proxy_authorization\015\012";
758 }
759 0 $p .= "\015\012";
760 0 ▶ $self->write_all($sock, $p, $timeout_at)
Branch: 1 branch
BranchTrueFalseTotal
$self->write_all($sock, $p, $timeout_at) or return $self->_r500('Failed to send HTTP request to proxy: ' . _strerror_or_timeout()) 0 0 0
761 or return $self->_r500(
762 "Failed to send HTTP request to proxy: " . _strerror_or_timeout());
763 0 my $buf = '';
764 my $read = $self->read_timeout($sock,
765 0 \$buf, $self->{bufsize}, length($buf), $timeout_at);
766 0 ▶ if (not defined $read) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $read) { } 0 0 0
elsif ($read == 0) { } 0 0 0
elsif (not $buf =~ m[^HTTP/1\.[0-9] 200 .+\015\012]) { } 0 0 0
767 0 return (undef, "Cannot read proxy response: " . _strerror_or_timeout());
768 } elsif ( $read == 0 ) { # eof
769 0 return (undef, "Unexpected EOF while reading proxy response");
770 } elsif ( $buf !~ /^HTTP\/1\.[0-9] 200 .+\015\012/ ) {
771 0 return (undef, "Invalid HTTP Response via proxy");
772 }
773  
774 0 my $timeout = $timeout_at - time;
775 0 ▶ return (undef, "Cannot start SSL connection: timeout")
Branch: 1 branch
BranchTrueFalseTotal
if $timeout_at <= 0 0 0 0
776 if $timeout_at <= 0;
777  
778 0 my $ssl_opts = $self->_ssl_opts;
779 0 ▶ unless (exists $ssl_opts->{SSL_verifycn_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ssl_opts->{"SSL_verifycn_name"}) 0 0 0
780 0 $ssl_opts->{SSL_verifycn_name} = $host;
781 }
782 IO::Socket::SSL->start_SSL(
783 0 ▶ $sock,
Branch: 1 branch
BranchTrueFalseTotal
"IO::Socket::SSL"->start_SSL($sock, "PeerHost", $host, "PeerPort", $port, "Timeout", $timeout, %$ssl_opts) or return undef, 'Cannot start SSL connection: ' . IO::Socket::SSL::errstr() 0 0 0
784 PeerHost => $host,
785 PeerPort => $port,
786 Timeout => $timeout,
787 %$ssl_opts
788 ) or return (undef, "Cannot start SSL connection: " . IO::Socket::SSL::errstr());
789 0 _set_sockopts($sock); # just in case (20101118 kazuho)
790 0 $sock;
791 }
792  
793 9 ▶sub _read_body_chunked {
Subroutine _read_body_chunked: called CC=9 SCAR=22.2
794 9 my ($self, $sock, $res_content, $rest_header, $timeout_at) = @_;
795  
796 9 my $buf = $rest_header;
797 9 READ_LOOP: while (1) {
798 2604 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (() = $buf =~ /\A ( # header ( [0-9a-fA-F]+ ) # next_len (hex number) (?:; $HTTP_TOKEN = (?: $HTTP_TOKEN | $HTTP_QUOTED_STRING ) )* # optional chunk-extensions [ ]* # www.yahoo.com adds spaces here. # Is this valid? \015\012 # CR+LF ) /mosx) 2085 519 2604
799 my ( $header, $next_len ) = (
800 $buf =~
801 m{\A ( # header
802 ( [0-9a-fA-F]+ ) # next_len (hex number)
803 (?:;
804 $HTTP_TOKEN
805 =
806 (?: $HTTP_TOKEN | $HTTP_QUOTED_STRING )
807 )* # optional chunk-extensions
808 [ ]* # www.yahoo.com adds spaces here.
809 # Is this valid?
810 \015\012 # CR+LF
811 ) }xmso
812 )
813 )
814 {
815 2085 $buf = substr($buf, length($header)); # remove header from buf
816 2085 $next_len = hex($next_len);
817 2085 ▶ if ($next_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_len == 0) 7 2078 2085
818 7 last READ_LOOP;
819 }
820  
821 # +2 means trailing CRLF
822 2078 ▶ READ_CHUNK: while ( $next_len+2 > length($buf) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_len + 2 > length $buf) 1037 2078 3115
823 my $n = $self->read_timeout( $sock,
824 1037 \$buf, $self->{bufsize}, length($buf), $timeout_at );
825 1037 ▶ if (!$n) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n) 0 1037 1037
826 0 ▶ return $self->_r500(
Branch: 1 branch
BranchTrueFalseTotal
!defined($n) ? : 0 0 0
827 !defined($n)
828 ? "Cannot read chunk: " . _strerror_or_timeout()
829 : "Unexpected EOF while reading packets"
830 );
831 }
832 }
833 2078 $$res_content .= substr($buf, 0, $next_len);
834 2077 $buf = substr($buf, $next_len+2);
835 2077 ▶ if (length($buf) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf > 0) 1821 256 2077
836 1821 next; # re-parse header
837 }
838 }
839  
840 my $n = $self->read_timeout( $sock,
841 775 \$buf, $self->{bufsize}, length($buf), $timeout_at );
842 775 ▶ if (!$n) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n) 1 774 775
843 1 ▶ return $self->_r500(
Branch: 1 branch
BranchTrueFalseTotal
!defined($n) ? : 0 1 1
844 !defined($n)
845 ? "Cannot read chunk: " . _strerror_or_timeout()
846 : "Unexpected EOF while reading packets"
847 );
848 }
849 }
850 # read last CRLF
851 7 return $self->_read_body_normal(
852 $sock, \$buf, length($buf), 2, $timeout_at);
853 }
854  
855 14 ▶sub _read_body_normal {
Subroutine _read_body_normal: called CC=6 SCAR=18.0
856 14 my ($self, $sock, $res_content, $nread, $res_content_length, $timeout_at)
857 = @_;
858 14 ▶ while (!defined($res_content_length) || $res_content_length != $nread) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $res_content_length or $res_content_length != $nread) 19 12 31
Condition100%3/3
not defined $res_content_length or $res_content_length != $nread
l!l&&r!l&&!r
21712
MC/DC100%2/2
not defined $res_content_length or $res_content_length != $nread
not defined $res_content_length $res_content_length != $nread
Truth table100%3/3
not(A) or B
A= not defined $res_content_lengthB= $res_content_length != $nread
ABresult
1X1
011
000
859 my $n = $self->read_timeout( $sock,
860 19 \my $buf, $self->{bufsize}, 0, $timeout_at );
861 19 ▶ if (!defined($n)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $n) { } 1 18 19
elsif (not $n) { } 1 17 18
862 1 return $self->_r500("Cannot read content body: " . _strerror_or_timeout());
863 } elsif (!$n) {
864 1 ▶ last if ! defined($res_content_length);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res_content_length 1 0 1
865 0 return $self->_r500("Unexpected EOF while reading content body");
866 }
867 17 $$res_content .= $buf;
868 17 $nread += $n;
869 }
870 13 return;
871 }
872  
873 # This function loads all content at once if it's possible. Since $res_content is just a plain scalar.
874 # Buffering is not needed.
875 41 ▶sub _read_body_normal_to_string_buffer {
Subroutine _read_body_normal_to_string_buffer: called CC=4 SCAR=15.0
876 41 my ($self, $sock, $res_content, $nread, $res_content_length, $timeout_at)
877 = @_;
878 41 ▶ while ($res_content_length != $nread) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res_content_length != $nread) 35 41 76
879 35 my $n = $self->read_timeout( $sock,
880 $res_content, $res_content_length, $nread, $timeout_at );
881 35 ▶ if (!$n) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n) 0 35 35
882 0 ▶ return $self->_r500(
Branch: 1 branch
BranchTrueFalseTotal
!defined($n) ? : 0 0 0
883 !defined($n)
884 ? "Cannot read content body: " . _strerror_or_timeout()
885 : "Unexpected EOF while reading content body"
886 );
887 }
888 35 $nread += $n;
889 }
890 41 return;
891 }
892  
893 # returns true if the socket is ready to read, false if timeout has occurred ($! will be cleared upon timeout)
894 119 ▶sub do_select {
Subroutine do_select: called CC=7 SCAR=19.5
Pod undocumented
895 119 my($self, $is_write, $sock, $timeout_at) = @_;
896 119 my $now = time;
897 119 my $inactivity_timeout_at = $now + $self->{inactivity_timeout};
898 119 ▶ $timeout_at = $inactivity_timeout_at
Branch: 1 branch
BranchTrueFalseTotal
if $timeout_at > $inactivity_timeout_at 0 119 119
899 if $timeout_at > $inactivity_timeout_at;
900 # wait for data
901 119 while (1) {
902 123 my $timeout = $timeout_at - $now;
903 123 ▶ if ($timeout <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout <= 0) 1 122 123
904 1 $! = 0;
905 1 return 0;
906 }
907 122 my $nfound = $self->_do_select($is_write, $sock, $timeout);
908 122 ▶ return 1 if $nfound > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $nfound > 0 115 7 122
909 7 ▶ return 0 if $nfound == -1 && $! == EINTR && $self->{stop_if}->();
Branch: 1 branch
BranchTrueFalseTotal
if $nfound == -1 and $! == 4 and $self->{"stop_if"}->() 3 4 7
Condition83%5/6
$nfound == -1 and $! == 4 and $self->{"stop_if"}->()
!ll&&!rl&&r
133
$nfound == -1 and $! == 4
!ll&&!rl&&r
106
MC/DC66%2/3
$nfound == -1 and $! == 4 and $self->{"stop_if"}->()
$nfound == -1 $! == 4 $self->{"stop_if"}->()
Truth table75%3/4
A and B and C
A= $nfound == -1B= $! == 4C= $self->{"stop_if"}->()
ABCresult
0XX0
10X0
1100
1111
910 4 $now = time;
911 }
912 0 die 'not reached';
913 }
914  
915 148 ▶sub _do_select {
Subroutine _do_select: called CC=2 SCAR=6.9
916 148 my($self, $is_write, $sock, $timeout) = @_;
917 148 my($rfd, $wfd);
918 148 my $efd = '';
919 148 vec($efd, fileno($sock), 1) = 1;
920 148 ▶ if ($is_write) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_write) { } 48 100 148
921 48 $wfd = $efd;
922 } else {
923 100 $rfd = $efd;
924 }
925 148 my $nfound = select($rfd, $wfd, $efd, $timeout);
926 148 return $nfound;
927 }
928  
929 # returns (positive) number of bytes read, or undef if the socket is to be closed
930 2090 ▶sub read_timeout {
Subroutine read_timeout: called CC=8 SCAR=25.8
Pod undocumented
931 2090 my ($self, $sock, $buf, $len, $off, $timeout_at) = @_;
932 2090 my $ret;
933  
934 # NOTE: select-read-select may get stuck in SSL,
935 # so we use read-select-read instead.
936 2090 while(1) {
937 # try to do the IO
938 2157 ▶ defined($ret = sysread($sock, $$buf, $len, $off))
Branch: 1 branch
BranchTrueFalseTotal
defined($ret = sysread($sock, $$buf, $len, $off)) and return $ret 2086 71 2157
939 and return $ret;
940 71 ▶ if ($! == EAGAIN || $! == EWOULDBLOCK || (WIN32 && $! == EISCONN)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($! == 11 or $! == 11 or 0) { } 71 0 71
elsif ($! == 4) { } 0 0 0
Condition40%2/5
$! == 11 or $! == 11 or !!0
l!l
710
$! == 11 or $! == 11
l!l&&r!l&&!r
7100
MC/DC0%0/2
$! == 11 or $! == 11 or !!0
$! == 11 $! == 11
Truth table33%1/3
A or B
A= $! == 11B= $! == 11
ABresult
1X1
011
000
941 # passthru
942 } elsif ($! == EINTR) {
943 0 ▶ return undef if $self->{stop_if}->();
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stop_if"}->() 0 0 0
944 # otherwise passthru
945 } else {
946 0 return undef;
947 }
948 # on EINTER/EAGAIN/EWOULDBLOCK
949 71 ▶ $self->do_select(0, $sock, $timeout_at) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->do_select(0, $sock, $timeout_at) or return undef 67 4 71
950 }
951 }
952  
953 # returns (positive) number of bytes written, or undef if the socket is to be closed
954 72 ▶sub write_timeout {
Subroutine write_timeout: called CC=8 SCAR=36.6
Pod undocumented
955 72 my ($self, $sock, $buf, $len, $off, $timeout_at) = @_;
956 72 my $ret;
957 72 while(1) {
958 # try to do the IO
959 72 ▶ defined($ret = syswrite($sock, $buf, $len, $off))
Branch: 1 branch
BranchTrueFalseTotal
defined($ret = syswrite($sock, $buf, $len, $off)) and return $ret 72 0 72
960 and return $ret;
961 0 ▶ if ($! == EAGAIN || $! == EWOULDBLOCK || (WIN32 && $! == EISCONN)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($! == 11 or $! == 11 or 0) { } 0 0 0
elsif ($! == 4) { } 0 0 0
Condition0%0/5
$! == 11 or $! == 11 or !!0
l!l
00
$! == 11 or $! == 11
l!l&&r!l&&!r
000
MC/DC0%0/2
$! == 11 or $! == 11 or !!0
$! == 11 $! == 11
Truth table0%0/3
A or B
A= $! == 11B= $! == 11
ABresult
1X1
011
000
962 # passthru
963 } elsif ($! == EINTR) {
964 0 ▶ return undef if $self->{stop_if}->();
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stop_if"}->() 0 0 0
965 # otherwise passthru
966 } else {
967 0 return undef;
968 }
969 0 ▶ $self->do_select(1, $sock, $timeout_at) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->do_select(1, $sock, $timeout_at) or return undef 0 0 0
970 }
971 }
972  
973 # writes all data in buf and returns number of bytes written or undef if failed
974 72 ▶sub write_all {
Subroutine write_all: called CC=3 SCAR=11.0
Pod undocumented
975 72 my ($self, $sock, $buf, $timeout_at) = @_;
976 72 my $off = 0;
977 72 ▶ while (my $len = length($buf) - $off) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $len = length($buf) - $off) 72 72 144
978 72 ▶ my $ret = $self->write_timeout($sock, $buf, $len, $off, $timeout_at)
Branch: 1 branch
BranchTrueFalseTotal
my $ret = $self->write_timeout($sock, $buf, $len, $off, $timeout_at) or return undef 72 0 72
979 or return undef;
980 72 $off += $ret;
981 }
982 72 return $off;
983 }
984  
985  
986 11 ▶sub _r500 {
Subroutine _r500: called CC=1 SCAR=0.0
987 11 my($self, $message) = @_;
988 11 $message = Carp::shortmess($message); # add lineno and filename
989 11 return(0, 500, "Internal Response: $message",
990 [
991 'Content-Length' => length($message),
992 'X-Internal-Response' => 1,
993 # XXX ^^ EXPERIMENTAL header. Do not depend to this.
994 ], $message
995 );
996 }
997  
998 4 ▶sub _strerror_or_timeout {
Subroutine _strerror_or_timeout: called CC=2 SCAR=6.9
999 4 ▶ $! != 0 ? "$!" : 'timeout';
Branch: 1 branch
BranchTrueFalseTotal
$! != 0 ? : 3 1 4
1000 }
1001  
1002 48 ▶sub _set_sockopts {
Subroutine _set_sockopts: called CC=4 SCAR=14.1
1003 48 my $sock = shift;
1004  
1005 48 ▶ setsockopt( $sock, IPPROTO_TCP, TCP_NODELAY, 1 )
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $sock, 6, 1, 1 or Carp::croak("Failed to setsockopt(TCP_NODELAY): $!") 48 0 48
1006 or Carp::croak("Failed to setsockopt(TCP_NODELAY): $!");
1007 48 if (WIN32) {
1008 if (ref($sock) ne 'IO::Socket::SSL') {
1009 my $tmp = 1;
1010 ioctl( $sock, 0x8004667E, \$tmp )
1011 or Carp::croak("Cannot set flags for the socket: $!");
1012 }
1013 } else {
1014 48 ▶ my $flags = fcntl( $sock, F_GETFL, 0 )
Branch: 1 branch
BranchTrueFalseTotal
my $flags = fcntl($sock, 3, 0) or Carp::croak("Cannot get flags for the socket: $!") 48 0 48
1015 or Carp::croak("Cannot get flags for the socket: $!");
1016 48 ▶ $flags = fcntl( $sock, F_SETFL, $flags | O_NONBLOCK )
Branch: 1 branch
BranchTrueFalseTotal
$flags = fcntl($sock, 4, $flags | 2048) or Carp::croak("Cannot set flags for the socket: $!") 48 0 48
1017 or Carp::croak("Cannot set flags for the socket: $!");
1018 }
1019  
1020 {
1021 # no buffering
1022 48 my $orig = select();
1023 48 select($sock); $|=1;
1024 48 select($orig);
1025 }
1026  
1027 48 binmode $sock;
1028 }
1029  
1030 # You can override this method if you want to use more powerful matcher.
1031 0 ▶sub match_no_proxy {
Subroutine match_no_proxy: not called CC=4 SCAR=30.0
Pod undocumented
1032 0 my ( $self, $no_proxy, $host ) = @_;
1033  
1034 # ref. curl.1.
1035 # list of host names that shouldn't go through any proxy.
1036 # If set to a asterisk '*' only, it matches all hosts.
1037 0 ▶ if ( $no_proxy eq '*' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_proxy eq '*') { } 0 0 0
1038 0 return 1;
1039 }
1040 else {
1041 0 for my $pat ( split /\s*,\s*/, lc $no_proxy ) {
1042 0 ▶ if ( $host =~ /\Q$pat\E$/ ) { # suffix match(same behavior with LWP)
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /\Q$pat\E$/) 0 0 0
1043 0 return 1;
1044 }
1045 }
1046 }
1047 0 return 0;
1048 }
1049  
1050 # utility class
1051 {
1052 package # hide from pause
1053 Furl::FileStream;
1054 48 ▶ use overload '.=' => 'append', fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
1055 0 ▶ sub new {
Subroutine new: not called CC=1 SCAR=6.9
1056 0 my ($class, $fh) = @_;
1057 0 bless {fh => $fh}, $class;
1058 }
1059 0 ▶ sub append {
Subroutine append: not called CC=1 SCAR=6.9
1060 0 my($self, $partial) = @_;
1061 0 print {$self->{fh}} $partial;
1062 0 return $self;
1063 }
1064 0 ▶ sub get_response_string { undef }
Subroutine get_response_string: not called CC=1 SCAR=6.9
1065 }
1066  
1067 {
1068 48 package # hide from pause
1069 Furl::CallbackStream;
1070 48 ▶ use overload '.=' => 'append', fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
1071 3 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
1072 3 my ($class, $cb) = @_;
1073 3 bless {cb => $cb}, $class;
1074 }
1075 31 ▶ sub append {
Subroutine append: called CC=1 SCAR=0.0
1076 31 my($self, $partial) = @_;
1077 31 $self->{cb}->($partial);
1078 30 return $self;
1079 }
1080 0 ▶ sub get_response_string { undef }
Subroutine get_response_string: not called CC=1 SCAR=6.9
1081 }
1082  
1083 48 1;
1084 __END__
↑ Summary Furl/Response.pm »