… | |
… | |
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.41'; |
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 | |
… | |
… | |
221 | This callback is useful when the data is too large to be held in memory |
221 | 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 |
222 | (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. |
223 | be extracted, or when the body should be processed incrementally. |
224 | |
224 | |
225 | It is usually preferred over doing your own body handling via |
225 | It is usually preferred over doing your own body handling via |
226 | 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. |
227 | |
230 | |
228 | =item want_body_handle => $enable |
231 | =item want_body_handle => $enable |
229 | |
232 | |
230 | When enabled (default is disabled), the behaviour of AnyEvent::HTTP |
233 | When enabled (default is disabled), the behaviour of AnyEvent::HTTP |
231 | changes considerably: after parsing the headers, and instead of |
234 | changes considerably: after parsing the headers, and instead of |
… | |
… | |
243 | 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 |
244 | headers, an interactive protocol is used (typical example would be the |
247 | headers, an interactive protocol is used (typical example would be the |
245 | push-style twitter API which starts a JSON/XML stream). |
248 | push-style twitter API which starts a JSON/XML stream). |
246 | |
249 | |
247 | 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 |
248 | that doesn'T solve your problem in a better way. |
251 | that doesn't solve your problem in a better way. |
249 | |
252 | |
250 | =back |
253 | =back |
251 | |
254 | |
252 | 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/ |
253 | |
256 | |
… | |
… | |
309 | push @{ $CO_SLOT{$_[0]}[1] }, $_[1]; |
312 | push @{ $CO_SLOT{$_[0]}[1] }, $_[1]; |
310 | |
313 | |
311 | _slot_schedule $_[0]; |
314 | _slot_schedule $_[0]; |
312 | } |
315 | } |
313 | |
316 | |
314 | our $qr_nl = qr<\015?\012>; |
317 | our $qr_nl = qr{\015?\012}; |
315 | our $qr_nlnl = qr<\015?\012\015?\012>; |
318 | our $qr_nlnl = qr{(?<![^\012])\015?\012}; |
316 | |
319 | |
317 | our $TLS_CTX_LOW = { cache => 1, sslv2 => 1 }; |
320 | our $TLS_CTX_LOW = { cache => 1, sslv2 => 1 }; |
318 | our $TLS_CTX_HIGH = { cache => 1, verify => 1, verify_peername => "https" }; |
321 | our $TLS_CTX_HIGH = { cache => 1, verify => 1, verify_peername => "https" }; |
319 | |
322 | |
320 | sub http_request($$@) { |
323 | sub http_request($$@) { |
… | |
… | |
354 | $uauthority =~ /^(?: .*\@ )? ([^\@:]+) (?: : (\d+) )?$/x |
357 | $uauthority =~ /^(?: .*\@ )? ([^\@:]+) (?: : (\d+) )?$/x |
355 | or return $cb->(undef, { Status => 599, Reason => "Unparsable URL", URL => $url }); |
358 | or return $cb->(undef, { Status => 599, Reason => "Unparsable URL", URL => $url }); |
356 | |
359 | |
357 | my $uhost = $1; |
360 | my $uhost = $1; |
358 | $uport = $2 if defined $2; |
361 | $uport = $2 if defined $2; |
|
|
362 | |
|
|
363 | $hdr{host} = defined $2 ? "$uhost:$2" : "$uhost"; |
359 | |
364 | |
360 | $uhost =~ s/^\[(.*)\]$/$1/; |
365 | $uhost =~ s/^\[(.*)\]$/$1/; |
361 | $upath .= "?$query" if length $query; |
366 | $upath .= "?$query" if length $query; |
362 | |
367 | |
363 | $upath =~ s%^/?%/%; |
368 | $upath =~ s%^/?%/%; |
… | |
… | |
406 | } |
411 | } |
407 | |
412 | |
408 | $hdr{"user-agent"} ||= $USERAGENT; |
413 | $hdr{"user-agent"} ||= $USERAGENT; |
409 | $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 |
410 | |
415 | |
411 | $hdr{host} = "$uhost:$uport"; |
|
|
412 | $hdr{"content-length"} = length $arg{body}; |
416 | $hdr{"content-length"} = length $arg{body}; |
413 | |
417 | |
414 | my %state = (connect_guard => 1); |
418 | my %state = (connect_guard => 1); |
415 | |
419 | |
416 | _get_slot $uhost, sub { |
420 | _get_slot $uhost, sub { |
… | |
… | |
418 | |
422 | |
419 | return unless $state{connect_guard}; |
423 | return unless $state{connect_guard}; |
420 | |
424 | |
421 | $state{connect_guard} = AnyEvent::Socket::tcp_connect $rhost, $rport, sub { |
425 | $state{connect_guard} = AnyEvent::Socket::tcp_connect $rhost, $rport, sub { |
422 | $state{fh} = shift |
426 | $state{fh} = shift |
|
|
427 | or do { |
|
|
428 | my $err = "$!"; |
|
|
429 | %state = (); |
423 | or return (%state = (), $cb->(undef, { Status => 599, Reason => "$!", URL => $url })); |
430 | return $cb->(undef, { Status => 599, Reason => $err, URL => $url }); |
|
|
431 | }; |
|
|
432 | |
424 | pop; # free memory, save a tree |
433 | pop; # free memory, save a tree |
425 | |
434 | |
426 | return unless delete $state{connect_guard}; |
435 | return unless delete $state{connect_guard}; |
427 | |
436 | |
428 | # get handle |
437 | # get handle |
… | |
… | |
482 | URL => ",$url" |
491 | URL => ",$url" |
483 | ); |
492 | ); |
484 | |
493 | |
485 | # headers, could be optimized a bit |
494 | # headers, could be optimized a bit |
486 | $state{handle}->unshift_read (line => $qr_nlnl, sub { |
495 | $state{handle}->unshift_read (line => $qr_nlnl, sub { |
487 | for ("$_[1]\012") { |
496 | for ("$_[1]") { |
488 | y/\015//d; # weed out any \015, as they show up in the weirdest of places. |
497 | y/\015//d; # weed out any \015, as they show up in the weirdest of places. |
489 | |
498 | |
490 | # things seen, not parsed: |
499 | # things seen, not parsed: |
491 | # p3pP="NON CUR OTPi OUR NOR UNI" |
500 | # p3pP="NON CUR OTPi OUR NOR UNI" |
492 | |
501 | |
493 | $hdr{lc $1} .= ",$2" |
502 | $hdr{lc $1} .= ",$2" |
494 | while /\G |
503 | while /\G |
495 | ([^:\000-\037]+): |
504 | ([^:\000-\037]*): |
496 | [\011\040]* |
505 | [\011\040]* |
497 | ((?: [^\012]+ | \012[\011\040] )*) |
506 | ((?: [^\012]+ | \012[\011\040] )*) |
498 | \012 |
507 | \012 |
499 | /gxc; |
508 | /gxc; |
500 | |
509 | |
… | |
… | |
654 | $finish->((substr delete $_[0]{rbuf}, 0, $len, ""), \%hdr) |
663 | $finish->((substr delete $_[0]{rbuf}, 0, $len, ""), \%hdr) |
655 | if $len <= length $_[0]{rbuf}; |
664 | if $len <= length $_[0]{rbuf}; |
656 | }); |
665 | }); |
657 | } else { |
666 | } else { |
658 | $_[0]->on_error (sub { |
667 | $_[0]->on_error (sub { |
659 | $! == Errno::EPIPE |
668 | $! == Errno::EPIPE || !$! |
660 | ? $finish->(delete $_[0]{rbuf}, \%hdr) |
669 | ? $finish->(delete $_[0]{rbuf}, \%hdr) |
661 | : $finish->(undef, { Status => 599, Reason => $_[2], URL => $url }); |
670 | : $finish->(undef, { Status => 599, Reason => $_[2], URL => $url }); |
662 | }); |
671 | }); |
663 | $_[0]->on_read (sub { }); |
672 | $_[0]->on_read (sub { }); |
664 | } |
673 | } |
… | |
… | |
732 | =item $AnyEvent::HTTP::USERAGENT |
741 | =item $AnyEvent::HTTP::USERAGENT |
733 | |
742 | |
734 | 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 |
735 | 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)>). |
736 | |
745 | |
737 | =item $AnyEvent::HTTP::MAX_PERSISTENT |
746 | =item $AnyEvent::HTTP::MAX_PER_HOST |
738 | |
747 | |
739 | 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. |
740 | |
751 | |
741 | Not implemented currently. |
752 | The default value for this is C<4>, and it is highly advisable to not |
742 | |
753 | 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 | |
754 | |
749 | =item $AnyEvent::HTTP::ACTIVE |
755 | =item $AnyEvent::HTTP::ACTIVE |
750 | |
756 | |
751 | 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 |
752 | 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 |