… | |
… | |
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.21; |
51 | our $VERSION = 2.24; |
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; |
… | |
… | |
195 | C<$scheme> must be either missing or must be C<http> for HTTP. |
195 | C<$scheme> must be either missing or must be C<http> for HTTP. |
196 | |
196 | |
197 | If not specified, then the default proxy is used (see |
197 | If not specified, then the default proxy is used (see |
198 | C<AnyEvent::HTTP::set_proxy>). |
198 | C<AnyEvent::HTTP::set_proxy>). |
199 | |
199 | |
|
|
200 | Currently, if your proxy requires authorization, you have to specify an |
|
|
201 | appropriate "Proxy-Authorization" header in every request. |
|
|
202 | |
200 | =item body => $string |
203 | =item body => $string |
201 | |
204 | |
202 | The request body, usually empty. Will be sent as-is (future versions of |
205 | The request body, usually empty. Will be sent as-is (future versions of |
203 | this module might offer more options). |
206 | this module might offer more options). |
204 | |
207 | |
… | |
… | |
339 | =item persistent => $boolean |
342 | =item persistent => $boolean |
340 | |
343 | |
341 | Try to create/reuse a persistent connection. When this flag is set |
344 | Try to create/reuse a persistent connection. When this flag is set |
342 | (default: true for idempotent requests, false for all others), then |
345 | (default: true for idempotent requests, false for all others), then |
343 | C<http_request> tries to re-use an existing (previously-created) |
346 | C<http_request> tries to re-use an existing (previously-created) |
344 | persistent connection to the host and, failing that, tries to create a new |
347 | persistent connection to same host (i.e. identical URL scheme, hostname, |
345 | one. |
348 | port and session) and, failing that, tries to create a new one. |
346 | |
349 | |
347 | Requests failing in certain ways will be automatically retried once, which |
350 | Requests failing in certain ways will be automatically retried once, which |
348 | is dangerous for non-idempotent requests, which is why it defaults to off |
351 | is dangerous for non-idempotent requests, which is why it defaults to off |
349 | for them. The reason for this is because the bozos who designed HTTP/1.1 |
352 | for them. The reason for this is because the bozos who designed HTTP/1.1 |
350 | made it impossible to distinguish between a fatal error and a normal |
353 | made it impossible to distinguish between a fatal error and a normal |
… | |
… | |
451 | |
454 | |
452 | # expire cookies |
455 | # expire cookies |
453 | sub cookie_jar_expire($;$) { |
456 | sub cookie_jar_expire($;$) { |
454 | my ($jar, $session_end) = @_; |
457 | my ($jar, $session_end) = @_; |
455 | |
458 | |
456 | %$jar = () if $jar->{version} != 1; |
459 | %$jar = () if $jar->{version} != 2; |
457 | |
460 | |
458 | my $anow = AE::now; |
461 | my $anow = AE::now; |
459 | |
462 | |
460 | while (my ($chost, $paths) = each %$jar) { |
463 | while (my ($chost, $paths) = each %$jar) { |
461 | next unless ref $paths; |
464 | next unless ref $paths; |
… | |
… | |
481 | |
484 | |
482 | # extract cookies from jar |
485 | # extract cookies from jar |
483 | sub cookie_jar_extract($$$$) { |
486 | sub cookie_jar_extract($$$$) { |
484 | my ($jar, $scheme, $host, $path) = @_; |
487 | my ($jar, $scheme, $host, $path) = @_; |
485 | |
488 | |
486 | %$jar = () if $jar->{version} != 1; |
489 | %$jar = () if $jar->{version} != 2; |
|
|
490 | |
|
|
491 | $host = AnyEvent::Util::idn_to_ascii $host |
|
|
492 | if $host =~ /[^\x00-\x7f]/; |
487 | |
493 | |
488 | my @cookies; |
494 | my @cookies; |
489 | |
495 | |
490 | while (my ($chost, $paths) = each %$jar) { |
496 | while (my ($chost, $paths) = each %$jar) { |
491 | next unless ref $paths; |
497 | next unless ref $paths; |
492 | |
498 | |
493 | if ($chost =~ /^\./) { |
499 | # exact match or suffix including . match |
494 | next unless $chost eq substr $host, -length $chost; |
500 | $chost eq $host or ".$chost" eq substr $host, -1 - length $chost |
495 | } elsif ($chost =~ /\./) { |
|
|
496 | next unless $chost eq $host; |
|
|
497 | } else { |
|
|
498 | next; |
501 | or next; |
499 | } |
|
|
500 | |
502 | |
501 | while (my ($cpath, $cookies) = each %$paths) { |
503 | while (my ($cpath, $cookies) = each %$paths) { |
502 | next unless $cpath eq substr $path, 0, length $cpath; |
504 | next unless $cpath eq substr $path, 0, length $cpath; |
503 | |
505 | |
504 | while (my ($cookie, $kv) = each %$cookies) { |
506 | while (my ($cookie, $kv) = each %$cookies) { |
… | |
… | |
525 | } |
527 | } |
526 | |
528 | |
527 | # parse set_cookie header into jar |
529 | # parse set_cookie header into jar |
528 | sub cookie_jar_set_cookie($$$$) { |
530 | sub cookie_jar_set_cookie($$$$) { |
529 | my ($jar, $set_cookie, $host, $date) = @_; |
531 | my ($jar, $set_cookie, $host, $date) = @_; |
|
|
532 | |
|
|
533 | %$jar = () if $jar->{version} != 2; |
530 | |
534 | |
531 | my $anow = int AE::now; |
535 | my $anow = int AE::now; |
532 | my $snow; # server-now |
536 | my $snow; # server-now |
533 | |
537 | |
534 | for ($set_cookie) { |
538 | for ($set_cookie) { |
… | |
… | |
580 | |
584 | |
581 | my $cdom; |
585 | my $cdom; |
582 | my $cpath = (delete $kv{path}) || "/"; |
586 | my $cpath = (delete $kv{path}) || "/"; |
583 | |
587 | |
584 | if (exists $kv{domain}) { |
588 | if (exists $kv{domain}) { |
585 | $cdom = delete $kv{domain}; |
589 | $cdom = $kv{domain}; |
586 | |
590 | |
587 | $cdom =~ s/^\.?/./; # make sure it starts with a "." |
591 | $cdom =~ s/^\.?/./; # make sure it starts with a "." |
588 | |
592 | |
589 | next if $cdom =~ /\.$/; |
593 | next if $cdom =~ /\.$/; |
590 | |
594 | |
591 | # this is not rfc-like and not netscape-like. go figure. |
595 | # this is not rfc-like and not netscape-like. go figure. |
592 | my $ndots = $cdom =~ y/.//; |
596 | my $ndots = $cdom =~ y/.//; |
593 | next if $ndots < ($cdom =~ /\.[^.][^.]\.[^.][^.]$/ ? 3 : 2); |
597 | next if $ndots < ($cdom =~ /\.[^.][^.]\.[^.][^.]$/ ? 3 : 2); |
|
|
598 | |
|
|
599 | $cdom = substr $cdom, 1; # remove initial . |
594 | } else { |
600 | } else { |
595 | $cdom = $host; |
601 | $cdom = $host; |
596 | } |
602 | } |
597 | |
603 | |
598 | # store it |
604 | # store it |
599 | $jar->{version} = 1; |
605 | $jar->{version} = 2; |
600 | $jar->{lc $cdom}{$cpath}{$name} = \%kv; |
606 | $jar->{lc $cdom}{$cpath}{$name} = \%kv; |
601 | |
607 | |
602 | redo if /\G\s*,/gc; |
608 | redo if /\G\s*,/gc; |
603 | } |
609 | } |
604 | } |
610 | } |
… | |
… | |
770 | |
776 | |
771 | my $uport = $uscheme eq "http" ? 80 |
777 | my $uport = $uscheme eq "http" ? 80 |
772 | : $uscheme eq "https" ? 443 |
778 | : $uscheme eq "https" ? 443 |
773 | : return $cb->(undef, { @pseudo, Status => 599, Reason => "Only http and https URL schemes supported" }); |
779 | : return $cb->(undef, { @pseudo, Status => 599, Reason => "Only http and https URL schemes supported" }); |
774 | |
780 | |
775 | $uauthority =~ /^(?: .*\@ )? ([^\@:]+) (?: : (\d+) )?$/x |
781 | $uauthority =~ /^(?: .*\@ )? ([^\@]+?) (?: : (\d+) )?$/x |
776 | or return $cb->(undef, { @pseudo, Status => 599, Reason => "Unparsable URL" }); |
782 | or return $cb->(undef, { @pseudo, Status => 599, Reason => "Unparsable URL" }); |
777 | |
783 | |
778 | my $uhost = lc $1; |
784 | my $uhost = lc $1; |
779 | $uport = $2 if defined $2; |
785 | $uport = $2 if defined $2; |
780 | |
786 | |
… | |
… | |
892 | # we give our best and fall back to URI if available. |
898 | # we give our best and fall back to URI if available. |
893 | if (exists $hdr{location}) { |
899 | if (exists $hdr{location}) { |
894 | my $loc = $hdr{location}; |
900 | my $loc = $hdr{location}; |
895 | |
901 | |
896 | if ($loc =~ m%^//%) { # // |
902 | if ($loc =~ m%^//%) { # // |
897 | $loc = "$rscheme:$loc"; |
903 | $loc = "$uscheme:$loc"; |
898 | |
904 | |
899 | } elsif ($loc eq "") { |
905 | } elsif ($loc eq "") { |
900 | $loc = $url; |
906 | $loc = $url; |
901 | |
907 | |
902 | } elsif ($loc !~ /^(?: $ | [^:\/?\#]+ : )/x) { # anything "simple" |
908 | } elsif ($loc !~ /^(?: $ | [^:\/?\#]+ : )/x) { # anything "simple" |
903 | $loc =~ s/^\.\/+//; |
909 | $loc =~ s/^\.\/+//; |
904 | |
910 | |
905 | if ($loc !~ m%^[.?#]%) { |
911 | if ($loc !~ m%^[.?#]%) { |
906 | my $prefix = "$rscheme://$uhost:$uport"; |
912 | my $prefix = "$uscheme://$uauthority"; |
907 | |
913 | |
908 | unless ($loc =~ s/^\///) { |
914 | unless ($loc =~ s/^\///) { |
909 | $prefix .= $upath; |
915 | $prefix .= $upath; |
910 | $prefix =~ s/\/[^\/]*$//; |
916 | $prefix =~ s/\/[^\/]*$//; |
911 | } |
917 | } |
… | |
… | |
1028 | $finish->(delete $state{handle}); |
1034 | $finish->(delete $state{handle}); |
1029 | |
1035 | |
1030 | } elsif ($chunked) { |
1036 | } elsif ($chunked) { |
1031 | my $cl = 0; |
1037 | my $cl = 0; |
1032 | my $body = ""; |
1038 | my $body = ""; |
1033 | my $on_body = $arg{on_body} || sub { $body .= shift; 1 }; |
1039 | my $on_body = (!$redirect && $arg{on_body}) || sub { $body .= shift; 1 }; |
1034 | |
1040 | |
1035 | $state{read_chunk} = sub { |
1041 | $state{read_chunk} = sub { |
1036 | $_[1] =~ /^([0-9a-fA-F]+)/ |
1042 | $_[1] =~ /^([0-9a-fA-F]+)/ |
1037 | or return $finish->(undef, $ae_error => "Garbled chunked transfer encoding"); |
1043 | or return $finish->(undef, $ae_error => "Garbled chunked transfer encoding"); |
1038 | |
1044 | |
… | |
… | |
1071 | } |
1077 | } |
1072 | }; |
1078 | }; |
1073 | |
1079 | |
1074 | $_[0]->push_read (line => $state{read_chunk}); |
1080 | $_[0]->push_read (line => $state{read_chunk}); |
1075 | |
1081 | |
1076 | } elsif ($arg{on_body}) { |
1082 | } elsif (!$redirect && $arg{on_body}) { |
1077 | if (defined $len) { |
1083 | if (defined $len) { |
1078 | $_[0]->on_read (sub { |
1084 | $_[0]->on_read (sub { |
1079 | $len -= length $_[0]{rbuf}; |
1085 | $len -= length $_[0]{rbuf}; |
1080 | |
1086 | |
1081 | $arg{on_body}(delete $_[0]{rbuf}, \%hdr) |
1087 | $arg{on_body}(delete $_[0]{rbuf}, \%hdr) |
… | |
… | |
1120 | _destroy_state %state; |
1126 | _destroy_state %state; |
1121 | |
1127 | |
1122 | %state = (); |
1128 | %state = (); |
1123 | $state{recurse} = |
1129 | $state{recurse} = |
1124 | http_request ( |
1130 | http_request ( |
1125 | $method => $url, |
1131 | $method => $url, |
1126 | %arg, |
1132 | %arg, |
1127 | recurse => $recurse - 1, |
1133 | recurse => $recurse - 1, |
1128 | keepalive => 0, |
1134 | persistent => 0, |
1129 | sub { |
1135 | sub { |
1130 | %state = (); |
1136 | %state = (); |
1131 | &$cb |
1137 | &$cb |
1132 | } |
1138 | } |
1133 | ); |
1139 | ); |
… | |
… | |
1214 | # on a keepalive request (in theory, this should be a separate config option). |
1220 | # on a keepalive request (in theory, this should be a separate config option). |
1215 | if ($persistent && $KA_CACHE{$ka_key}) { |
1221 | if ($persistent && $KA_CACHE{$ka_key}) { |
1216 | $was_persistent = 1; |
1222 | $was_persistent = 1; |
1217 | |
1223 | |
1218 | $state{handle} = ka_fetch $ka_key; |
1224 | $state{handle} = ka_fetch $ka_key; |
1219 | $state{handle}->destroyed |
1225 | # $state{handle}->destroyed |
1220 | and die "AnyEvent::HTTP: unexpectedly got a destructed handle (1), please report.";#d# |
1226 | # and die "AnyEvent::HTTP: unexpectedly got a destructed handle (1), please report.";#d# |
1221 | $prepare_handle->(); |
1227 | $prepare_handle->(); |
1222 | $state{handle}->destroyed |
1228 | # $state{handle}->destroyed |
1223 | and die "AnyEvent::HTTP: unexpectedly got a destructed handle (2), please report.";#d# |
1229 | # and die "AnyEvent::HTTP: unexpectedly got a destructed handle (2), please report.";#d# |
1224 | $handle_actual_request->(); |
1230 | $handle_actual_request->(); |
1225 | |
1231 | |
1226 | } else { |
1232 | } else { |
1227 | my $tcp_connect = $arg{tcp_connect} |
1233 | my $tcp_connect = $arg{tcp_connect} |
1228 | || do { require AnyEvent::Socket; \&AnyEvent::Socket::tcp_connect }; |
1234 | || do { require AnyEvent::Socket; \&AnyEvent::Socket::tcp_connect }; |
… | |
… | |
1288 | function from time to time. |
1294 | function from time to time. |
1289 | |
1295 | |
1290 | A cookie jar is initially an empty hash-reference that is managed by this |
1296 | A cookie jar is initially an empty hash-reference that is managed by this |
1291 | module. Its format is subject to change, but currently it is as follows: |
1297 | module. Its format is subject to change, but currently it is as follows: |
1292 | |
1298 | |
1293 | The key C<version> has to contain C<1>, otherwise the hash gets |
1299 | The key C<version> has to contain C<2>, otherwise the hash gets |
1294 | emptied. All other keys are hostnames or IP addresses pointing to |
1300 | cleared. All other keys are hostnames or IP addresses pointing to |
1295 | hash-references. The key for these inner hash references is the |
1301 | hash-references. The key for these inner hash references is the |
1296 | server path for which this cookie is meant, and the values are again |
1302 | server path for which this cookie is meant, and the values are again |
1297 | hash-references. Each key of those hash-references is a cookie name, and |
1303 | hash-references. Each key of those hash-references is a cookie name, and |
1298 | the value, you guessed it, is another hash-reference, this time with the |
1304 | the value, you guessed it, is another hash-reference, this time with the |
1299 | key-value pairs from the cookie, except for C<expires> and C<max-age>, |
1305 | key-value pairs from the cookie, except for C<expires> and C<max-age>, |
… | |
… | |
1303 | |
1309 | |
1304 | Here is an example of a cookie jar with a single cookie, so you have a |
1310 | Here is an example of a cookie jar with a single cookie, so you have a |
1305 | chance of understanding the above paragraph: |
1311 | chance of understanding the above paragraph: |
1306 | |
1312 | |
1307 | { |
1313 | { |
1308 | version => 1, |
1314 | version => 2, |
1309 | "10.0.0.1" => { |
1315 | "10.0.0.1" => { |
1310 | "/" => { |
1316 | "/" => { |
1311 | "mythweb_id" => { |
1317 | "mythweb_id" => { |
1312 | _expires => 1293917923, |
1318 | _expires => 1293917923, |
1313 | value => "ooRung9dThee3ooyXooM1Ohm", |
1319 | value => "ooRung9dThee3ooyXooM1Ohm", |
… | |
… | |
1458 | or die "$file: $!"; |
1464 | or die "$file: $!"; |
1459 | |
1465 | |
1460 | my %hdr; |
1466 | my %hdr; |
1461 | my $ofs = 0; |
1467 | my $ofs = 0; |
1462 | |
1468 | |
1463 | warn stat $fh; |
|
|
1464 | warn -s _; |
|
|
1465 | if (stat $fh and -s _) { |
1469 | if (stat $fh and -s _) { |
1466 | $ofs = -s _; |
1470 | $ofs = -s _; |
1467 | warn "-s is ", $ofs; |
1471 | warn "-s is ", $ofs; |
1468 | $hdr{"if-unmodified-since"} = AnyEvent::HTTP::format_date +(stat _)[9]; |
1472 | $hdr{"if-unmodified-since"} = AnyEvent::HTTP::format_date +(stat _)[9]; |
1469 | $hdr{"range"} = "bytes=$ofs-"; |
1473 | $hdr{"range"} = "bytes=$ofs-"; |
… | |
… | |
1497 | my (undef, $hdr) = @_; |
1501 | my (undef, $hdr) = @_; |
1498 | |
1502 | |
1499 | my $status = $hdr->{Status}; |
1503 | my $status = $hdr->{Status}; |
1500 | |
1504 | |
1501 | if (my $time = AnyEvent::HTTP::parse_date $hdr->{"last-modified"}) { |
1505 | if (my $time = AnyEvent::HTTP::parse_date $hdr->{"last-modified"}) { |
1502 | utime $fh, $time, $time; |
1506 | utime $time, $time, $fh; |
1503 | } |
1507 | } |
1504 | |
1508 | |
1505 | if ($status == 200 || $status == 206 || $status == 416) { |
1509 | if ($status == 200 || $status == 206 || $status == 416) { |
1506 | # download ok || resume ok || file already fully downloaded |
1510 | # download ok || resume ok || file already fully downloaded |
1507 | $cb->(1, $hdr); |
1511 | $cb->(1, $hdr); |