blib/lib/AnyEvent/UserAgent.pm

filter: Statementstmt 89.6% 113 / 126 Branchbran 82.5% 33 / 40 Conditioncond 52.7% 19 / 36 MC/DCmcdc 11.7% 2 / 17 Subroutinesub 79.3% 23 / 29 Podpod 100% 8 / 8 totaltotal 77.3% 198 / 256 CC 34 SCAR 22.0
CC
34
Cov
82%
_response
18.3
_redirect
13.5
_request
9.2
CRAP 9.0
↑ Summary
1 package AnyEvent::UserAgent;
2  
3 # This module based on original AnyEvent::HTTP::Simple module by punytan
4 # (punytan@gmail.com): http://github.com/punytan/AnyEvent-HTTP-Simple
5  
6 8 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 ▶use AnyEvent::HTTP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use HTTP::Cookies ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use HTTP::Request::Common ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use HTTP::Response ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 8 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 8 our $VERSION = '0.09';
17  
18  
19 10 ▶has agent => (is => 'rw', default => sub { $AnyEvent::HTTP::USERAGENT . ' AnyEvent-UserAgent/' . $VERSION });
Subroutine __ANON__: called CC=1 SCAR=0.0
20  
21 10 ▶has cookie_jar => (is => 'rw', default => sub { HTTP::Cookies->new });
Subroutine __ANON__: called CC=1 SCAR=0.0
22  
23 9 ▶has max_redirects => (is => 'rw', default => sub { 5 });
Subroutine __ANON__: called CC=1 SCAR=0.0
24  
25 9 ▶has inactivity_timeout => (is => 'rw', default => sub { 20 });
Subroutine __ANON__: called CC=1 SCAR=0.0
26  
27 9 ▶has request_timeout => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
28  
29 8 my @OPTIONS = qw(
30 proxy tls_ctx session timeout on_prepare tcp_connect on_header on_body
31 want_body_handle persistent keepalive handle_params
32 );
33  
34 8 for my $o (@OPTIONS) {
35 96 has $o => (is => 'rw', default => undef);
36 }
37  
38 12 ▶sub request {
Subroutine request: called CC=1 SCAR=0.0
Pod documented
39 12 my $cb = pop();
40 12 my ($self, $req, %opts) = @_;
41  
42 12 ▶ $self->_request($req, \%opts, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
43 12 $self->_response($req, @_, $cb);
44 12 });
45 }
46  
47 10 ▶sub get { _do_request(\&HTTP::Request::Common::GET => @_) }
Subroutine get: called CC=1 SCAR=0.0
Pod documented
48 0 ▶sub head { _do_request(\&HTTP::Request::Common::HEAD => @_) }
Subroutine head: not called CC=1 SCAR=6.9
Pod documented
49 0 ▶sub put { _do_request(\&HTTP::Request::Common::PUT => @_) }
Subroutine put: not called CC=1 SCAR=6.9
Pod documented
50 0 ▶sub delete { _do_request(\&HTTP::Request::Common::DELETE => @_) }
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
51 0 ▶sub post { _do_request(\&HTTP::Request::Common::POST => @_) }
Subroutine post: not called CC=1 SCAR=6.9
Pod documented
52 0 ▶sub patch { _do_request(\&HTTP::Request::Common::PATCH => @_) }
Subroutine patch: not called CC=1 SCAR=6.9
Pod documented
53 0 ▶sub options { _do_request(\&HTTP::Request::Common::OPTIONS => @_) }
Subroutine options: not called CC=1 SCAR=6.9
Pod documented
54  
55 10 ▶sub _do_request {
Subroutine _do_request: called CC=1 SCAR=0.0
56 10 my $cb = pop();
57 10 my $meth = shift();
58 10 my $self = shift();
59  
60 10 $self->request($meth->(@_), $cb);
61 }
62  
63 13 ▶sub _request {
Subroutine _request: called CC=9 SCAR=22.2
64 13 my ($self, $req, $opts, $cb) = @_;
65  
66 13 my $uri = $req->uri;
67 13 my $hdrs = $req->headers;
68  
69 13 ▶ unless ($hdrs->user_agent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hdrs->user_agent) 12 1 13
70 12 $hdrs->user_agent($self->agent);
71 }
72  
73 13 ▶ if ($uri->can('userinfo') && $uri->userinfo && !$hdrs->authorization) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->can("userinfo") and $uri->userinfo and not $hdrs->authorization) 0 13 13
Condition50%3/6
$uri->can("userinfo") and $uri->userinfo and not $hdrs->authorization
!ll&&!rl&&r
1300
$uri->can("userinfo") and $uri->userinfo
!ll&&!rl&&r
1120
MC/DC0%0/3
$uri->can("userinfo") and $uri->userinfo and not $hdrs->authorization
$uri->can("userinfo") $uri->userinfo not $hdrs->authorization
Truth table50%2/4
A and B and not(C)
A= $uri->can("userinfo")B= $uri->userinfoC= not $hdrs->authorization
ABCresult
0XX0
10X0
1100
1111
74 0 $hdrs->authorization_basic(split(':', $uri->userinfo, 2));
75 }
76 13 ▶ if ($uri->scheme) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->scheme) 12 1 13
77 12 $self->cookie_jar->add_cookie_header($req);
78 }
79  
80 13 for (qw(max_redirects inactivity_timeout request_timeout), @OPTIONS) {
81 195 ▶ $opts->{$_} = $self->$_() unless exists($opts->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{$_} 178 17 195
82 }
83  
84 13 my ($grd, $tmr);
85  
86 13 ▶ if ($opts->{request_timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"request_timeout"}) 1 12 13
87 1 ▶ $tmr = AE::timer $opts->{request_timeout}, 0, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
88 1 undef($grd);
89 1 $cb->($opts, undef, {Status => 597, Reason => 'Request timeout'});
90 1 };
91 }
92 $grd = AnyEvent::HTTP::http_request(
93 $req->method,
94 $req->uri,
95 19 headers => {map { $_ => scalar($hdrs->header($_)) } $hdrs->header_field_names},
96 body => $req->content,
97 recurse => 0,
98 timeout => $opts->{inactivity_timeout},
99 2 (map { $_ => $opts->{$_} } grep { defined($opts->{$_}) } @OPTIONS),
100 12 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
101 12 undef($grd);
102 12 undef($tmr);
103 12 $cb->($opts, @_);
104 }
105 13 );
106 }
107  
108 13 ▶sub _response {
Subroutine _response: called CC=17 SCAR=29.1
109 13 my $cb = pop();
110 13 my ($self, $req, $opts, $body, $hdrs, $prev, $count) = @_;
111  
112 13 my $res = HTTP::Response->new(delete($hdrs->{Status}), delete($hdrs->{Reason}));
113  
114 13 $res->request($req);
115 13 ▶ $res->previous($prev) if $prev;
Branch: 1 branch
BranchTrueFalseTotal
if $prev 1 12 13
116  
117 13 delete($hdrs->{URL});
118 13 ▶ if (defined($hdrs->{HTTPVersion})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hdrs->{"HTTPVersion"}) 0 13 13
119 0 $res->protocol('HTTP/' . delete($hdrs->{HTTPVersion}));
120 }
121 13 ▶ if (my $hdr = $hdrs->{'set-cookie'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hdr = $hdrs->{"set-cookie"}) 3 10 13
122 # Split comma-concatenated "Set-Cookie" values.
123 # Based on RFC 6265, section 4.1.1.
124 3 local @_ = split(/,([\w.!"'%\$&*+-^`]+=)/, ',' . $hdr);
125 3 shift();
126 3 my @val;
127 3 ▶ push(@val, join('', shift(), shift())) while @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ and do { push @val, join("", shift(), shift()) } 4 3 7
128 3 $hdrs->{'set-cookie'} = \@val;
129 }
130 13 ▶ if (keys(%$hdrs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$hdrs) 5 8 13
131 5 $res->header(%$hdrs);
132 }
133 13 ▶ if ($res->code >= 590 && $res->code <= 599 && $res->message) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code >= 590 and $res->code <= 599 and $res->message) 3 10 13
Condition66%4/6
$res->code >= 590 and $res->code <= 599 and $res->message
!ll&&!rl&&r
1003
$res->code >= 590 and $res->code <= 599
!ll&&!rl&&r
1003
MC/DC33%1/3
$res->code >= 590 and $res->code <= 599 and $res->message
$res->code >= 590 $res->code <= 599 $res->message
Truth table50%2/4
A and B and C
A= $res->code >= 590B= $res->code <= 599C= $res->message
ABCresult
0XX0
10X0
1100
1111
134 3 ▶ if ($res->message eq 'Connection timed out') {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->message eq "Connection timed out") 0 3 3
135 0 $res->message('Inactivity timeout');
136 }
137 3 ▶ unless ($res->header('client-warning')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->header("client-warning")) 3 0 3
138 3 $res->header('client-warning' => $res->message);
139 }
140 }
141 13 ▶ if (defined($body)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body) 10 3 13
142 10 $res->content_ref(\$body);
143 }
144 13 $self->cookie_jar->extract_cookies($res);
145  
146 13 my $code = $res->code;
147  
148 13 ▶ if ($code == 301 || $code == 302 || $code == 303 || $code == 307 || $code == 308) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 301 or $code == 302 or $code == 303 or $code == 307 or $code == 308) { } 2 11 13
Condition66%8/12
$code == 301 or $code == 302 or $code == 303 or $code == 307 or $code == 308
l!l&&r!l&&!r
2011
$code == 301 or $code == 302 or $code == 303 or $code == 307
l!l&&r!l&&!r
2011
$code == 301 or $code == 302 or $code == 303
l!l&&r!l&&!r
2011
$code == 301 or $code == 302
l!l&&r!l&&!r
0211
MC/DC20%1/5
$code == 301 or $code == 302 or $code == 303 or $code == 307 or $code == 308
$code == 301 $code == 302 $code == 303 $code == 307 $code == 308
Truth table33%2/6
A or B or C or D or E
A= $code == 301B= $code == 302C= $code == 303D= $code == 307E= $code == 308
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
149 2 $self->_redirect($req, $opts, $code, $res, $count, $cb);
150 }
151 else {
152 11 $cb->($res);
153 }
154 }
155  
156 2 ▶sub _redirect {
Subroutine _redirect: called CC=10 SCAR=26.0
157 2 my ($self, $req, $opts, $code, $prev, $count, $cb) = @_;
158  
159 2 ▶ unless (defined($count) ? $count : ($count = $opts->{max_redirects})) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $count ? $count : ($count = $opts->{"max_redirects"})) 1 1 2
defined $count ? : 1 1 2
160 1 $prev->header('client-warning' => 'Redirect loop detected (max_redirects = ' . $opts->{max_redirects} . ')');
161 1 $cb->($prev);
162 1 return;
163 }
164  
165 1 my $meth = $req->method;
166 1 my $proto = $req->uri->scheme;
167 1 my $uri = $prev->header('location');
168  
169 1 $req = $req->clone();
170 1 $req->remove_header('cookie');
171 1 ▶ if (($code == 302 || $code == 303) && !($meth eq 'GET' || $meth eq 'HEAD')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 302 || $code == 303 and not $meth eq "GET" || $meth eq "HEAD") 0 1 1
Condition33%3/9
$code == 302 || $code == 303 and not $meth eq "GET" || $meth eq "HEAD"
!ll&&!rl&&r
010
$code == 302 or $code == 303
l!l&&r!l&&!r
100
$meth eq 'GET' || $meth eq 'HEAD'
l!l&&r!l&&!r
100
MC/DC0%0/4
$code == 302 || $code == 303 and not $meth eq "GET" || $meth eq "HEAD"
$code == 302 $code == 303 $meth eq 'GET' $meth eq 'HEAD'
Truth table14%1/7
A or B and not(C || D)
A= $code == 302B= $code == 303C= $meth eq 'GET'D= $meth eq 'HEAD'
ABCDresult
00XX0
1X1X0
1X010
011X0
01010
1X001
01001
172 0 $req->method('GET');
173 0 $req->content('');
174 0 $req->remove_content_headers();
175 }
176 {
177 # Support for relative URL for redirect.
178 # Not correspond to RFC.
179 1 local $URI::ABS_ALLOW_RELATIVE_SCHEME = 1;
180 1 my $base = $prev->base;
181 1 ▶ $uri = $HTTP::URI_CLASS->new(defined($uri) ? $uri : '', $base)->abs($base);
Branch: 1 branch
BranchTrueFalseTotal
defined $uri ? : 1 0 1
182 }
183 1 $req->uri($uri);
184 1 ▶ if ($proto eq 'https' && $uri->scheme eq 'http') {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto eq "https" and $uri->scheme eq "http") 0 1 1
Condition33%1/3
$proto eq "https" and $uri->scheme eq "http"
!ll&&!rl&&r
100
MC/DC0%0/2
$proto eq "https" and $uri->scheme eq "http"
$proto eq "https" $uri->scheme eq "http"
Truth table33%1/3
A and B
A= $proto eq "https"B= $uri->scheme eq "http"
ABresult
0X0
100
111
185 # Suppress 'Referer' header for HTTPS to HTTP redirect.
186 # RFC 2616, section 15.1.3.
187 0 $req->remove_header('referer');
188 }
189  
190 1 ▶ $self->_request($req, $opts, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
191 1 ▶ $self->_response($req, @_, $prev, $count - 1, sub { return $cb->(@_); });
Subroutine __ANON__: called CC=1 SCAR=0.0
192 1 });
193 }
194  
195  
196 8 1;
197  
198  
199 __END__
↑ Summary