Plient/Handler/LWP.pm

filter: Statementstmt 19.4% 14 / 72 Branchbran 5.8% 2 / 34 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 22.2% 2 / 9 Podpod 0.0% 0 / 5 totaltotal 12.4% 18 / 145 CC 23 SCAR 43.6
CC
23
Cov
13%
__ANON__
156.0
add_headers
56.0
init
30.0
CRAP 78.5
« Plient.pm ↑ Summary Plient/Handler/curl.pm »
1 package Plient::Handler::LWP;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 ▶require Plient::Handler unless $Plient::bundle_mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $Plient::bundle_mode 5 0 5
6 5 our @ISA = 'Plient::Handler';
7 5 my ( $LWP, %all_protocol, %protocol, %method );
8  
9 5 %all_protocol = map { $_ => undef }
10 qw/http https ftp news gopher file mailto cpan data ldap nntp/;
11 0 ▶sub all_protocol { return \%all_protocol }
Subroutine all_protocol: not called CC=1 SCAR=6.9
Pod undocumented
12 0 ▶sub protocol { return \%protocol }
Subroutine protocol: not called CC=1 SCAR=6.9
Pod undocumented
13 0 ▶sub method { return \%method }
Subroutine method: not called CC=1 SCAR=6.9
Pod undocumented
14  
15 5 my $inited;
16 0 ▶sub init {
Subroutine init: not called CC=5 SCAR=34.0
Pod undocumented
17 0 ▶ return if $inited;
Branch: 1 branch
BranchTrueFalseTotal
if $inited 0 0 0
18 0 $inited = 1;
19 0 ▶ eval { require LWP::UserAgent } or return;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require LWP::UserAgent } } or return 0 0 0
20
21 0 undef $protocol{http};
22  
23 0 ▶ if ( eval { require Crypt::SSLeay } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Crypt::SSLeay } }) 0 0 0
24 0 undef $protocol{https};
25 }
26  
27 0 ▶ $method{http_get} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
28 0 my ( $uri, $args ) = @_;
29  
30 # XXX TODO tweak the new arguments
31 0 my $ua = LWP::UserAgent->new;
32 0 $ua->env_proxy;
33 0 add_headers( $ua, $uri, $args );
34 0 my $res = $ua->get($uri);
35 0 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
36 0 return $res->decoded_content;
37 }
38 else {
39 0 warn "failed to get $uri with lwp: " . $res->status_line;
40 0 return;
41 }
42 0 };
43  
44 0 ▶ $method{http_post} = sub {
Subroutine __ANON__: not called CC=12 SCAR=50.5
45 0 my ( $uri, $args ) = @_;
46  
47 # XXX TODO tweak the new arguments
48 0 my $ua = LWP::UserAgent->new;
49 0 $ua->env_proxy;
50 0 add_headers( $ua, $uri, $args );
51  
52 0 my $content = [];
53 0 my $is_form_data;
54 0 ▶ if ( $args->{body}
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'body'} and $args->{'content_type'} and $args->{'content_type'} =~ /form-data/) { } 0 0 0
Condition0%0/6
$args->{"body"} and $args->{"content_type"} and $args->{"content_type"} =~ /form-data/
!ll&&!rl&&r
000
$args->{"body"} and $args->{"content_type"}
!ll&&!rl&&r
000
MC/DC0%0/3
$args->{"body"} and $args->{"content_type"} and $args->{"content_type"} =~ /form-data/
$args->{"body"} $args->{"content_type"} $args->{"content_type"} =~ /form-data/
Truth table0%0/4
A and B and C
A= $args->{"body"}B= $args->{"content_type"}C= $args->{"content_type"} =~ /form-data/
ABCresult
0XX0
10X0
1100
1111
55 && $args->{content_type}
56 && $args->{content_type} =~ /form-data/ )
57 {
58 0 $is_form_data = 1;
59 0 ▶ if ( $args->{body_array} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"body_array"}) 0 0 0
60 0 my $body = $args->{body_array};
61  
62 0 ▶ for ( my $i = 0 ; $i < $#$body ; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $body->$#*) 0 0 0
63 0 my $key = $body->[$i];
64 0 ▶ my $value =
Branch: 1 branch
BranchTrueFalseTotal
defined $$body[$i + 1] ? : 0 0 0
65 defined $body->[ $i + 1 ] ? $body->[ $i + 1 ] : '';
66 0 ▶ if ( ref $value eq 'HASH' && $value->{file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'HASH' and $value->{'file'}) { } 0 0 0
Condition0%0/3
ref $value eq "HASH" and $value->{"file"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $value eq "HASH" and $value->{"file"}
ref $value eq "HASH" $value->{"file"}
Truth table0%0/3
A and B
A= ref $value eq "HASH"B= $value->{"file"}
ABresult
0X0
100
111
67  
68 # file upload
69 0 push @$content, $key, [ $value->{file} ];
70 }
71 else {
72 0 push @$content, $key, $value;
73 }
74 }
75 }
76 }
77 else {
78 0 $content = $args->{body_array};
79 }
80  
81 my $res = $ua->post(
82 $uri,
83 (
84 $is_form_data
85 ? ( Content_Type => 'form−data' )
86 : ()
87 ),
88 0 ▶ $args->{body} ? ( Content => $content ) : (),
Branch: 2 branches
BranchTrueFalseTotal
$is_form_data ? : 0 0 0
$args->{'body'} ? : 0 0 0
89 );
90  
91 0 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
92 0 return $res->decoded_content;
93 }
94 else {
95 0 warn "failed to get $uri with lwp: " . $res->status_line;
96 0 return;
97 }
98 0 };
99  
100 # XXX there is no official way to get the *origin* header output :/
101 # $res->headers->as_string isn't exactly the same head output
102 # e.g. it adds Client-... headers, and lacking the first line:
103 # HTTP/1.0 200 OK
104 #
105 #
106 # $method{http_head} = sub {
107 # my ( $uri, $args ) = @_;
108 #
109 # my $ua = LWP::UserAgent->new;
110 # my $res = $ua->head($uri);
111 # if ( $res->is_success ) {
112 # return $res->headers->as_string;
113 # }
114 # else {
115 # warn "failed to get head of $uri with lwp: " . $res->status_line;
116 # return;
117 # }
118 # };
119  
120 0 ▶ if ( exists $protocol{https} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $protocol{"https"}) 0 0 0
121 # have you seen https is available while http is not?
122 0 $method{https_get} = $method{http_get};
123 0 $method{https_post} = $method{http_post};
124 }
125 0 return 1;
126 }
127  
128 0 ▶sub add_headers {
Subroutine add_headers: not called CC=7 SCAR=40.3
Pod undocumented
129 0 my ( $ua, $uri, $args ) = @_;
130 0 ▶ my $headers = $args->{headers} || {};
Condition0%0/2
$args->{'headers'} || {}
l!l
00
MC/DC0%0/1
$args->{'headers'} || {}
$args->{'headers'}
Truth table0%0/2
A
A= $args->{'headers'}
Aresult
11
00
131 0 for my $k ( keys %$headers ) {
132 0 $ua->default_header( $k, $headers->{$k} );
133 }
134  
135 0 ▶ if ( $args->{user} && defined $args->{password} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"user"} and defined $args->{"password"}) 0 0 0
Condition0%0/3
$args->{"user"} and defined $args->{"password"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args->{"user"} and defined $args->{"password"}
$args->{"user"} defined $args->{"password"}
Truth table0%0/3
A and B
A= $args->{"user"}B= defined $args->{"password"}
ABresult
0X0
100
111
136 0 ▶ my $method = lc $args->{auth_method} || 'basic';
Condition0%0/2
lc $args->{'auth_method'} || 'basic'
l!l
00
MC/DC0%0/1
lc $args->{'auth_method'} || 'basic'
lc $args->{'auth_method'}
Truth table0%0/2
A
A= lc $args->{'auth_method'}
Aresult
11
00
137 0 ▶ if ( $method eq 'basic' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'basic') { } 0 0 0
138 0 require MIME::Base64;
139 0 $ua->default_header(
140 "Authorization",
141 'Basic '
142 . MIME::Base64::encode_base64(
143 "$args->{user}:$args->{password}", ''
144 )
145 )
146 }
147 else {
148 0 die "aborting: unsupported auth method: $method";
149 }
150 }
151 }
152  
153 5 ▶__PACKAGE__->_add_to_plient if $Plient::bundle_mode;
Branch: 1 branch
BranchTrueFalseTotal
if $Plient::bundle_mode 0 5 5
154  
155 5 1;
156  
157 __END__
« Plient.pm ↑ Summary Plient/Handler/curl.pm »