… | |
… | |
41 | use strict; |
41 | use strict; |
42 | no warnings; |
42 | no warnings; |
43 | |
43 | |
44 | use Errno (); |
44 | use Errno (); |
45 | |
45 | |
46 | use AnyEvent 4.452 (); |
46 | use AnyEvent 4.8 (); |
47 | use AnyEvent::Util (); |
47 | use AnyEvent::Util (); |
48 | use AnyEvent::Socket (); |
48 | use AnyEvent::Socket (); |
49 | use AnyEvent::Handle (); |
49 | use AnyEvent::Handle (); |
50 | |
50 | |
51 | use base Exporter::; |
51 | use base Exporter::; |
52 | |
52 | |
53 | our $VERSION = '1.12'; |
53 | our $VERSION = '1.4'; |
54 | |
54 | |
55 | our @EXPORT = qw(http_get http_post http_head http_request); |
55 | our @EXPORT = qw(http_get http_post http_head http_request); |
56 | |
56 | |
57 | our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; |
57 | our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; |
58 | our $MAX_RECURSE = 10; |
58 | our $MAX_RECURSE = 10; |
59 | our $MAX_PERSISTENT = 8; |
59 | our $MAX_PERSISTENT = 8; |
60 | our $PERSISTENT_TIMEOUT = 2; |
60 | our $PERSISTENT_TIMEOUT = 2; |
61 | our $TIMEOUT = 300; |
61 | our $TIMEOUT = 300; |
62 | |
62 | |
63 | # changing these is evil |
63 | # changing these is evil |
64 | our $MAX_PERSISTENT_PER_HOST = 2; |
64 | our $MAX_PERSISTENT_PER_HOST = 0; |
65 | our $MAX_PER_HOST = 4; |
65 | our $MAX_PER_HOST = 4; |
66 | |
66 | |
67 | our $PROXY; |
67 | our $PROXY; |
68 | our $ACTIVE = 0; |
68 | our $ACTIVE = 0; |
69 | |
69 | |
… | |
… | |
94 | When called in void context, nothing is returned. In other contexts, |
94 | When called in void context, nothing is returned. In other contexts, |
95 | C<http_request> returns a "cancellation guard" - you have to keep the |
95 | C<http_request> returns a "cancellation guard" - you have to keep the |
96 | object at least alive until the callback get called. If the object gets |
96 | object at least alive until the callback get called. If the object gets |
97 | destroyed before the callbakc is called, the request will be cancelled. |
97 | destroyed before the callbakc is called, the request will be cancelled. |
98 | |
98 | |
99 | The callback will be called with the response data as first argument |
99 | The callback will be called with the response body data as first argument |
100 | (or C<undef> if it wasn't available due to errors), and a hash-ref with |
100 | (or C<undef> if an error occured), and a hash-ref with response headers as |
101 | response headers as second argument. |
101 | second argument. |
102 | |
102 | |
103 | All the headers in that hash are lowercased. In addition to the response |
103 | All the headers in that hash are lowercased. In addition to the response |
104 | headers, the "pseudo-headers" C<HTTPVersion>, C<Status> and C<Reason> |
104 | headers, the "pseudo-headers" C<HTTPVersion>, C<Status> and C<Reason> |
105 | contain the three parts of the HTTP Status-Line of the same name. The |
105 | contain the three parts of the HTTP Status-Line of the same name. The |
106 | pseudo-header C<URL> contains the original URL (which can differ from the |
106 | pseudo-header C<URL> contains the original URL (which can differ from the |
… | |
… | |
186 | verification) TLS context. |
186 | verification) TLS context. |
187 | |
187 | |
188 | The default for this option is C<low>, which could be interpreted as "give |
188 | The default for this option is C<low>, which could be interpreted as "give |
189 | me the page, no matter what". |
189 | me the page, no matter what". |
190 | |
190 | |
191 | =item on_header => $callback->($hdr) |
191 | =item on_header => $callback->($headers) |
192 | |
192 | |
193 | When specified, this callback will be called with the header hash as soon |
193 | When specified, this callback will be called with the header hash as soon |
194 | as headers have been successfully received from the remote server (not on |
194 | as headers have been successfully received from the remote server (not on |
195 | locally-generated errors). |
195 | locally-generated errors). |
196 | |
196 | |
… | |
… | |
200 | |
200 | |
201 | This callback is useful, among other things, to quickly reject unwanted |
201 | This callback is useful, among other things, to quickly reject unwanted |
202 | content, which, if it is supposed to be rare, can be faster than first |
202 | content, which, if it is supposed to be rare, can be faster than first |
203 | doing a C<HEAD> request. |
203 | doing a C<HEAD> request. |
204 | |
204 | |
|
|
205 | Example: cancel the request unless the content-type is "text/html". |
|
|
206 | |
|
|
207 | on_header => sub { |
|
|
208 | $_[0]{"content-type"} =~ /^text\/html\s*(?:;|$)/ |
|
|
209 | }, |
|
|
210 | |
205 | =item on_body => $callback->($data, $hdr) |
211 | =item on_body => $callback->($partial_body, $headers) |
206 | |
212 | |
207 | When specified, all body data will be "filtered" through this callback. |
213 | When specified, all body data will be passed to this callback instead of |
|
|
214 | to the completion callback. The completion callback will get the empty |
|
|
215 | string instead of the body data. |
208 | |
216 | |
209 | The callback will incrementally receive body data, and is supposed to |
217 | It has to return either true (in which case AnyEvent::HTTP will continue), |
210 | return it or a modified version of it (empty strings are valid returns). |
218 | or false, in which case AnyEvent::HTTP will cancel the download (and call |
|
|
219 | the completion callback with an error code of C<598>). |
211 | |
220 | |
212 | If the callback returns C<undef>, then the request will be cancelled. |
221 | This callback is useful when the data is too large to be held in memory |
213 | |
222 | (so the callback writes it to a file) or when only some information should |
214 | This callback is useful when you want to do some processing on the data, |
223 | be extracted, or when the body should be processed incrementally. |
215 | or the data is too large to be held in memory (so the callback writes it |
|
|
216 | to a file and returns the empty string) and so on. |
|
|
217 | |
224 | |
218 | It is usually preferred over doing your own body handling via |
225 | It is usually preferred over doing your own body handling via |
219 | C<want_body_handle>. |
226 | C<want_body_handle>, but in case of streaming APIs, where HTTP is |
|
|
227 | only used to create a connection, C<want_body_handle> is the better |
|
|
228 | alternative, as it allows you to install your own event handler, reducing |
|
|
229 | resource usage. |
220 | |
230 | |
221 | =item want_body_handle => $enable |
231 | =item want_body_handle => $enable |
222 | |
232 | |
223 | When enabled (default is disabled), the behaviour of AnyEvent::HTTP |
233 | When enabled (default is disabled), the behaviour of AnyEvent::HTTP |
224 | changes considerably: after parsing the headers, and instead of |
234 | changes considerably: after parsing the headers, and instead of |
… | |
… | |
236 | This is useful with some push-type services, where, after the initial |
246 | This is useful with some push-type services, where, after the initial |
237 | headers, an interactive protocol is used (typical example would be the |
247 | headers, an interactive protocol is used (typical example would be the |
238 | push-style twitter API which starts a JSON/XML stream). |
248 | push-style twitter API which starts a JSON/XML stream). |
239 | |
249 | |
240 | If you think you need this, first have a look at C<on_body>, to see if |
250 | If you think you need this, first have a look at C<on_body>, to see if |
241 | that doesn'T solve your problem in a better way. |
251 | that doesn't solve your problem in a better way. |
242 | |
252 | |
243 | =back |
253 | =back |
244 | |
254 | |
245 | Example: make a simple HTTP GET request for http://www.nethype.de/ |
255 | Example: make a simple HTTP GET request for http://www.nethype.de/ |
246 | |
256 | |
… | |
… | |
347 | $uauthority =~ /^(?: .*\@ )? ([^\@:]+) (?: : (\d+) )?$/x |
357 | $uauthority =~ /^(?: .*\@ )? ([^\@:]+) (?: : (\d+) )?$/x |
348 | or return $cb->(undef, { Status => 599, Reason => "Unparsable URL", URL => $url }); |
358 | or return $cb->(undef, { Status => 599, Reason => "Unparsable URL", URL => $url }); |
349 | |
359 | |
350 | my $uhost = $1; |
360 | my $uhost = $1; |
351 | $uport = $2 if defined $2; |
361 | $uport = $2 if defined $2; |
|
|
362 | |
|
|
363 | $hdr{host} = defined $2 ? "$uhost:$2" : "$uhost"; |
352 | |
364 | |
353 | $uhost =~ s/^\[(.*)\]$/$1/; |
365 | $uhost =~ s/^\[(.*)\]$/$1/; |
354 | $upath .= "?$query" if length $query; |
366 | $upath .= "?$query" if length $query; |
355 | |
367 | |
356 | $upath =~ s%^/?%/%; |
368 | $upath =~ s%^/?%/%; |
… | |
… | |
399 | } |
411 | } |
400 | |
412 | |
401 | $hdr{"user-agent"} ||= $USERAGENT; |
413 | $hdr{"user-agent"} ||= $USERAGENT; |
402 | $hdr{referer} ||= "$uscheme://$uauthority$upath"; # leave out fragment and query string, just a heuristic |
414 | $hdr{referer} ||= "$uscheme://$uauthority$upath"; # leave out fragment and query string, just a heuristic |
403 | |
415 | |
404 | $hdr{host} = "$uhost:$uport"; |
|
|
405 | $hdr{"content-length"} = length $arg{body}; |
416 | $hdr{"content-length"} = length $arg{body}; |
406 | |
417 | |
407 | my %state = (connect_guard => 1); |
418 | my %state = (connect_guard => 1); |
408 | |
419 | |
409 | _get_slot $uhost, sub { |
420 | _get_slot $uhost, sub { |
… | |
… | |
411 | |
422 | |
412 | return unless $state{connect_guard}; |
423 | return unless $state{connect_guard}; |
413 | |
424 | |
414 | $state{connect_guard} = AnyEvent::Socket::tcp_connect $rhost, $rport, sub { |
425 | $state{connect_guard} = AnyEvent::Socket::tcp_connect $rhost, $rport, sub { |
415 | $state{fh} = shift |
426 | $state{fh} = shift |
|
|
427 | or do { |
|
|
428 | my $err = "$!"; |
|
|
429 | %state = (); |
416 | or return (%state = (), $cb->(undef, { Status => 599, Reason => "$!", URL => $url })); |
430 | return $cb->(undef, { Status => 599, Reason => $err, URL => $url }); |
|
|
431 | }; |
|
|
432 | |
417 | pop; # free memory, save a tree |
433 | pop; # free memory, save a tree |
418 | |
434 | |
419 | return unless delete $state{connect_guard}; |
435 | return unless delete $state{connect_guard}; |
420 | |
436 | |
421 | # get handle |
437 | # get handle |
… | |
… | |
483 | # things seen, not parsed: |
499 | # things seen, not parsed: |
484 | # p3pP="NON CUR OTPi OUR NOR UNI" |
500 | # p3pP="NON CUR OTPi OUR NOR UNI" |
485 | |
501 | |
486 | $hdr{lc $1} .= ",$2" |
502 | $hdr{lc $1} .= ",$2" |
487 | while /\G |
503 | while /\G |
488 | ([^:\000-\037]+): |
504 | ([^:\000-\037]*): |
489 | [\011\040]* |
505 | [\011\040]* |
490 | ((?: [^\012]+ | \012[\011\040] )*) |
506 | ((?: [^\012]+ | \012[\011\040] )*) |
491 | \012 |
507 | \012 |
492 | /gxc; |
508 | /gxc; |
493 | |
509 | |
… | |
… | |
597 | my $len = $hdr{"content-length"}; |
613 | my $len = $hdr{"content-length"}; |
598 | |
614 | |
599 | if (!$redirect && $arg{on_header} && !$arg{on_header}(\%hdr)) { |
615 | if (!$redirect && $arg{on_header} && !$arg{on_header}(\%hdr)) { |
600 | $finish->(undef, { Status => 598, Reason => "Request cancelled by on_header", URL => $url }); |
616 | $finish->(undef, { Status => 598, Reason => "Request cancelled by on_header", URL => $url }); |
601 | } elsif ( |
617 | } elsif ( |
602 | $hdr{Status} =~ /^(?:1..|204|304)$/ |
618 | $hdr{Status} =~ /^(?:1..|[23]04)$/ |
603 | or $method eq "HEAD" |
619 | or $method eq "HEAD" |
604 | or (defined $len && !$len) |
620 | or (defined $len && !$len) |
605 | ) { |
621 | ) { |
606 | # no body |
622 | # no body |
607 | $finish->("", \%hdr); |
623 | $finish->("", \%hdr); |
… | |
… | |
647 | $finish->((substr delete $_[0]{rbuf}, 0, $len, ""), \%hdr) |
663 | $finish->((substr delete $_[0]{rbuf}, 0, $len, ""), \%hdr) |
648 | if $len <= length $_[0]{rbuf}; |
664 | if $len <= length $_[0]{rbuf}; |
649 | }); |
665 | }); |
650 | } else { |
666 | } else { |
651 | $_[0]->on_error (sub { |
667 | $_[0]->on_error (sub { |
652 | $! == Errno::EPIPE |
668 | $! == Errno::EPIPE || !$! |
653 | ? $finish->(delete $_[0]{rbuf}, \%hdr) |
669 | ? $finish->(delete $_[0]{rbuf}, \%hdr) |
654 | : $finish->(undef, { Status => 599, Reason => $_[2], URL => $url }); |
670 | : $finish->(undef, { Status => 599, Reason => $_[2], URL => $url }); |
655 | }); |
671 | }); |
656 | $_[0]->on_read (sub { }); |
672 | $_[0]->on_read (sub { }); |
657 | } |
673 | } |
… | |
… | |
725 | =item $AnyEvent::HTTP::USERAGENT |
741 | =item $AnyEvent::HTTP::USERAGENT |
726 | |
742 | |
727 | The default value for the C<User-Agent> header (the default is |
743 | The default value for the C<User-Agent> header (the default is |
728 | C<Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)>). |
744 | C<Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)>). |
729 | |
745 | |
730 | =item $AnyEvent::HTTP::MAX_PERSISTENT |
746 | =item $AnyEvent::HTTP::MAX_PER_HOST |
731 | |
747 | |
732 | The maximum number of persistent connections to keep open (default: 8). |
748 | The maximum number of concurrent conenctions to the same host (identified |
|
|
749 | by the hostname). If the limit is exceeded, then the additional requests |
|
|
750 | are queued until previous connections are closed. |
733 | |
751 | |
734 | Not implemented currently. |
752 | The default value for this is C<4>, and it is highly advisable to not |
735 | |
753 | increase it. |
736 | =item $AnyEvent::HTTP::PERSISTENT_TIMEOUT |
|
|
737 | |
|
|
738 | The maximum time to cache a persistent connection, in seconds (default: 2). |
|
|
739 | |
|
|
740 | Not implemented currently. |
|
|
741 | |
754 | |
742 | =item $AnyEvent::HTTP::ACTIVE |
755 | =item $AnyEvent::HTTP::ACTIVE |
743 | |
756 | |
744 | The number of active connections. This is not the number of currently |
757 | The number of active connections. This is not the number of currently |
745 | running requests, but the number of currently open and non-idle TCP |
758 | running requests, but the number of currently open and non-idle TCP |