Any/AnyEvent.pm

filter: Statementstmt 6.6% 7 / 106 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 49 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 25.0% 2 / 8 Podpod 0.0% 0 / 2 totaltotal 3.3% 9 / 270 CC 58 SCAR 56.1
CC
58
Cov
3%
do_http
506.0
__ANON__
240.0
__ANON__
182.0
CRAP 272.7
« Any/Curl.pm ↑ Summary Any.pm »
1 package HTTP::Any::AnyEvent;
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 sub do_http {
8 0 ▶ my ($http_request, $url, $opt, $cb) = @_;
Subroutine do_http: not called CC=22 SCAR=62.3
Pod undocumented
9  
10 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
11  
12 0 my %headers = ();
13 0 ▶ %headers = %{$$opt{headers}} if $$opt{headers};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"headers"} 0 0 0
14  
15 0 ▶ $headers{"user-agent"} = $$opt{agent} if $$opt{agent};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"agent"} 0 0 0
16 0 ▶ $headers{"referer"} = $$opt{referer} if $$opt{referer};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"referer"} 0 0 0
17  
18 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
19 0 $headers{'Accept-Encoding'} = 'gzip, deflate';
20 0 require Compress::Raw::Zlib;
21 }
22  
23 0 my %args = ();
24 0 ▶ $args{headers} = \%headers if keys %headers;
Branch: 1 branch
BranchTrueFalseTotal
if keys %headers 0 0 0
25 0 ▶ $args{timeout} = $$opt{timeout} if $$opt{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"timeout"} 0 0 0
26  
27 0 ▶ if (defined $$opt{max_redirect}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt->{'max_redirect'}) { } 0 0 0
28 0 $args{recurse} = $$opt{max_redirect};
29 } else {
30 0 $args{recurse} = 7;
31 }
32  
33 0 ▶ if (my $proxy = $$opt{proxy}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $proxy = $opt->{"proxy"}) 0 0 0
34 0 ▶ if ($proxy =~ m/^socks/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($proxy =~ /^socks/) { } 0 0 0
elsif ($proxy =~ m[^(\w+://)?(.+):(\d+)$]) { } 0 0 0
35 0 $proxy =~ s!^socks://!socks5://!;
36 0 $args{socks} = $proxy;
37 } elsif ($proxy =~ m!^(\w+://)?(.+):(\d+)$!) {
38 0 ▶ if ($1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1) { } 0 0 0
39 0 $args{proxy} = [$2, $3, $1];
40 } else {
41 0 $args{proxy} = [$2, $3];
42 }
43 }
44 }
45  
46 0 ▶ if ($$opt{cookie}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opt->{'cookie'}) { } 0 0 0
elsif (defined $opt->{'cookie'}) { } 0 0 0
47 0 $args{cookie_jar} = $$opt{cookie};
48 } elsif (defined $$opt{cookie}) {
49 0 $args{cookie_jar} = {};
50 }
51  
52 0 ▶ $args{persistent} = $$opt{persistent} if exists $$opt{persistent};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt->{"persistent"} 0 0 0
53  
54 0 my $max_size = $$opt{max_size};
55 0 my $on_header = $$opt{on_header};
56 0 my $on_body = $$opt{on_body};
57  
58 0 my $body_from_on_body_length = 0;
59 0 my @body_from_on_body = ();
60  
61 0 ▶ if ($max_size or $on_header or $on_body) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_size or $on_header or $on_body) 0 0 0
Condition0%0/6
$max_size or $on_header or $on_body
l!l&&r!l&&!r
000
$max_size or $on_header
l!l&&r!l&&!r
000
MC/DC0%0/3
$max_size or $on_header or $on_body
$max_size $on_header $on_body
Truth table0%0/4
A or B or C
A= $max_sizeB= $on_headerC= $on_body
ABCresult
1XX1
01X1
0011
0000
62 0 my $headers_got = 0;
63 0 my $content_encoding;
64 my $inflate;
65  
66 $args{on_header} = sub {
67 0 ▶ my ($headers) = @_;
Subroutine __ANON__: not called CC=8 SCAR=42.8
68 0 $body_from_on_body_length = 0;
69 0 @body_from_on_body = ();
70 0 $headers_got = 1;
71 0 my ($is_success, $status, $h, $redirects) = headers($headers);
72 0 $content_encoding = $$h{'content-encoding'};
73 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
74 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
75 0 $inflate = Compress::Raw::Zlib::Inflate->new();
76 } elsif ($content_encoding eq 'gzip') {
77 0 $inflate = Compress::Raw::Zlib::Inflate->new(-WindowBits => Compress::Raw::Zlib::WANT_GZIP());
78 }
79 }
80  
81  
82 0 ▶ if ($on_header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_header) 0 0 0
83 0 ▶ $on_header->($is_success, $h, $redirects) or return;
Branch: 1 branch
BranchTrueFalseTotal
&$on_header($is_success, $h, $redirects) or return 0 0 0
84 }
85 0 return 1;
86 0 };
87  
88 $args{on_body} = sub {
89 0 ▶ my ($partial_body, $headers) = @_;
Subroutine __ANON__: not called CC=13 SCAR=52.0
90 0 $body_from_on_body_length += length $partial_body;
91 0 ▶ push @body_from_on_body, $partial_body unless $on_body;
Branch: 1 branch
BranchTrueFalseTotal
unless $on_body 0 0 0
92 0 ▶ if ($headers_got and $max_size and $body_from_on_body_length > $max_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headers_got and $max_size and $body_from_on_body_length > $max_size) 0 0 0
Condition0%0/6
$headers_got and $max_size and $body_from_on_body_length > $max_size
!ll&&!rl&&r
000
$headers_got and $max_size
!ll&&!rl&&r
000
MC/DC0%0/3
$headers_got and $max_size and $body_from_on_body_length > $max_size
$headers_got $max_size $body_from_on_body_length > $max_size
Truth table0%0/4
A and B and C
A= $headers_gotB= $max_sizeC= $body_from_on_body_length > $max_size
ABCresult
0XX0
10X0
1100
1111
93 0 return;
94 }
95 0 ▶ if ($headers_got and $on_body) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headers_got and $on_body) 0 0 0
Condition0%0/3
$headers_got and $on_body
!ll&&!rl&&r
000
MC/DC0%0/2
$headers_got and $on_body
$headers_got $on_body
Truth table0%0/3
A and B
A= $headers_gotB= $on_body
ABresult
0X0
100
111
96 0 ▶ if ($inflate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inflate) { } 0 0 0
97 0 my $status = $inflate->inflate($partial_body, my $output);
98 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
99 0 ▶ if ($output) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output) 0 0 0
100 0 ▶ $on_body->($output) or return;
Branch: 1 branch
BranchTrueFalseTotal
&$on_body($output) or return 0 0 0
101 }
102 } else {
103 0 ▶ $on_body->($partial_body) or return;
Branch: 1 branch
BranchTrueFalseTotal
&$on_body($partial_body) or return 0 0 0
104 }
105 }
106 0 return 1;
107 0 };
108 }
109  
110 0 ▶ if ($method eq "POST") {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "POST") 0 0 0
111 0 ▶ $args{headers}{"Content-Type"} ||= "application/x-www-form-urlencoded";
Condition0%0/2
$args{"headers"}{"Content-Type"} ||= "application/x-www-form-urlencoded"
l!l
00
MC/DC0%0/1
$args{"headers"}{"Content-Type"} ||= "application/x-www-form-urlencoded"
$args{"headers"}{"Content-Type"}
Truth table0%0/2
A
A= $args{"headers"}{"Content-Type"}
Aresult
11
00
112 0 $args{body} = $$opt{body};
113 }
114  
115 $http_request->(
116 $method => $url,
117 %args,
118 sub {
119 0 ▶ my ($body, $headers) = @_;
Subroutine __ANON__: not called CC=15 SCAR=54.8
120 0 my ($is_success, $status, $h, $redirects) = headers($headers);
121  
122 0 ▶ if (not $body) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($body) 0 0 0
123 0 ▶ if ($status >= 590) {
Branch: 2 branches
BranchTrueFalseTotal
if ($status >= 590) { } 0 0 0
elsif (@body_from_on_body) { } 0 0 0
124 0 $is_success = 0;
125 0 $body = "";
126 0 ▶ if ($status == 598) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 598) 0 0 0
127 0 $$h{"Reason"} = "MaxSize";
128 }
129 0 $$h{"Status"} = $status = 599;
130 0 delete $$h{"Orig$_"} foreach qw(Status Reason);
131 } elsif (@body_from_on_body) {
132 0 $body = join "", @body_from_on_body;
133 }
134 }
135  
136 0 $$h{Protocol} = "HTTP/" . $$h{HTTPVersion};
137  
138 0 my $content_encoding = $$h{'content-encoding'};
139 0 ▶ if ($body and ($$opt{compressed} or $$opt{gzip}) and $content_encoding) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body and $opt->{'compressed'} || $opt->{'gzip'} and $content_encoding) { } 0 0 0
Condition0%0/9
$body and $opt->{"compressed"} || $opt->{"gzip"} and $content_encoding
!ll&&!rl&&r
000
$body and $opt->{"compressed"} || $opt->{"gzip"}
!ll&&!rl&&r
000
$opt->{"compressed"} or $opt->{"gzip"}
l!l&&r!l&&!r
000
MC/DC0%0/4
$body and $opt->{"compressed"} || $opt->{"gzip"} and $content_encoding
$body $opt->{"compressed"} $opt->{"gzip"} $content_encoding
Truth table0%0/6
A and B or C and D
A= $bodyB= $opt->{"compressed"}C= $opt->{"gzip"}D= $content_encoding
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
140 0 ▶ if ($content_encoding eq 'deflate' or $content_encoding eq 'gzip') {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_encoding eq "deflate" or $content_encoding eq "gzip") 0 0 0
Condition0%0/3
$content_encoding eq "deflate" or $content_encoding eq "gzip"
l!l&&r!l&&!r
000
MC/DC0%0/2
$content_encoding eq "deflate" or $content_encoding eq "gzip"
$content_encoding eq "deflate" $content_encoding eq "gzip"
Truth table0%0/3
A or B
A= $content_encoding eq "deflate"B= $content_encoding eq "gzip"
ABresult
1X1
011
000
141 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
142 0 my $status = $inflate->inflate($body, my $output);
143 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
144 0 $cb->($is_success, $output, $h, $redirects);
145 }
146 } else {
147 0 $cb->($is_success, $body, $h, $redirects);
148 }
149  
150 }
151 0 );
152 }
153  
154  
155  
156 sub headers {
157 0 ▶ my ($headers) = @_;
Subroutine headers: not called CC=3 SCAR=24.8
Pod undocumented
158  
159 0 my $status = $$headers{Status};
160 0 ▶ my $is_success = ($status >= 200 and $status < 300) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$status >= 200 && $status < 300 ? : 0 0 0
Condition0%0/3
$status >= 200 and $status < 300
!ll&&!rl&&r
000
MC/DC0%0/2
$status >= 200 and $status < 300
$status >= 200 $status < 300
Truth table0%0/3
A and B
A= $status >= 200B= $status < 300
ABresult
0X0
100
111
161 0 my ($h, @hr) = _headers($headers);
162  
163 0 return $is_success, $status, $h, \@hr;
164 }
165  
166  
167  
168 sub _headers {
169 0 ▶ my ($h) = @_;
Subroutine _headers: not called CC=2 SCAR=17.9
170 0 my %h = map { $_ => $$h{$_} } grep { $_ ne 'Redirect' } keys %$h;
171 0 ▶ if (my $r = $$h{'Redirect'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $h->{'Redirect'}) { } 0 0 0
172 0 return \%h, _headers($$r[1]);
173 } else {
174 0 return \%h;
175 }
176 }
177  
178  
179 1 1;
« Any/Curl.pm ↑ Summary Any.pm »