line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
########################################### |
2
|
|
|
|
|
|
|
package LWP::UserAgent::POE; |
3
|
|
|
|
|
|
|
########################################### |
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
123687
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
65
|
|
6
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
84
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = "0.05"; |
9
|
2
|
|
|
2
|
|
2186
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
109657
|
|
|
2
|
|
|
|
|
65
|
|
10
|
2
|
|
|
2
|
|
23
|
use base "LWP::UserAgent"; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
237
|
|
11
|
|
|
|
|
|
|
|
12
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
74
|
|
13
|
2
|
|
|
2
|
|
10
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
2451
|
|
14
|
2
|
|
|
2
|
|
2258
|
use POE; |
|
2
|
|
|
|
|
260210
|
|
|
2
|
|
|
|
|
18
|
|
15
|
2
|
|
|
2
|
|
602490
|
use POE::Component::Client::HTTP; |
|
2
|
|
|
|
|
350088
|
|
|
2
|
|
|
|
|
133
|
|
16
|
2
|
|
|
2
|
|
19
|
use HTTP::Request; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
60
|
|
17
|
2
|
|
|
2
|
|
1473
|
use Log::Log4perl qw(:easy); |
|
2
|
|
|
|
|
56566
|
|
|
2
|
|
|
|
|
20
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
#use Scalar::Defer; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
########################################### |
22
|
|
|
|
|
|
|
sub new { |
23
|
|
|
|
|
|
|
########################################### |
24
|
2
|
|
|
2
|
1
|
5337
|
my ($class, %options) = @_; |
25
|
|
|
|
|
|
|
|
26
|
2
|
|
|
|
|
23
|
my $default_options = LWP::UserAgent->new( %options ); |
27
|
2
|
|
|
|
|
4267
|
delete $default_options->{proxy}; |
28
|
|
|
|
|
|
|
|
29
|
2
|
|
|
|
|
30
|
my $self = bless { |
30
|
|
|
|
|
|
|
await_id => 0, |
31
|
|
|
|
|
|
|
poco_alias => "lwp_useragent_poe_http_client_poco", |
32
|
|
|
|
|
|
|
%$default_options, |
33
|
|
|
|
|
|
|
%options, |
34
|
|
|
|
|
|
|
}, $class; |
35
|
|
|
|
|
|
|
|
36
|
2
|
|
|
|
|
38
|
DEBUG "Creating $self->{poco_alias} session"; |
37
|
|
|
|
|
|
|
|
38
|
2
|
|
|
|
|
73
|
POE::Session->create( |
39
|
|
|
|
|
|
|
object_states => [ |
40
|
|
|
|
|
|
|
$self => { |
41
|
|
|
|
|
|
|
on_response => '_poe_handle_response', |
42
|
|
|
|
|
|
|
on_request => '_poe_handle_request', |
43
|
|
|
|
|
|
|
on_shutdown => '_poe_handle_shutdown', |
44
|
|
|
|
|
|
|
_start => '_poe_handle_startup', |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
], |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
|
49
|
2
|
|
|
|
|
404
|
$self->spawn(); |
50
|
|
|
|
|
|
|
|
51
|
2
|
|
|
|
|
3591
|
return $self; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
########################################### |
55
|
|
|
|
|
|
|
sub spawn { |
56
|
|
|
|
|
|
|
########################################### |
57
|
2
|
|
|
2
|
0
|
6
|
my($self) = @_; |
58
|
|
|
|
|
|
|
|
59
|
2
|
50
|
|
|
|
14
|
if( $poe_kernel->alias_resolve( $self->{poco_alias} ) ){ |
60
|
0
|
|
|
|
|
0
|
DEBUG "Not spawning $self->{poco_alias}", |
61
|
|
|
|
|
|
|
" session (there's one already)"; |
62
|
0
|
|
|
|
|
0
|
return 1; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
2
|
|
|
|
|
88
|
DEBUG "Spawning $self->{poco_alias} POE HTTP client component"; |
66
|
|
|
|
|
|
|
|
67
|
2
|
|
|
|
|
21
|
POE::Component::Client::HTTP->spawn( |
68
|
|
|
|
|
|
|
Alias => $self->{poco_alias}, |
69
|
|
|
|
|
|
|
$self->poco_options(), |
70
|
|
|
|
|
|
|
); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
########################################### |
74
|
|
|
|
|
|
|
sub poco_options { |
75
|
|
|
|
|
|
|
########################################### |
76
|
2
|
|
|
2
|
0
|
4
|
my($self) = @_; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# agent "libwww-perl/#.##" |
79
|
|
|
|
|
|
|
# from undef |
80
|
|
|
|
|
|
|
# conn_cache undef |
81
|
|
|
|
|
|
|
# cookie_jar undef |
82
|
|
|
|
|
|
|
# default_headers HTTP::Headers->new |
83
|
|
|
|
|
|
|
# max_size undef |
84
|
|
|
|
|
|
|
# max_redirect 7 |
85
|
|
|
|
|
|
|
# parse_head 1 |
86
|
|
|
|
|
|
|
# protocols_allowed undef |
87
|
|
|
|
|
|
|
# protocols_forbidden undef |
88
|
|
|
|
|
|
|
# requests_redirectable [’GET’, ’HEAD’] |
89
|
|
|
|
|
|
|
# timeout 180 |
90
|
|
|
|
|
|
|
|
91
|
2
|
|
|
|
|
31
|
my %lwp2poco = ( |
92
|
|
|
|
|
|
|
agent => "Agent", |
93
|
|
|
|
|
|
|
from => "From", |
94
|
|
|
|
|
|
|
timeout => "Timeout", |
95
|
|
|
|
|
|
|
max_size => "MaxSize", |
96
|
|
|
|
|
|
|
cookie_jar => "CookieJar", |
97
|
|
|
|
|
|
|
proxy => "Proxy", |
98
|
|
|
|
|
|
|
noproxy => "NoProxy", |
99
|
|
|
|
|
|
|
redirects => "FollowRedirects", |
100
|
|
|
|
|
|
|
); |
101
|
|
|
|
|
|
|
|
102
|
2
|
|
|
|
|
5
|
my %poco_options = (); |
103
|
|
|
|
|
|
|
|
104
|
2
|
|
|
|
|
11
|
for my $lwp_opt (keys %lwp2poco) { |
105
|
16
|
50
|
|
|
|
42
|
if(exists $lwp2poco{ $lwp_opt } ) { |
106
|
16
|
|
|
|
|
51
|
$poco_options{ $lwp2poco{ $lwp_opt } } = |
107
|
|
|
|
|
|
|
$self->{ $lwp_opt }; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
2
|
|
|
|
|
37
|
return %poco_options; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
########################################### |
115
|
|
|
|
|
|
|
sub _poe_handle_startup { |
116
|
|
|
|
|
|
|
########################################### |
117
|
2
|
|
|
2
|
|
513
|
$_[KERNEL]->alias_set("$_[OBJECT]"); |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
########################################### |
121
|
|
|
|
|
|
|
sub _poe_handle_shutdown { |
122
|
|
|
|
|
|
|
########################################### |
123
|
0
|
|
|
0
|
|
0
|
$_[KERNEL]->alias_remove("$_[OBJECT]"); |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
########################################### |
127
|
|
|
|
|
|
|
sub simple_request { |
128
|
|
|
|
|
|
|
########################################### |
129
|
3
|
|
|
3
|
1
|
16002
|
my ($self, $request) = @_; |
130
|
|
|
|
|
|
|
|
131
|
3
|
|
|
|
|
16
|
INFO "Received request ", $request->url(); |
132
|
|
|
|
|
|
|
|
133
|
3
|
|
|
|
|
195
|
$self->prepare_request( $request ); |
134
|
|
|
|
|
|
|
|
135
|
3
|
|
|
|
|
917
|
my $promise = \my $scalar; |
136
|
3
|
|
|
|
|
30
|
$poe_kernel->post("$self", on_request => $request, $promise); |
137
|
3
|
|
|
|
|
812
|
$self->await( $promise, $request ); |
138
|
3
|
|
|
|
|
21
|
return $$promise; |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
# maybe we'll add that later to stack up requests before |
141
|
|
|
|
|
|
|
# processing them. |
142
|
|
|
|
|
|
|
# lazy { $$promise or $self->await($promise); $$promise }; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
########################################### |
146
|
|
|
|
|
|
|
sub _poe_handle_request { |
147
|
|
|
|
|
|
|
########################################### |
148
|
3
|
|
|
3
|
|
898
|
my ($self, $kernel, $request, $promise) = @_[OBJECT, KERNEL, ARG0, ARG1]; |
149
|
|
|
|
|
|
|
|
150
|
3
|
|
|
|
|
12
|
DEBUG "Handling request ", $request->url(); |
151
|
|
|
|
|
|
|
|
152
|
3
|
|
|
|
|
259
|
$kernel->post($self->{poco_alias} => |
153
|
|
|
|
|
|
|
request => on_response => $request, $promise); |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
########################################### |
157
|
|
|
|
|
|
|
sub _poe_handle_response { |
158
|
|
|
|
|
|
|
########################################### |
159
|
3
|
|
|
3
|
|
21314
|
my ($self, $request_info, $response_info) = @_[OBJECT, ARG0, ARG1]; |
160
|
|
|
|
|
|
|
|
161
|
3
|
|
|
|
|
27
|
DEBUG "Handling response ", $request_info->[0]->url(); |
162
|
|
|
|
|
|
|
|
163
|
3
|
|
|
|
|
80
|
my $promise = $request_info->[1]; |
164
|
3
|
|
|
|
|
7
|
my $response = $response_info->[0]; |
165
|
|
|
|
|
|
|
|
166
|
3
|
|
|
|
|
7
|
my $promise_number = $promise + 0; |
167
|
3
|
|
|
|
|
7
|
$$promise = $response; |
168
|
|
|
|
|
|
|
|
169
|
3
|
|
|
|
|
13
|
my $await_id = delete $self->{awaiting}{$promise_number}; |
170
|
3
|
|
|
|
|
23
|
delete $self->{promises}{$await_id}{$promise_number}; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
########################################### |
174
|
|
|
|
|
|
|
sub await { |
175
|
|
|
|
|
|
|
########################################### |
176
|
3
|
|
|
3
|
0
|
8
|
my ($self, $promise, $request) = @_; |
177
|
|
|
|
|
|
|
|
178
|
3
|
|
|
|
|
13
|
DEBUG "Awaiting ", $request->url(); |
179
|
|
|
|
|
|
|
|
180
|
3
|
|
|
|
|
48
|
my $await_id = $self->{await_id}++; |
181
|
|
|
|
|
|
|
|
182
|
3
|
|
|
|
|
7
|
my $promise_number = $promise+0; |
183
|
3
|
|
|
|
|
23
|
$self->{promises}{$await_id}{$promise_number} = $promise; |
184
|
3
|
|
|
|
|
7
|
$self->{awaiting}{$promise_number} = $await_id; |
185
|
|
|
|
|
|
|
|
186
|
3
|
|
|
|
|
6
|
while (keys %{$self->{promises}{$await_id}}) { |
|
1962
|
|
|
|
|
3063534
|
|
187
|
1959
|
|
|
|
|
6483
|
$poe_kernel->run_one_timeslice(); |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
3
|
|
|
|
|
14
|
INFO "Response from ", $request->url(), " arrived (", |
191
|
|
|
|
|
|
|
$$promise->code(), ")"; |
192
|
|
|
|
|
|
|
|
193
|
3
|
|
|
|
|
245
|
delete $self->{promises}{$await_id}; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
1; |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
__END__ |