Plient/Handler/wget.pm

filter: Statementstmt 15.7% 15 / 95 Branchbran 6.8% 3 / 44 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 16.6% 2 / 12 Podpod 0.0% 0 / 7 totaltotal 10.4% 20 / 191 CC 30 SCAR 36.9
CC
30
Cov
11%
init
90.0
__ANON__
42.0
translate_auth
42.0
CRAP 40.1
« Plient/Handler/HTTPTiny.pm ↑ Summary Plient/Handler/HTTPLite.pm »
1 package Plient::Handler::wget;
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 ( $wget, %protocol, %all_protocol, %method );
12  
13 0 ▶sub all_protocol { return \%all_protocol }
Subroutine all_protocol: not called CC=1 SCAR=6.9
Pod undocumented
14  
15 5 @all_protocol{qw/http https ftp/} = ();
16  
17 0 ▶sub protocol { return \%protocol }
Subroutine protocol: not called CC=1 SCAR=6.9
Pod undocumented
18 0 ▶sub method { return \%method }
Subroutine method: not called CC=1 SCAR=6.9
Pod undocumented
19  
20 0 ▶sub support_method {
Subroutine support_method: not called CC=4 SCAR=30.0
Pod undocumented
21 0 my $class = shift;
22 0 my ( $method, $args ) = @_;
23 0 ▶ if ( $args
Branch: 1 branch
BranchTrueFalseTotal
if ($args and $args->{"content_type"} and $args->{"content_type"} =~ /form-data/) 0 0 0
Condition0%0/6
$args and $args->{"content_type"} and $args->{"content_type"} =~ /form-data/
!ll&&!rl&&r
000
$args and $args->{"content_type"}
!ll&&!rl&&r
000
MC/DC0%0/3
$args and $args->{"content_type"} and $args->{"content_type"} =~ /form-data/
$args $args->{"content_type"} $args->{"content_type"} =~ /form-data/
Truth table0%0/4
A and B and C
A= $argsB= $args->{"content_type"}C= $args->{"content_type"} =~ /form-data/
ABCresult
0XX0
10X0
1100
1111
24 && $args->{content_type}
25 && $args->{content_type} =~ 'form-data' )
26 {
27 0 return;
28 }
29  
30 0 return $class->SUPER::support_method(@_);
31 }
32  
33 5 my $inited;
34 0 ▶sub init {
Subroutine init: not called CC=9 SCAR=45.0
Pod undocumented
35 0 ▶ return if $inited;
Branch: 1 branch
BranchTrueFalseTotal
if $inited 0 0 0
36 0 $inited = 1;
37  
38 0 ▶ $wget = $ENV{PLIENT_WGET} || which('wget');
Condition0%0/3
$ENV{'PLIENT_WGET'} || which('wget')
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'PLIENT_WGET'} || which('wget')
$ENV{'PLIENT_WGET'} which('wget')
Truth table0%0/3
A || B
A= $ENV{'PLIENT_WGET'}B= which('wget')
ABresult
1X1
011
000
39 0 ▶ return unless $wget;
Branch: 1 branch
BranchTrueFalseTotal
unless $wget 0 0 0
40  
41 0 @protocol{qw/http https ftp/} = ();
42  
43 {
44 0 local $ENV{LC_ALL} = 'en_US';
45 0 my $message = `$wget https:// 2>&1`;
46 0 ▶ if ( $message && $message =~ /HTTPS support not compiled in/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message and $message =~ /HTTPS support not compiled in/i) 0 0 0
Condition0%0/3
$message and $message =~ /HTTPS support not compiled in/i
!ll&&!rl&&r
000
MC/DC0%0/2
$message and $message =~ /HTTPS support not compiled in/i
$message $message =~ /HTTPS support not compiled in/i
Truth table0%0/3
A and B
A= $messageB= $message =~ /HTTPS support not compiled in/i
ABresult
0X0
100
111
47 0 delete $protocol{https};
48 }
49 }
50  
51 0 ▶ $method{http_get} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
52 0 my ( $uri, $args ) = @_;
53 0 my $headers = translate_headers( $args );
54 0 my $auth = translate_auth($args);
55 0 ▶ if ( open my $fh, "$wget -q -O - $headers $auth '$uri' |" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "$wget -q -O - $headers $auth '${uri}' |") { } 0 0 0
56 0 local $/;
57 0 <$fh>;
58 }
59 else {
60 0 warn "failed to get $uri with wget: $!";
61 0 return;
62 }
63 0 };
64  
65 0 ▶ $method{http_post} = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
66 0 my ( $uri, $args ) = @_;
67 0 my $headers = translate_headers( $args );
68 0 my $auth = translate_auth($args);
69  
70 0 my $data = '';
71 0 ▶ if ( $args->{body_array} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"body_array"}) 0 0 0
72 0 my $body = $args->{body_array};
73  
74 0 ▶ for ( my $i = 0 ; $i < $#$body ; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $body->$#*) 0 0 0
75 0 my $key = $body->[$i];
76 0 ▶ my $value = defined $body->[ $i + 1 ] ? $body->[ $i + 1 ] : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $$body[$i + 1] ? : 0 0 0
77 0 $data .= " --post-data $key=$value";
78 }
79 }
80  
81 0 ▶ if ( open my $fh, "$wget -q -O - $data $headers $auth '$uri' |" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "$wget -q -O - $data $headers $auth '${uri}' |") { } 0 0 0
82 0 local $/;
83 0 <$fh>;
84 }
85 else {
86 0 warn "failed to post $uri with curl: $!";
87 0 return;
88 }
89 0 };
90  
91 0 ▶ $method{http_head} = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
92 0 my ( $uri, $args ) = @_;
93 # we can't use -q here, or some version may not show the header
94 0 my $headers = translate_headers( $args );
95 0 my $auth = translate_auth($args);
96 0 ▶ if ( open my $fh, "$wget -S --spider $headers $auth '$uri' 2>&1 |" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "$wget -S --spider $headers $auth '${uri}' 2>&1 |") { } 0 0 0
97 0 my $head = '';
98 0 my $flag;
99 0 ▶ while ( my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
100 # yeah, the head output has 2 spaces as indents
101 0 ▶ if ( $line =~ m{^\s{2}HTTP} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s{2}HTTP/) 0 0 0
102 0 $flag = 1;
103 }
104  
105 0 ▶ if ($flag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag) 0 0 0
106 0 ▶ if ($line =~ s/^\s{2}(?=\S)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/^\s{2}(?=\S)//) { } 0 0 0
107 0 $head .= $line;
108 }
109 else {
110 0 undef $flag;
111 0 last;
112 }
113 }
114 }
115 0 return $head;
116 }
117 else {
118 0 warn "failed to get head of $uri with wget: $!";
119 0 return;
120 }
121 0 };
122  
123 0 ▶ if ( exists $protocol{https} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $protocol{"https"}) 0 0 0
124 0 for my $m (qw/get post head put/) {
125 $method{"https_$m"} = $method{"http_$m"}
126 0 ▶ if exists $method{"http_$m"};
Branch: 1 branch
BranchTrueFalseTotal
if exists $method{"http_$m"} 0 0 0
127 }
128 }
129  
130 0 return 1;
131 }
132  
133 0 ▶sub translate_headers {
Subroutine translate_headers: not called CC=4 SCAR=30.0
Pod undocumented
134 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
135 0 my $headers = $args->{headers};
136 0 ▶ return '' unless $headers;
Branch: 1 branch
BranchTrueFalseTotal
unless $headers 0 0 0
137 0 my $str;
138 0 for my $k ( keys %$headers ) {
139 0 $str .= " --header '$k:$headers->{$k}'";
140 }
141 0 return $str;
142 }
143  
144 0 ▶sub translate_auth {
Subroutine translate_auth: not called CC=6 SCAR=37.4
Pod undocumented
145 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
146 0 my $auth = '';
147 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
148 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
149 0 ▶ if ( $method eq 'basic' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'basic') { } 0 0 0
150 0 $auth =
151 " --user '$args->{user}' --password '$args->{password}'";
152 }
153 else {
154 0 die "aborting: unsupported auth method: $method";
155 }
156 }
157 0 return $auth;
158 }
159  
160 5 ▶__PACKAGE__->_add_to_plient if $Plient::bundle_mode;
Branch: 1 branch
BranchTrueFalseTotal
if $Plient::bundle_mode 0 5 5
161  
162 5 1;
163  
164 __END__
« Plient/Handler/HTTPTiny.pm ↑ Summary Plient/Handler/HTTPLite.pm »