… | |
… | |
46 | use AnyEvent::Util (); |
46 | use AnyEvent::Util (); |
47 | use AnyEvent::Handle (); |
47 | use AnyEvent::Handle (); |
48 | |
48 | |
49 | use base Exporter::; |
49 | use base Exporter::; |
50 | |
50 | |
51 | our $VERSION = 2.22; |
51 | our $VERSION = 2.25; |
52 | |
52 | |
53 | our @EXPORT = qw(http_get http_post http_head http_request); |
53 | our @EXPORT = qw(http_get http_post http_head http_request); |
54 | |
54 | |
55 | our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; |
55 | our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; |
56 | our $MAX_RECURSE = 10; |
56 | our $MAX_RECURSE = 10; |
… | |
… | |
895 | # we give our best and fall back to URI if available. |
895 | # we give our best and fall back to URI if available. |
896 | if (exists $hdr{location}) { |
896 | if (exists $hdr{location}) { |
897 | my $loc = $hdr{location}; |
897 | my $loc = $hdr{location}; |
898 | |
898 | |
899 | if ($loc =~ m%^//%) { # // |
899 | if ($loc =~ m%^//%) { # // |
900 | $loc = "$rscheme:$loc"; |
900 | $loc = "$uscheme:$loc"; |
901 | |
901 | |
902 | } elsif ($loc eq "") { |
902 | } elsif ($loc eq "") { |
903 | $loc = $url; |
903 | $loc = $url; |
904 | |
904 | |
905 | } elsif ($loc !~ /^(?: $ | [^:\/?\#]+ : )/x) { # anything "simple" |
905 | } elsif ($loc !~ /^(?: $ | [^:\/?\#]+ : )/x) { # anything "simple" |
906 | $loc =~ s/^\.\/+//; |
906 | $loc =~ s/^\.\/+//; |
907 | |
907 | |
908 | if ($loc !~ m%^[.?#]%) { |
908 | if ($loc !~ m%^[.?#]%) { |
909 | my $prefix = "$rscheme://$uhost:$uport"; |
909 | my $prefix = "$uscheme://$uauthority"; |
910 | |
910 | |
911 | unless ($loc =~ s/^\///) { |
911 | unless ($loc =~ s/^\///) { |
912 | $prefix .= $upath; |
912 | $prefix .= $upath; |
913 | $prefix =~ s/\/[^\/]*$//; |
913 | $prefix =~ s/\/[^\/]*$//; |
914 | } |
914 | } |
… | |
… | |
1031 | $finish->(delete $state{handle}); |
1031 | $finish->(delete $state{handle}); |
1032 | |
1032 | |
1033 | } elsif ($chunked) { |
1033 | } elsif ($chunked) { |
1034 | my $cl = 0; |
1034 | my $cl = 0; |
1035 | my $body = ""; |
1035 | my $body = ""; |
1036 | my $on_body = $arg{on_body} || sub { $body .= shift; 1 }; |
1036 | my $on_body = (!$redirect && $arg{on_body}) || sub { $body .= shift; 1 }; |
1037 | |
1037 | |
1038 | $state{read_chunk} = sub { |
1038 | $state{read_chunk} = sub { |
1039 | $_[1] =~ /^([0-9a-fA-F]+)/ |
1039 | $_[1] =~ /^([0-9a-fA-F]+)/ |
1040 | or return $finish->(undef, $ae_error => "Garbled chunked transfer encoding"); |
1040 | or return $finish->(undef, $ae_error => "Garbled chunked transfer encoding"); |
1041 | |
1041 | |
… | |
… | |
1074 | } |
1074 | } |
1075 | }; |
1075 | }; |
1076 | |
1076 | |
1077 | $_[0]->push_read (line => $state{read_chunk}); |
1077 | $_[0]->push_read (line => $state{read_chunk}); |
1078 | |
1078 | |
1079 | } elsif ($arg{on_body}) { |
1079 | } elsif (!$redirect && $arg{on_body}) { |
1080 | if (defined $len) { |
1080 | if (defined $len) { |
1081 | $_[0]->on_read (sub { |
1081 | $_[0]->on_read (sub { |
1082 | $len -= length $_[0]{rbuf}; |
1082 | $len -= length $_[0]{rbuf}; |
1083 | |
1083 | |
1084 | $arg{on_body}(delete $_[0]{rbuf}, \%hdr) |
1084 | $arg{on_body}(delete $_[0]{rbuf}, \%hdr) |
… | |
… | |
1217 | # on a keepalive request (in theory, this should be a separate config option). |
1217 | # on a keepalive request (in theory, this should be a separate config option). |
1218 | if ($persistent && $KA_CACHE{$ka_key}) { |
1218 | if ($persistent && $KA_CACHE{$ka_key}) { |
1219 | $was_persistent = 1; |
1219 | $was_persistent = 1; |
1220 | |
1220 | |
1221 | $state{handle} = ka_fetch $ka_key; |
1221 | $state{handle} = ka_fetch $ka_key; |
1222 | $state{handle}->destroyed |
1222 | # $state{handle}->destroyed |
1223 | and die "AnyEvent::HTTP: unexpectedly got a destructed handle (1), please report.";#d# |
1223 | # and die "AnyEvent::HTTP: unexpectedly got a destructed handle (1), please report.";#d# |
1224 | $prepare_handle->(); |
1224 | $prepare_handle->(); |
1225 | $state{handle}->destroyed |
1225 | # $state{handle}->destroyed |
1226 | and die "AnyEvent::HTTP: unexpectedly got a destructed handle (2), please report.";#d# |
1226 | # and die "AnyEvent::HTTP: unexpectedly got a destructed handle (2), please report.";#d# |
1227 | $handle_actual_request->(); |
1227 | $handle_actual_request->(); |
1228 | |
1228 | |
1229 | } else { |
1229 | } else { |
1230 | my $tcp_connect = $arg{tcp_connect} |
1230 | my $tcp_connect = $arg{tcp_connect} |
1231 | || do { require AnyEvent::Socket; \&AnyEvent::Socket::tcp_connect }; |
1231 | || do { require AnyEvent::Socket; \&AnyEvent::Socket::tcp_connect }; |
… | |
… | |
1461 | or die "$file: $!"; |
1461 | or die "$file: $!"; |
1462 | |
1462 | |
1463 | my %hdr; |
1463 | my %hdr; |
1464 | my $ofs = 0; |
1464 | my $ofs = 0; |
1465 | |
1465 | |
1466 | warn stat $fh; |
|
|
1467 | warn -s _; |
|
|
1468 | if (stat $fh and -s _) { |
1466 | if (stat $fh and -s _) { |
1469 | $ofs = -s _; |
1467 | $ofs = -s _; |
1470 | warn "-s is ", $ofs; |
1468 | warn "-s is ", $ofs; |
1471 | $hdr{"if-unmodified-since"} = AnyEvent::HTTP::format_date +(stat _)[9]; |
1469 | $hdr{"if-unmodified-since"} = AnyEvent::HTTP::format_date +(stat _)[9]; |
1472 | $hdr{"range"} = "bytes=$ofs-"; |
1470 | $hdr{"range"} = "bytes=$ofs-"; |
… | |
… | |
1500 | my (undef, $hdr) = @_; |
1498 | my (undef, $hdr) = @_; |
1501 | |
1499 | |
1502 | my $status = $hdr->{Status}; |
1500 | my $status = $hdr->{Status}; |
1503 | |
1501 | |
1504 | if (my $time = AnyEvent::HTTP::parse_date $hdr->{"last-modified"}) { |
1502 | if (my $time = AnyEvent::HTTP::parse_date $hdr->{"last-modified"}) { |
1505 | utime $fh, $time, $time; |
1503 | utime $time, $time, $fh; |
1506 | } |
1504 | } |
1507 | |
1505 | |
1508 | if ($status == 200 || $status == 206 || $status == 416) { |
1506 | if ($status == 200 || $status == 206 || $status == 416) { |
1509 | # download ok || resume ok || file already fully downloaded |
1507 | # download ok || resume ok || file already fully downloaded |
1510 | $cb->(1, $hdr); |
1508 | $cb->(1, $hdr); |