Plient/Handler/curl.pm

filter: Statementstmt 16.6% 16 / 96 Branchbran 6.2% 3 / 48 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 18.1% 2 / 11 Podpod 0.0% 0 / 6 totaltotal 10.7% 21 / 195 CC 32 SCAR 42.6
CC
32
Cov
12%
init
132.0
__ANON__
110.0
translate_auth
42.0
CRAP 70.6
« Plient/Handler/LWP.pm ↑ Summary Plient/Handler/HTTPTiny.pm »
1 package Plient::Handler::curl;
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  
8 5 ▶require Plient::Util unless $Plient::bundle_mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $Plient::bundle_mode 5 0 5
9 5 Plient::Util->import;
10  
11 5 my ( $curl, $curl_config, %all_protocol, %protocol, %method );
12  
13 %all_protocol =
14 5 map { $_ => undef } qw/http https ftp ftps file telnet ldap dict tftp/;
15 0 ▶sub all_protocol { return \%all_protocol }
Subroutine all_protocol: not called CC=1 SCAR=6.9
Pod undocumented
16 0 ▶sub protocol { return \%protocol }
Subroutine protocol: not called CC=1 SCAR=6.9
Pod undocumented
17 0 ▶sub method { return \%method }
Subroutine method: not called CC=1 SCAR=6.9
Pod undocumented
18  
19 5 my $inited;
20 0 ▶sub init {
Subroutine init: not called CC=11 SCAR=48.8
Pod undocumented
21 0 ▶ return if $inited;
Branch: 1 branch
BranchTrueFalseTotal
if $inited 0 0 0
22 0 $inited = 1;
23 0 ▶ $curl = $ENV{PLIENT_CURL} || which('curl');
Condition0%0/3
$ENV{'PLIENT_CURL'} || which('curl')
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'PLIENT_CURL'} || which('curl')
$ENV{'PLIENT_CURL'} which('curl')
Truth table0%0/3
A || B
A= $ENV{'PLIENT_CURL'}B= which('curl')
ABresult
1X1
011
000
24 0 ▶ $curl_config = $ENV{PLIENT_CURL_CONFIG} || which('curl-config');
Condition0%0/3
$ENV{'PLIENT_CURL_CONFIG'} || which('curl-config')
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'PLIENT_CURL_CONFIG'} || which('curl-config')
$ENV{'PLIENT_CURL_CONFIG'} which('curl-config')
Truth table0%0/3
A || B
A= $ENV{'PLIENT_CURL_CONFIG'}B= which('curl-config')
ABresult
1X1
011
000
25 0 ▶ return unless $curl;
Branch: 1 branch
BranchTrueFalseTotal
unless $curl 0 0 0
26 0 ▶ if ($curl_config) {
Branch: 1 branch
BranchTrueFalseTotal
if ($curl_config) { } 0 0 0
27 0 ▶ if ( my $out = `$curl_config --protocols` ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $out = `$curl_config --protocols`) { } 0 0 0
28 0 @protocol{ map { lc } split /\r?\n/, $out } = ();
29 }
30 else {
31 0 warn $!;
32 0 return;
33 }
34 }
35 else {
36 # by default, curl should support http
37 0 %protocol = ( http => undef );
38 }
39  
40 0 ▶ if ( exists $protocol{http} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $protocol{"http"}) 0 0 0
41 0 ▶ $method{http_get} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
42 0 my ( $uri, $args ) = @_;
43 0 my $headers = translate_headers( $args );
44 0 my $auth = translate_auth($args);
45 0 ▶ warn "$curl -k -s -L $headers $auth $uri\n" if $ENV{PLIENT_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"PLIENT_DEBUG"} 0 0 0
46 0 ▶ if ( open my $fh, "$curl -k -s -L $headers $auth '$uri' |" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "$curl -k -s -L $headers $auth '${uri}' |") { } 0 0 0
47 0 local $/;
48 0 <$fh>;
49 }
50 else {
51 0 warn "failed to get $uri with curl: $!";
52 0 return;
53 }
54 0 };
55  
56 0 ▶ $method{http_post} = sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
57 0 my ( $uri, $args ) = @_;
58  
59  
60 0 my $headers = translate_headers($args);
61 0 my $auth = translate_auth($args);
62  
63 0 my $data = '';
64 0 my $post_opt;
65 0 ▶ if ( $args->{content_type} && $args->{content_type} =~ /form-data/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'content_type'} and $args->{'content_type'} =~ /form-data/) { } 0 0 0
Condition0%0/3
$args->{"content_type"} and $args->{"content_type"} =~ /form-data/
!ll&&!rl&&r
000
MC/DC0%0/2
$args->{"content_type"} and $args->{"content_type"} =~ /form-data/
$args->{"content_type"} $args->{"content_type"} =~ /form-data/
Truth table0%0/3
A and B
A= $args->{"content_type"}B= $args->{"content_type"} =~ /form-data/
ABresult
0X0
100
111
66 {
67 0 $post_opt = '-F';
68 }
69 else {
70 0 $post_opt = '-d';
71 }
72  
73 0 ▶ if ( $args->{body_array} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"body_array"}) 0 0 0
74 0 my $body = $args->{body_array};
75  
76 0 ▶ for ( my $i = 0 ; $i < $#$body ; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $body->$#*) 0 0 0
77 0 my $key = $body->[$i];
78 0 ▶ my $value =
Branch: 1 branch
BranchTrueFalseTotal
defined $$body[$i + 1] ? : 0 0 0
79 defined $body->[ $i + 1 ] ? $body->[ $i + 1 ] : '';
80 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
81  
82 # file upload
83 0 $data .= " $post_opt '$key=\@$value->{file}'";
84 }
85 else {
86 0 $data .= " $post_opt '$key=$value'";
87 }
88 }
89 }
90  
91 0 ▶ warn "$curl -s -L $data $headers $auth $uri\n" if $ENV{PLIENT_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"PLIENT_DEBUG"} 0 0 0
92 0 ▶ if ( open my $fh, "$curl -s -L $data $headers $auth '$uri' |" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "$curl -s -L $data $headers $auth '${uri}' |") { } 0 0 0
93 0 local $/;
94 0 <$fh>;
95 }
96 else {
97 0 warn "failed to post $uri with curl: $!";
98 0 return;
99 }
100 0 };
101 0 ▶ $method{http_head} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
102 0 my ( $uri, $args ) = @_;
103 0 my $headers = translate_headers( $args );
104 0 my $auth = translate_auth($args);
105 0 ▶ warn "$curl -s -I -L $headers $auth $uri\n" if $ENV{PLIENT_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"PLIENT_DEBUG"} 0 0 0
106 0 ▶ if ( open my $fh, "$curl -s -I -L $headers $auth '$uri' |" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "$curl -s -I -L $headers $auth '${uri}' |") { } 0 0 0
107 0 local $/;
108 0 my $head = <$fh>;
109 0 $head =~ s/\r\n$//;
110 0 return $head;
111 }
112 else {
113 0 warn "failed to get head of $uri with curl: $!";
114 0 return;
115 }
116 0 };
117 }
118  
119 0 ▶ if ( exists $protocol{https} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $protocol{"https"}) 0 0 0
120 0 for my $m (qw/get post head put/) {
121 $method{"https_$m"} = $method{"http_$m"}
122 0 ▶ if exists $method{"http_$m"};
Branch: 1 branch
BranchTrueFalseTotal
if exists $method{"http_$m"} 0 0 0
123 }
124 }
125 0 return 1;
126 }
127  
128 0 ▶sub translate_headers {
Subroutine translate_headers: not called CC=4 SCAR=30.0
Pod undocumented
129 0 ▶ my $args = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
130 0 my $headers = $args->{headers};
131 0 ▶ return '' unless $headers;
Branch: 1 branch
BranchTrueFalseTotal
unless $headers 0 0 0
132 0 my $str;
133 0 for my $k ( keys %$headers ) {
134 0 $str .= " -H '$k:$headers->{$k}'";
135 }
136 0 return $str;
137  
138 }
139  
140 0 ▶sub translate_auth {
Subroutine translate_auth: not called CC=6 SCAR=37.4
Pod undocumented
141 0 ▶ my $args = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
142 0 my $auth = '';
143 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
144 0 ▶ my $method = lc( $args->{auth_method} || 'basic' );
Condition0%0/2
$args->{'auth_method'} || 'basic'
l!l
00
MC/DC0%0/1
$args->{'auth_method'} || 'basic'
$args->{'auth_method'}
Truth table0%0/2
A
A= $args->{'auth_method'}
Aresult
11
00
145 0 ▶ if ( $method eq 'basic' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'basic') { } 0 0 0
146 0 $auth = " -u '$args->{user}:$args->{password}'";
147 }
148 else {
149 0 die "aborting: unsupported auth method: $method";
150 }
151 }
152 0 return $auth;
153 }
154  
155 5 ▶__PACKAGE__->_add_to_plient if $Plient::bundle_mode;
Branch: 1 branch
BranchTrueFalseTotal
if $Plient::bundle_mode 0 5 5
156  
157 5 1;
158  
159 __END__
« Plient/Handler/LWP.pm ↑ Summary Plient/Handler/HTTPTiny.pm »