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.122 by root, Fri May 8 17:28:39 2015 UTC vs.
Revision 1.129 by root, Thu Aug 30 00:08:16 2018 UTC

46use AnyEvent::Util (); 46use AnyEvent::Util ();
47use AnyEvent::Handle (); 47use AnyEvent::Handle ();
48 48
49use base Exporter::; 49use base Exporter::;
50 50
51our $VERSION = 2.21; 51our $VERSION = 2.25;
52 52
53our @EXPORT = qw(http_get http_post http_head http_request); 53our @EXPORT = qw(http_get http_post http_head http_request);
54 54
55our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; 55our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)";
56our $MAX_RECURSE = 10; 56our $MAX_RECURSE = 10;
194 194
195C<$scheme> must be either missing or must be C<http> for HTTP. 195C<$scheme> must be either missing or must be C<http> for HTTP.
196 196
197If not specified, then the default proxy is used (see 197If not specified, then the default proxy is used (see
198C<AnyEvent::HTTP::set_proxy>). 198C<AnyEvent::HTTP::set_proxy>).
199
200Currently, if your proxy requires authorization, you have to specify an
201appropriate "Proxy-Authorization" header in every request.
199 202
200=item body => $string 203=item body => $string
201 204
202The request body, usually empty. Will be sent as-is (future versions of 205The request body, usually empty. Will be sent as-is (future versions of
203this module might offer more options). 206this module might offer more options).
770 773
771 my $uport = $uscheme eq "http" ? 80 774 my $uport = $uscheme eq "http" ? 80
772 : $uscheme eq "https" ? 443 775 : $uscheme eq "https" ? 443
773 : return $cb->(undef, { @pseudo, Status => 599, Reason => "Only http and https URL schemes supported" }); 776 : return $cb->(undef, { @pseudo, Status => 599, Reason => "Only http and https URL schemes supported" });
774 777
775 $uauthority =~ /^(?: .*\@ )? ([^\@:]+) (?: : (\d+) )?$/x 778 $uauthority =~ /^(?: .*\@ )? ([^\@]+?) (?: : (\d+) )?$/x
776 or return $cb->(undef, { @pseudo, Status => 599, Reason => "Unparsable URL" }); 779 or return $cb->(undef, { @pseudo, Status => 599, Reason => "Unparsable URL" });
777 780
778 my $uhost = lc $1; 781 my $uhost = lc $1;
779 $uport = $2 if defined $2; 782 $uport = $2 if defined $2;
780 783
892 # we give our best and fall back to URI if available. 895 # we give our best and fall back to URI if available.
893 if (exists $hdr{location}) { 896 if (exists $hdr{location}) {
894 my $loc = $hdr{location}; 897 my $loc = $hdr{location};
895 898
896 if ($loc =~ m%^//%) { # // 899 if ($loc =~ m%^//%) { # //
897 $loc = "$rscheme:$loc"; 900 $loc = "$uscheme:$loc";
898 901
899 } elsif ($loc eq "") { 902 } elsif ($loc eq "") {
900 $loc = $url; 903 $loc = $url;
901 904
902 } elsif ($loc !~ /^(?: $ | [^:\/?\#]+ : )/x) { # anything "simple" 905 } elsif ($loc !~ /^(?: $ | [^:\/?\#]+ : )/x) { # anything "simple"
903 $loc =~ s/^\.\/+//; 906 $loc =~ s/^\.\/+//;
904 907
905 if ($loc !~ m%^[.?#]%) { 908 if ($loc !~ m%^[.?#]%) {
906 my $prefix = "$rscheme://$uhost:$uport"; 909 my $prefix = "$uscheme://$uauthority";
907 910
908 unless ($loc =~ s/^\///) { 911 unless ($loc =~ s/^\///) {
909 $prefix .= $upath; 912 $prefix .= $upath;
910 $prefix =~ s/\/[^\/]*$//; 913 $prefix =~ s/\/[^\/]*$//;
911 } 914 }
1028 $finish->(delete $state{handle}); 1031 $finish->(delete $state{handle});
1029 1032
1030 } elsif ($chunked) { 1033 } elsif ($chunked) {
1031 my $cl = 0; 1034 my $cl = 0;
1032 my $body = ""; 1035 my $body = "";
1033 my $on_body = $arg{on_body} || sub { $body .= shift; 1 }; 1036 my $on_body = (!$redirect && $arg{on_body}) || sub { $body .= shift; 1 };
1034 1037
1035 $state{read_chunk} = sub { 1038 $state{read_chunk} = sub {
1036 $_[1] =~ /^([0-9a-fA-F]+)/ 1039 $_[1] =~ /^([0-9a-fA-F]+)/
1037 or return $finish->(undef, $ae_error => "Garbled chunked transfer encoding"); 1040 or return $finish->(undef, $ae_error => "Garbled chunked transfer encoding");
1038 1041
1071 } 1074 }
1072 }; 1075 };
1073 1076
1074 $_[0]->push_read (line => $state{read_chunk}); 1077 $_[0]->push_read (line => $state{read_chunk});
1075 1078
1076 } elsif ($arg{on_body}) { 1079 } elsif (!$redirect && $arg{on_body}) {
1077 if (defined $len) { 1080 if (defined $len) {
1078 $_[0]->on_read (sub { 1081 $_[0]->on_read (sub {
1079 $len -= length $_[0]{rbuf}; 1082 $len -= length $_[0]{rbuf};
1080 1083
1081 $arg{on_body}(delete $_[0]{rbuf}, \%hdr) 1084 $arg{on_body}(delete $_[0]{rbuf}, \%hdr)
1120 _destroy_state %state; 1123 _destroy_state %state;
1121 1124
1122 %state = (); 1125 %state = ();
1123 $state{recurse} = 1126 $state{recurse} =
1124 http_request ( 1127 http_request (
1125 $method => $url, 1128 $method => $url,
1126 %arg, 1129 %arg,
1127 recurse => $recurse - 1, 1130 recurse => $recurse - 1,
1128 keepalive => 0, 1131 persistent => 0,
1129 sub { 1132 sub {
1130 %state = (); 1133 %state = ();
1131 &$cb 1134 &$cb
1132 } 1135 }
1133 ); 1136 );
1214 # 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).
1215 if ($persistent && $KA_CACHE{$ka_key}) { 1218 if ($persistent && $KA_CACHE{$ka_key}) {
1216 $was_persistent = 1; 1219 $was_persistent = 1;
1217 1220
1218 $state{handle} = ka_fetch $ka_key; 1221 $state{handle} = ka_fetch $ka_key;
1219 $state{handle}->destroyed 1222# $state{handle}->destroyed
1220 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#
1221 $prepare_handle->(); 1224 $prepare_handle->();
1222 $state{handle}->destroyed 1225# $state{handle}->destroyed
1223 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#
1224 $handle_actual_request->(); 1227 $handle_actual_request->();
1225 1228
1226 } else { 1229 } else {
1227 my $tcp_connect = $arg{tcp_connect} 1230 my $tcp_connect = $arg{tcp_connect}
1228 || do { require AnyEvent::Socket; \&AnyEvent::Socket::tcp_connect }; 1231 || do { require AnyEvent::Socket; \&AnyEvent::Socket::tcp_connect };
1458 or die "$file: $!"; 1461 or die "$file: $!";
1459 1462
1460 my %hdr; 1463 my %hdr;
1461 my $ofs = 0; 1464 my $ofs = 0;
1462 1465
1463 warn stat $fh;
1464 warn -s _;
1465 if (stat $fh and -s _) { 1466 if (stat $fh and -s _) {
1466 $ofs = -s _; 1467 $ofs = -s _;
1467 warn "-s is ", $ofs; 1468 warn "-s is ", $ofs;
1468 $hdr{"if-unmodified-since"} = AnyEvent::HTTP::format_date +(stat _)[9]; 1469 $hdr{"if-unmodified-since"} = AnyEvent::HTTP::format_date +(stat _)[9];
1469 $hdr{"range"} = "bytes=$ofs-"; 1470 $hdr{"range"} = "bytes=$ofs-";
1497 my (undef, $hdr) = @_; 1498 my (undef, $hdr) = @_;
1498 1499
1499 my $status = $hdr->{Status}; 1500 my $status = $hdr->{Status};
1500 1501
1501 if (my $time = AnyEvent::HTTP::parse_date $hdr->{"last-modified"}) { 1502 if (my $time = AnyEvent::HTTP::parse_date $hdr->{"last-modified"}) {
1502 utime $fh, $time, $time; 1503 utime $time, $time, $fh;
1503 } 1504 }
1504 1505
1505 if ($status == 200 || $status == 206 || $status == 416) { 1506 if ($status == 200 || $status == 206 || $status == 416) {
1506 # download ok || resume ok || file already fully downloaded 1507 # download ok || resume ok || file already fully downloaded
1507 $cb->(1, $hdr); 1508 $cb->(1, $hdr);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines