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.84 by root, Sun Jan 2 05:13:27 2011 UTC vs.
Revision 1.90 by root, Mon Jan 3 00:41:25 2011 UTC

36 36
37=cut 37=cut
38 38
39package AnyEvent::HTTP; 39package AnyEvent::HTTP;
40 40
41use strict; 41use common::sense;
42no warnings;
43 42
44use Errno (); 43use Errno ();
45 44
46use AnyEvent 5.0 (); 45use AnyEvent 5.0 ();
47use AnyEvent::Util (); 46use AnyEvent::Util ();
58our $MAX_PERSISTENT = 8; 57our $MAX_PERSISTENT = 8;
59our $PERSISTENT_TIMEOUT = 2; 58our $PERSISTENT_TIMEOUT = 2;
60our $TIMEOUT = 300; 59our $TIMEOUT = 300;
61 60
62# changing these is evil 61# changing these is evil
63our $MAX_PERSISTENT_PER_HOST = 0; 62our $MAX_PERSISTENT_PER_HOST = 2;
64our $MAX_PER_HOST = 4; 63our $MAX_PER_HOST = 4;
65 64
66our $PROXY; 65our $PROXY;
67our $ACTIVE = 0; 66our $ACTIVE = 0;
68 67
170C<Host:>, C<Content-Length:>, C<Connection:> and C<Cookie:> headers and 169C<Host:>, C<Content-Length:>, C<Connection:> and C<Cookie:> headers and
171will provide defaults at least for C<TE:>, C<Referer:> and C<User-Agent:> 170will provide defaults at least for C<TE:>, C<Referer:> and C<User-Agent:>
172(this can be suppressed by using C<undef> for these headers in which case 171(this can be suppressed by using C<undef> for these headers in which case
173they won't be sent at all). 172they won't be sent at all).
174 173
174You really should provide your own C<User-Agent:> header value that is
175appropriate for your program - I wouldn't be surprised if the default
176AnyEvent string gets blocked by webservers sooner or later.
177
175=item timeout => $seconds 178=item timeout => $seconds
176 179
177The time-out to use for various stages - each connect attempt will reset 180The time-out to use for various stages - each connect attempt will reset
178the timeout, as will read or write activity, i.e. this is not an overall 181the timeout, as will read or write activity, i.e. this is not an overall
179timeout. 182timeout.
330Example: do a HTTP HEAD request on https://www.google.com/, use a 333Example: do a HTTP HEAD request on https://www.google.com/, use a
331timeout of 30 seconds. 334timeout of 30 seconds.
332 335
333 http_request 336 http_request
334 GET => "https://www.google.com", 337 GET => "https://www.google.com",
338 headers => { "user-agent" => "MySearchClient 1.0" },
335 timeout => 30, 339 timeout => 30,
336 sub { 340 sub {
337 my ($body, $hdr) = @_; 341 my ($body, $hdr) = @_;
338 use Data::Dumper; 342 use Data::Dumper;
339 print Dumper $hdr; 343 print Dumper $hdr;
415} 419}
416 420
417# extract cookies from jar 421# extract cookies from jar
418sub cookie_jar_extract($$$$) { 422sub cookie_jar_extract($$$$) {
419 my ($jar, $uscheme, $uhost, $upath) = @_; 423 my ($jar, $uscheme, $uhost, $upath) = @_;
420
421 $uhost = lc $uhost;
422 424
423 %$jar = () if $jar->{version} != 1; 425 %$jar = () if $jar->{version} != 1;
424 426
425 my @cookies; 427 my @cookies;
426 428
609 : return $cb->(undef, { @pseudo, Status => 599, Reason => "Only http and https URL schemes supported" }); 611 : return $cb->(undef, { @pseudo, Status => 599, Reason => "Only http and https URL schemes supported" });
610 612
611 $uauthority =~ /^(?: .*\@ )? ([^\@:]+) (?: : (\d+) )?$/x 613 $uauthority =~ /^(?: .*\@ )? ([^\@:]+) (?: : (\d+) )?$/x
612 or return $cb->(undef, { @pseudo, Status => 599, Reason => "Unparsable URL" }); 614 or return $cb->(undef, { @pseudo, Status => 599, Reason => "Unparsable URL" });
613 615
614 my $uhost = $1; 616 my $uhost = lc $1;
615 $uport = $2 if defined $2; 617 $uport = $2 if defined $2;
616 618
617 $hdr{host} = defined $2 ? "$uhost:$2" : "$uhost" 619 $hdr{host} = defined $2 ? "$uhost:$2" : "$uhost"
618 unless exists $hdr{host}; 620 unless exists $hdr{host};
619 621
638 $rscheme = "http" unless defined $rscheme; 640 $rscheme = "http" unless defined $rscheme;
639 641
640 # don't support https requests over https-proxy transport, 642 # don't support https requests over https-proxy transport,
641 # can't be done with tls as spec'ed, unless you double-encrypt. 643 # can't be done with tls as spec'ed, unless you double-encrypt.
642 $rscheme = "http" if $uscheme eq "https" && $rscheme eq "https"; 644 $rscheme = "http" if $uscheme eq "https" && $rscheme eq "https";
645
646 $rhost = lc $rhost;
647 $rscheme = lc $rscheme;
643 } else { 648 } else {
644 ($rhost, $rport, $rscheme, $rpath) = ($uhost, $uport, $uscheme, $upath); 649 ($rhost, $rport, $rscheme, $rpath) = ($uhost, $uport, $uscheme, $upath);
645 } 650 }
646 651
647 # leave out fragment and query string, just a heuristic 652 # leave out fragment and query string, just a heuristic
649 $hdr{"user-agent"} = $USERAGENT unless exists $hdr{"user-agent"}; 654 $hdr{"user-agent"} = $USERAGENT unless exists $hdr{"user-agent"};
650 655
651 $hdr{"content-length"} = length $arg{body} 656 $hdr{"content-length"} = length $arg{body}
652 if length $arg{body} || $method ne "GET"; 657 if length $arg{body} || $method ne "GET";
653 658
654 $hdr{connection} = "close TE"; #1.1 659 my $idempotent = $method =~ /^(?:GET|HEAD|PUT|DELETE|OPTIONS|TRACE)$/;
660
661 # default value for keepalive is true iff the request is for an idempotent method
662 my $keepalive = exists $arg{keepalive}
663 ? $arg{keepalive}*1
664 : $idempotent ? $PERSISTENT_TIMEOUT : 0;
665
666 $hdr{connection} = ($keepalive ? "" : "close ") . "Te"; #1.1
655 $hdr{te} = "trailers" unless exists $hdr{te}; #1.1 667 $hdr{te} = "trailers" unless exists $hdr{te}; #1.1
656 668
657 my %state = (connect_guard => 1); 669 my %state = (connect_guard => 1);
658 670
659 _get_slot $uhost, sub {
660 $state{slot_guard} = shift;
661
662 return unless $state{connect_guard};
663
664 my $ae_error = 595; # connecting 671 my $ae_error = 595; # connecting
665 672
666 # handle actual, non-tunneled, request 673 # handle actual, non-tunneled, request
667 my $handle_actual_request = sub { 674 my $handle_actual_request = sub {
668 $ae_error = 596; # request phase 675 $ae_error = 596; # request phase
669 676
670 $state{handle}->starttls ("connect") if $uscheme eq "https" && !exists $state{handle}{tls}; 677 $state{handle}->starttls ("connect") if $uscheme eq "https" && !exists $state{handle}{tls};
671 678
672 # send request 679 # send request
673 $state{handle}->push_write ( 680 $state{handle}->push_write (
674 "$method $rpath HTTP/1.1\015\012" 681 "$method $rpath HTTP/1.1\015\012"
675 . (join "", map "\u$_: $hdr{$_}\015\012", grep defined $hdr{$_}, keys %hdr) 682 . (join "", map "\u$_: $hdr{$_}\015\012", grep defined $hdr{$_}, keys %hdr)
676 . "\015\012" 683 . "\015\012"
677 . (delete $arg{body}) 684 . (delete $arg{body})
678 ); 685 );
679 686
680 # return if error occured during push_write() 687 # return if error occured during push_write()
681 return unless %state; 688 return unless %state;
682 689
683 %hdr = (); # reduce memory usage, save a kitten, also make it possible to re-use 690 # reduce memory usage, save a kitten, also re-use it for the response headers.
691 %hdr = ();
684 692
685 # status line and headers 693 # status line and headers
686 $state{read_response} = sub { 694 $state{read_response} = sub {
687 for ("$_[1]") { 695 for ("$_[1]") {
688 y/\015//d; # weed out any \015, as they show up in the weirdest of places. 696 y/\015//d; # weed out any \015, as they show up in the weirdest of places.
689 697
690 /^HTTP\/0*([0-9\.]+) \s+ ([0-9]{3}) (?: \s+ ([^\012]*) )? \012/gxci 698 /^HTTP\/0*([0-9\.]+) \s+ ([0-9]{3}) (?: \s+ ([^\012]*) )? \012/gxci
691 or return (%state = (), $cb->(undef, { @pseudo, Status => 599, Reason => "Invalid server response" })); 699 or return (%state = (), $cb->(undef, { @pseudo, Status => 599, Reason => "Invalid server response" }));
692 700
693 # 100 Continue handling 701 # 100 Continue handling
694 # should not happen as we don't send expect: 100-continue, 702 # should not happen as we don't send expect: 100-continue,
695 # but we handle it just in case. 703 # but we handle it just in case.
696 # since we send the request body regardless, if we get an error 704 # since we send the request body regardless, if we get an error
697 # we are out of-sync, which we currently do NOT handle correctly. 705 # we are out of-sync, which we currently do NOT handle correctly.
698 return $state{handle}->push_read (line => $qr_nlnl, $state{read_response}) 706 return $state{handle}->push_read (line => $qr_nlnl, $state{read_response})
699 if $2 eq 100; 707 if $2 eq 100;
700 708
701 push @pseudo, 709 push @pseudo,
702 HTTPVersion => $1, 710 HTTPVersion => $1,
703 Status => $2, 711 Status => $2,
704 Reason => $3, 712 Reason => $3,
705 ; 713 ;
706 714
707 my $hdr = parse_hdr 715 my $hdr = parse_hdr
708 or return (%state = (), $cb->(undef, { @pseudo, Status => 599, Reason => "Garbled response headers" })); 716 or return (%state = (), $cb->(undef, { @pseudo, Status => 599, Reason => "Garbled response headers" }));
709 717
710 %hdr = (%$hdr, @pseudo); 718 %hdr = (%$hdr, @pseudo);
719 }
720
721 # redirect handling
722 # microsoft and other shitheads don't give a shit for following standards,
723 # try to support some common forms of broken Location headers.
724 if ($hdr{location} !~ /^(?: $ | [^:\/?\#]+ : )/x) {
725 $hdr{location} =~ s/^\.\/+//;
726
727 my $url = "$rscheme://$uhost:$uport";
728
729 unless ($hdr{location} =~ s/^\///) {
730 $url .= $upath;
731 $url =~ s/\/[^\/]*$//;
711 } 732 }
712 733
713 # redirect handling
714 # microsoft and other shitheads don't give a shit for following standards,
715 # try to support some common forms of broken Location headers.
716 if ($hdr{location} !~ /^(?: $ | [^:\/?\#]+ : )/x) {
717 $hdr{location} =~ s/^\.\/+//;
718
719 my $url = "$rscheme://$uhost:$uport";
720
721 unless ($hdr{location} =~ s/^\///) {
722 $url .= $upath;
723 $url =~ s/\/[^\/]*$//;
724 }
725
726 $hdr{location} = "$url/$hdr{location}"; 734 $hdr{location} = "$url/$hdr{location}";
735 }
736
737 my $redirect;
738
739 if ($recurse) {
740 my $status = $hdr{Status};
741
742 # industry standard is to redirect POST as GET for
743 # 301, 302 and 303, in contrast to HTTP/1.0 and 1.1.
744 # also, the UA should ask the user for 301 and 307 and POST,
745 # industry standard seems to be to simply follow.
746 # we go with the industry standard.
747 if ($status == 301 or $status == 302 or $status == 303) {
748 # HTTP/1.1 is unclear on how to mutate the method
749 $method = "GET" unless $method eq "HEAD";
750 $redirect = 1;
751 } elsif ($status == 307) {
752 $redirect = 1;
727 } 753 }
728
729 my $redirect;
730
731 if ($recurse) {
732 my $status = $hdr{Status};
733
734 # industry standard is to redirect POST as GET for
735 # 301, 302 and 303, in contrast to HTTP/1.0 and 1.1.
736 # also, the UA should ask the user for 301 and 307 and POST,
737 # industry standard seems to be to simply follow.
738 # we go with the industry standard.
739 if ($status == 301 or $status == 302 or $status == 303) {
740 # HTTP/1.1 is unclear on how to mutate the method
741 $method = "GET" unless $method eq "HEAD";
742 $redirect = 1;
743 } elsif ($status == 307) {
744 $redirect = 1;
745 } 754 }
755
756 my $finish = sub { # ($data, $err_status, $err_reason[, $keepalive])
757 my $may_keep_alive = $_[3];
758
759 $state{handle}->destroy if $state{handle};
760 %state = ();
761
762 if (defined $_[1]) {
763 $hdr{OrigStatus} = $hdr{Status}; $hdr{Status} = $_[1];
764 $hdr{OrigReason} = $hdr{Reason}; $hdr{Reason} = $_[2];
746 } 765 }
747 766
748 my $finish = sub { # ($data, $err_status, $err_reason[, $keepalive])
749 my $may_keep_alive = $_[3];
750
751 $state{handle}->destroy if $state{handle};
752 %state = ();
753
754 if (defined $_[1]) {
755 $hdr{OrigStatus} = $hdr{Status}; $hdr{Status} = $_[1];
756 $hdr{OrigReason} = $hdr{Reason}; $hdr{Reason} = $_[2];
757 }
758
759 # set-cookie processing 767 # set-cookie processing
760 if ($arg{cookie_jar}) { 768 if ($arg{cookie_jar}) {
761 cookie_jar_set_cookie $arg{cookie_jar}, $hdr{"set-cookie"}, $uhost, $hdr{date}; 769 cookie_jar_set_cookie $arg{cookie_jar}, $hdr{"set-cookie"}, $uhost, $hdr{date};
762 } 770 }
763 771
764 if ($redirect && exists $hdr{location}) { 772 if ($redirect && exists $hdr{location}) {
765 # we ignore any errors, as it is very common to receive 773 # we ignore any errors, as it is very common to receive
766 # Content-Length != 0 but no actual body 774 # Content-Length != 0 but no actual body
767 # we also access %hdr, as $_[1] might be an erro 775 # we also access %hdr, as $_[1] might be an erro
768 http_request ( 776 http_request (
769 $method => $hdr{location}, 777 $method => $hdr{location},
770 %arg, 778 %arg,
771 recurse => $recurse - 1, 779 recurse => $recurse - 1,
772 Redirect => [$_[0], \%hdr], 780 Redirect => [$_[0], \%hdr],
773 $cb); 781 $cb);
782 } else {
783 $cb->($_[0], \%hdr);
784 }
785 };
786
787 $ae_error = 597; # body phase
788
789 my $len = $hdr{"content-length"};
790
791 # body handling, many different code paths
792 # - no body expected
793 # - want_body_handle
794 # - te chunked
795 # - 2x length known (with or without on_body)
796 # - 2x length not known (with or without on_body)
797 if (!$redirect && $arg{on_header} && !$arg{on_header}(\%hdr)) {
798 $finish->(undef, 598 => "Request cancelled by on_header");
799 } elsif (
800 $hdr{Status} =~ /^(?:1..|204|205|304)$/
801 or $method eq "HEAD"
802 or (defined $len && $len == 0) # == 0, not !, because "0 " is true
803 ) {
804 # no body
805 $finish->("", undef, undef, 1);
806
807 } elsif (!$redirect && $arg{want_body_handle}) {
808 $_[0]->on_eof (undef);
809 $_[0]->on_error (undef);
810 $_[0]->on_read (undef);
811
812 $finish->(delete $state{handle});
813
814 } elsif ($hdr{"transfer-encoding"} =~ /\bchunked\b/i) {
815 my $cl = 0;
816 my $body = undef;
817 my $on_body = $arg{on_body} || sub { $body .= shift; 1 };
818
819 $state{read_chunk} = sub {
820 $_[1] =~ /^([0-9a-fA-F]+)/
821 or $finish->(undef, $ae_error => "Garbled chunked transfer encoding");
822
823 my $len = hex $1;
824
825 if ($len) {
826 $cl += $len;
827
828 $_[0]->push_read (chunk => $len, sub {
829 $on_body->($_[1], \%hdr)
830 or return $finish->(undef, 598 => "Request cancelled by on_body");
831
832 $_[0]->push_read (line => sub {
833 length $_[1]
834 and return $finish->(undef, $ae_error => "Garbled chunked transfer encoding");
835 $_[0]->push_read (line => $state{read_chunk});
836 });
837 });
774 } else { 838 } else {
775 $cb->($_[0], \%hdr); 839 $hdr{"content-length"} ||= $cl;
840
841 $_[0]->push_read (line => $qr_nlnl, sub {
842 if (length $_[1]) {
843 for ("$_[1]") {
844 y/\015//d; # weed out any \015, as they show up in the weirdest of places.
845
846 my $hdr = parse_hdr
847 or return $finish->(undef, $ae_error => "Garbled response trailers");
848
849 %hdr = (%hdr, %$hdr);
850 }
851 }
852
853 $finish->($body, undef, undef, 1);
854 });
776 } 855 }
777 }; 856 };
778 857
779 $ae_error = 597; # body phase
780
781 my $len = $hdr{"content-length"};
782 warn "no content $redirect x<$len>$hdr{Status}\n";#d#
783
784 # body handling, many different code paths
785 # - no body expected
786 # - want_body_handle
787 # - te chunked
788 # - 2x length known (with or without on_body)
789 # - 2x length not known (with or without on_body)
790 if (!$redirect && $arg{on_header} && !$arg{on_header}(\%hdr)) {
791 $finish->(undef, 598 => "Request cancelled by on_header");
792 } elsif (
793 $hdr{Status} =~ /^(?:1..|204|205|304)$/
794 or $method eq "HEAD"
795 or (defined $len && $len == 0) # == 0, not !, because "0 " is true
796 ) {
797 # no body
798 $finish->("", undef, undef, 1);
799
800 } elsif (!$redirect && $arg{want_body_handle}) {
801 $_[0]->on_eof (undef);
802 $_[0]->on_error (undef);
803 $_[0]->on_read (undef);
804
805 $finish->(delete $state{handle});
806
807 } elsif ($hdr{"transfer-encoding"} =~ /\bchunked\b/i) {
808 my $cl = 0;
809 my $body = undef;
810 my $on_body = $arg{on_body} || sub { $body .= shift; 1 };
811
812 $state{read_chunk} = sub {
813 $_[1] =~ /^([0-9a-fA-F]+)/
814 or $finish->(undef, $ae_error => "Garbled chunked transfer encoding");
815
816 my $len = hex $1;
817
818 if ($len) {
819 $cl += $len;
820
821 $_[0]->push_read (chunk => $len, sub {
822 $on_body->($_[1], \%hdr)
823 or return $finish->(undef, 598 => "Request cancelled by on_body");
824
825 $_[0]->push_read (line => sub {
826 length $_[1]
827 and return $finish->(undef, $ae_error => "Garbled chunked transfer encoding");
828 $_[0]->push_read (line => $state{read_chunk});
829 });
830 });
831 } else {
832 $hdr{"content-length"} ||= $cl;
833
834 $_[0]->push_read (line => $qr_nlnl, sub {
835 if (length $_[1]) {
836 for ("$_[1]") {
837 y/\015//d; # weed out any \015, as they show up in the weirdest of places.
838
839 my $hdr = parse_hdr
840 or return $finish->(undef, $ae_error => "Garbled response trailers");
841
842 %hdr = (%hdr, %$hdr);
843 }
844 }
845
846 $finish->($body, undef, undef, 1);
847 });
848 }
849 };
850
851 $_[0]->push_read (line => $state{read_chunk}); 858 $_[0]->push_read (line => $state{read_chunk});
852 859
853 } elsif ($arg{on_body}) { 860 } elsif ($arg{on_body}) {
854 if (defined $len) { 861 if (defined $len) {
855 $_[0]->on_read (sub { 862 $_[0]->on_read (sub {
856 $len -= length $_[0]{rbuf}; 863 $len -= length $_[0]{rbuf};
857 864
858 $arg{on_body}(delete $_[0]{rbuf}, \%hdr) 865 $arg{on_body}(delete $_[0]{rbuf}, \%hdr)
859 or return $finish->(undef, 598 => "Request cancelled by on_body"); 866 or return $finish->(undef, 598 => "Request cancelled by on_body");
860 867
861 $len > 0 868 $len > 0
862 or $finish->("", undef, undef, 1); 869 or $finish->("", undef, undef, 1);
863 });
864 } else {
865 $_[0]->on_eof (sub {
866 $finish->("");
867 });
868 $_[0]->on_read (sub {
869 $arg{on_body}(delete $_[0]{rbuf}, \%hdr)
870 or $finish->(undef, 598 => "Request cancelled by on_body");
871 });
872 } 870 });
873 } else { 871 } else {
874 $_[0]->on_eof (undef); 872 $_[0]->on_eof (sub {
875 873 $finish->("");
876 if (defined $len) { 874 });
877 $_[0]->on_read (sub { 875 $_[0]->on_read (sub {
878 $finish->((substr delete $_[0]{rbuf}, 0, $len, ""), undef, undef, 1) 876 $arg{on_body}(delete $_[0]{rbuf}, \%hdr)
879 if $len <= length $_[0]{rbuf}; 877 or $finish->(undef, 598 => "Request cancelled by on_body");
880 });
881 } else {
882 $_[0]->on_error (sub {
883 ($! == Errno::EPIPE || !$!)
884 ? $finish->(delete $_[0]{rbuf})
885 : $finish->(undef, $ae_error => $_[2]);
886 });
887 $_[0]->on_read (sub { });
888 } 878 });
889 } 879 }
880 } else {
881 $_[0]->on_eof (undef);
882
883 if (defined $len) {
884 $_[0]->on_read (sub {
885 $finish->((substr delete $_[0]{rbuf}, 0, $len, ""), undef, undef, 1)
886 if $len <= length $_[0]{rbuf};
887 });
888 } else {
889 $_[0]->on_error (sub {
890 ($! == Errno::EPIPE || !$!)
891 ? $finish->(delete $_[0]{rbuf})
892 : $finish->(undef, $ae_error => $_[2]);
893 });
894 $_[0]->on_read (sub { });
895 }
896 }
897 };
898
899 $state{handle}->push_read (line => $qr_nlnl, $state{read_response});
900 };
901
902 my $connect_cb = sub {
903 $state{fh} = shift
904 or do {
905 my $err = "$!";
906 %state = ();
907 return $cb->(undef, { @pseudo, Status => $ae_error, Reason => $err });
890 }; 908 };
891 909
892 $state{handle}->push_read (line => $qr_nlnl, $state{read_response});
893 };
894
895 my $connect_cb = sub {
896 $state{fh} = shift
897 or do {
898 my $err = "$!";
899 %state = ();
900 return $cb->(undef, { @pseudo, Status => $ae_error, Reason => $err });
901 };
902
903 return unless delete $state{connect_guard}; 910 return unless delete $state{connect_guard};
904 911
905 # get handle 912 # get handle
906 $state{handle} = new AnyEvent::Handle 913 $state{handle} = new AnyEvent::Handle
907 fh => $state{fh}, 914 fh => $state{fh},
908 peername => $rhost, 915 peername => $rhost,
909 tls_ctx => $arg{tls_ctx}, 916 tls_ctx => $arg{tls_ctx},
910 # these need to be reconfigured on keepalive handles 917 # these need to be reconfigured on keepalive handles
911 timeout => $timeout, 918 timeout => $timeout,
912 on_error => sub { 919 on_error => sub {
913 %state = (); 920 %state = ();
914 $cb->(undef, { @pseudo, Status => $ae_error, Reason => $_[2] }); 921 $cb->(undef, { @pseudo, Status => $ae_error, Reason => $_[2] });
915 }, 922 },
916 on_eof => sub { 923 on_eof => sub {
917 %state = (); 924 %state = ();
918 $cb->(undef, { @pseudo, Status => $ae_error, Reason => "Unexpected end-of-file" }); 925 $cb->(undef, { @pseudo, Status => $ae_error, Reason => "Unexpected end-of-file" });
919 }, 926 },
920 ; 927 ;
921 928
922 # limit the number of persistent connections 929 # limit the number of persistent connections
923 # keepalive not yet supported 930 # keepalive not yet supported
924# if ($KA_COUNT{$_[1]} < $MAX_PERSISTENT_PER_HOST) { 931# if ($KA_COUNT{$_[1]} < $MAX_PERSISTENT_PER_HOST) {
925# ++$KA_COUNT{$_[1]}; 932# ++$KA_COUNT{$_[1]};
926# $state{handle}{ka_count_guard} = AnyEvent::Util::guard { 933# $state{handle}{ka_count_guard} = AnyEvent::Util::guard {
927# --$KA_COUNT{$_[1]} 934# --$KA_COUNT{$_[1]}
928# }; 935# };
929# $hdr{connection} = "keep-alive"; 936# $hdr{connection} = "keep-alive";
930# } 937# }
931 938
932 $state{handle}->starttls ("connect") if $rscheme eq "https"; 939 $state{handle}->starttls ("connect") if $rscheme eq "https";
933 940
934 # now handle proxy-CONNECT method 941 # now handle proxy-CONNECT method
935 if ($proxy && $uscheme eq "https") { 942 if ($proxy && $uscheme eq "https") {
936 # oh dear, we have to wrap it into a connect request 943 # oh dear, we have to wrap it into a connect request
937 944
938 # maybe re-use $uauthority with patched port? 945 # maybe re-use $uauthority with patched port?
939 $state{handle}->push_write ("CONNECT $uhost:$uport HTTP/1.0\015\012Host: $uhost\015\012\015\012"); 946 $state{handle}->push_write ("CONNECT $uhost:$uport HTTP/1.0\015\012\015\012");
940 $state{handle}->push_read (line => $qr_nlnl, sub { 947 $state{handle}->push_read (line => $qr_nlnl, sub {
941 $_[1] =~ /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) (?: \s+ ([^\015\012]*) )?/ix 948 $_[1] =~ /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) (?: \s+ ([^\015\012]*) )?/ix
942 or return (%state = (), $cb->(undef, { @pseudo, Status => 599, Reason => "Invalid proxy connect response ($_[1])" })); 949 or return (%state = (), $cb->(undef, { @pseudo, Status => 599, Reason => "Invalid proxy connect response ($_[1])" }));
943 950
944 if ($2 == 200) { 951 if ($2 == 200) {
945 $rpath = $upath; 952 $rpath = $upath;
946 $handle_actual_request->(); 953 $handle_actual_request->();
947 } else { 954 } else {
948 %state = (); 955 %state = ();
949 $cb->(undef, { @pseudo, Status => $2, Reason => $3 }); 956 $cb->(undef, { @pseudo, Status => $2, Reason => $3 });
950 }
951 }); 957 }
958 });
952 } else { 959 } else {
953 $handle_actual_request->(); 960 $handle_actual_request->();
954 }
955 }; 961 }
962 };
963
964 _get_slot $uhost, sub {
965 $state{slot_guard} = shift;
966
967 return unless $state{connect_guard};
956 968
957 my $tcp_connect = $arg{tcp_connect} 969 my $tcp_connect = $arg{tcp_connect}
958 || do { require AnyEvent::Socket; \&AnyEvent::Socket::tcp_connect }; 970 || do { require AnyEvent::Socket; \&AnyEvent::Socket::tcp_connect };
959 971
960 $state{connect_guard} = $tcp_connect->($rhost, $rport, $connect_cb, $arg{on_prepare} || sub { $timeout }); 972 $state{connect_guard} = $tcp_connect->($rhost, $rport, $connect_cb, $arg{on_prepare} || sub { $timeout });

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines