… | |
… | |
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.42'; |
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 | |
… | |
… | |
138 | |
138 | |
139 | =item headers => hashref |
139 | =item headers => hashref |
140 | |
140 | |
141 | The request headers to use. Currently, C<http_request> may provide its |
141 | The request headers to use. Currently, C<http_request> may provide its |
142 | own C<Host:>, C<Content-Length:>, C<Connection:> and C<Cookie:> headers |
142 | own C<Host:>, C<Content-Length:>, C<Connection:> and C<Cookie:> headers |
143 | and will provide defaults for C<User-Agent:> and C<Referer:>. |
143 | and will provide defaults for C<User-Agent:> and C<Referer:> (this can be |
|
|
144 | suppressed by using C<undef> for these headers in which case they won't be |
|
|
145 | sent at all). |
144 | |
146 | |
145 | =item timeout => $seconds |
147 | =item timeout => $seconds |
146 | |
148 | |
147 | The time-out to use for various stages - each connect attempt will reset |
149 | The time-out to use for various stages - each connect attempt will reset |
148 | the timeout, as will read or write activity. Default timeout is 5 minutes. |
150 | the timeout, as will read or write activity. Default timeout is 5 minutes. |
… | |
… | |
150 | =item proxy => [$host, $port[, $scheme]] or undef |
152 | =item proxy => [$host, $port[, $scheme]] or undef |
151 | |
153 | |
152 | Use the given http proxy for all requests. If not specified, then the |
154 | Use the given http proxy for all requests. If not specified, then the |
153 | default proxy (as specified by C<$ENV{http_proxy}>) is used. |
155 | default proxy (as specified by C<$ENV{http_proxy}>) is used. |
154 | |
156 | |
155 | C<$scheme> must be either missing or C<http> for HTTP, or C<https> for |
157 | C<$scheme> must be either missing, C<http> for HTTP or C<https> for |
156 | HTTPS. |
158 | HTTPS. |
157 | |
159 | |
158 | =item body => $string |
160 | =item body => $string |
159 | |
161 | |
160 | The request body, usually empty. Will be-sent as-is (future versions of |
162 | The request body, usually empty. Will be-sent as-is (future versions of |
… | |
… | |
221 | This callback is useful when the data is too large to be held in memory |
223 | This callback is useful when the data is too large to be held in memory |
222 | (so the callback writes it to a file) or when only some information should |
224 | (so the callback writes it to a file) or when only some information should |
223 | be extracted, or when the body should be processed incrementally. |
225 | be extracted, or when the body should be processed incrementally. |
224 | |
226 | |
225 | It is usually preferred over doing your own body handling via |
227 | It is usually preferred over doing your own body handling via |
226 | C<want_body_handle>. |
228 | C<want_body_handle>, but in case of streaming APIs, where HTTP is |
|
|
229 | only used to create a connection, C<want_body_handle> is the better |
|
|
230 | alternative, as it allows you to install your own event handler, reducing |
|
|
231 | resource usage. |
227 | |
232 | |
228 | =item want_body_handle => $enable |
233 | =item want_body_handle => $enable |
229 | |
234 | |
230 | When enabled (default is disabled), the behaviour of AnyEvent::HTTP |
235 | When enabled (default is disabled), the behaviour of AnyEvent::HTTP |
231 | changes considerably: after parsing the headers, and instead of |
236 | changes considerably: after parsing the headers, and instead of |
… | |
… | |
243 | This is useful with some push-type services, where, after the initial |
248 | This is useful with some push-type services, where, after the initial |
244 | headers, an interactive protocol is used (typical example would be the |
249 | headers, an interactive protocol is used (typical example would be the |
245 | push-style twitter API which starts a JSON/XML stream). |
250 | push-style twitter API which starts a JSON/XML stream). |
246 | |
251 | |
247 | If you think you need this, first have a look at C<on_body>, to see if |
252 | If you think you need this, first have a look at C<on_body>, to see if |
248 | that doesn'T solve your problem in a better way. |
253 | that doesn't solve your problem in a better way. |
249 | |
254 | |
250 | =back |
255 | =back |
251 | |
256 | |
252 | Example: make a simple HTTP GET request for http://www.nethype.de/ |
257 | Example: make a simple HTTP GET request for http://www.nethype.de/ |
253 | |
258 | |
… | |
… | |
309 | push @{ $CO_SLOT{$_[0]}[1] }, $_[1]; |
314 | push @{ $CO_SLOT{$_[0]}[1] }, $_[1]; |
310 | |
315 | |
311 | _slot_schedule $_[0]; |
316 | _slot_schedule $_[0]; |
312 | } |
317 | } |
313 | |
318 | |
314 | our $qr_nl = qr<\015?\012>; |
319 | our $qr_nl = qr{\015?\012}; |
315 | our $qr_nlnl = qr<\015?\012\015?\012>; |
320 | our $qr_nlnl = qr{(?<![^\012])\015?\012}; |
316 | |
321 | |
317 | our $TLS_CTX_LOW = { cache => 1, sslv2 => 1 }; |
322 | our $TLS_CTX_LOW = { cache => 1, sslv2 => 1 }; |
318 | our $TLS_CTX_HIGH = { cache => 1, verify => 1, verify_peername => "https" }; |
323 | our $TLS_CTX_HIGH = { cache => 1, verify => 1, verify_peername => "https" }; |
319 | |
324 | |
320 | sub http_request($$@) { |
325 | sub http_request($$@) { |
… | |
… | |
354 | $uauthority =~ /^(?: .*\@ )? ([^\@:]+) (?: : (\d+) )?$/x |
359 | $uauthority =~ /^(?: .*\@ )? ([^\@:]+) (?: : (\d+) )?$/x |
355 | or return $cb->(undef, { Status => 599, Reason => "Unparsable URL", URL => $url }); |
360 | or return $cb->(undef, { Status => 599, Reason => "Unparsable URL", URL => $url }); |
356 | |
361 | |
357 | my $uhost = $1; |
362 | my $uhost = $1; |
358 | $uport = $2 if defined $2; |
363 | $uport = $2 if defined $2; |
|
|
364 | |
|
|
365 | $hdr{host} = defined $2 ? "$uhost:$2" : "$uhost"; |
359 | |
366 | |
360 | $uhost =~ s/^\[(.*)\]$/$1/; |
367 | $uhost =~ s/^\[(.*)\]$/$1/; |
361 | $upath .= "?$query" if length $query; |
368 | $upath .= "?$query" if length $query; |
362 | |
369 | |
363 | $upath =~ s%^/?%/%; |
370 | $upath =~ s%^/?%/%; |
… | |
… | |
396 | my ($rhost, $rport, $rscheme, $rpath); # request host, port, path |
403 | my ($rhost, $rport, $rscheme, $rpath); # request host, port, path |
397 | |
404 | |
398 | if ($proxy) { |
405 | if ($proxy) { |
399 | ($rpath, $rhost, $rport, $rscheme) = ($url, @$proxy); |
406 | ($rpath, $rhost, $rport, $rscheme) = ($url, @$proxy); |
400 | |
407 | |
|
|
408 | $rscheme = "http" unless defined $rscheme; |
|
|
409 | |
401 | # don't support https requests over https-proxy transport, |
410 | # don't support https requests over https-proxy transport, |
402 | # can't be done with tls as spec'ed, unless you double-encrypt. |
411 | # can't be done with tls as spec'ed, unless you double-encrypt. |
403 | $rscheme = "http" if $uscheme eq "https" && $rscheme eq "https"; |
412 | $rscheme = "http" if $uscheme eq "https" && $rscheme eq "https"; |
404 | } else { |
413 | } else { |
405 | ($rhost, $rport, $rscheme, $rpath) = ($uhost, $uport, $uscheme, $upath); |
414 | ($rhost, $rport, $rscheme, $rpath) = ($uhost, $uport, $uscheme, $upath); |
406 | } |
415 | } |
407 | |
416 | |
408 | $hdr{"user-agent"} ||= $USERAGENT; |
417 | # leave out fragment and query string, just a heuristic |
409 | $hdr{referer} ||= "$uscheme://$uauthority$upath"; # leave out fragment and query string, just a heuristic |
418 | $hdr{referer} ||= "$uscheme://$uauthority$upath" unless exists $hdr{referer}; |
|
|
419 | $hdr{"user-agent"} ||= $USERAGENT unless exists $hdr{"user-agent"}; |
410 | |
420 | |
411 | $hdr{host} = "$uhost:$uport"; |
|
|
412 | $hdr{"content-length"} = length $arg{body}; |
421 | $hdr{"content-length"} = length $arg{body}; |
413 | |
422 | |
414 | my %state = (connect_guard => 1); |
423 | my %state = (connect_guard => 1); |
415 | |
424 | |
416 | _get_slot $uhost, sub { |
425 | _get_slot $uhost, sub { |
… | |
… | |
418 | |
427 | |
419 | return unless $state{connect_guard}; |
428 | return unless $state{connect_guard}; |
420 | |
429 | |
421 | $state{connect_guard} = AnyEvent::Socket::tcp_connect $rhost, $rport, sub { |
430 | $state{connect_guard} = AnyEvent::Socket::tcp_connect $rhost, $rport, sub { |
422 | $state{fh} = shift |
431 | $state{fh} = shift |
|
|
432 | or do { |
|
|
433 | my $err = "$!"; |
|
|
434 | %state = (); |
423 | or return (%state = (), $cb->(undef, { Status => 599, Reason => "$!", URL => $url })); |
435 | return $cb->(undef, { Status => 599, Reason => $err, URL => $url }); |
|
|
436 | }; |
|
|
437 | |
424 | pop; # free memory, save a tree |
438 | pop; # free memory, save a tree |
425 | |
439 | |
426 | return unless delete $state{connect_guard}; |
440 | return unless delete $state{connect_guard}; |
427 | |
441 | |
428 | # get handle |
442 | # get handle |
… | |
… | |
461 | $state{handle}->starttls ("connect") if $uscheme eq "https" && !exists $state{handle}{tls}; |
475 | $state{handle}->starttls ("connect") if $uscheme eq "https" && !exists $state{handle}{tls}; |
462 | |
476 | |
463 | # send request |
477 | # send request |
464 | $state{handle}->push_write ( |
478 | $state{handle}->push_write ( |
465 | "$method $rpath HTTP/1.0\015\012" |
479 | "$method $rpath HTTP/1.0\015\012" |
466 | . (join "", map "\u$_: $hdr{$_}\015\012", keys %hdr) |
480 | . (join "", map "\u$_: $hdr{$_}\015\012", grep defined $hdr{$_}, keys %hdr) |
467 | . "\015\012" |
481 | . "\015\012" |
468 | . (delete $arg{body}) |
482 | . (delete $arg{body}) |
469 | ); |
483 | ); |
470 | |
484 | |
471 | %hdr = (); # reduce memory usage, save a kitten |
485 | %hdr = (); # reduce memory usage, save a kitten |
… | |
… | |
482 | URL => ",$url" |
496 | URL => ",$url" |
483 | ); |
497 | ); |
484 | |
498 | |
485 | # headers, could be optimized a bit |
499 | # headers, could be optimized a bit |
486 | $state{handle}->unshift_read (line => $qr_nlnl, sub { |
500 | $state{handle}->unshift_read (line => $qr_nlnl, sub { |
487 | for ("$_[1]\012") { |
501 | for ("$_[1]") { |
488 | y/\015//d; # weed out any \015, as they show up in the weirdest of places. |
502 | y/\015//d; # weed out any \015, as they show up in the weirdest of places. |
489 | |
503 | |
490 | # things seen, not parsed: |
504 | # things seen, not parsed: |
491 | # p3pP="NON CUR OTPi OUR NOR UNI" |
505 | # p3pP="NON CUR OTPi OUR NOR UNI" |
492 | |
506 | |
493 | $hdr{lc $1} .= ",$2" |
507 | $hdr{lc $1} .= ",$2" |
494 | while /\G |
508 | while /\G |
495 | ([^:\000-\037]+): |
509 | ([^:\000-\037]*): |
496 | [\011\040]* |
510 | [\011\040]* |
497 | ((?: [^\012]+ | \012[\011\040] )*) |
511 | ((?: [^\012]+ | \012[\011\040] )*) |
498 | \012 |
512 | \012 |
499 | /gxc; |
513 | /gxc; |
500 | |
514 | |
… | |
… | |
589 | |
603 | |
590 | redo if /\G\s*,/gc; |
604 | redo if /\G\s*,/gc; |
591 | } |
605 | } |
592 | } |
606 | } |
593 | |
607 | |
594 | if ($redirect) { |
608 | if ($redirect && exists $hdr{location}) { |
595 | # we ignore any errors, as it is very common to receive |
609 | # we ignore any errors, as it is very common to receive |
596 | # Content-Length != 0 but no actual body |
610 | # Content-Length != 0 but no actual body |
597 | # we also access %hdr, as $_[1] might be an erro |
611 | # we also access %hdr, as $_[1] might be an erro |
598 | http_request ($method => $hdr{location}, %arg, recurse => $recurse - 1, $cb); |
612 | http_request ($method => $hdr{location}, %arg, recurse => $recurse - 1, $cb); |
599 | } else { |
613 | } else { |
… | |
… | |
654 | $finish->((substr delete $_[0]{rbuf}, 0, $len, ""), \%hdr) |
668 | $finish->((substr delete $_[0]{rbuf}, 0, $len, ""), \%hdr) |
655 | if $len <= length $_[0]{rbuf}; |
669 | if $len <= length $_[0]{rbuf}; |
656 | }); |
670 | }); |
657 | } else { |
671 | } else { |
658 | $_[0]->on_error (sub { |
672 | $_[0]->on_error (sub { |
659 | $! == Errno::EPIPE |
673 | $! == Errno::EPIPE || !$! |
660 | ? $finish->(delete $_[0]{rbuf}, \%hdr) |
674 | ? $finish->(delete $_[0]{rbuf}, \%hdr) |
661 | : $finish->(undef, { Status => 599, Reason => $_[2], URL => $url }); |
675 | : $finish->(undef, { Status => 599, Reason => $_[2], URL => $url }); |
662 | }); |
676 | }); |
663 | $_[0]->on_read (sub { }); |
677 | $_[0]->on_read (sub { }); |
664 | } |
678 | } |
… | |
… | |
732 | =item $AnyEvent::HTTP::USERAGENT |
746 | =item $AnyEvent::HTTP::USERAGENT |
733 | |
747 | |
734 | The default value for the C<User-Agent> header (the default is |
748 | The default value for the C<User-Agent> header (the default is |
735 | C<Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)>). |
749 | C<Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)>). |
736 | |
750 | |
737 | =item $AnyEvent::HTTP::MAX_PERSISTENT |
751 | =item $AnyEvent::HTTP::MAX_PER_HOST |
738 | |
752 | |
739 | The maximum number of persistent connections to keep open (default: 8). |
753 | The maximum number of concurrent connections to the same host (identified |
|
|
754 | by the hostname). If the limit is exceeded, then the additional requests |
|
|
755 | are queued until previous connections are closed. |
740 | |
756 | |
741 | Not implemented currently. |
757 | The default value for this is C<4>, and it is highly advisable to not |
742 | |
758 | increase it. |
743 | =item $AnyEvent::HTTP::PERSISTENT_TIMEOUT |
|
|
744 | |
|
|
745 | The maximum time to cache a persistent connection, in seconds (default: 2). |
|
|
746 | |
|
|
747 | Not implemented currently. |
|
|
748 | |
759 | |
749 | =item $AnyEvent::HTTP::ACTIVE |
760 | =item $AnyEvent::HTTP::ACTIVE |
750 | |
761 | |
751 | The number of active connections. This is not the number of currently |
762 | The number of active connections. This is not the number of currently |
752 | running requests, but the number of currently open and non-idle TCP |
763 | running requests, but the number of currently open and non-idle TCP |