| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Plient::Handler::HTTPTiny; |
|
2
|
5
|
|
|
5
|
|
3184
|
use strict; |
|
|
5
|
|
|
|
|
11
|
|
|
|
5
|
|
|
|
|
165
|
|
|
3
|
5
|
|
|
5
|
|
27
|
use warnings; |
|
|
5
|
|
|
|
|
8
|
|
|
|
5
|
|
|
|
|
4869
|
|
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
require Plient::Handler unless $Plient::bundle_mode; |
|
6
|
|
|
|
|
|
|
our @ISA = 'Plient::Handler'; |
|
7
|
|
|
|
|
|
|
my ( $HTTPTiny, %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
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# HTTPTiny doesn't support proxy auth |
|
27
|
0
|
|
|
|
|
|
return; |
|
28
|
|
|
|
|
|
|
} |
|
29
|
|
|
|
|
|
|
|
|
30
|
0
|
|
|
|
|
|
return $class->SUPER::support_method(@_); |
|
31
|
|
|
|
|
|
|
} |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my $inited; |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub init { |
|
36
|
0
|
0
|
|
0
|
0
|
|
return if $inited; |
|
37
|
0
|
|
|
|
|
|
$inited = 1; |
|
38
|
0
|
0
|
|
|
|
|
eval { require HTTP::Tiny } or return; |
|
|
0
|
|
|
|
|
|
|
|
39
|
0
|
|
|
|
|
|
undef $protocol{http}; |
|
40
|
|
|
|
|
|
|
$method{http_get} = sub { |
|
41
|
0
|
|
|
0
|
|
|
my ( $uri, $args ) = @_; |
|
42
|
0
|
|
|
|
|
|
my $http = HTTP::Tiny->new; |
|
43
|
0
|
|
|
|
|
|
add_headers( $http, $uri, $args ); |
|
44
|
0
|
0
|
|
|
|
|
$http->{proxy} = $ENV{http_proxy} if $ENV{http_proxy}; |
|
45
|
0
|
|
|
|
|
|
my $res = $http->get($uri); |
|
46
|
|
|
|
|
|
|
|
|
47
|
0
|
0
|
|
|
|
|
if ( $res->{success} ) { |
|
48
|
0
|
|
|
|
|
|
return $res->{content}; |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
else { |
|
51
|
0
|
|
|
|
|
|
warn "failed to get $uri with HTTP::Tiny: " . $res; |
|
52
|
0
|
|
|
|
|
|
return; |
|
53
|
|
|
|
|
|
|
} |
|
54
|
0
|
|
|
|
|
|
}; |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
$method{http_post} = sub { |
|
57
|
0
|
|
|
0
|
|
|
my ( $uri, $args ) = @_; |
|
58
|
0
|
|
|
|
|
|
my $http = HTTP::Tiny->new; |
|
59
|
0
|
0
|
|
|
|
|
$http->proxy( $ENV{http_proxy} ) if $ENV{http_proxy}; |
|
60
|
0
|
|
|
|
|
|
add_headers( $http, $uri, $args ); |
|
61
|
|
|
|
|
|
|
|
|
62
|
0
|
|
|
|
|
|
my $body; |
|
63
|
0
|
0
|
|
|
|
|
if ( $args->{body_hash} ) { |
|
64
|
0
|
|
|
|
|
|
for my $key ( keys %{$args->{body_hash}} ) { |
|
|
0
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# TODO uri escape key and value |
|
66
|
0
|
|
|
|
|
|
my $val = $args->{body_hash}{$key}; |
|
67
|
0
|
0
|
|
|
|
|
$body .= $body ? "&$key=$val" : "$key=$val"; |
|
68
|
|
|
|
|
|
|
} |
|
69
|
|
|
|
|
|
|
} |
|
70
|
|
|
|
|
|
|
|
|
71
|
0
|
0
|
|
|
|
|
$http->{default_headers}{'Content-Type'} = |
|
72
|
|
|
|
|
|
|
'application/x-www-form-urlencoded' |
|
73
|
|
|
|
|
|
|
unless $http->{default_headers}{'Content-Type'}; |
|
74
|
|
|
|
|
|
|
|
|
75
|
0
|
0
|
|
|
|
|
my $res = $http->request( |
|
76
|
|
|
|
|
|
|
'POST', $uri, |
|
77
|
|
|
|
|
|
|
{ |
|
78
|
|
|
|
|
|
|
defined $body |
|
79
|
|
|
|
|
|
|
? ( content => $body ) |
|
80
|
|
|
|
|
|
|
: () |
|
81
|
|
|
|
|
|
|
} |
|
82
|
|
|
|
|
|
|
); |
|
83
|
|
|
|
|
|
|
|
|
84
|
0
|
0
|
|
|
|
|
if ( $res->{success} ) { |
|
85
|
0
|
|
|
|
|
|
return $res->{content}; |
|
86
|
|
|
|
|
|
|
} |
|
87
|
|
|
|
|
|
|
else { |
|
88
|
0
|
|
|
|
|
|
warn "failed to post $uri with HTTP::Tiny: " . $res; |
|
89
|
0
|
|
|
|
|
|
return; |
|
90
|
|
|
|
|
|
|
} |
|
91
|
0
|
|
|
|
|
|
}; |
|
92
|
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
return 1; |
|
94
|
|
|
|
|
|
|
} |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub add_headers { |
|
97
|
0
|
|
|
0
|
0
|
|
my ( $http, $uri, $args ) = @_; |
|
98
|
0
|
|
0
|
|
|
|
my $headers = $args->{headers} || {}; |
|
99
|
0
|
|
|
|
|
|
for my $k ( keys %$headers ) { |
|
100
|
0
|
|
|
|
|
|
$http->{default_headers}{$k} = $headers->{$k}; |
|
101
|
|
|
|
|
|
|
} |
|
102
|
|
|
|
|
|
|
|
|
103
|
0
|
0
|
0
|
|
|
|
if ( $args->{user} && defined $args->{password} ) { |
|
104
|
0
|
|
0
|
|
|
|
my $method = lc $args->{auth_method} || 'basic'; |
|
105
|
0
|
0
|
|
|
|
|
if ( $method eq 'basic' ) { |
|
106
|
0
|
|
|
|
|
|
require MIME::Base64; |
|
107
|
0
|
|
|
|
|
|
$http->{default_headers}{"Authorization"} = 'Basic ' |
|
108
|
|
|
|
|
|
|
. MIME::Base64::encode_base64( "$args->{user}:$args->{password}", |
|
109
|
|
|
|
|
|
|
'' ); |
|
110
|
|
|
|
|
|
|
} |
|
111
|
|
|
|
|
|
|
else { |
|
112
|
0
|
|
|
|
|
|
die "aborting: unsupported auth method: $method"; |
|
113
|
|
|
|
|
|
|
} |
|
114
|
|
|
|
|
|
|
} |
|
115
|
|
|
|
|
|
|
} |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
__PACKAGE__->_add_to_plient if $Plient::bundle_mode; |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
1; |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
__END__ |