blib/lib/Net/HTTP/Tiny.pm

filter: Statementstmt 28.5% 55 / 193 Branchbran 6.7% 8 / 118 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 60.0% 9 / 15 Podpod 100% 1 / 1 totaltotal 21.1% 73 / 346 CC 62 SCAR 59.0
CC
62
Cov
20%
_http_get_one
870.0
_open_tcp
132.0
_recv_headers
56.0
CRAP 364.0
↑ Summary
1 =head1 NAME
2  
3 Net::HTTP::Tiny - minimal HTTP client
4  
5 =head1 SYNOPSIS
6  
7 use Net::HTTP::Tiny qw(http_get);
8  
9 $dat = http_get("http://maia.usno.navy.mil/ser7/tai-utc.dat");
10  
11 =head1 DESCRIPTION
12  
13 This module provides an easy interface to retrieve files using the HTTP
14 protocol. The location of a file to retrieve is specified using a URL.
15 The module conforms to HTTP/1.1, and follows redirections (up to a limit
16 of five chained redirections). Content-MD5 is checked, if the optional
17 module L<Digest::MD5> is installed. IPv6 is supported, if the optional
18 module L<IO::Socket::IP> is installed. Only retrieval is supported,
19 not posting or anything more exotic.
20  
21 =cut
22  
23 package Net::HTTP::Tiny;
24  
25 4 ▶{ use 5.006; }
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 4 our $VERSION = "0.002";
32  
33 # Set up superclass manually, rather than via "parent", to avoid non-core
34 # dependency.
35 4 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
36 4 our @ISA = qw(Exporter);
37 4 our @EXPORT_OK = qw(http_get);
38  
39 =head1 FUNCTIONS
40  
41 =over
42  
43 =item http_get(URL)
44  
45 I<URL> must be a URL using the C<http> scheme. The file that it refers to
46 is retrieved from the HTTP server, and its content is returned in the form
47 of a string of octets. If any error occurs then the function C<die>s.
48 Possible errors include the URL being malformed, inability to contact the
49 HTTP server, and the HTTP server reporting that the file doesn't exist.
50  
51 =cut
52  
53 {
54 4 local $SIG{__DIE__};
55 4 ▶ eval("$]" >= 5.008 ? q{
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
"$]" >= 5.008 ? : 4 0 4
56 use utf8 ();
57 *_downgrade = \&utf8::downgrade;
58 } : q{
59 sub _downgrade($) {
60 # Logic copied from Scalar::String. See there
61 # for explanation; the code depends on accidents
62 # of the Perl 5.6 implementation.
63 return if unpack("C", "\xaa".$_[0]) == 170;
64 {
65 use bytes;
66 $_[0] =~ /\A[\x00-\x7f\x80-\xbf\xc2\xc3]*\z/
67 or die "Wide character";
68 }
69 use utf8;
70 ($_[0]) = ($_[0] =~ /\A([\x00-\xff]*)\z/);
71 }
72 });
73 4 ▶ die $@ unless $@ eq "";
Branch: 1 branch
BranchTrueFalseTotal
unless $@ eq "" 0 4 4
74 }
75  
76 29 ▶sub _croak($) { croak "HTTP error: $_[0]" }
Subroutine _croak: called CC=1 SCAR=0.0
77  
78 #
79 # HTTP URL interpretation is governed by RFC 3986 (generic URI syntax),
80 # RFC 2616 (HTTP/1.1, giving top-level syntax), and RFC 2396 (older
81 # generic URI syntax, to which RFC 2616 refers). There is no formal
82 # specification for the syntax of HTTP URLs in the context of RFC 3986's
83 # base syntax, so this code merges the various sources in what seems like
84 # a reasonable manner. Generally, RFC 3986 is used to determine which
85 # characters are permitted in each component, and RFC 2616 determines
86 # higher-level structure.
87 #
88  
89 4 my $safechar_rx = qr/[0-9A-Za-z\-\.\_\~\!\$\&\'\(\)\*\+\,\;\=]/;
90 4 my $hexpair_rx = qr/\%[0-9A-Fa-f]{2}/;
91  
92 4 my $d8_rx = qr/25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9]/;
93 4 my $ipv4_address_rx = qr/$d8_rx\.$d8_rx\.$d8_rx\.$d8_rx/o;
94  
95 4 my $h16_rx = qr/[0-9A-Fa-f]{1,4}/;
96 4 my $ls32_rx = qr/$h16_rx\:$h16_rx|$ipv4_address_rx/o;
97 4 my $ipv6_address_rx = qr/
98 (?:) (?:$h16_rx\:){6} $ls32_rx
99 | \:\: (?:$h16_rx\:){5} $ls32_rx
100 | (?: $h16_rx )? \:\: (?:$h16_rx\:){4} $ls32_rx
101 | (?: (?:$h16_rx\:){0,1} $h16_rx )? \:\: (?:$h16_rx\:){3} $ls32_rx
102 | (?: (?:$h16_rx\:){0,2} $h16_rx )? \:\: (?:$h16_rx\:){2} $ls32_rx
103 | (?: (?:$h16_rx\:){0,3} $h16_rx )? \:\: (?:$h16_rx\:) $ls32_rx
104 | (?: (?:$h16_rx\:){0,4} $h16_rx )? \:\: $ls32_rx
105 | (?: (?:$h16_rx\:){0,5} $h16_rx )? \:\: $h16_rx
106 | (?: (?:$h16_rx\:){0,6} $h16_rx )? \:\:
107 /xo;
108  
109 4 my $ip_future_rx = qr/[vV][0-9A-Fa-f]+\.(?:$safechar_rx|\:)+/o;
110 4 my $ip_literal_rx = qr/\[(?:$ipv6_address_rx|$ip_future_rx)\]/o;
111 4 my $hostname_rx = qr/
112 (?:[0-9A-Za-z](?:[\-0-9A-Za-z]*[0-9A-Za-z])?\.)*
113 [A-Za-z](?:[\-0-9A-Za-z]*[0-9A-Za-z])?
114 /x;
115 4 my $host_rx = qr/$ip_literal_rx|$ipv4_address_rx|$hostname_rx/o;
116 4 my $port_rx = qr/[0-9]+/;
117  
118 4 my $http_prefix_rx = qr/[hH][tT][tT][pP]\:\/\//;
119 4 my $path_and_query_rx = qr/\/(?:$safechar_rx|[\:\@\/\?]|$hexpair_rx)*/;
120 4 my $http_url_rx =
121 qr/$http_prefix_rx(?>$host_rx)(?:\:$port_rx?)?$path_and_query_rx?/xo;
122  
123 169 ▶sub _parse_http_url($) {
Subroutine _parse_http_url: called CC=4 SCAR=13.9
124 169 my($url) = @_;
125 169 my($host, $port, $pathquery) = ($url =~ m/\A
126 $http_prefix_rx
127 ((?>$host_rx))(?:\:($port_rx)?)?
128 ($path_and_query_rx)?
129 \z/xo);
130 169 ▶ defined $host or _croak "<$url> is not an http URL";
Branch: 1 branch
BranchTrueFalseTotal
defined $host or &_croak("<$url> is not an http URL") 140 29 169
131 return {
132 140 ▶ host => $host,
Branch: 2 branches
BranchTrueFalseTotal
defined $port ? : 43 97 140
defined $pathquery ? : 55 85 140
133 port => defined($port) ? 0+$port : 80,
134 path_and_query => defined($pathquery) ? $pathquery : "/",
135 };
136 }
137  
138 4 my $blksize = 0x8000;
139 4 my $timeout = 50;
140  
141 4 my $socket_class;
142 0 ▶sub _open_tcp($$) {
Subroutine _open_tcp: not called CC=11 SCAR=48.8
143 0 my($host, $port) = @_;
144 0 ▶ if($host =~ /\A\[v/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /\A\[v/) 0 0 0
145 0 _croak "IP addresses from the future not supported";
146 }
147 $socket_class ||=
148 0 eval { local $SIG{__DIE__};
149 0 require IO::Socket::IP;
150 0 IO::Socket::IP->VERSION(0.08);
151 0 "IO::Socket::IP";
152 0 ▶ } || do {
Condition0%0/6
$socket_class ||= eval { do { local $SIG{"__DIE__"}; require IO::Socket::IP; "IO::Socket::IP"->VERSION("0.08"); "IO::Socket::IP" } } || do { require IO::Socket::INET; "IO::Socket::INET"->VERSION("1.24"); "IO::Socket::INET" }
l!l&&r!l&&!r
000
eval { do { local $SIG{'__DIE__'}; require IO::Socket::IP; 'IO::Socket::IP'->VERSION(0.08); 'IO::Socket::IP' } } || do { require IO::Socket::INET; 'IO::Socket::INET'->VERSION(1.24); 'IO::Socket::INET' }
l!l&&r!l&&!r
000
MC/DC0%0/3
$socket_class ||= eval { do { local $SIG{"__DIE__"}; require IO::Socket::IP; "IO::Socket::IP"->VERSION("0.08"); "IO::Socket::IP" } } || do { require IO::Socket::INET; "IO::Socket::INET"->VERSION("1.24"); "IO::Socket::INET" }
$socket_class eval { do { local $SIG{'__DIE__'}; require IO::Socket::IP; 'IO::Socket::IP'->VERSION(0.08); 'IO::Socket::IP' } } do { require IO::Socket::INET; 'IO::Socket::INET'->VERSION(1.24); 'IO::Socket::INET' }
Truth table0%0/4
A ||= B || C
A= $socket_classB= eval { do { local $SIG{'__DIE__'}; require IO::Socket::IP; 'IO::Socket::IP'->VERSION(0.08); 'IO::Socket::IP' } }C= do { require IO::Socket::INET; 'IO::Socket::INET'->VERSION(1.24); 'IO::Socket::INET' }
ABCresult
1XX1
01X1
0011
0000
153 0 require IO::Socket::INET;
154 0 IO::Socket::INET->VERSION(1.24);
155 0 "IO::Socket::INET";
156 };
157 0 ▶ if($host =~ /\A\[/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /\A\[/) 0 0 0
158 0 ▶ _croak "IPv6 support requires IO::Socket::IP"
Branch: 1 branch
BranchTrueFalseTotal
unless $socket_class eq "IO::Socket::IP" 0 0 0
159 unless $socket_class eq "IO::Socket::IP";
160 }
161 0 ▶ my $bare_host = $host =~ /\A\[(.*)\]\z/s ? $1 : $host;
Branch: 1 branch
BranchTrueFalseTotal
$host =~ /\A\[(.*)\]\z/s ? : 0 0 0
162 0 ▶ $port >= 1 && $port <= 65535
Branch: 1 branch
BranchTrueFalseTotal
$port >= 1 and $port <= 65535 or &_croak("failed to connect to $host TCP port ${port}: " . 'invalid port number') 0 0 0
Condition0%0/3
$port >= 1 and $port <= 65535
!ll&&!rl&&r
000
MC/DC0%0/2
$port >= 1 and $port <= 65535
$port >= 1 $port <= 65535
Truth table0%0/3
A and B
A= $port >= 1B= $port <= 65535
ABresult
0X0
100
111
163 or _croak "failed to connect to $host TCP port $port: ".
164 "invalid port number";
165 return $socket_class->new(
166 PeerHost => $bare_host,
167 PeerPort => $port,
168 Proto => "tcp",
169 Timeout => $timeout,
170 0 ▶ ) || do {
Condition0%0/3
$socket_class->new('PeerHost', $bare_host, 'PeerPort', $port, 'Proto', 'tcp', 'Timeout', $timeout) || do { my $err = $@; chomp $err; $err =~ s/\AIO::Socket::[A-Z0-9]+: //; $err ne '' or $err = "$socket_class didn't say why"; &_croak("failed to connect to $host TCP port ${port}: $err") }
l!l&&r!l&&!r
000
MC/DC0%0/2
$socket_class->new('PeerHost', $bare_host, 'PeerPort', $port, 'Proto', 'tcp', 'Timeout', $timeout) || do { my $err = $@; chomp $err; $err =~ s/\AIO::Socket::[A-Z0-9]+: //; $err ne '' or $err = "$socket_class didn't say why"; &_croak("failed to connect to $host TCP port ${port}: $err") }
$socket_class->new('PeerHost', $bare_host, 'PeerPort', $port, 'Proto', 'tcp', 'Timeout', $timeout) do { my $err = $@; chomp $err; $err =~ s/\AIO::Socket::[A-Z0-9]+: //; $err ne '' or $err = "$socket_class didn't say why"; &_croak("failed to connect to $host TCP port ${port}: $err") }
Truth table0%0/3
A || B
A= $socket_class->new('PeerHost', $bare_host, 'PeerPort', $port, 'Proto', 'tcp', 'Timeout', $timeout)B= do { my $err = $@; chomp $err; $err =~ s/\AIO::Socket::[A-Z0-9]+: //; $err ne '' or $err = "$socket_class didn't say why"; &_croak("failed to connect to $host TCP port ${port}: $err") }
ABresult
1X1
011
000
171 0 my $err = $@;
172 0 chomp $err;
173 0 $err =~ s/\AIO::Socket::[A-Z0-9]+: //;
174 0 ▶ $err ne "" or $err = "$socket_class didn't say why";
Branch: 1 branch
BranchTrueFalseTotal
$err ne "" or $err = "$socket_class didn't say why" 0 0 0
175 0 _croak "failed to connect to $host TCP port $port: $err";
176 };
177 }
178  
179 0 ▶sub _check_timeout($$$) {
Subroutine _check_timeout: not called CC=5 SCAR=34.0
180 0 my($sock, $writing, $what) = @_;
181 0 vec(my $b = "", $sock->fileno, 1) = 1;
182 0 ▶ my $s = select($writing ? undef : $b, $writing ? $b : undef, $b,
Branch: 2 branches
BranchTrueFalseTotal
$writing ? : 0 0 0
$writing ? : 0 0 0
183 $timeout);
184 0 ▶ $s >= 1 or _croak "failed to $what: @{[$s ? $! : q(timed out)]}";
Branch: 2 branches
BranchTrueFalseTotal
$s >= 1 or &_croak(qq[failed to ${what}: @{[$s ? $! : "timed out"];}]) 0 0 0
$s ? : 0 0 0
185 }
186  
187 0 ▶sub _recv_more_response($$$) {
Subroutine _recv_more_response: not called CC=4 SCAR=30.0
188 0 my($conn, $rbufp, $eof_ok) = @_;
189 0 _check_timeout($conn, 0, "receive response");
190 0 my $n = $conn->sysread($$rbufp, $blksize, length($$rbufp));
191 0 ▶ defined $n or _croak "failed to receive response: $!";
Branch: 1 branch
BranchTrueFalseTotal
defined $n or &_croak("failed to receive response: $!") 0 0 0
192 0 ▶ $n != 0 and return 1;
Branch: 1 branch
BranchTrueFalseTotal
$n != 0 and return 1 0 0 0
193 0 ▶ $eof_ok or _croak "failed to receive response: unexpected EOF";
Branch: 1 branch
BranchTrueFalseTotal
$eof_ok or &_croak("failed to receive response: unexpected EOF") 0 0 0
194 0 return 0;
195 }
196  
197 0 ▶sub _recv_line($$) {
Subroutine _recv_line: not called CC=2 SCAR=17.9
198 0 my($conn, $rbufp) = @_;
199 0 while(1) {
200 0 ▶ $$rbufp =~ s/\A(.*?)\r?\n//s and return $1;
Branch: 1 branch
BranchTrueFalseTotal
$$rbufp =~ s/\A(.*?)\r?\n//s and return $1 0 0 0
201 0 _recv_more_response($conn, $rbufp, 0);
202 }
203 }
204  
205 4 my $token_rx = qr/[\!\#\$\%\&\'\*\+\-\.0-9A-Z\^\_\`a-z\|\~]+/;
206 4 my $quoted_string_rx = qr/\"(?>[\ -\[\]-\~\x80-\xff]+|\\[\ -\~\x80-\xff])*\"/;
207 4 my $lws_rx = qr/[\ \t]*/;
208  
209 0 ▶sub _recv_headers($$$) {
Subroutine _recv_headers: not called CC=7 SCAR=40.3
210 0 my($conn, $rbufp, $h) = @_;
211 0 my $curhdr;
212 0 while(1) {
213 0 my $l = _recv_line($conn, $rbufp);
214 0 ▶ if($l =~ /\A[ \t]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ /\A[ \t]/) 0 0 0
215 0 ▶ defined $curhdr
Branch: 1 branch
BranchTrueFalseTotal
defined $curhdr or &_croak("malformed response from server") 0 0 0
216 or _croak "malformed response from server";
217 0 $curhdr .= $l;
218 0 next;
219 }
220 0 ▶ if(defined $curhdr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $curhdr) 0 0 0
221 0 ▶ $curhdr =~ /\A($token_rx)$lws_rx:(.*)\z/so
Branch: 1 branch
BranchTrueFalseTotal
$curhdr =~ /\A($token_rx)$lws_rx:(.*)\z/so or &_croak("malformed response from server") 0 0 0
222 or _croak "malformed response from server";
223 0 my($hname, $value) = ($1, $2);
224 0 $hname = lc($hname);
225 $h->{$hname} = exists($h->{$hname}) ?
226 0 ▶ $h->{$hname}.",".$value : $value;
Branch: 1 branch
BranchTrueFalseTotal
exists $h->{$hname} ? : 0 0 0
227 }
228 0 ▶ $l eq "" and last;
Branch: 1 branch
BranchTrueFalseTotal
$l eq "" and last 0 0 0
229 0 $curhdr = $l;
230 }
231 }
232  
233 4 my $loaded_digest_md5;
234 0 ▶sub _http_get_one($) {
Subroutine _http_get_one: not called CC=29 SCAR=67.7
235 0 my($url) = @_;
236 0 my $params = _parse_http_url($url);
237 0 my $request = "GET @{[$params->{path_and_query}]} HTTP/1.1\r\n".
238 "Connection: close\r\n".
239 0 "Host: @{[$params->{host}]}".
240 0 ▶ "@{[$params->{port}==80?q():q(:).$params->{port}]}\r\n".
Branch: 1 branch
BranchTrueFalseTotal
$params->{'port'} == 80 ? : 0 0 0
241 "Accept-Encoding: identity\r\n".
242 "\r\n";
243 0 my $conn = _open_tcp($params->{host}, $params->{port});
244 {
245 0 my $len = length($request);
246 0 local $SIG{PIPE} = "IGNORE";
247 0 ▶ for(my $pos = 0; $pos != $len; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos != $len) 0 0 0
248 0 _check_timeout($conn, 1, "send request");
249 0 my $n = $conn->syswrite($request, $len-$pos, $pos);
250 0 ▶ defined $n or _croak "failed to send request: $!";
Branch: 1 branch
BranchTrueFalseTotal
defined $n or &_croak("failed to send request: $!") 0 0 0
251 0 $pos += $n;
252 }
253 0 $request = undef;
254 }
255 0 my $rbuf = "";
256 0 my %response;
257 0 while(1) {
258 0 my $l = _recv_line($conn, \$rbuf);
259 0 ▶ $l =~ /\A
Branch: 1 branch
BranchTrueFalseTotal
$l =~ m[\A HTTP/[0-9]+\.[0-9]+[\ \t]+ ([0-9]{3}\ [\ -\~\x80-\xff]*) \z]x or &_croak("malformed response from server") 0 0 0
260 HTTP\/[0-9]+\.[0-9]+[\ \t]+
261 ([0-9]{3}\ [\ -\~\x80-\xff]*)
262 \z/x
263 or _croak "malformed response from server";
264 0 my $status = $1;
265 0 $status =~ s/([^\ -\~])/sprintf("%%%02X", ord($1))/eg;
266 0 ▶ $status =~ /\A(?:[13]|200)/ or _croak $status;
Branch: 1 branch
BranchTrueFalseTotal
$status =~ /\A(?:[13]|200)/ or &_croak($status) 0 0 0
267 0 my %h;
268 0 _recv_headers($conn, \$rbuf, \%h);
269 0 ▶ if($status !~ /\A1/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status =~ /\A1/) 0 0 0
270 0 $response{status} = $status;
271 0 $response{headers} = \%h;
272 0 last;
273 }
274 }
275 0 ▶ return \%response unless $response{status} =~ /\A200/;
Branch: 1 branch
BranchTrueFalseTotal
unless $response{"status"} =~ /\A200/ 0 0 0
276 my $ce = lc(exists($response{headers}->{"content-encoding"}) ?
277 0 ▶ $response{headers}->{"content-encoding"} : "identity");
Branch: 1 branch
BranchTrueFalseTotal
exists $response{'headers'}{'content-encoding'} ? : 0 0 0
278 0 ▶ $ce =~ /\A${lws_rx}identity${lws_rx}\z/o
Branch: 1 branch
BranchTrueFalseTotal
$ce =~ /\A${lws_rx}identity$lws_rx\z/o or &_croak("unsupported Content-Encoding") 0 0 0
279 or _croak "unsupported Content-Encoding";
280 my $te = lc(exists($response{headers}->{"transfer-encoding"}) ?
281 0 ▶ $response{headers}->{"transfer-encoding"} : "identity");
Branch: 1 branch
BranchTrueFalseTotal
exists $response{'headers'}{'transfer-encoding'} ? : 0 0 0
282 0 ▶ if($te =~ /\A${lws_rx}chunked${lws_rx}\z/o) {
Branch: 3 branches
BranchTrueFalseTotal
if ($te =~ /\A${lws_rx}chunked$lws_rx\z/o) { } 0 0 0
elsif (not $te =~ /\A${lws_rx}identity$lws_rx\z/o) { } 0 0 0
elsif (exists $response{'headers'}{'content-length'}) { } 0 0 0
283 0 $response{body} = "";
284 0 while(1) {
285 0 ▶ _recv_line($conn, \$rbuf) =~ /\A
Branch: 1 branch
BranchTrueFalseTotal
&_recv_line($conn, \$rbuf) =~ /\A ([0-9A-Fa-f]+)$lws_rx (?> ;$lws_rx$token_rx$lws_rx (?>\=$lws_rx (?:$token_rx|$quoted_string_rx) $lws_rx )? )* \z/ox or &_croak("malformed chunk") 0 0 0
286 ([0-9A-Fa-f]+)$lws_rx
287 (?>
288 ;$lws_rx$token_rx$lws_rx
289 (?>\=$lws_rx
290 (?:$token_rx|$quoted_string_rx)
291 $lws_rx
292 )?
293 )*
294 \z/xo or _croak "malformed chunk";
295 0 my $csize = $1;
296 0 $csize =~ s/\A0+//;
297 0 ▶ last if $csize eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $csize eq "" 0 0 0
298 0 ▶ length($csize) <= 8 or _croak "excessive chunk length";
Branch: 1 branch
BranchTrueFalseTotal
length $csize <= 8 or &_croak("excessive chunk length") 0 0 0
299 0 $csize = hex($csize);
300 0 ▶ while(length($rbuf) < $csize) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rbuf < $csize) 0 0 0
301 0 _recv_more_response($conn, \$rbuf, 0);
302 }
303 0 $response{body} .= substr($rbuf, 0, $csize, "");
304 0 ▶ _recv_line($conn, \$rbuf) eq ""
Branch: 1 branch
BranchTrueFalseTotal
&_recv_line($conn, \$rbuf) eq "" or &_croak("malformed chunk") 0 0 0
305 or _croak "malformed chunk";
306 }
307 0 _recv_headers($conn, \$rbuf, $response{headers});
308 } elsif($te !~ /\A${lws_rx}identity${lws_rx}\z/o) {
309 0 _croak "unsupported Transfer-Encoding";
310 } elsif(exists $response{headers}->{"content-length"}) {
311 0 ▶ $response{headers}->{"content-length"}
Branch: 1 branch
BranchTrueFalseTotal
$response{"headers"}{"content-length"} =~ /\A$lws_rx([0-9]+)$lws_rx\z/o or &_croak("malformed Content-Length") 0 0 0
312 =~ /\A$lws_rx([0-9]+)$lws_rx\z/o
313 or _croak "malformed Content-Length";
314 0 my $body_length = $1;
315 0 ▶ $body_length < 0xffffffff or _croak "excessive Content-Length";
Branch: 1 branch
BranchTrueFalseTotal
$body_length < 4294967295 or &_croak("excessive Content-Length") 0 0 0
316 0 $response{body} = $rbuf;
317 0 ▶ while(length($response{body}) < $body_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $response{"body"} < $body_length) 0 0 0
318 0 _recv_more_response($conn, \$response{body}, 0);
319 }
320 substr $response{body}, $body_length,
321 0 length($response{body})-$body_length, "";
322 } else {
323 0 $response{body} = $rbuf;
324 0 ▶ 1 while _recv_more_response($conn, \$response{body}, 1);
Branch: 1 branch
BranchTrueFalseTotal
&_recv_more_response($conn, \$response{"body"}, 1) and do { '???' } 0 0 0
325 }
326 0 $conn = undef;
327 0 ▶ if(exists $response{headers}->{"content-md5"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $response{"headers"}{"content-md5"}) 0 0 0
328 0 ▶ $response{headers}->{"content-md5"}
Branch: 1 branch
BranchTrueFalseTotal
$response{"headers"}{"content-md5"} =~ m[\A$lws_rx([A-Za-z0-9\+/]{21}[AQgw])\=\=$lws_rx\z]o or &_croak("malformed Content-MD5") 0 0 0
329 =~ /\A$lws_rx([A-Za-z0-9\+\/]{21}[AQgw])\=\=$lws_rx\z/o
330 or _croak "malformed Content-MD5";
331 0 my $digest = $1;
332 0 ▶ unless(defined $loaded_digest_md5) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $loaded_digest_md5) 0 0 0
333 0 ▶ $loaded_digest_md5 = eval { local $SIG{__DIE__};
Branch: 1 branch
BranchTrueFalseTotal
eval { do { local $SIG{'__DIE__'}; require Digest::MD5; 'Digest::MD5'->VERSION(2.17); 1 } } ? : 0 0 0
334 0 require Digest::MD5;
335 0 Digest::MD5->VERSION(2.17);
336 0 1;
337 } ? 1 : 0;
338 }
339 0 ▶ if($loaded_digest_md5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loaded_digest_md5) 0 0 0
340 0 ▶ Digest::MD5::md5_base64($response{body}) eq $digest
Branch: 1 branch
BranchTrueFalseTotal
Digest::MD5::md5_base64($response{"body"}) eq $digest or &_croak("Content-MD5 mismatch") 0 0 0
341 or _croak "Content-MD5 mismatch";
342 }
343 }
344 0 return \%response;
345 }
346  
347 1 ▶sub http_get($) {
Subroutine http_get: called CC=7 SCAR=38.3
Pod documented
348 1 my($url) = @_;
349 1 _downgrade($url);
350 0 my %seen;
351 0 ▶ for(my $redir_limit = 6; $redir_limit--; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($redir_limit--) 0 0 0
352 0 my $response = _http_get_one($url);
353 0 ▶ $response->{status} =~ /\A200/ and return $response->{body};
Branch: 1 branch
BranchTrueFalseTotal
$response->{"status"} =~ /\A200/ and return $response->{"body"} 0 0 0
354 0 $seen{$url} = undef;
355 0 my $loc = $response->{headers}->{location};
356 0 ▶ defined $loc or _croak "redirection with no target";
Branch: 1 branch
BranchTrueFalseTotal
defined $loc or &_croak("redirection with no target") 0 0 0
357 0 ▶ if($loc =~ /\A$lws_rx($http_url_rx)$lws_rx\z/o) {
Branch: 2 branches
BranchTrueFalseTotal
if ($loc =~ /\A$lws_rx($http_url_rx)$lws_rx\z/o) { } 0 0 0
elsif ($loc =~ /\A$lws_rx($path_and_query_rx)$lws_rx\z/o) { } 0 0 0
358 0 $url = $1;
359 } elsif($loc =~ /\A$lws_rx($path_and_query_rx)$lws_rx\z/o) {
360 # Illegal, but common and easy to handle sanely.
361 0 my $pathquery = $1;
362 0 $url =~ s/\A($http_prefix_rx[^\/]*).*\z/$1$pathquery/so;
363 } else {
364 0 _croak "redirection to malformed target";
365 }
366 0 ▶ exists $seen{$url} and _croak "redirection loop";
Branch: 1 branch
BranchTrueFalseTotal
exists $seen{$url} and &_croak("redirection loop") 0 0 0
367 }
368 0 _croak "too many redirections";
369 }
370  
371 =back
372  
373 =head1 BUGS
374  
375 IPv6 support is largely untested. Reports of experiences with it would
376 be appreciated.
377  
378 =head1 SEE ALSO
379  
380 L<HTTP::Tiny>,
381 L<IO::Socket::IP>,
382 L<Net::FTP::Tiny>
383  
384 =head1 AUTHOR
385  
386 Andrew Main (Zefram) <zefram@fysh.org>
387  
388 =head1 COPYRIGHT
389  
390 Copyright (C) 2012, 2017 Andrew Main (Zefram) <zefram@fysh.org>
391  
392 =head1 LICENSE
393  
394 This module is free software; you can redistribute it and/or modify it
395 under the same terms as Perl itself.
396  
397 =cut
398  
399 4 1;
↑ Summary