line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Plient::Handler::wget; |
2
|
5
|
|
|
5
|
|
4612
|
use strict; |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
174
|
|
3
|
5
|
|
|
5
|
|
27
|
use warnings; |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
7803
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
require Plient::Handler unless $Plient::bundle_mode; |
6
|
|
|
|
|
|
|
our @ISA = 'Plient::Handler'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
require Plient::Util unless $Plient::bundle_mode; |
9
|
|
|
|
|
|
|
Plient::Util->import; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
my ( $wget, %protocol, %all_protocol, %method ); |
12
|
|
|
|
|
|
|
|
13
|
0
|
|
|
0
|
0
|
|
sub all_protocol { return \%all_protocol } |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
@all_protocol{qw/http https ftp/} = (); |
16
|
|
|
|
|
|
|
|
17
|
0
|
|
|
0
|
0
|
|
sub protocol { return \%protocol } |
18
|
0
|
|
|
0
|
0
|
|
sub method { return \%method } |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub support_method { |
21
|
0
|
|
|
0
|
0
|
|
my $class = shift; |
22
|
0
|
|
|
|
|
|
my ( $method, $args ) = @_; |
23
|
0
|
0
|
0
|
|
|
|
if ( $args |
|
|
|
0
|
|
|
|
|
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
|
|
|
|
|
|
|
my $inited; |
34
|
|
|
|
|
|
|
sub init { |
35
|
0
|
0
|
|
0
|
0
|
|
return if $inited; |
36
|
0
|
|
|
|
|
|
$inited = 1; |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
0
|
|
|
|
$wget = $ENV{PLIENT_WGET} || which('wget'); |
39
|
0
|
0
|
|
|
|
|
return unless $wget; |
40
|
|
|
|
|
|
|
|
41
|
0
|
|
|
|
|
|
@protocol{qw/http https ftp/} = (); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
{ |
44
|
0
|
|
|
|
|
|
local $ENV{LC_ALL} = 'en_US'; |
|
0
|
|
|
|
|
|
|
45
|
0
|
|
|
|
|
|
my $message = `$wget https:// 2>&1`; |
46
|
0
|
0
|
0
|
|
|
|
if ( $message && $message =~ /HTTPS support not compiled in/i ) { |
47
|
0
|
|
|
|
|
|
delete $protocol{https}; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
$method{http_get} = sub { |
52
|
0
|
|
|
0
|
|
|
my ( $uri, $args ) = @_; |
53
|
0
|
|
|
|
|
|
my $headers = translate_headers( $args ); |
54
|
0
|
|
|
|
|
|
my $auth = translate_auth($args); |
55
|
0
|
0
|
|
|
|
|
if ( open my $fh, "$wget -q -O - $headers $auth '$uri' |" ) { |
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
|
|
|
|
|
|
|
$method{http_post} = sub { |
66
|
0
|
|
|
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
|
0
|
|
|
|
|
if ( $args->{body_array} ) { |
72
|
0
|
|
|
|
|
|
my $body = $args->{body_array}; |
73
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
for ( my $i = 0 ; $i < $#$body ; $i += 2 ) { |
75
|
0
|
|
|
|
|
|
my $key = $body->[$i]; |
76
|
0
|
0
|
|
|
|
|
my $value = defined $body->[ $i + 1 ] ? $body->[ $i + 1 ] : ''; |
77
|
0
|
|
|
|
|
|
$data .= " --post-data $key=$value"; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
0
|
0
|
|
|
|
|
if ( open my $fh, "$wget -q -O - $data $headers $auth '$uri' |" ) { |
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
|
|
|
|
|
|
|
$method{http_head} = sub { |
92
|
0
|
|
|
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
|
0
|
|
|
|
|
if ( open my $fh, "$wget -S --spider $headers $auth '$uri' 2>&1 |" ) { |
97
|
0
|
|
|
|
|
|
my $head = ''; |
98
|
0
|
|
|
|
|
|
my $flag; |
99
|
0
|
|
|
|
|
|
while ( my $line = <$fh>) { |
100
|
|
|
|
|
|
|
# yeah, the head output has 2 spaces as indents |
101
|
0
|
0
|
|
|
|
|
if ( $line =~ m{^\s{2}HTTP} ) { |
102
|
0
|
|
|
|
|
|
$flag = 1; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
0
|
0
|
|
|
|
|
if ($flag) { |
106
|
0
|
0
|
|
|
|
|
if ($line =~ s/^\s{2}(?=\S)//) { |
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
|
0
|
|
|
|
|
if ( exists $protocol{https} ) { |
124
|
0
|
|
|
|
|
|
for my $m (qw/get post head put/) { |
125
|
0
|
0
|
|
|
|
|
$method{"https_$m"} = $method{"http_$m"} |
126
|
|
|
|
|
|
|
if exists $method{"http_$m"}; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
0
|
|
|
|
|
|
return 1; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub translate_headers { |
134
|
0
|
|
0
|
0
|
0
|
|
my $args = shift || {}; |
135
|
0
|
|
|
|
|
|
my $headers = $args->{headers}; |
136
|
0
|
0
|
|
|
|
|
return '' unless $headers; |
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
|
|
|
|
|
|
|
sub translate_auth { |
145
|
0
|
|
0
|
0
|
0
|
|
my $args = shift || {}; |
146
|
0
|
|
|
|
|
|
my $auth = ''; |
147
|
0
|
0
|
0
|
|
|
|
if ( $args->{user} && defined $args->{password} ) { |
148
|
0
|
|
0
|
|
|
|
my $method = lc $args->{auth_method} || 'basic'; |
149
|
0
|
0
|
|
|
|
|
if ( $method eq 'basic' ) { |
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
|
|
|
|
|
|
|
__PACKAGE__->_add_to_plient if $Plient::bundle_mode; |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
1; |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
__END__ |