line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Plient::Handler::HTTPLite; |
2
|
5
|
|
|
5
|
|
3825
|
use strict; |
|
5
|
|
|
|
|
8
|
|
|
5
|
|
|
|
|
186
|
|
3
|
5
|
|
|
5
|
|
26
|
use warnings; |
|
5
|
|
|
|
|
8
|
|
|
5
|
|
|
|
|
5083
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
require Plient::Handler unless $Plient::bundle_mode; |
6
|
|
|
|
|
|
|
our @ISA = 'Plient::Handler'; |
7
|
|
|
|
|
|
|
my ( $HTTPLite, %all_protocol, %protocol, %method ); |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
%all_protocol = ( http => undef ); |
10
|
0
|
|
|
0
|
0
|
|
sub all_protocol { return \%all_protocol } |
11
|
0
|
|
|
0
|
0
|
|
sub protocol { return \%protocol } |
12
|
0
|
|
|
0
|
0
|
|
sub method { return \%method } |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub support_method { |
15
|
0
|
|
|
0
|
0
|
|
my $class = shift; |
16
|
0
|
|
|
|
|
|
my ( $method, $args ) = @_; |
17
|
0
|
0
|
0
|
|
|
|
if ( $args |
|
|
|
0
|
|
|
|
|
18
|
|
|
|
|
|
|
&& $args->{content_type} |
19
|
|
|
|
|
|
|
&& $args->{content_type} =~ 'form-data' ) |
20
|
|
|
|
|
|
|
{ |
21
|
0
|
|
|
|
|
|
return; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
0
|
0
|
0
|
|
|
|
if ( $ENV{http_proxy} && $ENV{http_proxy} =~ /@/ ) { |
25
|
|
|
|
|
|
|
# HTTPLite doesn't support proxy auth |
26
|
0
|
|
|
|
|
|
return; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
0
|
|
|
|
|
|
return $class->SUPER::support_method(@_); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
my $inited; |
33
|
|
|
|
|
|
|
sub init { |
34
|
0
|
0
|
|
0
|
0
|
|
return if $inited; |
35
|
0
|
|
|
|
|
|
$inited = 1; |
36
|
0
|
0
|
|
|
|
|
eval { require HTTP::Lite } or return; |
|
0
|
|
|
|
|
|
|
37
|
0
|
|
|
|
|
|
undef $protocol{http}; |
38
|
|
|
|
|
|
|
$method{http_get} = sub { |
39
|
0
|
|
|
0
|
|
|
my ( $uri, $args ) = @_; |
40
|
0
|
|
|
|
|
|
my $http = HTTP::Lite->new; |
41
|
0
|
|
|
|
|
|
add_headers( $http, $uri, $args ); |
42
|
0
|
0
|
|
|
|
|
$http->proxy( $ENV{http_proxy} ) if $ENV{http_proxy}; |
43
|
0
|
|
0
|
|
|
|
my $res = $http->request($uri) || ''; |
44
|
|
|
|
|
|
|
|
45
|
0
|
0
|
0
|
|
|
|
if ( $res == 200 || $res == 301 || $res == 302 ) { |
|
|
|
0
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# XXX TODO handle redirect |
48
|
0
|
|
|
|
|
|
return $http->body; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
else { |
51
|
0
|
|
|
|
|
|
warn "failed to get $uri with HTTP::Lite: " . $res; |
52
|
0
|
|
|
|
|
|
return; |
53
|
|
|
|
|
|
|
} |
54
|
0
|
|
|
|
|
|
}; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
$method{http_post} = sub { |
57
|
0
|
|
|
0
|
|
|
my ( $uri, $args ) = @_; |
58
|
0
|
|
|
|
|
|
my $http = HTTP::Lite->new; |
59
|
0
|
0
|
|
|
|
|
$http->proxy( $ENV{http_proxy} ) if $ENV{http_proxy}; |
60
|
0
|
|
|
|
|
|
add_headers( $http, $uri, $args ); |
61
|
0
|
0
|
|
|
|
|
$http->prepare_post( $args->{body_hash} ) if $args->{body_hash}; |
62
|
0
|
|
0
|
|
|
|
my $res = $http->request($uri) || ''; |
63
|
0
|
0
|
0
|
|
|
|
if ( $res == 200 || $res == 301 || $res == 302 ) { |
|
|
|
0
|
|
|
|
|
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# XXX TODO handle redirect |
66
|
0
|
|
|
|
|
|
return $http->body; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
else { |
69
|
0
|
|
|
|
|
|
warn "failed to post $uri with HTTP::Lite: " . $res; |
70
|
0
|
|
|
|
|
|
return; |
71
|
|
|
|
|
|
|
} |
72
|
0
|
|
|
|
|
|
}; |
73
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
return 1; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub add_headers { |
78
|
0
|
|
|
0
|
0
|
|
my ( $http, $uri, $args ) = @_; |
79
|
0
|
|
0
|
|
|
|
my $headers = $args->{headers} || {}; |
80
|
0
|
|
|
|
|
|
for my $k ( keys %$headers ) { |
81
|
0
|
|
|
|
|
|
$http->add_req_header( $k, $headers->{$k} ); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
0
|
0
|
0
|
|
|
|
if ( $args->{user} && defined $args->{password} ) { |
85
|
0
|
|
0
|
|
|
|
my $method = lc $args->{auth_method} || 'basic'; |
86
|
0
|
0
|
|
|
|
|
if ( $method eq 'basic' ) { |
87
|
0
|
|
|
|
|
|
require MIME::Base64; |
88
|
0
|
|
|
|
|
|
$http->add_req_header( "Authorization", |
89
|
|
|
|
|
|
|
'Basic ' |
90
|
|
|
|
|
|
|
. MIME::Base64::encode_base64( "$args->{user}:$args->{password}", '' ) |
91
|
|
|
|
|
|
|
); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
else { |
94
|
0
|
|
|
|
|
|
die "aborting: unsupported auth method: $method"; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
__PACKAGE__->_add_to_plient if $Plient::bundle_mode; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
1; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
__END__ |