AnyUA.pm

filter: Statementstmt 77.4% 137 / 177 Branchbran 52.2% 47 / 90 Conditioncond 34.1% 28 / 82 MC/DCmcdc 11.1% 5 / 45 Subroutinesub 84.3% 27 / 32 Podpod 100% 14 / 14 totaltotal 58.6% 258 / 440 CC 77 SCAR 27.8
CC
77
Cov
61%
mirror
90.0
__ANON__
56.0
__ANON__
12.0
CRAP 16.1
« AnyUA/Util.pm ↑ Summary AnyUA/Backend/Furl.pm »
1 package HTTP::AnyUA;
2 # ABSTRACT: An HTTP user agent programming interface unification layer
3  
4  
5 19 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 19 our $VERSION = '0.904'; # VERSION
10  
11 19 ▶use HTTP::AnyUA::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 19 ▶use Module::Loader;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 19 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 19 our $BACKEND_NAMESPACE;
17 19 our $MIDDLEWARE_NAMESPACE;
18 19 our @BACKENDS;
19 19 our %REGISTERED_BACKENDS;
20  
21 19 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
22 19 $BACKEND_NAMESPACE = __PACKAGE__ . '::Backend';
23 19 $MIDDLEWARE_NAMESPACE = __PACKAGE__ . '::Middleware';
24 }
25  
26  
27 23 ▶sub _debug_log { print STDERR join(' ', @_), "\n" if $ENV{PERL_HTTP_ANYUA_DEBUG} }
Subroutine _debug_log: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"PERL_HTTP_ANYUA_DEBUG"} 0 23 23
28  
29 1 ▶sub _croak { require Carp; Carp::croak(@_) }
Subroutine _croak: called CC=1 SCAR=0.0
30 1 ▶sub _usage { _croak("Usage: @_\n") }
Subroutine _usage: called CC=1 SCAR=0.0
31  
32  
33  
34 11 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
35 11 my $class = shift;
36 11 ▶ unshift @_, 'ua' if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 3 8 11
37 11 my %args = @_;
38 11 ▶ $args{ua} or _usage(q{HTTP::AnyUA->new(ua => $user_agent, %attr)});
Branch: 1 branch
BranchTrueFalseTotal
$args{"ua"} or _usage("HTTP::AnyUA->new(ua => \$user_agent, %attr)") 10 1 11
39  
40 10 my $self;
41 10 my @attr = qw(ua backend response_is_future);
42  
43 10 for my $attr (@attr) {
44 30 ▶ $self->{$attr} = $args{$attr} if defined $args{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{$attr} 12 18 30
45 }
46  
47 10 bless $self, $class;
48  
49 10 $self->_debug_log('Created with user agent', $self->ua);
50  
51 # call accessors to get the checks to run
52 10 $self->ua;
53 10 ▶ $self->response_is_future($args{response_is_future}) if defined $args{response_is_future};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"response_is_future"} 2 8 10
54  
55 10 return $self;
56 }
57  
58  
59 36 ▶sub ua { shift->{ua} or _croak 'User agent is required' }
Subroutine ua: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"ua"} or _croak("User agent is required") 36 0 36
60  
61  
62 30 ▶sub response_is_future {
Subroutine response_is_future: called CC=8 SCAR=20.9
Pod documented
63 30 my $self = shift;
64 30 my $val = shift;
65  
66 30 ▶ if (defined $val) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $val) { } 2 28 30
elsif (not defined $self->{'response_is_future'} and $self->{'backend'}) { } 24 8 32
67 2 ▶ $self->_debug_log('Set response_is_future to', $val ? 'ON' : 'OFF');
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 2 0 2
68  
69 2 $self->_check_response_is_future($val);
70 2 $self->{response_is_future} = $val;
71  
72 2 ▶ $self->_module_loader->load('Future') if $self->{response_is_future};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"response_is_future"} 2 0 2
73 }
74 ▶ elsif (!defined $self->{response_is_future} && $self->{backend}) {
Condition66%2/3
not defined $self->{"response_is_future"} and $self->{"backend"}
!ll&&!rl&&r
4024
MC/DC50%1/2
not defined $self->{"response_is_future"} and $self->{"backend"}
not defined $self->{"response_is_future"} $self->{"backend"}
Truth table66%2/3
not(A) and B
A= not defined $self->{"response_is_future"}B= $self->{"backend"}
ABresult
0X0
100
111
75 24 $self->{response_is_future} = $self->backend->response_is_future;
76  
77 24 ▶ $self->_module_loader->load('Future') if $self->{response_is_future};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"response_is_future"} 1 23 24
78 }
79  
80 30 ▶ return $self->{response_is_future} || '';
Condition100%2/2
$self->{'response_is_future'} || ''
l!l
723
MC/DC100%1/1
$self->{'response_is_future'} || ''
$self->{'response_is_future'}
Truth table100%2/2
A
A= $self->{'response_is_future'}
Aresult
11
00
81 }
82  
83  
84 63 ▶sub backend {
Subroutine backend: called CC=2 SCAR=6.9
Pod documented
85 63 my $self = shift;
86  
87 63 ▶ return $self->{backend} if defined $self->{backend};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"backend"} 55 8 63
88  
89 8 $self->{backend} = $self->_build_backend;
90 8 $self->_check_response_is_future($self->response_is_future);
91  
92 8 return $self->{backend};
93 }
94  
95  
96 16 ▶sub request {
Subroutine request: called CC=6 SCAR=20.2
Pod documented
97 16 my ($self, $method, $url, $args) = @_;
98 16 ▶ $args ||= {};
Condition100%2/2
$args ||= {}
l!l
610
MC/DC100%1/1
$args ||= {}
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
99 16 ▶ @_ == 3 || (@_ == 4 && ref $args eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or @_ == 4 and ref $args eq "HASH" or _usage("\$any_ua->request(\$method, \$url, \\%options)") 16 0 16
Condition33%2/6
@_ == 3 or @_ == 4 and ref $args eq "HASH"
l!l&&r!l&&!r
0160
@_ == 4 and ref $args eq "HASH"
!ll&&!rl&&r
0016
MC/DC0%0/3
@_ == 3 or @_ == 4 and ref $args eq "HASH"
@_ == 3 @_ == 4 ref $args eq "HASH"
Truth table25%1/4
A or B and C
A= @_ == 3B= @_ == 4C= ref $args eq "HASH"
ABCresult
1XX1
0111
00X0
0100
100 or _usage(q{$any_ua->request($method, $url, \%options)});
101  
102 16 my $resp = eval { $self->backend->request(uc($method) => $url, $args) };
103 16 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 16 16
104 0 return $self->_wrap_internal_exception($err);
105 }
106  
107 16 return $self->_wrap_response($resp);
108 }
109  
110  
111 # adapted from HTTP/Tiny.pm
112 19 for my $sub_name (qw{get head put post delete}) {
113 95 my %swap = (SUBNAME => $sub_name, METHOD => uc($sub_name));
114 95 my $code = q[
115 sub {{SUBNAME}} {
116 my ($self, $url, $args) = @_;
117 @_ == 2 || (@_ == 3 && ref $args eq 'HASH')
118 or _usage(q{$any_ua->{{SUBNAME}}($url, \%options)});
119 return $self->request('{{METHOD}}', $url, $args);
120 }
121 ];
122 95 $code =~ s/\{\{([A-Z_]+)\}\}/$swap{$1}/ge;
123 1 ▶ eval $code; ## no critic
Subroutine delete: called CC=4 SCAR=17.2
Subroutine get: called CC=4 SCAR=17.2
Subroutine head: called CC=4 SCAR=17.2
Subroutine post: called CC=4 SCAR=17.2
Subroutine put: called CC=4 SCAR=17.2
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Branch: 5 branches
BranchTrueFalseTotal
@_ == 2 or @_ == 3 and ref $args eq "HASH" or _usage("\$any_ua->delete(\$url, \\%options)") 1 0 1
@_ == 2 or @_ == 3 and ref $args eq "HASH" or _usage("\$any_ua->get(\$url, \\%options)") 8 0 8
@_ == 2 or @_ == 3 and ref $args eq "HASH" or _usage("\$any_ua->head(\$url, \\%options)") 1 0 1
@_ == 2 or @_ == 3 and ref $args eq "HASH" or _usage("\$any_ua->post(\$url, \\%options)") 4 0 4
@_ == 2 or @_ == 3 and ref $args eq "HASH" or _usage("\$any_ua->put(\$url, \\%options)") 1 0 1
Condition30%9/30
@_ == 2 or @_ == 3 and ref $args eq "HASH"
l!l&&r!l&&!r
100
@_ == 3 and ref $args eq "HASH"
!ll&&!rl&&r
000
@_ == 2 or @_ == 3 and ref $args eq "HASH"
l!l&&r!l&&!r
530
@_ == 3 and ref $args eq "HASH"
!ll&&!rl&&r
003
@_ == 2 or @_ == 3 and ref $args eq "HASH"
l!l&&r!l&&!r
100
@_ == 3 and ref $args eq "HASH"
!ll&&!rl&&r
000
@_ == 2 or @_ == 3 and ref $args eq "HASH"
l!l&&r!l&&!r
220
@_ == 3 and ref $args eq "HASH"
!ll&&!rl&&r
002
@_ == 2 or @_ == 3 and ref $args eq "HASH"
l!l&&r!l&&!r
100
@_ == 3 and ref $args eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
@_ == 2 or @_ == 3 and ref $args eq "HASH"
@_ == 2 @_ == 3 ref $args eq "HASH"
MC/DC0%0/3
@_ == 2 or @_ == 3 and ref $args eq "HASH"
@_ == 2 @_ == 3 ref $args eq "HASH"
MC/DC0%0/3
@_ == 2 or @_ == 3 and ref $args eq "HASH"
@_ == 2 @_ == 3 ref $args eq "HASH"
MC/DC0%0/3
@_ == 2 or @_ == 3 and ref $args eq "HASH"
@_ == 2 @_ == 3 ref $args eq "HASH"
MC/DC0%0/3
@_ == 2 or @_ == 3 and ref $args eq "HASH"
@_ == 2 @_ == 3 ref $args eq "HASH"
Truth table25%1/4
A or B and C
A= @_ == 2B= @_ == 3C= ref $args eq "HASH"
ABCresult
1XX1
0111
00X0
0100
Truth table50%2/4
A or B and C
A= @_ == 2B= @_ == 3C= ref $args eq "HASH"
ABCresult
1XX1
0111
00X0
0100
Truth table25%1/4
A or B and C
A= @_ == 2B= @_ == 3C= ref $args eq "HASH"
ABCresult
1XX1
0111
00X0
0100
Truth table50%2/4
A or B and C
A= @_ == 2B= @_ == 3C= ref $args eq "HASH"
ABCresult
1XX1
0111
00X0
0100
Truth table25%1/4
A or B and C
A= @_ == 2B= @_ == 3C= ref $args eq "HASH"
ABCresult
1XX1
0111
00X0
0100
124 }
125  
126  
127 # adapted from HTTP/Tiny.pm
128 1 ▶sub post_form {
Subroutine post_form: called CC=4 SCAR=17.2
Pod documented
129 1 my ($self, $url, $data, $args) = @_;
130 1 ▶ (@_ == 3 || @_ == 4 && ref $args eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or @_ == 4 and ref $args eq "HASH" or _usage("\$any_ua->post_form(\$url, \$formdata, \\%options)") 1 0 1
Condition16%1/6
@_ == 3 or @_ == 4 and ref $args eq "HASH"
l!l&&r!l&&!r
100
@_ == 4 and ref $args eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
@_ == 3 or @_ == 4 and ref $args eq "HASH"
@_ == 3 @_ == 4 ref $args eq "HASH"
Truth table25%1/4
A or B and C
A= @_ == 3B= @_ == 4C= ref $args eq "HASH"
ABCresult
1XX1
0111
00X0
0100
131 or _usage(q{$any_ua->post_form($url, $formdata, \%options)});
132  
133 1 my $headers = HTTP::AnyUA::Util::normalize_headers($args->{headers});
134 1 delete $args->{headers};
135  
136 1 return $self->request(POST => $url, {
137 %$args,
138 content => HTTP::AnyUA::Util::www_form_urlencode($data),
139 headers => {
140 %$headers,
141 'content-type' => 'application/x-www-form-urlencoded',
142 },
143 });
144 }
145  
146  
147 # adapted from HTTP/Tiny.pm
148 0 ▶sub mirror {
Subroutine mirror: not called CC=9 SCAR=45.0
Pod documented
149 0 my ($self, $url, $file, $args) = @_;
150 0 ▶ @_ == 3 || (@_ == 4 && ref $args eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or @_ == 4 and ref $args eq "HASH" or _usage("\$any_ua->mirror(\$url, \$filepath, \\%options)") 0 0 0
Condition0%0/6
@_ == 3 or @_ == 4 and ref $args eq "HASH"
l!l&&r!l&&!r
000
@_ == 4 and ref $args eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
@_ == 3 or @_ == 4 and ref $args eq "HASH"
@_ == 3 @_ == 4 ref $args eq "HASH"
Truth table0%0/4
A or B and C
A= @_ == 3B= @_ == 4C= ref $args eq "HASH"
ABCresult
1XX1
0111
00X0
0100
151 or _usage(q{$any_ua->mirror($url, $filepath, \%options)});
152  
153 0 $args->{headers} = HTTP::AnyUA::Util::normalize_headers($args->{headers});
154  
155 0 ▶ if (-e $file and my $mtime = (stat($file))[9]) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file and my $mtime = (stat $file)[9]) 0 0 0
Condition0%0/3
-e $file and my $mtime = (stat $file)[9]
!ll&&!rl&&r
000
MC/DC0%0/2
-e $file and my $mtime = (stat $file)[9]
-e $file my $mtime = (stat $file)[9]
Truth table0%0/3
A and B
A= -e $fileB= my $mtime = (stat $file)[9]
ABresult
0X0
100
111
156 0 ▶ $args->{headers}{'if-modified-since'} ||= HTTP::AnyUA::Util::http_date($mtime);
Condition0%0/3
$args->{"headers"}{"if-modified-since"} ||= HTTP::AnyUA::Util::http_date($mtime)
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"headers"}{"if-modified-since"} ||= HTTP::AnyUA::Util::http_date($mtime)
$args->{"headers"}{"if-modified-since"} HTTP::AnyUA::Util::http_date($mtime)
Truth table0%0/3
A ||= B
A= $args->{"headers"}{"if-modified-since"}B= HTTP::AnyUA::Util::http_date($mtime)
ABresult
1X1
011
000
157 }
158 0 my $tempfile = $file . int(rand(2**31));
159  
160 # set up the response body to be written to the file
161 0 require Fcntl;
162 0 ▶ sysopen(my $fh, $tempfile, Fcntl::O_CREAT()|Fcntl::O_EXCL()|Fcntl::O_WRONLY())
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $fh, $tempfile, 193 or return $self->_wrap_internal_exception("Error: Could not create temporary file $tempfile for downloading: $!\n") 0 0 0
163 or return $self->_wrap_internal_exception(qq/Error: Could not create temporary file $tempfile for downloading: $!\n/);
164 0 binmode $fh;
165 0 ▶ $args->{data_callback} = sub { print $fh $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
166  
167 0 my $resp = $self->request(GET => $url, $args);
168  
169 0 ▶ my $finish = sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
170 0 my $resp = shift;
171  
172 0 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or return HTTP::AnyUA::Util::internal_exception("Error: Caught error closing temporary file ${tempfile}: $!\n") 0 0 0
173 or return HTTP::AnyUA::Util::internal_exception(qq/Error: Caught error closing temporary file $tempfile: $!\n/);
174  
175 0 ▶ if ($resp->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->{"success"}) 0 0 0
176 0 ▶ rename($tempfile, $file)
Branch: 1 branch
BranchTrueFalseTotal
rename $tempfile, $file or return HTTP::AnyUA::Util::internal_exception("Error replacing $file with ${tempfile}: $!\n") 0 0 0
177 or return HTTP::AnyUA::Util::internal_exception(qq/Error replacing $file with $tempfile: $!\n/);
178 0 my $lm = $resp->{headers}{'last-modified'};
179 0 ▶ if ($lm and my $mtime = HTTP::AnyUA::Util::parse_http_date($lm)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lm and my $mtime = HTTP::AnyUA::Util::parse_http_date($lm)) 0 0 0
Condition0%0/3
$lm and my $mtime = HTTP::AnyUA::Util::parse_http_date($lm)
!ll&&!rl&&r
000
MC/DC0%0/2
$lm and my $mtime = HTTP::AnyUA::Util::parse_http_date($lm)
$lm my $mtime = HTTP::AnyUA::Util::parse_http_date($lm)
Truth table0%0/3
A and B
A= $lmB= my $mtime = HTTP::AnyUA::Util::parse_http_date($lm)
ABresult
0X0
100
111
180 0 utime($mtime, $mtime, $file);
181 }
182 }
183 0 unlink($tempfile);
184  
185 0 ▶ $resp->{success} ||= $resp->{status} eq '304';
Condition0%0/3
$resp->{"success"} ||= $resp->{"status"} eq 304
l!l&&r!l&&!r
000
MC/DC0%0/2
$resp->{"success"} ||= $resp->{"status"} eq 304
$resp->{"success"} $resp->{"status"} eq 304
Truth table0%0/3
A ||= B
A= $resp->{"success"}B= $resp->{"status"} eq 304
ABresult
1X1
011
000
186  
187 0 return $resp;
188 0 };
189  
190 0 ▶ if ($self->response_is_future) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->response_is_future) { } 0 0 0
191 0 ▶ return $resp->followed_by(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
192 0 my $future = shift;
193 0 ▶ my @resp = $future->is_done ? $future->get : $future->failure;
Branch: 1 branch
BranchTrueFalseTotal
$future->is_done ? : 0 0 0
194 0 my $resp = $finish->(@resp);
195 0 ▶ if ($resp->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->{'success'}) { } 0 0 0
196 0 return Future->done(@resp);
197 }
198 else {
199 0 return Future->fail(@resp);
200 }
201 0 });
202 }
203 else {
204 0 return $finish->($resp);
205 }
206 }
207  
208  
209 4 ▶sub apply_middleware {
Subroutine apply_middleware: called CC=3 SCAR=11.1
Pod documented
210 4 my $self = shift;
211 4 my $class = shift;
212  
213 4 ▶ if (!ref $class) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $class) 4 0 4
214 4 ▶ $class = "${MIDDLEWARE_NAMESPACE}::${class}" unless $class =~ s/^\+//;
Branch: 1 branch
BranchTrueFalseTotal
unless $class =~ s/^\+// 4 0 4
215 4 $self->_module_loader->load($class);
216 }
217  
218 4 $self->{backend} = $class->wrap($self->backend, @_);
219 4 $self->_check_response_is_future($self->response_is_future);
220  
221 4 return $self;
222 }
223  
224  
225 6 ▶sub register_backend {
Subroutine register_backend: called CC=4 SCAR=15.2
Pod documented
226 6 my ($class, $ua_type, $backend_class) = @_;
227 6 ▶ @_ == 3 or _usage(q{HTTP::AnyUA->register_backend($ua_type, $backend_package)});
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or _usage("HTTP::AnyUA->register_backend(\$ua_type, \$backend_package)") 6 0 6
228  
229 6 ▶ if ($backend_class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($backend_class) { } 6 0 6
230 6 ▶ $backend_class = "${BACKEND_NAMESPACE}::${backend_class}" unless $backend_class =~ s/^\+//;
Branch: 1 branch
BranchTrueFalseTotal
unless $backend_class =~ s/^\+// 0 6 6
231 6 $REGISTERED_BACKENDS{$ua_type} = $backend_class;
232 }
233 else {
234 0 delete $REGISTERED_BACKENDS{$ua_type};
235 }
236 }
237  
238  
239 # turn a response into a Future if it needs to be
240 16 ▶sub _wrap_response {
Subroutine _wrap_response: called CC=4 SCAR=14.1
241 16 my $self = shift;
242 16 my $resp = shift;
243  
244 16 ▶ if ($self->response_is_future && !$self->backend->response_is_future) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->response_is_future and not $self->backend->response_is_future) 2 14 16
Condition100%3/3
$self->response_is_future and not $self->backend->response_is_future
!ll&&!rl&&r
1312
MC/DC100%2/2
$self->response_is_future and not $self->backend->response_is_future
$self->response_is_future not $self->backend->response_is_future
Truth table100%3/3
A and not(B)
A= $self->response_is_futureB= not $self->backend->response_is_future
ABresult
0X0
100
111
245 # wrap the response in a Future
246 2 ▶ if ($resp->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->{'success'}) { } 0 2 2
247 0 $self->_debug_log('Wrapped successful response in a Future');
248 0 $resp = Future->done($resp);
249 }
250 else {
251 2 $self->_debug_log('Wrapped failed response in a Future');
252 2 $resp = Future->fail($resp);
253 }
254 }
255  
256 16 return $resp;
257 }
258  
259 0 ▶sub _wrap_internal_exception { shift->_wrap_response(HTTP::AnyUA::Util::internal_exception(@_)) }
Subroutine _wrap_internal_exception: not called CC=1 SCAR=6.9
260  
261 # get a module loader object
262 16 ▶sub _module_loader { shift->{_module_loader} ||= Module::Loader->new }
Subroutine _module_loader: called CC=2 SCAR=7.2
Condition66%2/3
+(shift())->{"_module_loader"} ||= "Module::Loader"->new
l!l&&r!l&&!r
880
MC/DC0%0/2
+(shift())->{"_module_loader"} ||= "Module::Loader"->new
+(shift())->{"_module_loader"} "Module::Loader"->new
Truth table66%2/3
A ||= B
A= +(shift())->{"_module_loader"}B= "Module::Loader"->new
ABresult
1X1
011
000
263  
264 # get a list of potential backends that may be able to handle the user agent
265 8 ▶sub _build_backend {
Subroutine _build_backend: called CC=11 SCAR=24.8
266 8 my $self = shift;
267 8 ▶ my $ua = shift || $self->ua or _croak 'User agent is required';
Branch: 1 branch
BranchTrueFalseTotal
my $ua = shift() || $self->ua or _croak("User agent is required") 8 0 8
Condition33%1/3
shift() || $self->ua
l!l&&r!l&&!r
080
MC/DC0%0/2
shift() || $self->ua
shift() $self->ua
Truth table33%1/3
A || B
A= shift()B= $self->ua
ABresult
1X1
011
000
268  
269 8 my $ua_type = Scalar::Util::blessed($ua);
270  
271 8 my @classes;
272  
273 8 ▶ if ($ua_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ua_type) { } 2 6 8
274 2 ▶ push @classes, $REGISTERED_BACKENDS{$ua_type} if $REGISTERED_BACKENDS{$ua_type};
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::AnyUA::REGISTERED_BACKENDS{$ua_type} 0 2 2
275  
276 2 push @classes, "${BACKEND_NAMESPACE}::${ua_type}";
277  
278 2 ▶ if (!@BACKENDS) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@HTTP::AnyUA::BACKENDS) 1 1 2
279 # search for some backends to try
280 1 @BACKENDS = sort $self->_module_loader->find_modules($BACKEND_NAMESPACE);
281 1 $self->_debug_log('Found backends to try (' . join(', ', @BACKENDS) . ')');
282 }
283  
284 2 for my $backend_type (@BACKENDS) {
285 14 my $plugin = $backend_type;
286 14 $plugin =~ s/^\Q${BACKEND_NAMESPACE}\E:://;
287 14 ▶ push @classes, $backend_type if $ua->isa($plugin);
Branch: 1 branch
BranchTrueFalseTotal
if $ua->isa($plugin) 2 12 14
288 }
289 }
290 else {
291 6 ▶ push @classes, $REGISTERED_BACKENDS{$ua} if $REGISTERED_BACKENDS{$ua};
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::AnyUA::REGISTERED_BACKENDS{$ua} 6 0 6
292 6 push @classes, "${BACKEND_NAMESPACE}::${ua}";
293 }
294  
295 8 for my $class (@classes) {
296 8 ▶ if (eval { $self->_module_loader->load($class); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $self->_module_loader->load($class); 1 } }) { } 8 0 8
297 8 $self->_debug_log("Found usable backend (${class})");
298 8 return $class->new($self->ua);
299 }
300 else {
301 0 $self->_debug_log($@);
302 }
303 }
304  
305 0 _croak 'Cannot find a usable backend that supports the given user agent';
306 }
307  
308 # make sure the response_is_future setting is compatible with the backend
309 14 ▶sub _check_response_is_future {
Subroutine _check_response_is_future: called CC=4 SCAR=15.2
310 14 my $self = shift;
311 14 my $val = shift;
312  
313 # make sure the user agent is not non-blocking
314 14 ▶ if (!$val && $self->{backend} && $self->backend->response_is_future) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $val and $self->{"backend"} and $self->backend->response_is_future) 0 14 14
Condition66%4/6
not $val and $self->{"backend"} and $self->backend->response_is_future
!ll&&!rl&&r
4100
not $val and $self->{"backend"}
!ll&&!rl&&r
4010
MC/DC0%0/3
not $val and $self->{"backend"} and $self->backend->response_is_future
not $val $self->{"backend"} $self->backend->response_is_future
Truth table50%2/4
not(A) and B and C
A= not $valB= $self->{"backend"}C= $self->backend->response_is_future
ABCresult
0XX0
10X0
1100
1111
315 0 _croak 'Cannot disable response_is_future with a non-blocking user agent';
316 }
317 }
318  
319 19 1;
320  
321 __END__
« AnyUA/Util.pm ↑ Summary AnyUA/Backend/Furl.pm »