Any/LWP.pm

filter: Statementstmt 6.6% 7 / 106 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 28.5% 2 / 7 Podpod 0.0% 0 / 1 totaltotal 3.6% 9 / 249 CC 54 SCAR 66.4
CC
54
Cov
3%
_do_http
1260.0
__ANON__
110.0
__ANON__
72.0
CRAP 763.8
↑ Summary Any/Curl.pm »
1 package HTTP::Any::LWP;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6  
7  
8 sub _do_http {
9 0 ▶ my ($ua, $url, $opt) = @_;
Subroutine _do_http: not called CC=35 SCAR=71.4
10  
11 0 ▶ $ua->agent($$opt{agent}) if $$opt{agent};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"agent"} 0 0 0
12 0 ▶ $ua->timeout($$opt{timeout}) if $$opt{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"timeout"} 0 0 0
13 0 ▶ $ua->max_size($$opt{max_size}) if $$opt{max_size};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"max_size"} 0 0 0
14  
15 0 ▶ if (defined $$opt{max_redirect}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt->{'max_redirect'}) { } 0 0 0
16 0 $ua->max_redirect($$opt{max_redirect});
17 } else {
18 0 $ua->max_redirect(7);
19 }
20  
21 0 $ua->parse_head(0);
22  
23 0 ▶ if ($$opt{cookie}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opt->{'cookie'}) { } 0 0 0
elsif (defined $opt->{'cookie'}) { } 0 0 0
24 0 $ua->cookie_jar($$opt{cookie});
25 } elsif (defined $$opt{cookie}) {
26 0 $ua->cookie_jar({});
27 }
28  
29 0 ▶ if (my $proxy = $$opt{proxy}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $proxy = $opt->{"proxy"}) 0 0 0
30 0 ▶ unless ($proxy =~ m!^\w+://!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proxy =~ m[^\w+://]) { } 0 0 0
31 0 $proxy = "http://$proxy"
32 } else {
33 0 $proxy =~ s!^socks5://!socks://!;
34 }
35 0 $ua->proxy(['http', 'https'] => $proxy);
36 }
37  
38 0 ▶ my $method = $$opt{method} || "GET";
Condition0%0/2
$opt->{'method'} || 'GET'
l!l
00
MC/DC0%0/1
$opt->{'method'} || 'GET'
$opt->{'method'}
Truth table0%0/2
A
A= $opt->{'method'}
Aresult
11
00
39  
40 0 my $req = HTTP::Request->new($method => $url);
41  
42 0 ▶ if ($$opt{headers}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"headers"}) 0 0 0
43 0 foreach (keys %{$$opt{headers}}) {
44 0 $req->header($_ => $$opt{headers}{$_});
45 }
46 }
47  
48 0 ▶ $req->referer($$opt{referer}) if $$opt{referer};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"referer"} 0 0 0
49  
50 0 ▶ if ($$opt{compressed} or $$opt{gzip}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"compressed"} or $opt->{"gzip"}) 0 0 0
Condition0%0/3
$opt->{"compressed"} or $opt->{"gzip"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt->{"compressed"} or $opt->{"gzip"}
$opt->{"compressed"} $opt->{"gzip"}
Truth table0%0/3
A or B
A= $opt->{"compressed"}B= $opt->{"gzip"}
ABresult
1X1
011
000
51 0 $req->header('Accept-Encoding', 'gzip, deflate');
52 0 require Compress::Raw::Zlib;
53 }
54  
55 0 ▶ if ($method eq "POST") {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "POST") 0 0 0
56 0 ▶ unless ($$opt{headers}{"Content-Type"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt->{"headers"}{"Content-Type"}) 0 0 0
57 0 $req->content_type("application/x-www-form-urlencoded");
58 }
59 0 $req->content($$opt{body});
60 }
61  
62  
63 0 my $on_header = $$opt{on_header};
64 0 my $on_body = $$opt{on_body};
65  
66 0 ▶ if ($on_header or $on_body) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_header or $on_body) 0 0 0
Condition0%0/3
$on_header or $on_body
l!l&&r!l&&!r
000
MC/DC0%0/2
$on_header or $on_body
$on_header $on_body
Truth table0%0/3
A or B
A= $on_headerB= $on_body
ABresult
1X1
011
000
67 0 my $headers_got = 0;
68 0 my $content_encoding;
69 my $inflate;
70  
71 0 my @hrs = ();
72 $ua->set_my_handler( response_header => sub {
73 0 ▶ my($res, $ua, undef) = @_;
Subroutine __ANON__: not called CC=10 SCAR=47.0
74 0 my ($h) = _headers($res);
75 0 push @hrs, $h;
76 0 ▶ unless ($$h{Status} == 301) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($h->{"Status"} == 301) 0 0 0
77 0 my ($h, @hr) = reverse @hrs;
78 0 $headers_got = 1;
79 0 $content_encoding = $$h{'content-encoding'};
80 0 ▶ if (($$opt{compressed} or $$opt{gzip}) and $content_encoding) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"compressed"} || $opt->{"gzip"} and $content_encoding) 0 0 0
Condition0%0/6
$opt->{"compressed"} || $opt->{"gzip"} and $content_encoding
!ll&&!rl&&r
000
$opt->{"compressed"} or $opt->{"gzip"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$opt->{"compressed"} || $opt->{"gzip"} and $content_encoding
$opt->{"compressed"} $opt->{"gzip"} $content_encoding
Truth table0%0/5
A or B and C
A= $opt->{"compressed"}B= $opt->{"gzip"}C= $content_encoding
ABCresult
00X0
1X00
0100
1X11
0111
81 0 ▶ if ($content_encoding eq 'deflate') {
Branch: 2 branches
BranchTrueFalseTotal
if ($content_encoding eq 'deflate') { } 0 0 0
elsif ($content_encoding eq 'gzip') { } 0 0 0
82 0 $inflate = Compress::Raw::Zlib::Inflate->new();
83 } elsif ($content_encoding eq 'gzip') {
84 0 $inflate = Compress::Raw::Zlib::Inflate->new(-WindowBits => Compress::Raw::Zlib::WANT_GZIP());
85 }
86 }
87 0 ▶ if ($on_header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_header) 0 0 0
88 0 ▶ $on_header->($res->is_success || 0, $h, \@hr) or return;
Branch: 1 branch
BranchTrueFalseTotal
&$on_header($res->is_success || 0, $h, \@hr) or return 0 0 0
Condition0%0/2
$res->is_success || 0
l!l
00
MC/DC0%0/1
$res->is_success || 0
$res->is_success
Truth table0%0/2
A
A= $res->is_success
Aresult
11
00
89 }
90 }
91 0 return 1;
92 0 });
93  
94 0 ▶ if ($on_body) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_body) 0 0 0
95 $ua->set_my_handler( response_data => sub {
96 0 ▶ my($res, $ua, undef, $data) = @_;
Subroutine __ANON__: not called CC=8 SCAR=42.8
97 0 ▶ if ($headers_got) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headers_got) 0 0 0
98 0 ▶ if ($inflate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inflate) { } 0 0 0
99 0 my $status = $inflate->inflate($data, my $output);
100 0 ▶ $status == Compress::Raw::Zlib::Z_OK() or $status == Compress::Raw::Zlib::Z_STREAM_END() or warn "inflation failed: $status\n";
Branch: 1 branch
BranchTrueFalseTotal
$status == Compress::Raw::Zlib::Z_OK() or $status == Compress::Raw::Zlib::Z_STREAM_END() or warn "inflation failed: $status\n" 0 0 0
Condition0%0/3
$status == Compress::Raw::Zlib::Z_OK() or $status == Compress::Raw::Zlib::Z_STREAM_END()
l!l&&r!l&&!r
000
MC/DC0%0/2
$status == Compress::Raw::Zlib::Z_OK() or $status == Compress::Raw::Zlib::Z_STREAM_END()
$status == Compress::Raw::Zlib::Z_OK() $status == Compress::Raw::Zlib::Z_STREAM_END()
Truth table0%0/3
A or B
A= $status == Compress::Raw::Zlib::Z_OK()B= $status == Compress::Raw::Zlib::Z_STREAM_END()
ABresult
1X1
011
000
101 0 ▶ if ($output) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output) 0 0 0
102 0 ▶ $on_body->($output) or return;
Branch: 1 branch
BranchTrueFalseTotal
&$on_body($output) or return 0 0 0
103 }
104 } else {
105 0 ▶ $on_body->($data) or return;
Branch: 1 branch
BranchTrueFalseTotal
&$on_body($data) or return 0 0 0
106 }
107 0 $res->content("");
108 }
109 0 return 1;
110 0 });
111 }
112  
113 }
114  
115  
116  
117 0 my $res = $ua->request($req);
118  
119 0 my ($h, @hr) = _headers($res);
120  
121 0 ▶ my $is_success = $res->is_success || 0;
Condition0%0/2
$res->is_success || 0
l!l
00
MC/DC0%0/1
$res->is_success || 0
$res->is_success
Truth table0%0/2
A
A= $res->is_success
Aresult
11
00
122  
123 0 ▶ if ($$h{'client-aborted'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h->{"client-aborted"}) 0 0 0
124 0 ▶ if ($$h{'client-aborted'} eq 'max_size') {
Branch: 2 branches
BranchTrueFalseTotal
if ($h->{'client-aborted'} eq 'max_size') { } 0 0 0
elsif ($h->{'client-aborted'} eq 'die') { } 0 0 0
125 0 $$h{Reason} = 'MaxSize';
126 } elsif ($$h{'client-aborted'} eq 'die') {
127 0 ▶ if ($$h{'x-died'} =~ m/timeout/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h->{"x-died"} =~ /timeout/) 0 0 0
128 0 $$h{Reason} = 'Timeout';
129 }
130 }
131 0 $$h{Status} = 599;
132 0 return (0, "", $h, \@hr);
133 }
134  
135 0 ▶ if ($on_body) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_body) { } 0 0 0
136 0 return ($is_success, undef, $h, \@hr);
137 } else {
138 0 my $content_encoding = $$h{'content-encoding'};
139 0 ▶ if ($res->content and ($$opt{compressed} or $$opt{gzip}) and $content_encoding and ($content_encoding eq 'deflate' or $content_encoding eq 'gzip')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->content and $opt->{'compressed'} || $opt->{'gzip'} and $content_encoding and $content_encoding eq 'deflate' || $content_encoding eq 'gzip') { } 0 0 0
Condition0%0/15
$res->content and $opt->{"compressed"} || $opt->{"gzip"} and $content_encoding and $content_encoding eq "deflate" || $content_encoding eq "gzip"
!ll&&!rl&&r
000
$res->content and $opt->{"compressed"} || $opt->{"gzip"} and $content_encoding
!ll&&!rl&&r
000
$res->content and $opt->{"compressed"} || $opt->{"gzip"}
!ll&&!rl&&r
000
$opt->{"compressed"} or $opt->{"gzip"}
l!l&&r!l&&!r
000
$content_encoding eq "deflate" or $content_encoding eq "gzip"
l!l&&r!l&&!r
000
MC/DC0%0/6
$res->content and $opt->{"compressed"} || $opt->{"gzip"} and $content_encoding and $content_encoding eq "deflate" || $content_encoding eq "gzip"
$res->content $opt->{"compressed"} $opt->{"gzip"} $content_encoding $content_encoding eq "deflate" $content_encoding eq "gzip"
Truth table0%0/10
A and B or C and D and E or F
A= $res->contentB= $opt->{"compressed"}C= $opt->{"gzip"}D= $content_encodingE= $content_encoding eq "deflate"F= $content_encoding eq "gzip"
ABCDEFresult
0XXXXX0
100XXX0
11X0XX0
1010XX0
11X1000
1011000
11X11X1
11X1011
10111X1
1011011
140 0 ▶ my $inflate = Compress::Raw::Zlib::Inflate->new($content_encoding eq 'gzip' ? (-WindowBits => Compress::Raw::Zlib::WANT_GZIP()) : ());
Branch: 1 branch
BranchTrueFalseTotal
$content_encoding eq 'gzip' ? : 0 0 0
141 0 my $status = $inflate->inflate($res->content, my $output);
142 0 ▶ $status == Compress::Raw::Zlib::Z_OK() or $status == Compress::Raw::Zlib::Z_STREAM_END() or warn "inflation failed: $status\n";
Branch: 1 branch
BranchTrueFalseTotal
$status == Compress::Raw::Zlib::Z_OK() or $status == Compress::Raw::Zlib::Z_STREAM_END() or warn "inflation failed: $status\n" 0 0 0
Condition0%0/3
$status == Compress::Raw::Zlib::Z_OK() or $status == Compress::Raw::Zlib::Z_STREAM_END()
l!l&&r!l&&!r
000
MC/DC0%0/2
$status == Compress::Raw::Zlib::Z_OK() or $status == Compress::Raw::Zlib::Z_STREAM_END()
$status == Compress::Raw::Zlib::Z_OK() $status == Compress::Raw::Zlib::Z_STREAM_END()
Truth table0%0/3
A or B
A= $status == Compress::Raw::Zlib::Z_OK()B= $status == Compress::Raw::Zlib::Z_STREAM_END()
ABresult
1X1
011
000
143 0 return ($is_success, $output, $h, \@hr);
144 } else {
145 0 return ($is_success, $res->content, $h, \@hr);
146 }
147 }
148 }
149  
150  
151 sub do_http {
152 0 ▶ my ($ua, $url, $opt, $cb) = @_;
Subroutine do_http: not called CC=2 SCAR=17.9
Pod undocumented
153 0 ▶ if (@_ == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 4) { } 0 0 0
154 0 $cb->(_do_http($ua, $url, $opt));
155 } else {
156 0 goto &_do_http;
157 }
158 }
159  
160 sub _headers {
161 0 ▶ my ($res) = @_;
Subroutine _headers: not called CC=3 SCAR=24.8
162 0 my %h = %{$res->headers};
163 0 ▶ my %r = map { my $v = $h{$_}; $_ => ref $v eq "ARRAY" ? join(",", @$v) : $v } keys %h;
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 0 0 0
164 0 $r{Protocol} = $res->protocol;
165 0 $r{Status} = $res->code;
166 0 $r{Reason} = $res->message;
167 0 $r{URL} = $res->base->as_string;
168 0 ▶ if (my $prev = $res->previous) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $prev = $res->previous) { } 0 0 0
169 0 return \%r, _headers($prev);
170 } else {
171 0 return \%r;
172 }
173 }
174  
175  
176 1 1;
↑ Summary Any/Curl.pm »