… | |
… | |
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 | |
… | |
… | |
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 }); |
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" }); |
335 | }); |
343 | }); |
… | |
… | |
443 | }; |
451 | }; |
444 | |
452 | |
445 | defined wantarray && AnyEvent::Util::guard { %state = () } |
453 | defined wantarray && AnyEvent::Util::guard { %state = () } |
446 | } |
454 | } |
447 | |
455 | |
448 | sub http_get($$;@) { |
456 | sub http_get($@) { |
449 | unshift @_, "GET"; |
457 | unshift @_, "GET"; |
450 | &http_request |
458 | &http_request |
451 | } |
459 | } |
452 | |
460 | |
453 | sub http_head($$;@) { |
461 | sub http_head($@) { |
454 | unshift @_, "HEAD"; |
462 | unshift @_, "HEAD"; |
455 | &http_request |
463 | &http_request |
456 | } |
464 | } |
457 | |
465 | |
458 | sub http_post($$$;@) { |
466 | sub http_post($$@) { |
459 | unshift @_, "POST", "body"; |
467 | unshift @_, "POST", "body"; |
460 | &http_request |
468 | &http_request |
461 | } |
469 | } |
462 | |
470 | |
463 | =back |
471 | =back |
… | |
… | |
490 | |
498 | |
491 | The maximum time to cache a persistent connection, in seconds (default: 2). |
499 | The maximum time to cache a persistent connection, in seconds (default: 2). |
492 | |
500 | |
493 | Not implemented currently. |
501 | Not implemented currently. |
494 | |
502 | |
|
|
503 | =item $AnyEvent::HTTP::ACTIVE |
|
|
504 | |
|
|
505 | The number of active connections. This is not the number of currently |
|
|
506 | running requests, but the number of currently open and non-idle TCP |
|
|
507 | connections. This number of can be useful for load-leveling. |
|
|
508 | |
495 | =back |
509 | =back |
496 | |
510 | |
497 | =cut |
511 | =cut |
498 | |
512 | |
499 | sub set_proxy($) { |
513 | sub set_proxy($) { |
… | |
… | |
507 | |
521 | |
508 | L<AnyEvent>. |
522 | L<AnyEvent>. |
509 | |
523 | |
510 | =head1 AUTHOR |
524 | =head1 AUTHOR |
511 | |
525 | |
512 | Marc Lehmann <schmorp@schmorp.de> |
526 | Marc Lehmann <schmorp@schmorp.de> |
513 | http://home.schmorp.de/ |
527 | http://home.schmorp.de/ |
514 | |
528 | |
515 | =cut |
529 | =cut |
516 | |
530 | |
517 | 1 |
531 | 1 |
518 | |
532 | |