… | |
… | |
843 | } elsif ($hdr{"transfer-encoding"} =~ /\bchunked\b/i) { |
843 | } elsif ($hdr{"transfer-encoding"} =~ /\bchunked\b/i) { |
844 | my $cl = 0; |
844 | my $cl = 0; |
845 | my $body = undef; |
845 | my $body = undef; |
846 | my $on_body = $arg{on_body} || sub { $body .= shift; 1 }; |
846 | my $on_body = $arg{on_body} || sub { $body .= shift; 1 }; |
847 | |
847 | |
848 | my $read_chunk; $read_chunk = sub { |
848 | $state{read_chunk} = sub { |
849 | $_[1] =~ /^([0-9a-fA-F]+)/ |
849 | $_[1] =~ /^([0-9a-fA-F]+)/ |
850 | or $finish->(undef, $ae_error => "Garbled chunked transfer encoding"); |
850 | or $finish->(undef, $ae_error => "Garbled chunked transfer encoding"); |
851 | |
851 | |
852 | my $len = hex $1; |
852 | my $len = hex $1; |
853 | |
853 | |
… | |
… | |
859 | or return $finish->(undef, 598 => "Request cancelled by on_body"); |
859 | or return $finish->(undef, 598 => "Request cancelled by on_body"); |
860 | |
860 | |
861 | $_[0]->push_read (line => sub { |
861 | $_[0]->push_read (line => sub { |
862 | length $_[1] |
862 | length $_[1] |
863 | and return $finish->(undef, $ae_error => "Garbled chunked transfer encoding"); |
863 | and return $finish->(undef, $ae_error => "Garbled chunked transfer encoding"); |
864 | $_[0]->push_read (line => $read_chunk); |
864 | $_[0]->push_read (line => $state{read_chunk}); |
865 | }); |
865 | }); |
866 | }); |
866 | }); |
867 | } else { |
867 | } else { |
868 | $hdr{"content-length"} ||= $cl; |
868 | $hdr{"content-length"} ||= $cl; |
869 | |
869 | |
… | |
… | |
882 | $finish->($body, undef, undef, 1); |
882 | $finish->($body, undef, undef, 1); |
883 | }); |
883 | }); |
884 | } |
884 | } |
885 | }; |
885 | }; |
886 | |
886 | |
887 | $_[0]->push_read (line => $read_chunk); |
887 | $_[0]->push_read (line => $state{read_chunk}); |
888 | |
888 | |
889 | } elsif ($arg{on_body}) { |
889 | } elsif ($arg{on_body}) { |
890 | if ($len) { |
890 | if ($len) { |
891 | $_[0]->on_read (sub { |
891 | $_[0]->on_read (sub { |
892 | $len -= length $_[0]{rbuf}; |
892 | $len -= length $_[0]{rbuf}; |
… | |
… | |
939 | $_[1] =~ /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) (?: \s+ ([^\015\012]*) )?/ix |
939 | $_[1] =~ /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) (?: \s+ ([^\015\012]*) )?/ix |
940 | or return (%state = (), $cb->(undef, { @pseudo, Status => 599, Reason => "Invalid proxy connect response ($_[1])" })); |
940 | or return (%state = (), $cb->(undef, { @pseudo, Status => 599, Reason => "Invalid proxy connect response ($_[1])" })); |
941 | |
941 | |
942 | if ($2 == 200) { |
942 | if ($2 == 200) { |
943 | $rpath = $upath; |
943 | $rpath = $upath; |
944 | &$handle_actual_request; |
944 | $handle_actual_request->(); |
945 | } else { |
945 | } else { |
946 | %state = (); |
946 | %state = (); |
947 | $cb->(undef, { @pseudo, Status => $2, Reason => $3 }); |
947 | $cb->(undef, { @pseudo, Status => $2, Reason => $3 }); |
948 | } |
948 | } |
949 | }); |
949 | }); |
950 | } else { |
950 | } else { |
951 | &$handle_actual_request; |
951 | $handle_actual_request->(); |
952 | } |
952 | } |
953 | }; |
953 | }; |
954 | |
954 | |
955 | my $tcp_connect = $arg{tcp_connect} |
955 | my $tcp_connect = $arg{tcp_connect} |
956 | || do { require AnyEvent::Socket; \&AnyEvent::Socket::tcp_connect }; |
956 | || do { require AnyEvent::Socket; \&AnyEvent::Socket::tcp_connect }; |