Webqq/UserAgent.pm

filter: Statementstmt 17.1% 19 / 111 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 47 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 27.2% 6 / 22 Podpod 0.0% 0 / 7 totaltotal 10.0% 25 / 250 CC 39 SCAR 48.1
CC
39
Cov
10%
_response
306.0
_redirect
110.0
_request
90.0
CRAP 123.2
« Webqq/Message.pm ↑ Summary Webqq/Client/Plugin/SendMsgFromSocket.pm »
1 package Webqq::UserAgent;
2
3 1 ▶use AnyEvent::HTTP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use HTTP::Cookies ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use HTTP::Request::Common ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use HTTP::Response ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 0 ▶sub new {
Subroutine new: not called CC=6 SCAR=37.4
Pod undocumented
10 0 my $class = shift;
11 0 my %p = @_;
12 return bless {
13 agent => $p{agent} || $AnyEvent::HTTP::USERAGENT . ' AnyEvent-UserAgent/' . $VERSION ,
14 cookie_jar => $p{cookie_jar} || HTTP::Cookies->new,
15 max_redirects => $p{max_redirects} || 5,
16 inactivity_timeout => $p{inactivity_timeout} || 20,
17 0 ▶ request_timeout => $p{request_timeout} || 0
Condition0%0/11
$p{'agent'} || $AnyEvent::HTTP::USERAGENT . ' AnyEvent-UserAgent/' . $VERSION
l!l
00
$p{'cookie_jar'} || 'HTTP::Cookies'->new
l!l&&r!l&&!r
000
$p{'max_redirects'} || 5
l!l
00
$p{'inactivity_timeout'} || 20
l!l
00
$p{'request_timeout'} || 0
l!l
00
MC/DC0%0/1
$p{'agent'} || $AnyEvent::HTTP::USERAGENT . ' AnyEvent-UserAgent/' . $VERSION
$p{'agent'}
MC/DC0%0/2
$p{'cookie_jar'} || 'HTTP::Cookies'->new
$p{'cookie_jar'} 'HTTP::Cookies'->new
MC/DC0%0/1
$p{'max_redirects'} || 5
$p{'max_redirects'}
MC/DC0%0/1
$p{'inactivity_timeout'} || 20
$p{'inactivity_timeout'}
MC/DC0%0/1
$p{'request_timeout'} || 0
$p{'request_timeout'}
Truth table0%0/2
A
A= $p{'agent'}
Aresult
11
00
Truth table0%0/3
A || B
A= $p{'cookie_jar'}B= 'HTTP::Cookies'->new
ABresult
1X1
011
000
Truth table0%0/2
A
A= $p{'max_redirects'}
Aresult
11
00
Truth table0%0/2
A
A= $p{'inactivity_timeout'}
Aresult
11
00
Truth table0%0/2
A
A= $p{'request_timeout'}
Aresult
11
00
18 },$class;
19 }
20 0 ▶sub request {
Subroutine request: not called CC=1 SCAR=6.9
Pod undocumented
21 0 my $cb = pop();
22 0 my ($self, $req, %opts) = @_;
23 0 ▶ $self->_request($req, \%opts, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 0 $self->_response($req, @_, $cb);
25 0 });
26 }
27
28 0 ▶sub get { _make_request(GET => @_) }
Subroutine get: not called CC=1 SCAR=6.9
Pod undocumented
29 0 ▶sub head { _make_request(HEAD => @_) }
Subroutine head: not called CC=1 SCAR=6.9
Pod undocumented
30 0 ▶sub put { _make_request(PUT => @_) }
Subroutine put: not called CC=1 SCAR=6.9
Pod undocumented
31 0 ▶sub delete { _make_request(DELETE => @_) }
Subroutine delete: not called CC=1 SCAR=6.9
Pod undocumented
32 0 ▶sub post { _make_request(POST => @_) }
Subroutine post: not called CC=1 SCAR=6.9
Pod undocumented
33
34 0 ▶sub _make_request {
Subroutine _make_request: not called CC=1 SCAR=2.2
35 0 my $cb = pop();
36 0 my $meth = shift();
37 0 my $self = shift();
38
39 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
40 0 $self->request(&{'HTTP::Request::Common::' . $meth}(@_), $cb);
41 }
42
43 0 ▶sub _request {
Subroutine _request: not called CC=9 SCAR=45.0
44 0 my ($self, $req, $opts, $cb) = @_;
45
46 0 my $uri = $req->uri;
47 0 my $hdrs = $req->headers;
48
49 0 ▶ unless ($hdrs->user_agent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hdrs->user_agent) 0 0 0
50 0 $hdrs->user_agent($self->{agent});
51 }
52
53 0 ▶ if ($uri->can('userinfo') && $uri->userinfo && !$hdrs->authorization) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->can("userinfo") and $uri->userinfo and not $hdrs->authorization) 0 0 0
Condition0%0/6
$uri->can("userinfo") and $uri->userinfo and not $hdrs->authorization
!ll&&!rl&&r
000
$uri->can("userinfo") and $uri->userinfo
!ll&&!rl&&r
000
MC/DC0%0/3
$uri->can("userinfo") and $uri->userinfo and not $hdrs->authorization
$uri->can("userinfo") $uri->userinfo not $hdrs->authorization
Truth table0%0/4
A and B and not(C)
A= $uri->can("userinfo")B= $uri->userinfoC= not $hdrs->authorization
ABCresult
0XX0
10X0
1100
1111
54 0 $hdrs->authorization_basic(split(':', $uri->userinfo, 2));
55 }
56 0 ▶ if ($uri->scheme) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->scheme) 0 0 0
57 0 $self->{cookie_jar}->add_cookie_header($req);
58 }
59
60 0 for (qw(max_redirects inactivity_timeout request_timeout)) {
61 0 ▶ $opts->{$_} = $self->{$_} unless exists($opts->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{$_} 0 0 0
62 }
63
64 0 my ($grd, $tmr);
65
66 0 ▶ if ($opts->{request_timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"request_timeout"}) 0 0 0
67 0 ▶ $tmr = AE::timer $opts->{request_timeout}, 0, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 0 undef($grd);
69 0 $cb->($opts, undef, {Status => 597, Reason => 'Request timeout'});
70 0 };
71 }
72 $grd = AnyEvent::HTTP::http_request(
73 $req->method,
74 $req->uri,
75 0 headers => {map { $_ => $hdrs->header($_) } $hdrs->header_field_names},
76 body => $req->content,
77 recurse => 0,
78 timeout => $opts->{inactivity_timeout},
79 0 (map { $_ => $opts->{$_} } grep { exists($opts->{$_}) }
80 qw(proxy tls_ctx session timeout on_prepare tcp_connect on_header
81 on_body want_body_handle persistent keepalive handle_params)),
82 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
83 0 undef($grd);
84 0 undef($tmr);
85 0 $cb->($opts, @_);
86 }
87 0 );
88 }
89
90 0 ▶sub _response {
Subroutine _response: not called CC=17 SCAR=57.2
91 0 my $cb = pop();
92 0 my ($self, $req, $opts, $body, $hdrs, $prev, $count) = @_;
93
94 0 my $res = HTTP::Response->new(delete($hdrs->{Status}), delete($hdrs->{Reason}));
95
96 0 $res->request($req);
97 0 ▶ $res->previous($prev) if $prev;
Branch: 1 branch
BranchTrueFalseTotal
if $prev 0 0 0
98
99 0 delete($hdrs->{URL});
100 0 ▶ if (defined($hdrs->{HTTPVersion})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hdrs->{"HTTPVersion"}) 0 0 0
101 0 $res->protocol('HTTP/' . delete($hdrs->{HTTPVersion}));
102 }
103 0 ▶ if (my $hdr = $hdrs->{'set-cookie'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hdr = $hdrs->{"set-cookie"}) 0 0 0
104 # Split comma-concatenated "Set-Cookie" values.
105 # Based on RFC 6265, section 4.1.1.
106 0 local @_ = split(/,([\w.!"'%\$&*+-^`]+=)/, ',' . $hdr);
107 0 shift();
108 0 my @val;
109 0 ▶ push(@val, join('', shift(), shift())) while @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ and do { push @val, join("", shift(), shift()) } 0 0 0
110 0 $hdrs->{'set-cookie'} = \@val;
111 }
112 0 ▶ if (keys(%$hdrs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$hdrs) 0 0 0
113 0 $res->header(%$hdrs);
114 }
115 0 ▶ if ($res->code >= 590 && $res->code <= 599 && $res->message) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code >= 590 and $res->code <= 599 and $res->message) 0 0 0
Condition0%0/6
$res->code >= 590 and $res->code <= 599 and $res->message
!ll&&!rl&&r
000
$res->code >= 590 and $res->code <= 599
!ll&&!rl&&r
000
MC/DC0%0/3
$res->code >= 590 and $res->code <= 599 and $res->message
$res->code >= 590 $res->code <= 599 $res->message
Truth table0%0/4
A and B and C
A= $res->code >= 590B= $res->code <= 599C= $res->message
ABCresult
0XX0
10X0
1100
1111
116 0 ▶ if ($res->message eq 'Connection timed out') {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->message eq "Connection timed out") 0 0 0
117 0 $res->message('Inactivity timeout');
118 }
119 0 ▶ unless ($res->header('client-warning')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->header("client-warning")) 0 0 0
120 0 $res->header('client-warning' => $res->message);
121 }
122 }
123 0 ▶ if (defined($body)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body) 0 0 0
124 0 $res->content_ref(\$body);
125 }
126 0 $self->{cookie_jar}->extract_cookies($res);
127
128 0 my $code = $res->code;
129
130 0 ▶ 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) { } 0 0 0
Condition0%0/12
$code == 301 or $code == 302 or $code == 303 or $code == 307 or $code == 308
l!l&&r!l&&!r
000
$code == 301 or $code == 302 or $code == 303 or $code == 307
l!l&&r!l&&!r
000
$code == 301 or $code == 302 or $code == 303
l!l&&r!l&&!r
000
$code == 301 or $code == 302
l!l&&r!l&&!r
000
MC/DC0%0/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 table0%0/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
131 0 $self->_redirect($req, $opts, $code, $res, $count, $cb);
132 }
133 else {
134 0 $cb->($res);
135 }
136 }
137
138 0 ▶sub _redirect {
Subroutine _redirect: not called CC=10 SCAR=47.0
139 0 my ($self, $req, $opts, $code, $prev, $count, $cb) = @_;
140
141 0 ▶ unless (defined($count) ? $count : ($count = $opts->{max_redirects})) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $count ? $count : ($count = $opts->{"max_redirects"})) 0 0 0
defined $count ? : 0 0 0
142 0 $prev->header('client-warning' => 'Redirect loop detected (max_redirects = ' . $opts->{max_redirects} . ')');
143 0 $cb->($prev);
144 0 return;
145 }
146
147 0 my $meth = $req->method;
148 0 my $proto = $req->uri->scheme;
149 0 my $uri = $prev->header('location');
150
151 0 $req = $req->clone();
152 0 $req->remove_header('cookie');
153 0 ▶ 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 0 0
Condition0%0/9
$code == 302 || $code == 303 and not $meth eq "GET" || $meth eq "HEAD"
!ll&&!rl&&r
000
$code == 302 or $code == 303
l!l&&r!l&&!r
000
$meth eq 'GET' || $meth eq 'HEAD'
l!l&&r!l&&!r
000
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 table0%0/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
154 0 $req->method('GET');
155 0 $req->content('');
156 0 $req->remove_content_headers();
157 }
158 {
159 # Support for relative URL for redirect.
160 # Not correspond to RFC.
161 0 local $URI::ABS_ALLOW_RELATIVE_SCHEME = 1;
162 0 my $base = $prev->base;
163 0 ▶ $uri = $HTTP::URI_CLASS->new(defined($uri) ? $uri : '', $base)->abs($base);
Branch: 1 branch
BranchTrueFalseTotal
defined $uri ? : 0 0 0
164 }
165 0 $req->uri($uri);
166 0 ▶ if ($proto eq 'https' && $uri->scheme eq 'http') {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto eq "https" and $uri->scheme eq "http") 0 0 0
Condition0%0/3
$proto eq "https" and $uri->scheme eq "http"
!ll&&!rl&&r
000
MC/DC0%0/2
$proto eq "https" and $uri->scheme eq "http"
$proto eq "https" $uri->scheme eq "http"
Truth table0%0/3
A and B
A= $proto eq "https"B= $uri->scheme eq "http"
ABresult
0X0
100
111
167 # Suppress 'Referer' header for HTTPS to HTTP redirect.
168 # RFC 2616, section 15.1.3.
169 0 $req->remove_header('referer');
170 }
171
172 0 ▶ $self->_request($req, $opts, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
173 0 ▶ $self->_response($req, @_, $prev, $count - 1, sub { return $cb->(@_); });
Subroutine __ANON__: not called CC=1 SCAR=6.9
174 0 });
175 }
176
177
178 1 1;
179
180
181 __END__
« Webqq/Message.pm ↑ Summary Webqq/Client/Plugin/SendMsgFromSocket.pm »