Plient.pm

filter: Statementstmt 37.8% 59 / 156 Branchbran 20.0% 20 / 100 Conditioncond 20.5% 8 / 39 MC/DCmcdc 4.3% 1 / 23 Subroutinesub 62.5% 10 / 16 Podpod 28.5% 2 / 7 totaltotal 29.3% 100 / 341 CC 68 SCAR 46.8
CC
68
Cov
29%
plient
380.0
handlers
72.0
_extract_protocol
56.0
CRAP 108.3
↑ Summary Plient/Handler/LWP.pm »
1 package Plient;
2  
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 our $VERSION = '0.03';
7 5 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 our @EXPORT = 'plient';
10 5 our @EXPORT_OK = 'plient_support';
11 5 our $bundle_mode = $ENV{PLIENT_BUNDLE_MODE};
12  
13 0 ▶sub plient {
Subroutine plient: not called CC=19 SCAR=59.4
Pod documented
14 0 my ( $method, $uri, $args ) = @_;
15 0 ▶ if ( $args && ref $args ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args and ref $args ne "HASH") 0 0 0
Condition0%0/3
$args and ref $args ne "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$args and ref $args ne "HASH"
$args ref $args ne "HASH"
Truth table0%0/3
A and B
A= $argsB= ref $args ne "HASH"
ABresult
0X0
100
111
16 0 warn 'invalid args: should be a hashref';
17 0 return;
18 }
19 0 ▶ $args ||= {};
Condition0%0/2
$args ||= {}
l!l
00
MC/DC0%0/1
$args ||= {}
$args
Truth table0%0/2
A
A= $args
Aresult
11
00
20 0 ▶ return unless $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri 0 0 0
21 0 $uri =~ s/^\s+//;
22  
23 # XXX TODO move this $uri tweak thing to HTTP part
24 # http://localhost:5000 => http://localhost:5000/
25 0 ▶ $uri .= '/' if $uri =~ m{^https?://[^/]+$};
Branch: 1 branch
BranchTrueFalseTotal
if $uri =~ m[^https?://[^/]+$] 0 0 0
26  
27 # generate both body_hash and body_array to make handlers' life easier
28 0 ▶ if ( $args->{body} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"body"}) 0 0 0
29 0 ▶ if ( ref $args->{body} eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args->{'body'} eq 'HASH') { } 0 0 0
elsif (ref $args->{'body'} eq 'ARRAY') { } 0 0 0
30 0 $args->{body_hash} = $args->{body};
31 0 $args->{body_array} = [];
32 0 for my $k ( keys %{ $args->{body} } ) {
33 0 ▶ if ( ref $args->{body}{$k} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->{'body'}{$k} eq 'ARRAY') { } 0 0 0
34 0 push @{ $args->{body_array} }, $k, $_
35 0 for @{ $args->{body}{$k} };
36 }
37 else {
38 0 push @{ $args->{body_array} }, $k, $args->{body}{$k};
39 }
40 }
41 }
42 elsif ( ref $args->{body} eq 'ARRAY' ) {
43 0 $args->{body_array} = $args->{body};
44 0 $args->{body_hash} = {};
45 0 ▶ for ( my $i = 0 ; $i < $#{ $args->{body} } ; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#{$$args{"body"};}) 0 0 0
46 0 my $key = $args->{body}[$i];
47 my $value =
48 defined $args->{body}[ $i + 1 ]
49 0 ▶ ? $args->{body}[ $i + 1 ]
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'body'}[$i + 1] ? : 0 0 0
50 : '';
51 0 ▶ if ( exists $args->{body_hash}{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"body_hash"}{$key}) 0 0 0
52 0 ▶ if ( ref $args->{body_hash}{$key} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->{'body_hash'}{$key} eq 'ARRAY') { } 0 0 0
53 0 push @{ $args->{body_hash}{$key} }, $value;
54 }
55 else {
56 $args->{body_hash}{$key} =
57 0 [ $args->{body_hash}{$key}, $value ];
58 }
59 }
60 }
61 }
62 else {
63 0 die 'invalid body args, should be either hashref or arrayref';
64 }
65 }
66  
67 0 my $sub = dispatch( $method, $uri, $args );
68 0 ▶ if ( $sub ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub) { } 0 0 0
69 0 ▶ if ( $args->{output_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'output_file'}) { } 0 0 0
70 0 ▶ open my $fh, '>', $args->{output_file} or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $args->{"output_file"} or die $! 0 0 0
71 0 print $fh $sub->();
72 0 close $fh;
73 0 return 1;
74 }
75 else {
76 0 return $sub->();
77 }
78 }
79 else {
80 0 warn "failed to $method on $uri";
81 0 return;
82 }
83  
84 }
85  
86 0 ▶sub _extract_protocol {
Subroutine _extract_protocol: not called CC=7 SCAR=40.3
87 0 ▶ shift if $_[0] && $_[0] eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and $_[0] eq __PACKAGE__ 0 0 0
Condition0%0/3
$_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and $_[0] eq __PACKAGE__
$_[0] $_[0] eq __PACKAGE__
Truth table0%0/3
A and B
A= $_[0]B= $_[0] eq __PACKAGE__
ABresult
0X0
100
111
88 0 my $uri = shift;
89 0 ▶ return unless $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri 0 0 0
90 0 ▶ if ( $uri =~ /^http:/i ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($uri =~ /^http:/i) { } 0 0 0
elsif ($uri =~ /^https:/i) { } 0 0 0
elsif ($uri =~ /^file:/i) { } 0 0 0
91 0 return 'http';
92 }
93 elsif ( $uri =~ /^https:/i ) {
94 0 return 'https';
95 }
96 elsif ( $uri =~ /^file:/i ) {
97 0 return 'file';
98 }
99 else {
100 0 warn "unsupported uri: $uri";
101 0 return;
102 }
103 }
104  
105 1 ▶sub _dispatch_protocol {
Subroutine _dispatch_protocol: called CC=10 SCAR=36.1
106 1 ▶ shift if $_[0] && $_[0] eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and $_[0] eq __PACKAGE__ 0 1 1
Condition33%1/3
$_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
010
MC/DC0%0/2
$_[0] and $_[0] eq __PACKAGE__
$_[0] $_[0] eq __PACKAGE__
Truth table33%1/3
A and B
A= $_[0]B= $_[0] eq __PACKAGE__
ABresult
0X0
100
111
107 1 my $protocol = shift;
108 1 ▶ return unless $protocol;
Branch: 1 branch
BranchTrueFalseTotal
unless $protocol 0 1 1
109 1 ▶ if ( $protocol eq 'file' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($protocol eq 'file') { } 1 0 1
elsif ($protocol eq 'http') { } 0 0 0
elsif ($protocol eq 'https') { } 0 0 0
110 1 ▶ require Plient::Protocol::File unless $bundle_mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $Plient::bundle_mode 1 0 1
111 1 return 'Plient::Protocol::File';
112 }
113 elsif ( $protocol eq 'http' ) {
114 0 ▶ require Plient::Protocol::HTTP unless $bundle_mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $Plient::bundle_mode 0 0 0
115 0 return 'Plient::Protocol::HTTP';
116 }
117 elsif ( $protocol eq 'https' ) {
118 0 ▶ require Plient::Protocol::HTTPS unless $bundle_mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $Plient::bundle_mode 0 0 0
119 0 return 'Plient::Protocol::HTTPS';
120 }
121 else {
122 0 warn "unsupported protocol: $protocol";
123 0 return;
124 }
125 }
126  
127  
128 1 ▶sub plient_support {
Subroutine plient_support: called CC=7 SCAR=21.8
Pod documented
129 1 ▶ shift if $_[0] && $_[0] eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and $_[0] eq __PACKAGE__ 1 0 1
Condition33%1/3
$_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
001
MC/DC0%0/2
$_[0] and $_[0] eq __PACKAGE__
$_[0] $_[0] eq __PACKAGE__
Truth table33%1/3
A and B
A= $_[0]B= $_[0] eq __PACKAGE__
ABresult
0X0
100
111
130 1 my ( $protocol, $method, $args ) = @_;
131 1 ▶ return unless $protocol;
Branch: 1 branch
BranchTrueFalseTotal
unless $protocol 0 1 1
132 1 ▶ $method ||= 'get';
Condition50%1/2
$method ||= "get"
l!l
10
MC/DC0%0/1
$method ||= "get"
$method
Truth table50%1/2
A
A= $method
Aresult
11
00
133 1 ▶ $args ||= {};
Condition50%1/2
$args ||= {}
l!l
01
MC/DC0%0/1
$args ||= {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
134 1 my $class = _dispatch_protocol( lc $protocol );
135 1 ▶ return unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 1 1
136 1 return $class->support_method( $method, { %$args, check_only => 1 } );
137 }
138  
139 0 ▶sub dispatch {
Subroutine dispatch: not called CC=4 SCAR=30.0
Pod undocumented
140 0 my ( $method, $uri, $args ) = @_;
141 0 $method = lc $method;
142 0 ▶ $method ||= 'get'; # people use get most of the time.
Condition0%0/2
$method ||= "get"
l!l
00
MC/DC0%0/1
$method ||= "get"
$method
Truth table0%0/2
A
A= $method
Aresult
11
00
143 0 my $class = _dispatch_protocol( _extract_protocol($uri) );
144 0 ▶ return unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 0 0
145  
146 0 ▶ if ( my $sub = $class->support_method( $method, $args ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $class->support_method($method, $args)) { } 0 0 0
147 0 ▶ return sub { $sub->( $uri, $args ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
148 }
149 else {
150 0 warn "unsupported method: $method";
151 0 return;
152 }
153 }
154  
155 5 my %all_handlers;
156 my $found_handlers;
157 3 ▶sub all_handlers {
Subroutine all_handlers: called CC=2 SCAR=6.9
Pod undocumented
158 3 ▶ return keys %all_handlers if $found_handlers;
Branch: 1 branch
BranchTrueFalseTotal
if $found_handlers 2 1 3
159 1 @all_handlers{keys %all_handlers, find_handlers()} = ();
160 1 keys %all_handlers;
161 }
162  
163 # to include handlers not in @INC.
164 3 ▶sub _add_handlers {
Subroutine _add_handlers: called CC=7 SCAR=20.7
165 3 ▶ shift if $_[0] && $_[0] eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and $_[0] eq __PACKAGE__ 3 0 3
Condition33%1/3
$_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
003
MC/DC0%0/2
$_[0] and $_[0] eq __PACKAGE__
$_[0] $_[0] eq __PACKAGE__
Truth table33%1/3
A and B
A= $_[0]B= $_[0] eq __PACKAGE__
ABresult
0X0
100
111
166 3 for my $handler (@_) {
167 3 ▶ next unless $handler;
Branch: 1 branch
BranchTrueFalseTotal
unless $handler 0 3 3
168 3 ▶ if ( $handler->can('support_protocol')
Branch: 1 branch
BranchTrueFalseTotal
if ($handler->can('support_protocol') and $handler->can('support_method')) { } 2 1 3
Condition66%2/3
$handler->can("support_protocol") and $handler->can("support_method")
!ll&&!rl&&r
102
MC/DC50%1/2
$handler->can("support_protocol") and $handler->can("support_method")
$handler->can("support_protocol") $handler->can("support_method")
Truth table66%2/3
A and B
A= $handler->can("support_protocol")B= $handler->can("support_method")
ABresult
0X0
100
111
169 && $handler->can('support_method') )
170 {
171 2 $all_handlers{$handler} = ();
172 }
173 else {
174 1 warn "$handler is not a valid Plient handler";
175 }
176 }
177  
178 3 return keys %all_handlers;
179 }
180  
181 0 ▶sub handlers {
Subroutine handlers: not called CC=8 SCAR=42.8
Pod undocumented
182 0 ▶ shift if $_[0] && $_[0] eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and $_[0] eq __PACKAGE__ 0 0 0
Condition0%0/3
$_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and $_[0] eq __PACKAGE__
$_[0] $_[0] eq __PACKAGE__
Truth table0%0/3
A and B
A= $_[0]B= $_[0] eq __PACKAGE__
ABresult
0X0
100
111
183 0 ▶ if ( my $protocol = lc shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $protocol = lc shift()) { } 0 0 0
184 my %map =
185 0 map { $_ => 1 }
186 0 grep { $_->may_support_protocol($protocol) } all_handlers();
187 0 my @handlers;
188 0 my $preference = handler_preference($protocol);
189 0 ▶ if ($preference) {
Branch: 1 branch
BranchTrueFalseTotal
if ($preference) 0 0 0
190 @handlers =
191 0 ▶ map { /^Plient::Handler::/ ? $_ : "Plient::Handler::$_" }
Branch: 1 branch
BranchTrueFalseTotal
/^Plient::Handler::/ ? : 0 0 0
192 grep {
193 0 $_ =~ /::/
194 ? delete $map{$_}
195 0 ▶ : delete $map{"Plient::Handler::$_"}
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /::/ ? : 0 0 0
196 } @$preference;
197 }
198 0 ▶ push @handlers, keys %map unless $ENV{PLIENT_HANDLER_PREFERENCE_STRICT};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"PLIENT_HANDLER_PREFERENCE_STRICT"} 0 0 0
199 0 return @handlers;
200 }
201 else {
202 # fallback to return all the handlers
203 0 return keys %all_handlers;
204 }
205 }
206  
207 1 ▶sub find_handlers {
Subroutine find_handlers: called CC=8 SCAR=22.2
Pod undocumented
208 1 $found_handlers = 1;
209 1 ▶ return if $bundle_mode;
Branch: 1 branch
BranchTrueFalseTotal
if $Plient::bundle_mode 0 1 1
210 1 my @hd;
211 1 for my $inc (@INC) {
212 8 my $handler_dir = catdir( $inc, 'Plient', 'Handler' );
213 8 ▶ if ( -e $handler_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $handler_dir) 3 5 8
214 3 ▶ if ( opendir my $dh, $handler_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (opendir my $dh, $handler_dir) { } 3 0 3
215 push @hd,
216 3 ▶ map { /(\w+)\.pm/ ? "Plient::Handler::$1" : () } readdir $dh;
Branch: 1 branch
BranchTrueFalseTotal
/(\w+)\.pm/ ? : 15 6 21
217 }
218 else {
219 0 warn "can't read $handler_dir";
220 }
221 }
222 }
223  
224 1 ▶ @hd = grep { eval "require $_" or warn "failed to require $_" and 0 } @hd;
Condition20%1/5
eval "require $_" || warn("failed to require $_") && 0
l!l&&r!l&&!r
1500
warn "failed to require $_" and 0
!ll
00
MC/DC0%0/2
eval "require $_" || warn("failed to require $_") && 0
eval "require $_" warn "failed to require $_"
Truth table33%1/3
A || B
A= eval "require $_"B= warn "failed to require $_"
ABresult
1X1
011
000
225  
226 1 @hd;
227 }
228  
229 5 my %handler_preference = (
230 http => [qw/curl wget HTTPTiny HTTPLite LWP/],
231 https => [qw/curl wget HTTPTiny LWP/],
232 );
233 5 ▶if ( my $env = $ENV{PLIENT_HANDLER_PREFERENCE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $env = $ENV{"PLIENT_HANDLER_PREFERENCE"}) 0 5 5
234 0 my %entry = map { split /:/, $_, 2 } split /;/, $env;
235 0 ▶ %entry = map { $_ => [ split /,/, $entry{$_} || '' ] } keys %entry;
Condition0%0/2
$entry{$_} || ''
l!l
00
MC/DC0%0/1
$entry{$_} || ''
$entry{$_}
Truth table0%0/2
A
A= $entry{$_}
Aresult
11
00
236 0 for my $p ( keys %entry ) {
237 0 $handler_preference{$p} = $entry{$p};
238 }
239 }
240  
241 0 ▶sub handler_preference {
Subroutine handler_preference: not called CC=5 SCAR=34.0
Pod undocumented
242 0 ▶ shift if $_[0] && $_[0] eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and $_[0] eq __PACKAGE__ 0 0 0
Condition0%0/3
$_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and $_[0] eq __PACKAGE__
$_[0] $_[0] eq __PACKAGE__
Truth table0%0/3
A and B
A= $_[0]B= $_[0] eq __PACKAGE__
ABresult
0X0
100
111
243 0 my ( $protocol, $handlers ) = @_;
244 0 $protocol = lc $protocol;
245 0 ▶ if ($handlers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handlers) { } 0 0 0
246 0 ▶ if ( ref $handlers eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $handlers eq 'ARRAY') { } 0 0 0
247 0 return $handler_preference{ $protocol } = $handlers;
248 }
249 else {
250 0 warn "handlers shold be an arrayref";
251 0 return;
252 }
253 }
254 else {
255 0 return $handler_preference{ $protocol };
256 }
257 }
258  
259  
260 5 1;
261  
262 __END__
↑ Summary Plient/Handler/LWP.pm »