… | |
… | |
3 | AnyEvent::HTTP - simple but non-blocking HTTP/HTTPS client |
3 | AnyEvent::HTTP - simple but non-blocking HTTP/HTTPS client |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use AnyEvent::HTTP; |
7 | use AnyEvent::HTTP; |
|
|
8 | |
|
|
9 | http_get "http://www.nethype.de/", sub { print $_[1] }; |
|
|
10 | |
|
|
11 | # ... do something else here |
8 | |
12 | |
9 | =head1 DESCRIPTION |
13 | =head1 DESCRIPTION |
10 | |
14 | |
11 | This module is an L<AnyEvent> user, you need to make sure that you use and |
15 | This module is an L<AnyEvent> user, you need to make sure that you use and |
12 | run a supported event loop. |
16 | run a supported event loop. |
… | |
… | |
44 | use AnyEvent::Socket (); |
48 | use AnyEvent::Socket (); |
45 | use AnyEvent::Handle (); |
49 | use AnyEvent::Handle (); |
46 | |
50 | |
47 | use base Exporter::; |
51 | use base Exporter::; |
48 | |
52 | |
49 | our $VERSION = '1.0'; |
53 | our $VERSION = '1.01'; |
50 | |
54 | |
51 | our @EXPORT = qw(http_get http_request); |
55 | our @EXPORT = qw(http_get http_post http_head http_request); |
52 | |
56 | |
53 | our $USERAGENT = "Mozilla/5.0 (compatible; AnyEvent::HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; |
57 | our $USERAGENT = "Mozilla/5.0 (compatible; AnyEvent::HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; |
54 | our $MAX_RECURSE = 10; |
58 | our $MAX_RECURSE = 10; |
55 | our $MAX_PERSISTENT = 8; |
59 | our $MAX_PERSISTENT = 8; |
56 | our $PERSISTENT_TIMEOUT = 2; |
60 | our $PERSISTENT_TIMEOUT = 2; |
… | |
… | |
59 | # changing these is evil |
63 | # changing these is evil |
60 | our $MAX_PERSISTENT_PER_HOST = 2; |
64 | our $MAX_PERSISTENT_PER_HOST = 2; |
61 | our $MAX_PER_HOST = 4; |
65 | our $MAX_PER_HOST = 4; |
62 | |
66 | |
63 | our $PROXY; |
67 | our $PROXY; |
|
|
68 | our $ACTIVE = 0; |
64 | |
69 | |
65 | my %KA_COUNT; # number of open keep-alive connections per host |
70 | my %KA_COUNT; # number of open keep-alive connections per host |
66 | my %CO_SLOT; # number of open connections, and wait queue, per host |
71 | my %CO_SLOT; # number of open connections, and wait queue, per host |
67 | |
72 | |
68 | =item http_get $url, key => value..., $cb->($data, $headers) |
73 | =item http_get $url, key => value..., $cb->($data, $headers) |
… | |
… | |
191 | |
196 | |
192 | while ($CO_SLOT{$host}[0] < $MAX_PER_HOST) { |
197 | while ($CO_SLOT{$host}[0] < $MAX_PER_HOST) { |
193 | if (my $cb = shift @{ $CO_SLOT{$host}[1] }) { |
198 | if (my $cb = shift @{ $CO_SLOT{$host}[1] }) { |
194 | # somebody wants that slot |
199 | # somebody wants that slot |
195 | ++$CO_SLOT{$host}[0]; |
200 | ++$CO_SLOT{$host}[0]; |
|
|
201 | ++$ACTIVE; |
196 | |
202 | |
197 | $cb->(AnyEvent::Util::guard { |
203 | $cb->(AnyEvent::Util::guard { |
|
|
204 | --$ACTIVE; |
198 | --$CO_SLOT{$host}[0]; |
205 | --$CO_SLOT{$host}[0]; |
199 | _slot_schedule $host; |
206 | _slot_schedule $host; |
200 | }); |
207 | }); |
201 | } else { |
208 | } else { |
202 | # nobody wants the slot, maybe we can forget about it |
209 | # nobody wants the slot, maybe we can forget about it |
… | |
… | |
211 | push @{ $CO_SLOT{$_[0]}[1] }, $_[1]; |
218 | push @{ $CO_SLOT{$_[0]}[1] }, $_[1]; |
212 | |
219 | |
213 | _slot_schedule $_[0]; |
220 | _slot_schedule $_[0]; |
214 | } |
221 | } |
215 | |
222 | |
216 | sub http_request($$$;@) { |
223 | sub http_request($$@) { |
217 | my $cb = pop; |
224 | my $cb = pop; |
218 | my ($method, $url, %arg) = @_; |
225 | my ($method, $url, %arg) = @_; |
219 | |
226 | |
220 | my %hdr; |
227 | my %hdr; |
221 | |
228 | |
… | |
… | |
227 | } |
234 | } |
228 | } |
235 | } |
229 | |
236 | |
230 | my $recurse = exists $arg{recurse} ? $arg{recurse} : $MAX_RECURSE; |
237 | my $recurse = exists $arg{recurse} ? $arg{recurse} : $MAX_RECURSE; |
231 | |
238 | |
232 | return $cb->(undef, { Status => 599, Reason => "recursion limit reached" }) |
239 | return $cb->(undef, { Status => 599, Reason => "recursion limit reached", URL => $url }) |
233 | if $recurse < 0; |
240 | if $recurse < 0; |
234 | |
241 | |
235 | my $proxy = $arg{proxy} || $PROXY; |
242 | my $proxy = $arg{proxy} || $PROXY; |
236 | my $timeout = $arg{timeout} || $TIMEOUT; |
243 | my $timeout = $arg{timeout} || $TIMEOUT; |
237 | |
244 | |
… | |
… | |
242 | |
249 | |
243 | $scheme = lc $scheme; |
250 | $scheme = lc $scheme; |
244 | |
251 | |
245 | my $uport = $scheme eq "http" ? 80 |
252 | my $uport = $scheme eq "http" ? 80 |
246 | : $scheme eq "https" ? 443 |
253 | : $scheme eq "https" ? 443 |
247 | : return $cb->(undef, { Status => 599, Reason => "only http and https URL schemes supported" }); |
254 | : return $cb->(undef, { Status => 599, Reason => "only http and https URL schemes supported", URL => $url }); |
|
|
255 | |
|
|
256 | $hdr{referer} ||= "$scheme://$authority$upath"; # leave out fragment and query string, just a heuristic |
248 | |
257 | |
249 | $authority =~ /^(?: .*\@ )? ([^\@:]+) (?: : (\d+) )?$/x |
258 | $authority =~ /^(?: .*\@ )? ([^\@:]+) (?: : (\d+) )?$/x |
250 | or return $cb->(undef, { Status => 599, Reason => "unparsable URL" }); |
259 | or return $cb->(undef, { Status => 599, Reason => "unparsable URL", URL => $url }); |
251 | |
260 | |
252 | my $uhost = $1; |
261 | my $uhost = $1; |
253 | $uport = $2 if defined $2; |
262 | $uport = $2 if defined $2; |
254 | |
263 | |
255 | $uhost =~ s/^\[(.*)\]$/$1/; |
264 | $uhost =~ s/^\[(.*)\]$/$1/; |
256 | $upath .= "?$query" if length $query; |
265 | $upath .= "?$query" if length $query; |
257 | |
266 | |
258 | $upath =~ s%^/?%/%; |
267 | $upath =~ s%^/?%/%; |
259 | |
|
|
260 | $hdr{referer} ||= "$scheme://$authority$upath"; |
|
|
261 | |
268 | |
262 | # cookie processing |
269 | # cookie processing |
263 | if (my $jar = $arg{cookie_jar}) { |
270 | if (my $jar = $arg{cookie_jar}) { |
264 | %$jar = () if $jar->{version} < 1; |
271 | %$jar = () if $jar->{version} < 1; |
265 | |
272 | |
… | |
… | |
302 | |
309 | |
303 | return unless $state{connect_guard}; |
310 | return unless $state{connect_guard}; |
304 | |
311 | |
305 | $state{connect_guard} = AnyEvent::Socket::tcp_connect $rhost, $rport, sub { |
312 | $state{connect_guard} = AnyEvent::Socket::tcp_connect $rhost, $rport, sub { |
306 | $state{fh} = shift |
313 | $state{fh} = shift |
307 | or return $cb->(undef, { Status => 599, Reason => "$!" }); |
314 | or return $cb->(undef, { Status => 599, Reason => "$!", URL => $url }); |
308 | |
315 | |
309 | delete $state{connect_guard}; # reduce memory usage, save a tree |
316 | delete $state{connect_guard}; # reduce memory usage, save a tree |
310 | |
317 | |
311 | # get handle |
318 | # get handle |
312 | $state{handle} = new AnyEvent::Handle |
319 | $state{handle} = new AnyEvent::Handle |
… | |
… | |
324 | } |
331 | } |
325 | |
332 | |
326 | # (re-)configure handle |
333 | # (re-)configure handle |
327 | $state{handle}->timeout ($timeout); |
334 | $state{handle}->timeout ($timeout); |
328 | $state{handle}->on_error (sub { |
335 | $state{handle}->on_error (sub { |
|
|
336 | my $errno = "$!"; |
329 | %state = (); |
337 | %state = (); |
330 | $cb->(undef, { Status => 599, Reason => "$!" }); |
338 | $cb->(undef, { Status => 599, Reason => $errno, URL => $url }); |
331 | }); |
339 | }); |
332 | $state{handle}->on_eof (sub { |
340 | $state{handle}->on_eof (sub { |
333 | %state = (); |
341 | %state = (); |
334 | $cb->(undef, { Status => 599, Reason => "unexpected end-of-file" }); |
342 | $cb->(undef, { Status => 599, Reason => "unexpected end-of-file", URL => $url }); |
335 | }); |
343 | }); |
336 | |
344 | |
337 | # send request |
345 | # send request |
338 | $state{handle}->push_write ( |
346 | $state{handle}->push_write ( |
339 | "$method $rpath HTTP/1.0\015\012" |
347 | "$method $rpath HTTP/1.0\015\012" |
… | |
… | |
345 | %hdr = (); # reduce memory usage, save a kitten |
353 | %hdr = (); # reduce memory usage, save a kitten |
346 | |
354 | |
347 | # status line |
355 | # status line |
348 | $state{handle}->push_read (line => qr/\015?\012/, sub { |
356 | $state{handle}->push_read (line => qr/\015?\012/, sub { |
349 | $_[1] =~ /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) \s+ ([^\015\012]+)/ix |
357 | $_[1] =~ /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) \s+ ([^\015\012]+)/ix |
350 | or return (%state = (), $cb->(undef, { Status => 599, Reason => "invalid server response ($_[1])" })); |
358 | or return (%state = (), $cb->(undef, { Status => 599, Reason => "invalid server response ($_[1])", URL => $url })); |
351 | |
359 | |
352 | my %hdr = ( # response headers |
360 | my %hdr = ( # response headers |
353 | HTTPVersion => "\x00$1", |
361 | HTTPVersion => "\x00$1", |
354 | Status => "\x00$2", |
362 | Status => "\x00$2", |
355 | Reason => "\x00$3", |
363 | Reason => "\x00$3", |
|
|
364 | URL => "\x00$url" |
356 | ); |
365 | ); |
357 | |
366 | |
358 | # headers, could be optimized a bit |
367 | # headers, could be optimized a bit |
359 | $state{handle}->unshift_read (line => qr/\015?\012\015?\012/, sub { |
368 | $state{handle}->unshift_read (line => qr/\015?\012\015?\012/, sub { |
360 | for ("$_[1]\012") { |
369 | for ("$_[1]\012") { |
… | |
… | |
367 | ((?: [^\015\012]+ | \015?\012[\011\040] )*) |
376 | ((?: [^\015\012]+ | \015?\012[\011\040] )*) |
368 | \015?\012 |
377 | \015?\012 |
369 | /gxc; |
378 | /gxc; |
370 | |
379 | |
371 | /\G$/ |
380 | /\G$/ |
372 | or return (%state = (), $cb->(undef, { Status => 599, Reason => "garbled response headers" })); |
381 | or return (%state = (), $cb->(undef, { Status => 599, Reason => "garbled response headers", URL => $url })); |
373 | } |
382 | } |
374 | |
383 | |
375 | substr $_, 0, 1, "" |
384 | substr $_, 0, 1, "" |
376 | for values %hdr; |
385 | for values %hdr; |
377 | |
386 | |
… | |
… | |
400 | $arg{cookie_jar}{version} = 1; |
409 | $arg{cookie_jar}{version} = 1; |
401 | $arg{cookie_jar}{$cdom}{$cpath}{$name} = \%kv; |
410 | $arg{cookie_jar}{$cdom}{$cpath}{$name} = \%kv; |
402 | } |
411 | } |
403 | } |
412 | } |
404 | |
413 | |
405 | if ($_[1]{Status} =~ /^x30[12]$/ && $recurse) { |
414 | if ($_[1]{Status} =~ /^30[12]$/ && $recurse) { |
406 | # microsoft and other assholes don't give a shit for following standards, |
415 | # microsoft and other assholes don't give a shit for following standards, |
407 | # try to support a common form of broken Location header. |
416 | # try to support a common form of broken Location header. |
408 | $_[1]{location} =~ s%^/%$scheme://$uhost:$uport/%; |
417 | $_[1]{location} =~ s%^/%$scheme://$uhost:$uport/%; |
409 | |
418 | |
410 | http_request ($method, $_[1]{location}, %arg, recurse => $recurse - 1, $cb); |
419 | http_request ($method, $_[1]{location}, %arg, recurse => $recurse - 1, $cb); |
… | |
… | |
443 | }; |
452 | }; |
444 | |
453 | |
445 | defined wantarray && AnyEvent::Util::guard { %state = () } |
454 | defined wantarray && AnyEvent::Util::guard { %state = () } |
446 | } |
455 | } |
447 | |
456 | |
448 | sub http_get($$;@) { |
457 | sub http_get($@) { |
449 | unshift @_, "GET"; |
458 | unshift @_, "GET"; |
450 | &http_request |
459 | &http_request |
451 | } |
460 | } |
452 | |
461 | |
453 | sub http_head($$;@) { |
462 | sub http_head($@) { |
454 | unshift @_, "HEAD"; |
463 | unshift @_, "HEAD"; |
455 | &http_request |
464 | &http_request |
456 | } |
465 | } |
457 | |
466 | |
458 | sub http_post($$$;@) { |
467 | sub http_post($$@) { |
459 | unshift @_, "POST", "body"; |
468 | unshift @_, "POST", "body"; |
460 | &http_request |
469 | &http_request |
461 | } |
470 | } |
462 | |
471 | |
463 | =back |
472 | =back |
… | |
… | |
490 | |
499 | |
491 | The maximum time to cache a persistent connection, in seconds (default: 2). |
500 | The maximum time to cache a persistent connection, in seconds (default: 2). |
492 | |
501 | |
493 | Not implemented currently. |
502 | Not implemented currently. |
494 | |
503 | |
|
|
504 | =item $AnyEvent::HTTP::ACTIVE |
|
|
505 | |
|
|
506 | The number of active connections. This is not the number of currently |
|
|
507 | running requests, but the number of currently open and non-idle TCP |
|
|
508 | connections. This number of can be useful for load-leveling. |
|
|
509 | |
495 | =back |
510 | =back |
496 | |
511 | |
497 | =cut |
512 | =cut |
498 | |
513 | |
499 | sub set_proxy($) { |
514 | sub set_proxy($) { |
… | |
… | |
507 | |
522 | |
508 | L<AnyEvent>. |
523 | L<AnyEvent>. |
509 | |
524 | |
510 | =head1 AUTHOR |
525 | =head1 AUTHOR |
511 | |
526 | |
512 | Marc Lehmann <schmorp@schmorp.de> |
527 | Marc Lehmann <schmorp@schmorp.de> |
513 | http://home.schmorp.de/ |
528 | http://home.schmorp.de/ |
514 | |
529 | |
515 | =cut |
530 | =cut |
516 | |
531 | |
517 | 1 |
532 | 1 |
518 | |
533 | |