| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Plient::Handler::LWP; |
|
2
|
5
|
|
|
5
|
|
3285
|
use strict; |
|
|
5
|
|
|
|
|
11
|
|
|
|
5
|
|
|
|
|
192
|
|
|
3
|
5
|
|
|
5
|
|
28
|
use warnings; |
|
|
5
|
|
|
|
|
10
|
|
|
|
5
|
|
|
|
|
5120
|
|
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
require Plient::Handler unless $Plient::bundle_mode; |
|
6
|
|
|
|
|
|
|
our @ISA = 'Plient::Handler'; |
|
7
|
|
|
|
|
|
|
my ( $LWP, %all_protocol, %protocol, %method ); |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
%all_protocol = map { $_ => undef } |
|
10
|
|
|
|
|
|
|
qw/http https ftp news gopher file mailto cpan data ldap nntp/; |
|
11
|
0
|
|
|
0
|
0
|
|
sub all_protocol { return \%all_protocol } |
|
12
|
0
|
|
|
0
|
0
|
|
sub protocol { return \%protocol } |
|
13
|
0
|
|
|
0
|
0
|
|
sub method { return \%method } |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my $inited; |
|
16
|
|
|
|
|
|
|
sub init { |
|
17
|
0
|
0
|
|
0
|
0
|
|
return if $inited; |
|
18
|
0
|
|
|
|
|
|
$inited = 1; |
|
19
|
0
|
0
|
|
|
|
|
eval { require LWP::UserAgent } or return; |
|
|
0
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
|
|
21
|
0
|
|
|
|
|
|
undef $protocol{http}; |
|
22
|
|
|
|
|
|
|
|
|
23
|
0
|
0
|
|
|
|
|
if ( eval { require Crypt::SSLeay } ) { |
|
|
0
|
|
|
|
|
|
|
|
24
|
0
|
|
|
|
|
|
undef $protocol{https}; |
|
25
|
|
|
|
|
|
|
} |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
$method{http_get} = sub { |
|
28
|
0
|
|
|
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
|
0
|
|
|
|
|
if ( $res->is_success ) { |
|
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
|
|
|
|
|
|
|
$method{http_post} = sub { |
|
45
|
0
|
|
|
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
|
0
|
0
|
|
|
|
if ( $args->{body} |
|
|
|
|
0
|
|
|
|
|
|
55
|
|
|
|
|
|
|
&& $args->{content_type} |
|
56
|
|
|
|
|
|
|
&& $args->{content_type} =~ /form-data/ ) |
|
57
|
|
|
|
|
|
|
{ |
|
58
|
0
|
|
|
|
|
|
$is_form_data = 1; |
|
59
|
0
|
0
|
|
|
|
|
if ( $args->{body_array} ) { |
|
60
|
0
|
|
|
|
|
|
my $body = $args->{body_array}; |
|
61
|
|
|
|
|
|
|
|
|
62
|
0
|
|
|
|
|
|
for ( my $i = 0 ; $i < $#$body ; $i += 2 ) { |
|
63
|
0
|
|
|
|
|
|
my $key = $body->[$i]; |
|
64
|
0
|
0
|
|
|
|
|
my $value = |
|
65
|
|
|
|
|
|
|
defined $body->[ $i + 1 ] ? $body->[ $i + 1 ] : ''; |
|
66
|
0
|
0
|
0
|
|
|
|
if ( ref $value eq 'HASH' && $value->{file} ) { |
|
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
|
0
|
0
|
|
|
|
|
my $res = $ua->post( |
|
|
|
0
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
$uri, |
|
83
|
|
|
|
|
|
|
( |
|
84
|
|
|
|
|
|
|
$is_form_data |
|
85
|
|
|
|
|
|
|
? ( Content_Type => 'form−data' ) |
|
86
|
|
|
|
|
|
|
: () |
|
87
|
|
|
|
|
|
|
), |
|
88
|
|
|
|
|
|
|
$args->{body} ? ( Content => $content ) : (), |
|
89
|
|
|
|
|
|
|
); |
|
90
|
|
|
|
|
|
|
|
|
91
|
0
|
0
|
|
|
|
|
if ( $res->is_success ) { |
|
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
|
0
|
|
|
|
|
if ( exists $protocol{https} ) { |
|
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
|
|
|
|
|
|
|
sub add_headers { |
|
129
|
0
|
|
|
0
|
0
|
|
my ( $ua, $uri, $args ) = @_; |
|
130
|
0
|
|
0
|
|
|
|
my $headers = $args->{headers} || {}; |
|
131
|
0
|
|
|
|
|
|
for my $k ( keys %$headers ) { |
|
132
|
0
|
|
|
|
|
|
$ua->default_header( $k, $headers->{$k} ); |
|
133
|
|
|
|
|
|
|
} |
|
134
|
|
|
|
|
|
|
|
|
135
|
0
|
0
|
0
|
|
|
|
if ( $args->{user} && defined $args->{password} ) { |
|
136
|
0
|
|
0
|
|
|
|
my $method = lc $args->{auth_method} || 'basic'; |
|
137
|
0
|
0
|
|
|
|
|
if ( $method eq 'basic' ) { |
|
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
|
|
|
|
|
|
|
__PACKAGE__->_add_to_plient if $Plient::bundle_mode; |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
1; |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
__END__ |