--- AnyEvent-HTTP/HTTP.pm 2015/04/07 01:18:20 1.121 +++ AnyEvent-HTTP/HTTP.pm 2018/08/30 00:08:16 1.129 @@ -48,7 +48,7 @@ use base Exporter::; -our $VERSION = 2.21; +our $VERSION = 2.25; our @EXPORT = qw(http_get http_post http_head http_request); @@ -197,6 +197,9 @@ If not specified, then the default proxy is used (see C). +Currently, if your proxy requires authorization, you have to specify an +appropriate "Proxy-Authorization" header in every request. + =item body => $string The request body, usually empty. Will be sent as-is (future versions of @@ -772,7 +775,7 @@ : $uscheme eq "https" ? 443 : return $cb->(undef, { @pseudo, Status => 599, Reason => "Only http and https URL schemes supported" }); - $uauthority =~ /^(?: .*\@ )? ([^\@:]+) (?: : (\d+) )?$/x + $uauthority =~ /^(?: .*\@ )? ([^\@]+?) (?: : (\d+) )?$/x or return $cb->(undef, { @pseudo, Status => 599, Reason => "Unparsable URL" }); my $uhost = lc $1; @@ -894,7 +897,7 @@ my $loc = $hdr{location}; if ($loc =~ m%^//%) { # // - $loc = "$rscheme:$loc"; + $loc = "$uscheme:$loc"; } elsif ($loc eq "") { $loc = $url; @@ -903,7 +906,7 @@ $loc =~ s/^\.\/+//; if ($loc !~ m%^[.?#]%) { - my $prefix = "$rscheme://$uhost:$uport"; + my $prefix = "$uscheme://$uauthority"; unless ($loc =~ s/^\///) { $prefix .= $upath; @@ -938,9 +941,12 @@ # we go with the industry standard. 308 is defined # by rfc7538 if ($status == 301 or $status == 302 or $status == 303) { - # HTTP/1.1 is unclear on how to mutate the method - $method = "GET" unless $method eq "HEAD"; $redirect = 1; + # HTTP/1.1 is unclear on how to mutate the method + unless ($method eq "HEAD") { + $method = "GET"; + delete $arg{body}; + } } elsif ($status == 307 or $status == 308) { $redirect = 1; } @@ -1027,7 +1033,7 @@ } elsif ($chunked) { my $cl = 0; my $body = ""; - my $on_body = $arg{on_body} || sub { $body .= shift; 1 }; + my $on_body = (!$redirect && $arg{on_body}) || sub { $body .= shift; 1 }; $state{read_chunk} = sub { $_[1] =~ /^([0-9a-fA-F]+)/ @@ -1070,7 +1076,7 @@ $_[0]->push_read (line => $state{read_chunk}); - } elsif ($arg{on_body}) { + } elsif (!$redirect && $arg{on_body}) { if (defined $len) { $_[0]->on_read (sub { $len -= length $_[0]{rbuf}; @@ -1119,10 +1125,10 @@ %state = (); $state{recurse} = http_request ( - $method => $url, + $method => $url, %arg, - recurse => $recurse - 1, - keepalive => 0, + recurse => $recurse - 1, + persistent => 0, sub { %state = (); &$cb @@ -1178,8 +1184,12 @@ if ($proxy && $uscheme eq "https") { # oh dear, we have to wrap it into a connect request + my $auth = exists $hdr{"proxy-authorization"} + ? "proxy-authorization: " . (delete $hdr{"proxy-authorization"}) . "\015\012" + : ""; + # maybe re-use $uauthority with patched port? - $state{handle}->push_write ("CONNECT $uhost:$uport HTTP/1.0\015\012\015\012"); + $state{handle}->push_write ("CONNECT $uhost:$uport HTTP/1.0\015\012$auth\015\012"); $state{handle}->push_read (line => $qr_nlnl, sub { $_[1] =~ /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) (?: \s+ ([^\015\012]*) )?/ix or return _error %state, $cb, { @pseudo, Status => 599, Reason => "Invalid proxy connect response ($_[1])" }; @@ -1192,6 +1202,8 @@ } }); } else { + delete $hdr{"proxy-authorization"} unless $proxy; + $handle_actual_request->(); } }; @@ -1207,11 +1219,11 @@ $was_persistent = 1; $state{handle} = ka_fetch $ka_key; - $state{handle}->destroyed - and die "AnyEvent::HTTP: unexpectedly got a destructed handle (1), please report.";#d# +# $state{handle}->destroyed +# and die "AnyEvent::HTTP: unexpectedly got a destructed handle (1), please report.";#d# $prepare_handle->(); - $state{handle}->destroyed - and die "AnyEvent::HTTP: unexpectedly got a destructed handle (2), please report.";#d# +# $state{handle}->destroyed +# and die "AnyEvent::HTTP: unexpectedly got a destructed handle (2), please report.";#d# $handle_actual_request->(); } else { @@ -1451,8 +1463,6 @@ my %hdr; my $ofs = 0; - warn stat $fh; - warn -s _; if (stat $fh and -s _) { $ofs = -s _; warn "-s is ", $ofs; @@ -1490,7 +1500,7 @@ my $status = $hdr->{Status}; if (my $time = AnyEvent::HTTP::parse_date $hdr->{"last-modified"}) { - utime $fh, $time, $time; + utime $time, $time, $fh; } if ($status == 200 || $status == 206 || $status == 416) {