1 |
root |
1.1 |
=head1 NAME |
2 |
|
|
|
3 |
|
|
AnyEvent::HTTP - simple but non-blocking HTTP/HTTPS client |
4 |
|
|
|
5 |
|
|
=head1 SYNOPSIS |
6 |
|
|
|
7 |
|
|
use AnyEvent::HTTP; |
8 |
|
|
|
9 |
|
|
=head1 DESCRIPTION |
10 |
|
|
|
11 |
|
|
This module is an L<AnyEvent> user, you need to make sure that you use and |
12 |
|
|
run a supported event loop. |
13 |
|
|
|
14 |
|
|
=head2 METHODS |
15 |
|
|
|
16 |
|
|
=over 4 |
17 |
|
|
|
18 |
|
|
=cut |
19 |
|
|
|
20 |
|
|
package AnyEvent::HTTP; |
21 |
|
|
|
22 |
|
|
use strict; |
23 |
|
|
no warnings; |
24 |
|
|
|
25 |
|
|
use Carp; |
26 |
|
|
|
27 |
|
|
use AnyEvent (); |
28 |
|
|
use AnyEvent::Util (); |
29 |
|
|
use AnyEvent::Socket (); |
30 |
|
|
use AnyEvent::Handle (); |
31 |
|
|
|
32 |
|
|
use base Exporter::; |
33 |
|
|
|
34 |
|
|
our $VERSION = '1.0'; |
35 |
|
|
|
36 |
|
|
our @EXPORT = qw(http_get http_request); |
37 |
|
|
|
38 |
|
|
our $USERAGENT = "Mozilla/5.0 (compatible; AnyEvent::HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; |
39 |
root |
1.3 |
our $MAX_RECURSE = 10; |
40 |
root |
1.2 |
our $MAX_PERSISTENT = 8; |
41 |
|
|
our $PERSISTENT_TIMEOUT = 2; |
42 |
|
|
our $TIMEOUT = 300; |
43 |
root |
1.1 |
|
44 |
|
|
# changing these is evil |
45 |
|
|
our $MAX_PERSISTENT_PER_HOST = 2; |
46 |
|
|
our $MAX_PER_HOST = 4; # not respected yet :( |
47 |
|
|
|
48 |
root |
1.2 |
our $PROXY; |
49 |
|
|
|
50 |
root |
1.1 |
my %KA_COUNT; # number of open keep-alive connections per host |
51 |
|
|
|
52 |
|
|
=item http_get $url, key => value..., $cb->($data, $headers) |
53 |
|
|
|
54 |
|
|
Executes an HTTP-GET request. See the http_request function for details on |
55 |
|
|
additional parameters. |
56 |
|
|
|
57 |
root |
1.3 |
=item http_get $url, $body, key => value..., $cb->($data, $headers) |
58 |
|
|
|
59 |
|
|
Executes an HTTP-POST request with a requets body of C<$bod>. See the |
60 |
|
|
http_request function for details on additional parameters. |
61 |
|
|
|
62 |
root |
1.1 |
=item http_request $method => $url, key => value..., $cb->($data, $headers) |
63 |
|
|
|
64 |
|
|
Executes a HTTP request of type C<$method> (e.g. C<GET>, C<POST>). The URL |
65 |
|
|
must be an absolute http or https URL. |
66 |
|
|
|
67 |
root |
1.2 |
The callback will be called with the response data as first argument |
68 |
|
|
(or C<undef> if it wasn't available due to errors), and a hash-ref with |
69 |
|
|
response headers as second argument. |
70 |
|
|
|
71 |
|
|
All the headers in that has are lowercased. In addition to the response |
72 |
|
|
headers, the three "pseudo-headers" C<HTTPVersion>, C<Status> and |
73 |
|
|
C<Reason> contain the three parts of the HTTP Status-Line of the same |
74 |
|
|
name. |
75 |
|
|
|
76 |
|
|
If an internal error occurs, such as not being able to resolve a hostname, |
77 |
|
|
then C<$data> will be C<undef>, C<< $headers->{Status} >> will be C<599> |
78 |
|
|
and the C<Reason> pseudo-header will contain an error message. |
79 |
|
|
|
80 |
root |
1.1 |
Additional parameters are key-value pairs, and are fully optional. They |
81 |
|
|
include: |
82 |
|
|
|
83 |
|
|
=over 4 |
84 |
|
|
|
85 |
root |
1.3 |
=item recurse => $count (default: $MAX_RECURSE) |
86 |
root |
1.1 |
|
87 |
|
|
Whether to recurse requests or not, e.g. on redirects, authentication |
88 |
root |
1.3 |
retries and so on, and how often to do so. |
89 |
root |
1.1 |
|
90 |
|
|
=item headers => hashref |
91 |
|
|
|
92 |
|
|
The request headers to use. |
93 |
|
|
|
94 |
|
|
=item timeout => $seconds |
95 |
|
|
|
96 |
|
|
The time-out to use for various stages - each connect attempt will reset |
97 |
root |
1.2 |
the timeout, as will read or write activity. Default timeout is 5 minutes. |
98 |
|
|
|
99 |
|
|
=item proxy => [$host, $port[, $scheme]] or undef |
100 |
|
|
|
101 |
|
|
Use the given http proxy for all requests. If not specified, then the |
102 |
|
|
default proxy (as specified by C<$ENV{http_proxy}>) is used. |
103 |
|
|
|
104 |
|
|
C<$scheme> must be either missing or C<http> for HTTP, or C<https> for |
105 |
|
|
HTTPS. |
106 |
root |
1.1 |
|
107 |
root |
1.3 |
=item body => $string |
108 |
|
|
|
109 |
|
|
The request body, usually empty. Will be-sent as-is (future versions of |
110 |
|
|
this module might offer more options). |
111 |
|
|
|
112 |
root |
1.1 |
=back |
113 |
|
|
|
114 |
|
|
=back |
115 |
|
|
|
116 |
|
|
=cut |
117 |
|
|
|
118 |
|
|
sub http_request($$$;@) { |
119 |
|
|
my $cb = pop; |
120 |
|
|
my ($method, $url, %arg) = @_; |
121 |
|
|
|
122 |
|
|
my %hdr; |
123 |
|
|
|
124 |
root |
1.3 |
$method = uc $method; |
125 |
|
|
|
126 |
root |
1.1 |
if (my $hdr = delete $arg{headers}) { |
127 |
|
|
while (my ($k, $v) = each %$hdr) { |
128 |
|
|
$hdr{lc $k} = $v; |
129 |
|
|
} |
130 |
|
|
} |
131 |
|
|
|
132 |
root |
1.2 |
my $proxy = $arg{proxy} || $PROXY; |
133 |
root |
1.1 |
my $timeout = $arg{timeout} || $TIMEOUT; |
134 |
root |
1.3 |
my $recurse = exists $arg{recurse} ? $arg{recurse} : $MAX_RECURSE; |
135 |
root |
1.1 |
|
136 |
|
|
$hdr{"user-agent"} ||= $USERAGENT; |
137 |
|
|
|
138 |
root |
1.2 |
my ($host, $port, $path, $scheme); |
139 |
|
|
|
140 |
|
|
if ($proxy) { |
141 |
|
|
($host, $port, $scheme) = @$proxy; |
142 |
|
|
$path = $url; |
143 |
|
|
} else { |
144 |
|
|
($scheme, my $authority, $path, my $query, my $fragment) = |
145 |
|
|
$url =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?|; |
146 |
root |
1.1 |
|
147 |
root |
1.2 |
$port = $scheme eq "http" ? 80 |
148 |
root |
1.1 |
: $scheme eq "https" ? 443 |
149 |
|
|
: croak "$url: only http and https URLs supported"; |
150 |
|
|
|
151 |
root |
1.2 |
$authority =~ /^(?: .*\@ )? ([^\@:]+) (?: : (\d+) )?$/x |
152 |
|
|
or croak "$authority: unparsable URL"; |
153 |
|
|
|
154 |
|
|
$host = $1; |
155 |
|
|
$port = $2 if defined $2; |
156 |
root |
1.1 |
|
157 |
root |
1.2 |
$host =~ s/^\[(.*)\]$/$1/; |
158 |
|
|
$path .= "?$query" if length $query; |
159 |
root |
1.1 |
|
160 |
root |
1.2 |
$path = "/" unless $path; |
161 |
root |
1.1 |
|
162 |
root |
1.2 |
$hdr{host} = $host = lc $host; |
163 |
|
|
} |
164 |
|
|
|
165 |
|
|
$scheme = lc $scheme; |
166 |
root |
1.1 |
|
167 |
|
|
my %state; |
168 |
|
|
|
169 |
root |
1.3 |
$state{body} = delete $arg{body}; |
170 |
root |
1.1 |
|
171 |
root |
1.3 |
$hdr{"content-length"} = length $state{body}; |
172 |
root |
1.1 |
|
173 |
|
|
$state{connect_guard} = AnyEvent::Socket::tcp_connect $host, $port, sub { |
174 |
|
|
$state{fh} = shift |
175 |
|
|
or return $cb->(undef, { Status => 599, Reason => "$!" }); |
176 |
|
|
|
177 |
|
|
delete $state{connect_guard}; # reduce memory usage, save a tree |
178 |
|
|
|
179 |
|
|
# get handle |
180 |
|
|
$state{handle} = new AnyEvent::Handle |
181 |
|
|
fh => $state{fh}, |
182 |
|
|
($scheme eq "https" ? (tls => "connect") : ()); |
183 |
|
|
|
184 |
|
|
# limit the number of persistent connections |
185 |
|
|
if ($KA_COUNT{$_[1]} < $MAX_PERSISTENT_PER_HOST) { |
186 |
|
|
++$KA_COUNT{$_[1]}; |
187 |
|
|
$state{handle}{ka_count_guard} = AnyEvent::Util::guard { --$KA_COUNT{$_[1]} }; |
188 |
|
|
$hdr{connection} = "keep-alive"; |
189 |
root |
1.2 |
delete $hdr{connection}; # keep-alive not yet supported |
190 |
root |
1.1 |
} else { |
191 |
|
|
delete $hdr{connection}; |
192 |
|
|
} |
193 |
|
|
|
194 |
|
|
# (re-)configure handle |
195 |
|
|
$state{handle}->timeout ($timeout); |
196 |
|
|
$state{handle}->on_error (sub { |
197 |
|
|
%state = (); |
198 |
|
|
$cb->(undef, { Status => 599, Reason => "$!" }); |
199 |
|
|
}); |
200 |
|
|
$state{handle}->on_eof (sub { |
201 |
|
|
%state = (); |
202 |
|
|
$cb->(undef, { Status => 599, Reason => "unexpected end-of-file" }); |
203 |
|
|
}); |
204 |
|
|
|
205 |
|
|
# send request |
206 |
|
|
$state{handle}->push_write ( |
207 |
root |
1.3 |
"$method $path HTTP/1.0\015\012" |
208 |
root |
1.1 |
. (join "", map "$_: $hdr{$_}\015\012", keys %hdr) |
209 |
|
|
. "\015\012" |
210 |
|
|
. (delete $state{body}) |
211 |
|
|
); |
212 |
|
|
|
213 |
|
|
%hdr = (); # reduce memory usage, save a kitten |
214 |
|
|
|
215 |
|
|
# status line |
216 |
|
|
$state{handle}->push_read (line => qr/\015?\012/, sub { |
217 |
|
|
$_[1] =~ /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) \s+ ([^\015\012]+)/ix |
218 |
|
|
or return (%state = (), $cb->(undef, { Status => 599, Reason => "invalid server response ($_[1])" })); |
219 |
|
|
|
220 |
|
|
my %hdr = ( # response headers |
221 |
|
|
HTTPVersion => ",$1", |
222 |
|
|
Status => ",$2", |
223 |
|
|
Reason => ",$3", |
224 |
|
|
); |
225 |
|
|
|
226 |
|
|
# headers, could be optimized a bit |
227 |
|
|
$state{handle}->unshift_read (line => qr/\015?\012\015?\012/, sub { |
228 |
|
|
for ("$_[1]\012") { |
229 |
root |
1.2 |
# we support spaces in field names, as lotus domino |
230 |
|
|
# creates them. |
231 |
root |
1.1 |
$hdr{lc $1} .= ",$2" |
232 |
|
|
while /\G |
233 |
root |
1.2 |
([^:\000-\037]+): |
234 |
root |
1.1 |
[\011\040]* |
235 |
|
|
((?: [^\015\012]+ | \015?\012[\011\040] )*) |
236 |
|
|
\015?\012 |
237 |
|
|
/gxc; |
238 |
|
|
|
239 |
|
|
/\G$/ |
240 |
|
|
or return $cb->(undef, { Status => 599, Reason => "garbled response headers" }); |
241 |
|
|
} |
242 |
|
|
|
243 |
|
|
substr $_, 0, 1, "" |
244 |
|
|
for values %hdr; |
245 |
|
|
|
246 |
root |
1.4 |
if ($method eq "HEAD") { |
247 |
|
|
%state = (); |
248 |
|
|
$cb->(undef, \%hdr); |
249 |
|
|
} else { |
250 |
root |
1.3 |
if (exists $hdr{"content-length"}) { |
251 |
|
|
$_[0]->unshift_read (chunk => $hdr{"content-length"}, sub { |
252 |
|
|
# could cache persistent connection now |
253 |
|
|
if ($hdr{connection} =~ /\bkeep-alive\b/i) { |
254 |
|
|
# but we don't, due to misdesigns, this is annoyingly complex |
255 |
|
|
}; |
256 |
|
|
|
257 |
|
|
%state = (); |
258 |
|
|
$cb->($_[1], \%hdr); |
259 |
|
|
}); |
260 |
|
|
} else { |
261 |
|
|
# too bad, need to read until we get an error or EOF, |
262 |
|
|
# no way to detect winged data. |
263 |
|
|
$_[0]->on_error (sub { |
264 |
|
|
%state = (); |
265 |
|
|
$cb->($_[0]{rbuf}, \%hdr); |
266 |
|
|
}); |
267 |
|
|
$_[0]->on_eof (undef); |
268 |
|
|
$_[0]->on_read (sub { }); |
269 |
|
|
} |
270 |
root |
1.1 |
} |
271 |
|
|
}); |
272 |
|
|
}); |
273 |
|
|
}, sub { |
274 |
|
|
$timeout |
275 |
|
|
}; |
276 |
|
|
|
277 |
|
|
defined wantarray && AnyEvent::Util::guard { %state = () } |
278 |
|
|
} |
279 |
|
|
|
280 |
|
|
sub http_get($$;@) { |
281 |
|
|
unshift @_, "GET"; |
282 |
|
|
&http_request |
283 |
|
|
} |
284 |
|
|
|
285 |
root |
1.4 |
sub http_head($$;@) { |
286 |
|
|
unshift @_, "HEAD"; |
287 |
|
|
&http_request |
288 |
|
|
} |
289 |
|
|
|
290 |
root |
1.3 |
sub http_post($$$;@) { |
291 |
|
|
unshift @_, "POST", "body"; |
292 |
|
|
&http_request |
293 |
|
|
} |
294 |
|
|
|
295 |
root |
1.2 |
=head2 GLOBAL FUNCTIONS AND VARIABLES |
296 |
root |
1.1 |
|
297 |
|
|
=over 4 |
298 |
|
|
|
299 |
root |
1.2 |
=item AnyEvent::HTTP::set_proxy "proxy-url" |
300 |
|
|
|
301 |
|
|
Sets the default proxy server to use. The proxy-url must begin with a |
302 |
|
|
string of the form C<http://host:port> (optionally C<https:...>). |
303 |
|
|
|
304 |
root |
1.3 |
=item $AnyEvent::HTTP::MAX_RECURSE |
305 |
root |
1.1 |
|
306 |
root |
1.3 |
The default value for the C<recurse> request parameter (default: C<10>). |
307 |
root |
1.1 |
|
308 |
|
|
=item $AnyEvent::HTTP::USERAGENT |
309 |
|
|
|
310 |
|
|
The default value for the C<User-Agent> header (the default is |
311 |
|
|
C<Mozilla/5.0 (compatible; AnyEvent::HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)>). |
312 |
|
|
|
313 |
|
|
=item $AnyEvent::HTTP::MAX_PERSISTENT |
314 |
|
|
|
315 |
|
|
The maximum number of persistent connections to keep open (default: 8). |
316 |
|
|
|
317 |
root |
1.3 |
Not implemented currently. |
318 |
|
|
|
319 |
root |
1.1 |
=item $AnyEvent::HTTP::PERSISTENT_TIMEOUT |
320 |
|
|
|
321 |
root |
1.2 |
The maximum time to cache a persistent connection, in seconds (default: 2). |
322 |
root |
1.1 |
|
323 |
root |
1.3 |
Not implemented currently. |
324 |
|
|
|
325 |
root |
1.1 |
=back |
326 |
|
|
|
327 |
|
|
=cut |
328 |
|
|
|
329 |
root |
1.2 |
sub set_proxy($) { |
330 |
|
|
$PROXY = [$2, $3 || 3128, $1] if $_[0] =~ m%^(https?):// ([^:/]+) (?: : (\d*) )?%ix; |
331 |
|
|
} |
332 |
|
|
|
333 |
|
|
# initialise proxy from environment |
334 |
|
|
set_proxy $ENV{http_proxy}; |
335 |
|
|
|
336 |
root |
1.1 |
=head1 SEE ALSO |
337 |
|
|
|
338 |
|
|
L<AnyEvent>. |
339 |
|
|
|
340 |
|
|
=head1 AUTHOR |
341 |
|
|
|
342 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
343 |
|
|
http://home.schmorp.de/ |
344 |
|
|
|
345 |
|
|
=cut |
346 |
|
|
|
347 |
|
|
1 |
348 |
|
|
|