… | |
… | |
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.44'; |
53 | our $VERSION = '1.46'; |
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; |
… | |
… | |
99 | The callback will be called with the response body data as first argument |
99 | The callback will be called with the response body data as first argument |
100 | (or C<undef> if an error occured), and a hash-ref with response headers as |
100 | (or C<undef> if an error occured), and a hash-ref with response headers as |
101 | 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" (uppercase to avoid clashing with possible |
|
|
105 | response headers) C<HTTPVersion>, C<Status> and C<Reason> contain the |
105 | contain the three parts of the HTTP Status-Line of the same name. The |
106 | three parts of the HTTP Status-Line of the same name. |
|
|
107 | |
106 | pseudo-header C<URL> contains the original URL (which can differ from the |
108 | The pseudo-header C<URL> contains the actual URL (which can differ from |
107 | requested URL when following redirects). |
109 | the requested URL when following redirects - for example, you might get |
|
|
110 | an error that your URL scheme is not supported even though your URL is a |
|
|
111 | valid http URL because it redirected to an ftp URL, in which case you can |
|
|
112 | look at the URL pseudo header). |
|
|
113 | |
|
|
114 | The pseudo-header C<Redirect> only exists when the request was a result |
|
|
115 | of an internal redirect. In that case it is an array reference with |
|
|
116 | the C<($data, $headers)> from the redirect response. Note that this |
|
|
117 | response could in turn be the result of a redirect itself, and C<< |
|
|
118 | $headers->{Redirect}[1]{Redirect} >> will then contain the original |
|
|
119 | response, and so on. |
108 | |
120 | |
109 | If the server sends a header multiple times, then their contents will be |
121 | If the server sends a header multiple times, then their contents will be |
110 | joined together with a comma (C<,>), as per the HTTP spec. |
122 | joined together with a comma (C<,>), as per the HTTP spec. |
111 | |
123 | |
112 | If an internal error occurs, such as not being able to resolve a hostname, |
124 | If an internal error occurs, such as not being able to resolve a hostname, |
… | |
… | |
326 | push @{ $CO_SLOT{$_[0]}[1] }, $_[1]; |
338 | push @{ $CO_SLOT{$_[0]}[1] }, $_[1]; |
327 | |
339 | |
328 | _slot_schedule $_[0]; |
340 | _slot_schedule $_[0]; |
329 | } |
341 | } |
330 | |
342 | |
331 | our $qr_nl = qr{\015?\012}; |
|
|
332 | our $qr_nlnl = qr{(?<![^\012])\015?\012}; |
343 | our $qr_nlnl = qr{(?<![^\012])\015?\012}; |
333 | |
344 | |
334 | our $TLS_CTX_LOW = { cache => 1, sslv2 => 1 }; |
345 | our $TLS_CTX_LOW = { cache => 1, sslv2 => 1 }; |
335 | our $TLS_CTX_HIGH = { cache => 1, verify => 1, verify_peername => "https" }; |
346 | our $TLS_CTX_HIGH = { cache => 1, verify => 1, verify_peername => "https" }; |
336 | |
347 | |
… | |
… | |
349 | while (my ($k, $v) = each %$hdr) { |
360 | while (my ($k, $v) = each %$hdr) { |
350 | $hdr{lc $k} = $v; |
361 | $hdr{lc $k} = $v; |
351 | } |
362 | } |
352 | } |
363 | } |
353 | |
364 | |
|
|
365 | # pseudo headers for all subsequent responses |
|
|
366 | my @pseudo = (URL => $url); |
|
|
367 | push @pseudo, Redirect => delete $arg{Redirect} if exists $arg{Redirect}; |
|
|
368 | |
354 | my $recurse = exists $arg{recurse} ? delete $arg{recurse} : $MAX_RECURSE; |
369 | my $recurse = exists $arg{recurse} ? delete $arg{recurse} : $MAX_RECURSE; |
355 | |
370 | |
356 | return $cb->(undef, { Status => 599, Reason => "Too many redirections", URL => $url }) |
371 | return $cb->(undef, { Status => 599, Reason => "Too many redirections", @pseudo }) |
357 | if $recurse < 0; |
372 | if $recurse < 0; |
358 | |
373 | |
359 | my $proxy = $arg{proxy} || $PROXY; |
374 | my $proxy = $arg{proxy} || $PROXY; |
360 | my $timeout = $arg{timeout} || $TIMEOUT; |
375 | my $timeout = $arg{timeout} || $TIMEOUT; |
361 | |
376 | |
362 | my ($uscheme, $uauthority, $upath, $query, $fragment) = |
377 | my ($uscheme, $uauthority, $upath, $query, $fragment) = |
363 | $url =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?|; |
378 | $url =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:(\?[^#]*))?(?:#(.*))?|; |
364 | |
379 | |
365 | $uscheme = lc $uscheme; |
380 | $uscheme = lc $uscheme; |
366 | |
381 | |
367 | my $uport = $uscheme eq "http" ? 80 |
382 | my $uport = $uscheme eq "http" ? 80 |
368 | : $uscheme eq "https" ? 443 |
383 | : $uscheme eq "https" ? 443 |
369 | : return $cb->(undef, { Status => 599, Reason => "Only http and https URL schemes supported", URL => $url }); |
384 | : return $cb->(undef, { Status => 599, Reason => "Only http and https URL schemes supported", @pseudo }); |
370 | |
385 | |
371 | $uauthority =~ /^(?: .*\@ )? ([^\@:]+) (?: : (\d+) )?$/x |
386 | $uauthority =~ /^(?: .*\@ )? ([^\@:]+) (?: : (\d+) )?$/x |
372 | or return $cb->(undef, { Status => 599, Reason => "Unparsable URL", URL => $url }); |
387 | or return $cb->(undef, { Status => 599, Reason => "Unparsable URL", @pseudo }); |
373 | |
388 | |
374 | my $uhost = $1; |
389 | my $uhost = $1; |
375 | $uport = $2 if defined $2; |
390 | $uport = $2 if defined $2; |
376 | |
391 | |
377 | $hdr{host} = defined $2 ? "$uhost:$2" : "$uhost" |
392 | $hdr{host} = defined $2 ? "$uhost:$2" : "$uhost" |
378 | unless exists $hdr{host}; |
393 | unless exists $hdr{host}; |
379 | |
394 | |
380 | $uhost =~ s/^\[(.*)\]$/$1/; |
395 | $uhost =~ s/^\[(.*)\]$/$1/; |
381 | $upath .= "?$query" if length $query; |
396 | $upath .= $query if length $query; |
382 | |
397 | |
383 | $upath =~ s%^/?%/%; |
398 | $upath =~ s%^/?%/%; |
384 | |
399 | |
385 | # cookie processing |
400 | # cookie processing |
386 | if (my $jar = $arg{cookie_jar}) { |
401 | if (my $jar = $arg{cookie_jar}) { |
… | |
… | |
444 | $state{connect_guard} = AnyEvent::Socket::tcp_connect $rhost, $rport, sub { |
459 | $state{connect_guard} = AnyEvent::Socket::tcp_connect $rhost, $rport, sub { |
445 | $state{fh} = shift |
460 | $state{fh} = shift |
446 | or do { |
461 | or do { |
447 | my $err = "$!"; |
462 | my $err = "$!"; |
448 | %state = (); |
463 | %state = (); |
449 | return $cb->(undef, { Status => 599, Reason => $err, URL => $url }); |
464 | return $cb->(undef, { Status => 599, Reason => $err, @pseudo }); |
450 | }; |
465 | }; |
451 | |
466 | |
452 | pop; # free memory, save a tree |
467 | pop; # free memory, save a tree |
453 | |
468 | |
454 | return unless delete $state{connect_guard}; |
469 | return unless delete $state{connect_guard}; |
455 | |
470 | |
456 | # get handle |
471 | # get handle |
457 | $state{handle} = new AnyEvent::Handle |
472 | $state{handle} = new AnyEvent::Handle |
458 | fh => $state{fh}, |
473 | fh => $state{fh}, |
|
|
474 | peername => $rhost, |
|
|
475 | tls_ctx => $arg{tls_ctx}, |
|
|
476 | # these need to be reconfigured on keepalive handles |
459 | timeout => $timeout, |
477 | timeout => $timeout, |
460 | peername => $rhost, |
478 | on_error => sub { |
461 | tls_ctx => $arg{tls_ctx}; |
479 | %state = (); |
|
|
480 | $cb->(undef, { Status => 599, Reason => $_[2], @pseudo }); |
|
|
481 | }, |
|
|
482 | on_eof => sub { |
|
|
483 | %state = (); |
|
|
484 | $cb->(undef, { Status => 599, Reason => "Unexpected end-of-file", @pseudo }); |
|
|
485 | }, |
|
|
486 | ; |
462 | |
487 | |
463 | # limit the number of persistent connections |
488 | # limit the number of persistent connections |
464 | # keepalive not yet supported |
489 | # keepalive not yet supported |
465 | if ($KA_COUNT{$_[1]} < $MAX_PERSISTENT_PER_HOST) { |
490 | # if ($KA_COUNT{$_[1]} < $MAX_PERSISTENT_PER_HOST) { |
466 | ++$KA_COUNT{$_[1]}; |
491 | # ++$KA_COUNT{$_[1]}; |
467 | $state{handle}{ka_count_guard} = AnyEvent::Util::guard { |
492 | # $state{handle}{ka_count_guard} = AnyEvent::Util::guard { |
468 | --$KA_COUNT{$_[1]} |
493 | # --$KA_COUNT{$_[1]} |
469 | }; |
494 | # }; |
470 | $hdr{connection} = "keep-alive"; |
495 | # $hdr{connection} = "keep-alive"; |
471 | } else { |
496 | # } else { |
472 | delete $hdr{connection}; |
497 | delete $hdr{connection}; |
473 | } |
498 | # } |
474 | |
|
|
475 | # (re-)configure handle |
|
|
476 | $state{handle}->on_error (sub { |
|
|
477 | %state = (); |
|
|
478 | $cb->(undef, { Status => 599, Reason => $_[2], URL => $url }); |
|
|
479 | }); |
|
|
480 | $state{handle}->on_eof (sub { |
|
|
481 | %state = (); |
|
|
482 | $cb->(undef, { Status => 599, Reason => "Unexpected end-of-file", URL => $url }); |
|
|
483 | }); |
|
|
484 | |
499 | |
485 | $state{handle}->starttls ("connect") if $rscheme eq "https"; |
500 | $state{handle}->starttls ("connect") if $rscheme eq "https"; |
486 | |
501 | |
487 | # handle actual, non-tunneled, request |
502 | # handle actual, non-tunneled, request |
488 | my $handle_actual_request = sub { |
503 | my $handle_actual_request = sub { |
… | |
… | |
497 | ); |
512 | ); |
498 | |
513 | |
499 | # return if error occured during push_write() |
514 | # return if error occured during push_write() |
500 | return unless %state; |
515 | return unless %state; |
501 | |
516 | |
502 | %hdr = (); # reduce memory usage, save a kitten |
517 | %hdr = (); # reduce memory usage, save a kitten, also make it possible to re-use |
503 | |
518 | |
504 | # status line |
519 | # status line and headers |
505 | $state{handle}->push_read (line => $qr_nl, sub { |
520 | $state{handle}->push_read (line => $qr_nlnl, sub { |
506 | $_[1] =~ /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) (?: \s+ ([^\015\012]*) )?/ix |
|
|
507 | or return (%state = (), $cb->(undef, { Status => 599, Reason => "Invalid server response ($_[1])", URL => $url })); |
|
|
508 | |
|
|
509 | my %hdr = ( # response headers |
|
|
510 | HTTPVersion => ",$1", |
|
|
511 | Status => ",$2", |
|
|
512 | Reason => ",$3", |
|
|
513 | URL => ",$url" |
|
|
514 | ); |
|
|
515 | |
|
|
516 | # headers, could be optimized a bit |
|
|
517 | $state{handle}->unshift_read (line => $qr_nlnl, sub { |
|
|
518 | for ("$_[1]") { |
521 | for ("$_[1]") { |
519 | y/\015//d; # weed out any \015, as they show up in the weirdest of places. |
522 | y/\015//d; # weed out any \015, as they show up in the weirdest of places. |
520 | |
523 | |
|
|
524 | /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) (?: \s+ ([^\015\012]*) )? \015?\012/igxc |
|
|
525 | or return (%state = (), $cb->(undef, { Status => 599, Reason => "Invalid server response", @pseudo })); |
|
|
526 | |
|
|
527 | push @pseudo, |
|
|
528 | HTTPVersion => $1, |
|
|
529 | Status => $2, |
|
|
530 | Reason => $3, |
|
|
531 | ; |
|
|
532 | |
521 | # things seen, not parsed: |
533 | # things seen, not parsed: |
522 | # p3pP="NON CUR OTPi OUR NOR UNI" |
534 | # p3pP="NON CUR OTPi OUR NOR UNI" |
523 | |
535 | |
524 | $hdr{lc $1} .= ",$2" |
536 | $hdr{lc $1} .= ",$2" |
525 | while /\G |
537 | while /\G |
526 | ([^:\000-\037]*): |
538 | ([^:\000-\037]*): |
527 | [\011\040]* |
539 | [\011\040]* |
528 | ((?: [^\012]+ | \012[\011\040] )*) |
540 | ((?: [^\012]+ | \012[\011\040] )*) |
529 | \012 |
541 | \012 |
530 | /gxc; |
542 | /gxc; |
531 | |
543 | |
532 | /\G$/ |
544 | /\G$/ |
533 | or return (%state = (), $cb->(undef, { Status => 599, Reason => "Garbled response headers", URL => $url })); |
545 | or return (%state = (), $cb->(undef, { Status => 599, Reason => "Garbled response headers", @pseudo })); |
|
|
546 | } |
|
|
547 | |
|
|
548 | # remove the "," prefix we added to all headers above |
|
|
549 | substr $_, 0, 1, "" |
|
|
550 | for values %hdr; |
|
|
551 | |
|
|
552 | # patch in all pseudo headers |
|
|
553 | %hdr = (%hdr, @pseudo); |
|
|
554 | |
|
|
555 | # redirect handling |
|
|
556 | # microsoft and other shitheads don't give a shit for following standards, |
|
|
557 | # try to support some common forms of broken Location headers. |
|
|
558 | if ($hdr{location} !~ /^(?: $ | [^:\/?\#]+ : )/x) { |
|
|
559 | $hdr{location} =~ s/^\.\/+//; |
|
|
560 | |
|
|
561 | my $url = "$rscheme://$uhost:$uport"; |
|
|
562 | |
|
|
563 | unless ($hdr{location} =~ s/^\///) { |
|
|
564 | $url .= $upath; |
|
|
565 | $url =~ s/\/[^\/]*$//; |
534 | } |
566 | } |
535 | |
567 | |
536 | substr $_, 0, 1, "" |
|
|
537 | for values %hdr; |
|
|
538 | |
|
|
539 | # redirect handling |
|
|
540 | # microsoft and other shitheads don't give a shit for following standards, |
|
|
541 | # try to support some common forms of broken Location headers. |
|
|
542 | if ($hdr{location} !~ /^(?: $ | [^:\/?\#]+ : )/x) { |
|
|
543 | $hdr{location} =~ s/^\.\/+//; |
|
|
544 | |
|
|
545 | my $url = "$rscheme://$uhost:$uport"; |
|
|
546 | |
|
|
547 | unless ($hdr{location} =~ s/^\///) { |
|
|
548 | $url .= $upath; |
|
|
549 | $url =~ s/\/[^\/]*$//; |
|
|
550 | } |
|
|
551 | |
|
|
552 | $hdr{location} = "$url/$hdr{location}"; |
568 | $hdr{location} = "$url/$hdr{location}"; |
|
|
569 | } |
|
|
570 | |
|
|
571 | my $redirect; |
|
|
572 | |
|
|
573 | if ($recurse) { |
|
|
574 | my $status = $hdr{Status}; |
|
|
575 | |
|
|
576 | # industry standard is to redirect POST as GET for |
|
|
577 | # 301, 302 and 303, in contrast to http/1.0 and 1.1. |
|
|
578 | # also, the UA should ask the user for 301 and 307 and POST, |
|
|
579 | # industry standard seems to be to simply follow. |
|
|
580 | # we go with the industry standard. |
|
|
581 | if ($status == 301 or $status == 302 or $status == 303) { |
|
|
582 | # HTTP/1.1 is unclear on how to mutate the method |
|
|
583 | $method = "GET" unless $method eq "HEAD"; |
|
|
584 | $redirect = 1; |
|
|
585 | } elsif ($status == 307) { |
|
|
586 | $redirect = 1; |
553 | } |
587 | } |
|
|
588 | } |
554 | |
589 | |
555 | my $redirect; |
590 | my $finish = sub { |
|
|
591 | $state{handle}->destroy if $state{handle}; |
|
|
592 | %state = (); |
556 | |
593 | |
|
|
594 | # set-cookie processing |
557 | if ($recurse) { |
595 | if ($arg{cookie_jar}) { |
558 | if ($hdr{Status} =~ /^30[12]$/ && $method ne "POST") { |
596 | for ($_[1]{"set-cookie"}) { |
559 | # apparently, mozilla et al. just change POST to GET here |
597 | # parse NAME=VALUE |
560 | # more research is needed before we do the same |
598 | my @kv; |
|
|
599 | |
|
|
600 | while (/\G\s* ([^=;,[:space:]]+) \s*=\s* (?: "((?:[^\\"]+|\\.)*)" | ([^=;,[:space:]]*) )/gcxs) { |
561 | $redirect = 1; |
601 | my $name = $1; |
562 | } elsif ($hdr{Status} == 303) { |
602 | my $value = $3; |
563 | # even http/1.1 is unclear on how to mutate the method |
603 | |
564 | $method = "GET" unless $method eq "HEAD"; |
604 | unless ($value) { |
|
|
605 | $value = $2; |
|
|
606 | $value =~ s/\\(.)/$1/gs; |
|
|
607 | } |
|
|
608 | |
|
|
609 | push @kv, $name => $value; |
|
|
610 | |
|
|
611 | last unless /\G\s*;/gc; |
|
|
612 | } |
|
|
613 | |
|
|
614 | last unless @kv; |
|
|
615 | |
|
|
616 | my $name = shift @kv; |
|
|
617 | my %kv = (value => shift @kv, @kv); |
|
|
618 | |
|
|
619 | my $cdom; |
|
|
620 | my $cpath = (delete $kv{path}) || "/"; |
|
|
621 | |
|
|
622 | if (exists $kv{domain}) { |
|
|
623 | $cdom = delete $kv{domain}; |
|
|
624 | |
|
|
625 | $cdom =~ s/^\.?/./; # make sure it starts with a "." |
|
|
626 | |
|
|
627 | next if $cdom =~ /\.$/; |
|
|
628 | |
|
|
629 | # this is not rfc-like and not netscape-like. go figure. |
|
|
630 | my $ndots = $cdom =~ y/.//; |
|
|
631 | next if $ndots < ($cdom =~ /\.[^.][^.]\.[^.][^.]$/ ? 3 : 2); |
|
|
632 | } else { |
|
|
633 | $cdom = $uhost; |
|
|
634 | } |
|
|
635 | |
565 | $redirect = 1; |
636 | # store it |
566 | } elsif ($hdr{Status} == 307 && $method =~ /^(?:GET|HEAD)$/) { |
637 | $arg{cookie_jar}{version} = 1; |
567 | $redirect = 1; |
638 | $arg{cookie_jar}{$cdom}{$cpath}{$name} = \%kv; |
|
|
639 | |
|
|
640 | redo if /\G\s*,/gc; |
568 | } |
641 | } |
569 | } |
642 | } |
570 | |
643 | |
571 | my $finish = sub { |
644 | if ($redirect && exists $hdr{location}) { |
572 | $state{handle}->destroy if $state{handle}; |
645 | # we ignore any errors, as it is very common to receive |
573 | %state = (); |
646 | # Content-Length != 0 but no actual body |
574 | |
647 | # we also access %hdr, as $_[1] might be an erro |
575 | # set-cookie processing |
648 | http_request ( |
576 | if ($arg{cookie_jar}) { |
649 | $method => $hdr{location}, |
577 | for ($_[1]{"set-cookie"}) { |
|
|
578 | # parse NAME=VALUE |
|
|
579 | my @kv; |
|
|
580 | |
|
|
581 | while (/\G\s* ([^=;,[:space:]]+) \s*=\s* (?: "((?:[^\\"]+|\\.)*)" | ([^=;,[:space:]]*) )/gcxs) { |
|
|
582 | my $name = $1; |
|
|
583 | my $value = $3; |
|
|
584 | |
|
|
585 | unless ($value) { |
|
|
586 | $value = $2; |
|
|
587 | $value =~ s/\\(.)/$1/gs; |
|
|
588 | } |
650 | %arg, |
|
|
651 | recurse => $recurse - 1, |
|
|
652 | Redirect => \@_, |
|
|
653 | $cb); |
|
|
654 | } else { |
|
|
655 | $cb->($_[0], $_[1]); |
|
|
656 | } |
|
|
657 | }; |
589 | |
658 | |
590 | push @kv, $name => $value; |
659 | my $len = $hdr{"content-length"}; |
591 | |
660 | |
592 | last unless /\G\s*;/gc; |
661 | if (!$redirect && $arg{on_header} && !$arg{on_header}(\%hdr)) { |
|
|
662 | $finish->(undef, { Status => 598, Reason => "Request cancelled by on_header", @pseudo }); |
|
|
663 | } elsif ( |
|
|
664 | $hdr{Status} =~ /^(?:1..|[23]04)$/ |
|
|
665 | or $method eq "HEAD" |
|
|
666 | or (defined $len && !$len) |
|
|
667 | ) { |
|
|
668 | # no body |
|
|
669 | $finish->("", \%hdr); |
|
|
670 | } else { |
|
|
671 | # body handling, four different code paths |
|
|
672 | # for want_body_handle, on_body (2x), normal (2x) |
|
|
673 | # we might read too much here, but it does not matter yet (no pers. connections) |
|
|
674 | if (!$redirect && $arg{want_body_handle}) { |
|
|
675 | $_[0]->on_eof (undef); |
|
|
676 | $_[0]->on_error (undef); |
|
|
677 | $_[0]->on_read (undef); |
|
|
678 | |
|
|
679 | $finish->(delete $state{handle}, \%hdr); |
|
|
680 | |
|
|
681 | } elsif ($arg{on_body}) { |
|
|
682 | $_[0]->on_error (sub { $finish->(undef, { Status => 599, Reason => $_[2], @pseudo }) }); |
|
|
683 | if ($len) { |
|
|
684 | $_[0]->on_eof (undef); |
|
|
685 | $_[0]->on_read (sub { |
|
|
686 | $len -= length $_[0]{rbuf}; |
|
|
687 | |
|
|
688 | $arg{on_body}(delete $_[0]{rbuf}, \%hdr) |
|
|
689 | or $finish->(undef, { Status => 598, Reason => "Request cancelled by on_body", @pseudo }); |
|
|
690 | |
593 | } |
691 | $len > 0 |
594 | |
692 | or $finish->("", \%hdr); |
595 | last unless @kv; |
|
|
596 | |
|
|
597 | my $name = shift @kv; |
|
|
598 | my %kv = (value => shift @kv, @kv); |
|
|
599 | |
|
|
600 | my $cdom; |
|
|
601 | my $cpath = (delete $kv{path}) || "/"; |
|
|
602 | |
|
|
603 | if (exists $kv{domain}) { |
|
|
604 | $cdom = delete $kv{domain}; |
|
|
605 | |
|
|
606 | $cdom =~ s/^\.?/./; # make sure it starts with a "." |
|
|
607 | |
|
|
608 | next if $cdom =~ /\.$/; |
|
|
609 | |
|
|
610 | # this is not rfc-like and not netscape-like. go figure. |
|
|
611 | my $ndots = $cdom =~ y/.//; |
|
|
612 | next if $ndots < ($cdom =~ /\.[^.][^.]\.[^.][^.]$/ ? 3 : 2); |
|
|
613 | } else { |
|
|
614 | $cdom = $uhost; |
|
|
615 | } |
|
|
616 | |
|
|
617 | # store it |
|
|
618 | $arg{cookie_jar}{version} = 1; |
|
|
619 | $arg{cookie_jar}{$cdom}{$cpath}{$name} = \%kv; |
|
|
620 | |
|
|
621 | redo if /\G\s*,/gc; |
|
|
622 | } |
693 | }); |
|
|
694 | } else { |
|
|
695 | $_[0]->on_eof (sub { |
|
|
696 | $finish->("", \%hdr); |
|
|
697 | }); |
|
|
698 | $_[0]->on_read (sub { |
|
|
699 | $arg{on_body}(delete $_[0]{rbuf}, \%hdr) |
|
|
700 | or $finish->(undef, { Status => 598, Reason => "Request cancelled by on_body", @pseudo }); |
|
|
701 | }); |
623 | } |
702 | } |
|
|
703 | } else { |
|
|
704 | $_[0]->on_eof (undef); |
624 | |
705 | |
625 | if ($redirect && exists $hdr{location}) { |
706 | if ($len) { |
626 | # we ignore any errors, as it is very common to receive |
707 | $_[0]->on_error (sub { $finish->(undef, { Status => 599, Reason => $_[2], @pseudo }) }); |
627 | # Content-Length != 0 but no actual body |
708 | $_[0]->on_read (sub { |
628 | # we also access %hdr, as $_[1] might be an erro |
709 | $finish->((substr delete $_[0]{rbuf}, 0, $len, ""), \%hdr) |
629 | http_request ($method => $hdr{location}, %arg, recurse => $recurse - 1, $cb); |
710 | if $len <= length $_[0]{rbuf}; |
|
|
711 | }); |
630 | } else { |
712 | } else { |
631 | $cb->($_[0], $_[1]); |
|
|
632 | } |
|
|
633 | }; |
|
|
634 | |
|
|
635 | my $len = $hdr{"content-length"}; |
|
|
636 | |
|
|
637 | if (!$redirect && $arg{on_header} && !$arg{on_header}(\%hdr)) { |
|
|
638 | $finish->(undef, { Status => 598, Reason => "Request cancelled by on_header", URL => $url }); |
|
|
639 | } elsif ( |
|
|
640 | $hdr{Status} =~ /^(?:1..|[23]04)$/ |
|
|
641 | or $method eq "HEAD" |
|
|
642 | or (defined $len && !$len) |
|
|
643 | ) { |
|
|
644 | # no body |
|
|
645 | $finish->("", \%hdr); |
|
|
646 | } else { |
|
|
647 | # body handling, four different code paths |
|
|
648 | # for want_body_handle, on_body (2x), normal (2x) |
|
|
649 | # we might read too much here, but it does not matter yet (no pers. connections) |
|
|
650 | if (!$redirect && $arg{want_body_handle}) { |
|
|
651 | $_[0]->on_eof (undef); |
|
|
652 | $_[0]->on_error (undef); |
|
|
653 | $_[0]->on_read (undef); |
|
|
654 | |
|
|
655 | $finish->(delete $state{handle}, \%hdr); |
|
|
656 | |
|
|
657 | } elsif ($arg{on_body}) { |
|
|
658 | $_[0]->on_error (sub { $finish->(undef, { Status => 599, Reason => $_[2], URL => $url }) }); |
|
|
659 | if ($len) { |
|
|
660 | $_[0]->on_eof (undef); |
|
|
661 | $_[0]->on_read (sub { |
|
|
662 | $len -= length $_[0]{rbuf}; |
|
|
663 | |
|
|
664 | $arg{on_body}(delete $_[0]{rbuf}, \%hdr) |
|
|
665 | or $finish->(undef, { Status => 598, Reason => "Request cancelled by on_body", URL => $url }); |
|
|
666 | |
|
|
667 | $len > 0 |
|
|
668 | or $finish->("", \%hdr); |
|
|
669 | }); |
|
|
670 | } else { |
|
|
671 | $_[0]->on_eof (sub { |
|
|
672 | $finish->("", \%hdr); |
|
|
673 | }); |
|
|
674 | $_[0]->on_read (sub { |
|
|
675 | $arg{on_body}(delete $_[0]{rbuf}, \%hdr) |
|
|
676 | or $finish->(undef, { Status => 598, Reason => "Request cancelled by on_body", URL => $url }); |
|
|
677 | }); |
|
|
678 | } |
|
|
679 | } else { |
|
|
680 | $_[0]->on_eof (undef); |
|
|
681 | |
|
|
682 | if ($len) { |
|
|
683 | $_[0]->on_error (sub { $finish->(undef, { Status => 599, Reason => $_[2], URL => $url }) }); |
|
|
684 | $_[0]->on_read (sub { |
|
|
685 | $finish->((substr delete $_[0]{rbuf}, 0, $len, ""), \%hdr) |
|
|
686 | if $len <= length $_[0]{rbuf}; |
|
|
687 | }); |
|
|
688 | } else { |
|
|
689 | $_[0]->on_error (sub { |
713 | $_[0]->on_error (sub { |
690 | $! == Errno::EPIPE || !$! |
714 | ($! == Errno::EPIPE || !$!) |
691 | ? $finish->(delete $_[0]{rbuf}, \%hdr) |
715 | ? $finish->(delete $_[0]{rbuf}, \%hdr) |
692 | : $finish->(undef, { Status => 599, Reason => $_[2], URL => $url }); |
716 | : $finish->(undef, { Status => 599, Reason => $_[2], @pseudo }); |
693 | }); |
717 | }); |
694 | $_[0]->on_read (sub { }); |
718 | $_[0]->on_read (sub { }); |
695 | } |
|
|
696 | } |
719 | } |
697 | } |
720 | } |
698 | }); |
721 | } |
699 | }); |
722 | }); |
700 | }; |
723 | }; |
701 | |
724 | |
702 | # now handle proxy-CONNECT method |
725 | # now handle proxy-CONNECT method |
703 | if ($proxy && $uscheme eq "https") { |
726 | if ($proxy && $uscheme eq "https") { |
… | |
… | |
705 | |
728 | |
706 | # maybe re-use $uauthority with patched port? |
729 | # maybe re-use $uauthority with patched port? |
707 | $state{handle}->push_write ("CONNECT $uhost:$uport HTTP/1.0\015\012Host: $uhost\015\012\015\012"); |
730 | $state{handle}->push_write ("CONNECT $uhost:$uport HTTP/1.0\015\012Host: $uhost\015\012\015\012"); |
708 | $state{handle}->push_read (line => $qr_nlnl, sub { |
731 | $state{handle}->push_read (line => $qr_nlnl, sub { |
709 | $_[1] =~ /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) (?: \s+ ([^\015\012]*) )?/ix |
732 | $_[1] =~ /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) (?: \s+ ([^\015\012]*) )?/ix |
710 | or return (%state = (), $cb->(undef, { Status => 599, Reason => "Invalid proxy connect response ($_[1])", URL => $url })); |
733 | or return (%state = (), $cb->(undef, { Status => 599, Reason => "Invalid proxy connect response ($_[1])", @pseudo })); |
711 | |
734 | |
712 | if ($2 == 200) { |
735 | if ($2 == 200) { |
713 | $rpath = $upath; |
736 | $rpath = $upath; |
714 | &$handle_actual_request; |
737 | &$handle_actual_request; |
715 | } else { |
738 | } else { |
716 | %state = (); |
739 | %state = (); |
717 | $cb->(undef, { Status => $2, Reason => $3, URL => $url }); |
740 | $cb->(undef, { Status => $2, Reason => $3, @pseudo }); |
718 | } |
741 | } |
719 | }); |
742 | }); |
720 | } else { |
743 | } else { |
721 | &$handle_actual_request; |
744 | &$handle_actual_request; |
722 | } |
745 | } |
… | |
… | |
742 | unshift @_, "POST", $url, "body"; |
765 | unshift @_, "POST", $url, "body"; |
743 | &http_request |
766 | &http_request |
744 | } |
767 | } |
745 | |
768 | |
746 | =back |
769 | =back |
|
|
770 | |
|
|
771 | =head2 DNS CACHING |
|
|
772 | |
|
|
773 | AnyEvent::HTTP uses the AnyEvent::Socket::tcp_connect function for |
|
|
774 | the actual connection, which in turn uses AnyEvent::DNS to resolve |
|
|
775 | hostnames. The latter is a simple stub resolver and does no caching |
|
|
776 | on its own. If you want DNS caching, you currently have to provide |
|
|
777 | your own default resolver (by storing a suitable resolver object in |
|
|
778 | C<$AnyEvent::DNS::RESOLVER>). |
747 | |
779 | |
748 | =head2 GLOBAL FUNCTIONS AND VARIABLES |
780 | =head2 GLOBAL FUNCTIONS AND VARIABLES |
749 | |
781 | |
750 | =over 4 |
782 | =over 4 |
751 | |
783 | |