ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-HTTP/HTTP.pm
(Generate patch)

Comparing AnyEvent-HTTP/HTTP.pm (file contents):
Revision 1.53 by root, Sat Dec 5 15:37:07 2009 UTC vs.
Revision 1.58 by root, Sun Nov 14 20:23:00 2010 UTC

48use AnyEvent::Socket (); 48use AnyEvent::Socket ();
49use AnyEvent::Handle (); 49use AnyEvent::Handle ();
50 50
51use base Exporter::; 51use base Exporter::;
52 52
53our $VERSION = '1.44'; 53our $VERSION = '1.46';
54 54
55our @EXPORT = qw(http_get http_post http_head http_request); 55our @EXPORT = qw(http_get http_post http_head http_request);
56 56
57our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; 57our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)";
58our $MAX_RECURSE = 10; 58our $MAX_RECURSE = 10;
92must be an absolute http or https URL. 92must be an absolute http or https URL.
93 93
94When called in void context, nothing is returned. In other contexts, 94When called in void context, nothing is returned. In other contexts,
95C<http_request> returns a "cancellation guard" - you have to keep the 95C<http_request> returns a "cancellation guard" - you have to keep the
96object at least alive until the callback get called. If the object gets 96object at least alive until the callback get called. If the object gets
97destroyed before the callbakc is called, the request will be cancelled. 97destroyed before the callback is called, the request will be cancelled.
98 98
99The callback will be called with the response body data as first argument 99The 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
101second argument. 101second argument.
102 102
103All the headers in that hash are lowercased. In addition to the response 103All the headers in that hash are lowercased. In addition to the response
104headers, the "pseudo-headers" C<HTTPVersion>, C<Status> and C<Reason> 104headers, the "pseudo-headers" (uppercase to avoid clashing with possible
105response headers) C<HTTPVersion>, C<Status> and C<Reason> contain the
105contain the three parts of the HTTP Status-Line of the same name. The 106three parts of the HTTP Status-Line of the same name.
107
106pseudo-header C<URL> contains the original URL (which can differ from the 108The pseudo-header C<URL> contains the actual URL (which can differ from
107requested URL when following redirects). 109the requested URL when following redirects - for example, you might get
110an error that your URL scheme is not supported even though your URL is a
111valid http URL because it redirected to an ftp URL, in which case you can
112look at the URL pseudo header).
113
114The pseudo-header C<Redirect> only exists when the request was a result
115of an internal redirect. In that case it is an array reference with
116the C<($data, $headers)> from the redirect response. Note that this
117response could in turn be the result of a redirect itself, and C<<
118$headers->{Redirect}[1]{Redirect} >> will then contain the original
119response, and so on.
108 120
109If the server sends a header multiple times, then their contents will be 121If the server sends a header multiple times, then their contents will be
110joined together with a comma (C<,>), as per the HTTP spec. 122joined together with a comma (C<,>), as per the HTTP spec.
111 123
112If an internal error occurs, such as not being able to resolve a hostname, 124If 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
331our $qr_nl = qr{\015?\012};
332our $qr_nlnl = qr{(?<![^\012])\015?\012}; 343our $qr_nlnl = qr{(?<![^\012])\015?\012};
333 344
334our $TLS_CTX_LOW = { cache => 1, sslv2 => 1 }; 345our $TLS_CTX_LOW = { cache => 1, sslv2 => 1 };
335our $TLS_CTX_HIGH = { cache => 1, verify => 1, verify_peername => "https" }; 346our $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 {
494 . (join "", map "\u$_: $hdr{$_}\015\012", grep defined $hdr{$_}, keys %hdr) 509 . (join "", map "\u$_: $hdr{$_}\015\012", grep defined $hdr{$_}, keys %hdr)
495 . "\015\012" 510 . "\015\012"
496 . (delete $arg{body}) 511 . (delete $arg{body})
497 ); 512 );
498 513
514 # return if error occured during push_write()
515 return unless %state;
516
499 %hdr = (); # reduce memory usage, save a kitten 517 %hdr = (); # reduce memory usage, save a kitten, also make it possible to re-use
500 518
501 # status line 519 # status line and headers
502 $state{handle}->push_read (line => $qr_nl, sub { 520 $state{handle}->push_read (line => $qr_nlnl, sub {
503 $_[1] =~ /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) (?: \s+ ([^\015\012]*) )?/ix
504 or return (%state = (), $cb->(undef, { Status => 599, Reason => "Invalid server response ($_[1])", URL => $url }));
505
506 my %hdr = ( # response headers
507 HTTPVersion => ",$1",
508 Status => ",$2",
509 Reason => ",$3",
510 URL => ",$url"
511 );
512
513 # headers, could be optimized a bit
514 $state{handle}->unshift_read (line => $qr_nlnl, sub {
515 for ("$_[1]") { 521 for ("$_[1]") {
516 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.
517 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
518 # things seen, not parsed: 533 # things seen, not parsed:
519 # p3pP="NON CUR OTPi OUR NOR UNI" 534 # p3pP="NON CUR OTPi OUR NOR UNI"
520 535
521 $hdr{lc $1} .= ",$2" 536 $hdr{lc $1} .= ",$2"
522 while /\G 537 while /\G
523 ([^:\000-\037]*): 538 ([^:\000-\037]*):
524 [\011\040]* 539 [\011\040]*
525 ((?: [^\012]+ | \012[\011\040] )*) 540 ((?: [^\012]+ | \012[\011\040] )*)
526 \012 541 \012
527 /gxc; 542 /gxc;
528 543
529 /\G$/ 544 /\G$/
530 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/\/[^\/]*$//;
531 } 566 }
532 567
533 substr $_, 0, 1, ""
534 for values %hdr;
535
536 # redirect handling
537 # microsoft and other shitheads don't give a shit for following standards,
538 # try to support some common forms of broken Location headers.
539 if ($hdr{location} !~ /^(?: $ | [^:\/?\#]+ : )/x) {
540 $hdr{location} =~ s/^\.\/+//;
541
542 my $url = "$rscheme://$uhost:$uport";
543
544 unless ($hdr{location} =~ s/^\///) {
545 $url .= $upath;
546 $url =~ s/\/[^\/]*$//;
547 }
548
549 $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;
550 } 587 }
588 }
551 589
552 my $redirect; 590 my $finish = sub {
591 $state{handle}->destroy if $state{handle};
592 %state = ();
553 593
594 # set-cookie processing
554 if ($recurse) { 595 if ($arg{cookie_jar}) {
555 if ($hdr{Status} =~ /^30[12]$/ && $method ne "POST") { 596 for ($_[1]{"set-cookie"}) {
556 # apparently, mozilla et al. just change POST to GET here 597 # parse NAME=VALUE
557 # more research is needed before we do the same 598 my @kv;
599
600 while (/\G\s* ([^=;,[:space:]]+) \s*=\s* (?: "((?:[^\\"]+|\\.)*)" | ([^=;,[:space:]]*) )/gcxs) {
558 $redirect = 1; 601 my $name = $1;
559 } elsif ($hdr{Status} == 303) { 602 my $value = $3;
560 # even http/1.1 is unclear on how to mutate the method 603
561 $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
562 $redirect = 1; 636 # store it
563 } elsif ($hdr{Status} == 307 && $method =~ /^(?:GET|HEAD)$/) { 637 $arg{cookie_jar}{version} = 1;
564 $redirect = 1; 638 $arg{cookie_jar}{$cdom}{$cpath}{$name} = \%kv;
639
640 redo if /\G\s*,/gc;
565 } 641 }
566 } 642 }
567 643
568 my $finish = sub { 644 if ($redirect && exists $hdr{location}) {
569 $state{handle}->destroy if $state{handle}; 645 # we ignore any errors, as it is very common to receive
570 %state = (); 646 # Content-Length != 0 but no actual body
571 647 # we also access %hdr, as $_[1] might be an erro
572 # set-cookie processing 648 http_request (
573 if ($arg{cookie_jar}) { 649 $method => $hdr{location},
574 for ($_[1]{"set-cookie"}) {
575 # parse NAME=VALUE
576 my @kv;
577
578 while (/\G\s* ([^=;,[:space:]]+) \s*=\s* (?: "((?:[^\\"]+|\\.)*)" | ([^=;,[:space:]]*) )/gcxs) {
579 my $name = $1;
580 my $value = $3;
581
582 unless ($value) {
583 $value = $2;
584 $value =~ s/\\(.)/$1/gs;
585 } 650 %arg,
651 recurse => $recurse - 1,
652 Redirect => \@_,
653 $cb);
654 } else {
655 $cb->($_[0], $_[1]);
656 }
657 };
586 658
587 push @kv, $name => $value; 659 my $len = $hdr{"content-length"};
588 660
589 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
590 } 691 $len > 0
591 692 or $finish->("", \%hdr);
592 last unless @kv;
593
594 my $name = shift @kv;
595 my %kv = (value => shift @kv, @kv);
596
597 my $cdom;
598 my $cpath = (delete $kv{path}) || "/";
599
600 if (exists $kv{domain}) {
601 $cdom = delete $kv{domain};
602
603 $cdom =~ s/^\.?/./; # make sure it starts with a "."
604
605 next if $cdom =~ /\.$/;
606
607 # this is not rfc-like and not netscape-like. go figure.
608 my $ndots = $cdom =~ y/.//;
609 next if $ndots < ($cdom =~ /\.[^.][^.]\.[^.][^.]$/ ? 3 : 2);
610 } else {
611 $cdom = $uhost;
612 }
613
614 # store it
615 $arg{cookie_jar}{version} = 1;
616 $arg{cookie_jar}{$cdom}{$cpath}{$name} = \%kv;
617
618 redo if /\G\s*,/gc;
619 } 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 });
620 } 702 }
703 } else {
704 $_[0]->on_eof (undef);
621 705
622 if ($redirect && exists $hdr{location}) { 706 if ($len) {
623 # we ignore any errors, as it is very common to receive 707 $_[0]->on_error (sub { $finish->(undef, { Status => 599, Reason => $_[2], @pseudo }) });
624 # Content-Length != 0 but no actual body 708 $_[0]->on_read (sub {
625 # we also access %hdr, as $_[1] might be an erro 709 $finish->((substr delete $_[0]{rbuf}, 0, $len, ""), \%hdr)
626 http_request ($method => $hdr{location}, %arg, recurse => $recurse - 1, $cb); 710 if $len <= length $_[0]{rbuf};
711 });
627 } else { 712 } else {
628 $cb->($_[0], $_[1]);
629 }
630 };
631
632 my $len = $hdr{"content-length"};
633
634 if (!$redirect && $arg{on_header} && !$arg{on_header}(\%hdr)) {
635 $finish->(undef, { Status => 598, Reason => "Request cancelled by on_header", URL => $url });
636 } elsif (
637 $hdr{Status} =~ /^(?:1..|[23]04)$/
638 or $method eq "HEAD"
639 or (defined $len && !$len)
640 ) {
641 # no body
642 $finish->("", \%hdr);
643 } else {
644 # body handling, four different code paths
645 # for want_body_handle, on_body (2x), normal (2x)
646 # we might read too much here, but it does not matter yet (no pers. connections)
647 if (!$redirect && $arg{want_body_handle}) {
648 $_[0]->on_eof (undef);
649 $_[0]->on_error (undef);
650 $_[0]->on_read (undef);
651
652 $finish->(delete $state{handle}, \%hdr);
653
654 } elsif ($arg{on_body}) {
655 $_[0]->on_error (sub { $finish->(undef, { Status => 599, Reason => $_[2], URL => $url }) });
656 if ($len) {
657 $_[0]->on_eof (undef);
658 $_[0]->on_read (sub {
659 $len -= length $_[0]{rbuf};
660
661 $arg{on_body}(delete $_[0]{rbuf}, \%hdr)
662 or $finish->(undef, { Status => 598, Reason => "Request cancelled by on_body", URL => $url });
663
664 $len > 0
665 or $finish->("", \%hdr);
666 });
667 } else {
668 $_[0]->on_eof (sub {
669 $finish->("", \%hdr);
670 });
671 $_[0]->on_read (sub {
672 $arg{on_body}(delete $_[0]{rbuf}, \%hdr)
673 or $finish->(undef, { Status => 598, Reason => "Request cancelled by on_body", URL => $url });
674 });
675 }
676 } else {
677 $_[0]->on_eof (undef);
678
679 if ($len) {
680 $_[0]->on_error (sub { $finish->(undef, { Status => 599, Reason => $_[2], URL => $url }) });
681 $_[0]->on_read (sub {
682 $finish->((substr delete $_[0]{rbuf}, 0, $len, ""), \%hdr)
683 if $len <= length $_[0]{rbuf};
684 });
685 } else {
686 $_[0]->on_error (sub { 713 $_[0]->on_error (sub {
687 $! == Errno::EPIPE || !$! 714 ($! == Errno::EPIPE || !$!)
688 ? $finish->(delete $_[0]{rbuf}, \%hdr) 715 ? $finish->(delete $_[0]{rbuf}, \%hdr)
689 : $finish->(undef, { Status => 599, Reason => $_[2], URL => $url }); 716 : $finish->(undef, { Status => 599, Reason => $_[2], @pseudo });
690 }); 717 });
691 $_[0]->on_read (sub { }); 718 $_[0]->on_read (sub { });
692 }
693 } 719 }
694 } 720 }
695 }); 721 }
696 }); 722 });
697 }; 723 };
698 724
699 # now handle proxy-CONNECT method 725 # now handle proxy-CONNECT method
700 if ($proxy && $uscheme eq "https") { 726 if ($proxy && $uscheme eq "https") {
702 728
703 # maybe re-use $uauthority with patched port? 729 # maybe re-use $uauthority with patched port?
704 $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");
705 $state{handle}->push_read (line => $qr_nlnl, sub { 731 $state{handle}->push_read (line => $qr_nlnl, sub {
706 $_[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
707 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 }));
708 734
709 if ($2 == 200) { 735 if ($2 == 200) {
710 $rpath = $upath; 736 $rpath = $upath;
711 &$handle_actual_request; 737 &$handle_actual_request;
712 } else { 738 } else {
713 %state = (); 739 %state = ();
714 $cb->(undef, { Status => $2, Reason => $3, URL => $url }); 740 $cb->(undef, { Status => $2, Reason => $3, @pseudo });
715 } 741 }
716 }); 742 });
717 } else { 743 } else {
718 &$handle_actual_request; 744 &$handle_actual_request;
719 } 745 }
739 unshift @_, "POST", $url, "body"; 765 unshift @_, "POST", $url, "body";
740 &http_request 766 &http_request
741} 767}
742 768
743=back 769=back
770
771=head2 DNS CACHING
772
773AnyEvent::HTTP uses the AnyEvent::Socket::tcp_connect function for
774the actual connection, which in turn uses AnyEvent::DNS to resolve
775hostnames. The latter is a simple stub resolver and does no caching
776on its own. If you want DNS caching, you currently have to provide
777your own default resolver (by storing a suitable resolver object in
778C<$AnyEvent::DNS::RESOLVER>).
744 779
745=head2 GLOBAL FUNCTIONS AND VARIABLES 780=head2 GLOBAL FUNCTIONS AND VARIABLES
746 781
747=over 4 782=over 4
748 783

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines