… | |
… | |
57 | our $MAX_PERSISTENT = 8; |
57 | our $MAX_PERSISTENT = 8; |
58 | our $PERSISTENT_TIMEOUT = 2; |
58 | our $PERSISTENT_TIMEOUT = 2; |
59 | our $TIMEOUT = 300; |
59 | our $TIMEOUT = 300; |
60 | |
60 | |
61 | # changing these is evil |
61 | # changing these is evil |
62 | our $MAX_PERSISTENT_PER_HOST = 0; |
62 | our $MAX_PERSISTENT_PER_HOST = 2; |
63 | our $MAX_PER_HOST = 4; |
63 | our $MAX_PER_HOST = 4; |
64 | |
64 | |
65 | our $PROXY; |
65 | our $PROXY; |
66 | our $ACTIVE = 0; |
66 | our $ACTIVE = 0; |
67 | |
67 | |
… | |
… | |
169 | C<Host:>, C<Content-Length:>, C<Connection:> and C<Cookie:> headers and |
169 | C<Host:>, C<Content-Length:>, C<Connection:> and C<Cookie:> headers and |
170 | will provide defaults at least for C<TE:>, C<Referer:> and C<User-Agent:> |
170 | will provide defaults at least for C<TE:>, C<Referer:> and C<User-Agent:> |
171 | (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 |
172 | they won't be sent at all). |
172 | they won't be sent at all). |
173 | |
173 | |
|
|
174 | You really should provide your own C<User-Agent:> header value that is |
|
|
175 | appropriate for your program - I wouldn't be surprised if the default |
|
|
176 | AnyEvent string gets blocked by webservers sooner or later. |
|
|
177 | |
174 | =item timeout => $seconds |
178 | =item timeout => $seconds |
175 | |
179 | |
176 | The time-out to use for various stages - each connect attempt will reset |
180 | The time-out to use for various stages - each connect attempt will reset |
177 | the timeout, as will read or write activity, i.e. this is not an overall |
181 | the timeout, as will read or write activity, i.e. this is not an overall |
178 | timeout. |
182 | timeout. |
… | |
… | |
329 | Example: do a HTTP HEAD request on https://www.google.com/, use a |
333 | Example: do a HTTP HEAD request on https://www.google.com/, use a |
330 | timeout of 30 seconds. |
334 | timeout of 30 seconds. |
331 | |
335 | |
332 | http_request |
336 | http_request |
333 | GET => "https://www.google.com", |
337 | GET => "https://www.google.com", |
|
|
338 | headers => { "user-agent" => "MySearchClient 1.0" }, |
334 | timeout => 30, |
339 | timeout => 30, |
335 | sub { |
340 | sub { |
336 | my ($body, $hdr) = @_; |
341 | my ($body, $hdr) = @_; |
337 | use Data::Dumper; |
342 | use Data::Dumper; |
338 | print Dumper $hdr; |
343 | print Dumper $hdr; |
… | |
… | |
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 | |
|
|
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 | |
654 | $hdr{connection} = "close Te"; #1.1 |
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 $chunked = $hdr{"transfer-encoding"} =~ /\bchunked\b/i; # not quite correct... |
|
|
790 | |
|
|
791 | my $len = $chunked ? undef : $hdr{"content-length"}; |
|
|
792 | |
|
|
793 | # body handling, many different code paths |
|
|
794 | # - no body expected |
|
|
795 | # - want_body_handle |
|
|
796 | # - te chunked |
|
|
797 | # - 2x length known (with or without on_body) |
|
|
798 | # - 2x length not known (with or without on_body) |
|
|
799 | if (!$redirect && $arg{on_header} && !$arg{on_header}(\%hdr)) { |
|
|
800 | $finish->(undef, 598 => "Request cancelled by on_header"); |
|
|
801 | } elsif ( |
|
|
802 | $hdr{Status} =~ /^(?:1..|204|205|304)$/ |
|
|
803 | or $method eq "HEAD" |
|
|
804 | or (defined $len && $len == 0) # == 0, not !, because "0 " is true |
|
|
805 | ) { |
|
|
806 | # no body |
|
|
807 | $finish->("", undef, undef, 1); |
|
|
808 | |
|
|
809 | } elsif (!$redirect && $arg{want_body_handle}) { |
|
|
810 | $_[0]->on_eof (undef); |
|
|
811 | $_[0]->on_error (undef); |
|
|
812 | $_[0]->on_read (undef); |
|
|
813 | |
|
|
814 | $finish->(delete $state{handle}); |
|
|
815 | |
|
|
816 | } elsif ($chunked) { |
|
|
817 | my $cl = 0; |
|
|
818 | my $body = undef; |
|
|
819 | my $on_body = $arg{on_body} || sub { $body .= shift; 1 }; |
|
|
820 | |
|
|
821 | $state{read_chunk} = sub { |
|
|
822 | $_[1] =~ /^([0-9a-fA-F]+)/ |
|
|
823 | or $finish->(undef, $ae_error => "Garbled chunked transfer encoding"); |
|
|
824 | |
|
|
825 | my $len = hex $1; |
|
|
826 | |
|
|
827 | if ($len) { |
|
|
828 | $cl += $len; |
|
|
829 | |
|
|
830 | $_[0]->push_read (chunk => $len, sub { |
|
|
831 | $on_body->($_[1], \%hdr) |
|
|
832 | or return $finish->(undef, 598 => "Request cancelled by on_body"); |
|
|
833 | |
|
|
834 | $_[0]->push_read (line => sub { |
|
|
835 | length $_[1] |
|
|
836 | and return $finish->(undef, $ae_error => "Garbled chunked transfer encoding"); |
|
|
837 | $_[0]->push_read (line => $state{read_chunk}); |
|
|
838 | }); |
|
|
839 | }); |
774 | } else { |
840 | } else { |
775 | $cb->($_[0], \%hdr); |
841 | $hdr{"content-length"} ||= $cl; |
|
|
842 | |
|
|
843 | $_[0]->push_read (line => $qr_nlnl, sub { |
|
|
844 | if (length $_[1]) { |
|
|
845 | for ("$_[1]") { |
|
|
846 | y/\015//d; # weed out any \015, as they show up in the weirdest of places. |
|
|
847 | |
|
|
848 | my $hdr = parse_hdr |
|
|
849 | or return $finish->(undef, $ae_error => "Garbled response trailers"); |
|
|
850 | |
|
|
851 | %hdr = (%hdr, %$hdr); |
|
|
852 | } |
|
|
853 | } |
|
|
854 | |
|
|
855 | $finish->($body, undef, undef, 1); |
|
|
856 | }); |
776 | } |
857 | } |
777 | }; |
858 | }; |
778 | |
859 | |
779 | $ae_error = 597; # body phase |
|
|
780 | |
|
|
781 | my $len = $hdr{"content-length"}; |
|
|
782 | |
|
|
783 | # body handling, many different code paths |
|
|
784 | # - no body expected |
|
|
785 | # - want_body_handle |
|
|
786 | # - te chunked |
|
|
787 | # - 2x length known (with or without on_body) |
|
|
788 | # - 2x length not known (with or without on_body) |
|
|
789 | if (!$redirect && $arg{on_header} && !$arg{on_header}(\%hdr)) { |
|
|
790 | $finish->(undef, 598 => "Request cancelled by on_header"); |
|
|
791 | } elsif ( |
|
|
792 | $hdr{Status} =~ /^(?:1..|204|205|304)$/ |
|
|
793 | or $method eq "HEAD" |
|
|
794 | or (defined $len && $len == 0) # == 0, not !, because "0 " is true |
|
|
795 | ) { |
|
|
796 | # no body |
|
|
797 | $finish->("", undef, undef, 1); |
|
|
798 | |
|
|
799 | } elsif (!$redirect && $arg{want_body_handle}) { |
|
|
800 | $_[0]->on_eof (undef); |
|
|
801 | $_[0]->on_error (undef); |
|
|
802 | $_[0]->on_read (undef); |
|
|
803 | |
|
|
804 | $finish->(delete $state{handle}); |
|
|
805 | |
|
|
806 | } elsif ($hdr{"transfer-encoding"} =~ /\bchunked\b/i) { |
|
|
807 | my $cl = 0; |
|
|
808 | my $body = undef; |
|
|
809 | my $on_body = $arg{on_body} || sub { $body .= shift; 1 }; |
|
|
810 | |
|
|
811 | $state{read_chunk} = sub { |
|
|
812 | $_[1] =~ /^([0-9a-fA-F]+)/ |
|
|
813 | or $finish->(undef, $ae_error => "Garbled chunked transfer encoding"); |
|
|
814 | |
|
|
815 | my $len = hex $1; |
|
|
816 | |
|
|
817 | if ($len) { |
|
|
818 | $cl += $len; |
|
|
819 | |
|
|
820 | $_[0]->push_read (chunk => $len, sub { |
|
|
821 | $on_body->($_[1], \%hdr) |
|
|
822 | or return $finish->(undef, 598 => "Request cancelled by on_body"); |
|
|
823 | |
|
|
824 | $_[0]->push_read (line => sub { |
|
|
825 | length $_[1] |
|
|
826 | and return $finish->(undef, $ae_error => "Garbled chunked transfer encoding"); |
|
|
827 | $_[0]->push_read (line => $state{read_chunk}); |
|
|
828 | }); |
|
|
829 | }); |
|
|
830 | } else { |
|
|
831 | $hdr{"content-length"} ||= $cl; |
|
|
832 | |
|
|
833 | $_[0]->push_read (line => $qr_nlnl, sub { |
|
|
834 | if (length $_[1]) { |
|
|
835 | for ("$_[1]") { |
|
|
836 | y/\015//d; # weed out any \015, as they show up in the weirdest of places. |
|
|
837 | |
|
|
838 | my $hdr = parse_hdr |
|
|
839 | or return $finish->(undef, $ae_error => "Garbled response trailers"); |
|
|
840 | |
|
|
841 | %hdr = (%hdr, %$hdr); |
|
|
842 | } |
|
|
843 | } |
|
|
844 | |
|
|
845 | $finish->($body, undef, undef, 1); |
|
|
846 | }); |
|
|
847 | } |
|
|
848 | }; |
|
|
849 | |
|
|
850 | $_[0]->push_read (line => $state{read_chunk}); |
860 | $_[0]->push_read (line => $state{read_chunk}); |
851 | |
861 | |
852 | } elsif ($arg{on_body}) { |
862 | } elsif ($arg{on_body}) { |
853 | if (defined $len) { |
863 | if (defined $len) { |
854 | $_[0]->on_read (sub { |
864 | $_[0]->on_read (sub { |
855 | $len -= length $_[0]{rbuf}; |
865 | $len -= length $_[0]{rbuf}; |
856 | |
866 | |
857 | $arg{on_body}(delete $_[0]{rbuf}, \%hdr) |
867 | $arg{on_body}(delete $_[0]{rbuf}, \%hdr) |
858 | or return $finish->(undef, 598 => "Request cancelled by on_body"); |
868 | or return $finish->(undef, 598 => "Request cancelled by on_body"); |
859 | |
869 | |
860 | $len > 0 |
870 | $len > 0 |
861 | or $finish->("", undef, undef, 1); |
871 | or $finish->("", undef, undef, 1); |
862 | }); |
|
|
863 | } else { |
|
|
864 | $_[0]->on_eof (sub { |
|
|
865 | $finish->(""); |
|
|
866 | }); |
|
|
867 | $_[0]->on_read (sub { |
|
|
868 | $arg{on_body}(delete $_[0]{rbuf}, \%hdr) |
|
|
869 | or $finish->(undef, 598 => "Request cancelled by on_body"); |
|
|
870 | }); |
|
|
871 | } |
872 | }); |
872 | } else { |
873 | } else { |
873 | $_[0]->on_eof (undef); |
874 | $_[0]->on_eof (sub { |
874 | |
875 | $finish->(""); |
875 | if (defined $len) { |
876 | }); |
876 | $_[0]->on_read (sub { |
877 | $_[0]->on_read (sub { |
877 | $finish->((substr delete $_[0]{rbuf}, 0, $len, ""), undef, undef, 1) |
878 | $arg{on_body}(delete $_[0]{rbuf}, \%hdr) |
878 | if $len <= length $_[0]{rbuf}; |
879 | or $finish->(undef, 598 => "Request cancelled by on_body"); |
879 | }); |
|
|
880 | } else { |
|
|
881 | $_[0]->on_error (sub { |
|
|
882 | ($! == Errno::EPIPE || !$!) |
|
|
883 | ? $finish->(delete $_[0]{rbuf}) |
|
|
884 | : $finish->(undef, $ae_error => $_[2]); |
|
|
885 | }); |
|
|
886 | $_[0]->on_read (sub { }); |
|
|
887 | } |
880 | }); |
888 | } |
881 | } |
|
|
882 | } else { |
|
|
883 | $_[0]->on_eof (undef); |
|
|
884 | |
|
|
885 | if (defined $len) { |
|
|
886 | $_[0]->on_read (sub { |
|
|
887 | $finish->((substr delete $_[0]{rbuf}, 0, $len, ""), undef, undef, 1) |
|
|
888 | if $len <= length $_[0]{rbuf}; |
|
|
889 | }); |
|
|
890 | } else { |
|
|
891 | $_[0]->on_error (sub { |
|
|
892 | ($! == Errno::EPIPE || !$!) |
|
|
893 | ? $finish->(delete $_[0]{rbuf}) |
|
|
894 | : $finish->(undef, $ae_error => $_[2]); |
|
|
895 | }); |
|
|
896 | $_[0]->on_read (sub { }); |
|
|
897 | } |
|
|
898 | } |
|
|
899 | }; |
|
|
900 | |
|
|
901 | $state{handle}->push_read (line => $qr_nlnl, $state{read_response}); |
|
|
902 | }; |
|
|
903 | |
|
|
904 | my $connect_cb = sub { |
|
|
905 | $state{fh} = shift |
|
|
906 | or do { |
|
|
907 | my $err = "$!"; |
|
|
908 | %state = (); |
|
|
909 | return $cb->(undef, { @pseudo, Status => $ae_error, Reason => $err }); |
889 | }; |
910 | }; |
890 | |
911 | |
891 | $state{handle}->push_read (line => $qr_nlnl, $state{read_response}); |
|
|
892 | }; |
|
|
893 | |
|
|
894 | my $connect_cb = sub { |
|
|
895 | $state{fh} = shift |
|
|
896 | or do { |
|
|
897 | my $err = "$!"; |
|
|
898 | %state = (); |
|
|
899 | return $cb->(undef, { @pseudo, Status => $ae_error, Reason => $err }); |
|
|
900 | }; |
|
|
901 | |
|
|
902 | return unless delete $state{connect_guard}; |
912 | return unless delete $state{connect_guard}; |
903 | |
913 | |
904 | # get handle |
914 | # get handle |
905 | $state{handle} = new AnyEvent::Handle |
915 | $state{handle} = new AnyEvent::Handle |
906 | fh => $state{fh}, |
916 | fh => $state{fh}, |
907 | peername => $rhost, |
917 | peername => $rhost, |
908 | tls_ctx => $arg{tls_ctx}, |
918 | tls_ctx => $arg{tls_ctx}, |
909 | # these need to be reconfigured on keepalive handles |
919 | # these need to be reconfigured on keepalive handles |
910 | timeout => $timeout, |
920 | timeout => $timeout, |
911 | on_error => sub { |
921 | on_error => sub { |
912 | %state = (); |
922 | %state = (); |
913 | $cb->(undef, { @pseudo, Status => $ae_error, Reason => $_[2] }); |
923 | $cb->(undef, { @pseudo, Status => $ae_error, Reason => $_[2] }); |
914 | }, |
924 | }, |
915 | on_eof => sub { |
925 | on_eof => sub { |
916 | %state = (); |
926 | %state = (); |
917 | $cb->(undef, { @pseudo, Status => $ae_error, Reason => "Unexpected end-of-file" }); |
927 | $cb->(undef, { @pseudo, Status => $ae_error, Reason => "Unexpected end-of-file" }); |
918 | }, |
928 | }, |
919 | ; |
929 | ; |
920 | |
930 | |
921 | # limit the number of persistent connections |
931 | # limit the number of persistent connections |
922 | # keepalive not yet supported |
932 | # keepalive not yet supported |
923 | # if ($KA_COUNT{$_[1]} < $MAX_PERSISTENT_PER_HOST) { |
933 | # if ($KA_COUNT{$_[1]} < $MAX_PERSISTENT_PER_HOST) { |
924 | # ++$KA_COUNT{$_[1]}; |
934 | # ++$KA_COUNT{$_[1]}; |
925 | # $state{handle}{ka_count_guard} = AnyEvent::Util::guard { |
935 | # $state{handle}{ka_count_guard} = AnyEvent::Util::guard { |
926 | # --$KA_COUNT{$_[1]} |
936 | # --$KA_COUNT{$_[1]} |
927 | # }; |
937 | # }; |
928 | # $hdr{connection} = "keep-alive"; |
938 | # $hdr{connection} = "keep-alive"; |
929 | # } |
939 | # } |
930 | |
940 | |
931 | $state{handle}->starttls ("connect") if $rscheme eq "https"; |
941 | $state{handle}->starttls ("connect") if $rscheme eq "https"; |
932 | |
942 | |
933 | # now handle proxy-CONNECT method |
943 | # now handle proxy-CONNECT method |
934 | if ($proxy && $uscheme eq "https") { |
944 | if ($proxy && $uscheme eq "https") { |
935 | # oh dear, we have to wrap it into a connect request |
945 | # oh dear, we have to wrap it into a connect request |
936 | |
946 | |
937 | # maybe re-use $uauthority with patched port? |
947 | # maybe re-use $uauthority with patched port? |
938 | $state{handle}->push_write ("CONNECT $uhost:$uport HTTP/1.0\015\012\015\012"); |
948 | $state{handle}->push_write ("CONNECT $uhost:$uport HTTP/1.0\015\012\015\012"); |
939 | $state{handle}->push_read (line => $qr_nlnl, sub { |
949 | $state{handle}->push_read (line => $qr_nlnl, sub { |
940 | $_[1] =~ /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) (?: \s+ ([^\015\012]*) )?/ix |
950 | $_[1] =~ /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) (?: \s+ ([^\015\012]*) )?/ix |
941 | or return (%state = (), $cb->(undef, { @pseudo, Status => 599, Reason => "Invalid proxy connect response ($_[1])" })); |
951 | or return (%state = (), $cb->(undef, { @pseudo, Status => 599, Reason => "Invalid proxy connect response ($_[1])" })); |
942 | |
952 | |
943 | if ($2 == 200) { |
953 | if ($2 == 200) { |
944 | $rpath = $upath; |
954 | $rpath = $upath; |
945 | $handle_actual_request->(); |
955 | $handle_actual_request->(); |
946 | } else { |
956 | } else { |
947 | %state = (); |
957 | %state = (); |
948 | $cb->(undef, { @pseudo, Status => $2, Reason => $3 }); |
958 | $cb->(undef, { @pseudo, Status => $2, Reason => $3 }); |
949 | } |
|
|
950 | }); |
959 | } |
|
|
960 | }); |
951 | } else { |
961 | } else { |
952 | $handle_actual_request->(); |
962 | $handle_actual_request->(); |
953 | } |
|
|
954 | }; |
963 | } |
|
|
964 | }; |
|
|
965 | |
|
|
966 | _get_slot $uhost, sub { |
|
|
967 | $state{slot_guard} = shift; |
|
|
968 | |
|
|
969 | return unless $state{connect_guard}; |
955 | |
970 | |
956 | my $tcp_connect = $arg{tcp_connect} |
971 | my $tcp_connect = $arg{tcp_connect} |
957 | || do { require AnyEvent::Socket; \&AnyEvent::Socket::tcp_connect }; |
972 | || do { require AnyEvent::Socket; \&AnyEvent::Socket::tcp_connect }; |
958 | |
973 | |
959 | $state{connect_guard} = $tcp_connect->($rhost, $rport, $connect_cb, $arg{on_prepare} || sub { $timeout }); |
974 | $state{connect_guard} = $tcp_connect->($rhost, $rport, $connect_cb, $arg{on_prepare} || sub { $timeout }); |