… | |
… | |
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; |
… | |
… | |
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 |
200 | Currently, if your proxy requires authorization, you have to specify an |
201 | appropriate "Proxy-Authorization" header in every request. |
201 | appropriate "Proxy-Authorization" header in every request. |
202 | |
202 | |
|
|
203 | Note that this module will prefer an existing persistent connection, |
|
|
204 | even if that connection was made using another proxy. If you need to |
|
|
205 | ensure that a new connection is made in this case, you can either force |
|
|
206 | C<persistent> to false or e.g. use the proxy address in your C<sessionid>. |
|
|
207 | |
203 | =item body => $string |
208 | =item body => $string |
204 | |
209 | |
205 | The request body, usually empty. Will be sent as-is (future versions of |
210 | The request body, usually empty. Will be sent as-is (future versions of |
206 | this module might offer more options). |
211 | this module might offer more options). |
207 | |
212 | |
… | |
… | |
239 | The default for this option is C<low>, which could be interpreted as "give |
244 | The default for this option is C<low>, which could be interpreted as "give |
240 | me the page, no matter what". |
245 | me the page, no matter what". |
241 | |
246 | |
242 | See also the C<sessionid> parameter. |
247 | See also the C<sessionid> parameter. |
243 | |
248 | |
244 | =item session => $string |
249 | =item sessionid => $string |
245 | |
250 | |
246 | The module might reuse connections to the same host internally. Sometimes |
251 | The module might reuse connections to the same host internally (regardless |
247 | (e.g. when using TLS), you do not want to reuse connections from other |
252 | of other settings, such as C<tcp_connect> or C<proxy>). Sometimes (e.g. |
|
|
253 | when using TLS or a specfic proxy), you do not want to reuse connections |
248 | sessions. This can be achieved by setting this parameter to some unique |
254 | from other sessions. This can be achieved by setting this parameter to |
249 | ID (such as the address of an object storing your state data, or the TLS |
255 | some unique ID (such as the address of an object storing your state data |
250 | context) - only connections using the same unique ID will be reused. |
256 | or the TLS context, or the proxy IP) - only connections using the same |
|
|
257 | unique ID will be reused. |
251 | |
258 | |
252 | =item on_prepare => $callback->($fh) |
259 | =item on_prepare => $callback->($fh) |
253 | |
260 | |
254 | In rare cases you need to "tune" the socket before it is used to |
261 | In rare cases you need to "tune" the socket before it is used to |
255 | connect (for example, to bind it on a given IP address). This parameter |
262 | connect (for example, to bind it on a given IP address). This parameter |
… | |
… | |
263 | In even rarer cases you want total control over how AnyEvent::HTTP |
270 | In even rarer cases you want total control over how AnyEvent::HTTP |
264 | establishes connections. Normally it uses L<AnyEvent::Socket::tcp_connect> |
271 | establishes connections. Normally it uses L<AnyEvent::Socket::tcp_connect> |
265 | to do this, but you can provide your own C<tcp_connect> function - |
272 | to do this, but you can provide your own C<tcp_connect> function - |
266 | obviously, it has to follow the same calling conventions, except that it |
273 | obviously, it has to follow the same calling conventions, except that it |
267 | may always return a connection guard object. |
274 | may always return a connection guard object. |
|
|
275 | |
|
|
276 | The connections made by this hook will be treated as equivalent to |
|
|
277 | connections made the built-in way, specifically, they will be put into |
|
|
278 | and taken from the persistent connection cache. If your C<$tcp_connect> |
|
|
279 | function is incompatible with this kind of re-use, consider switching off |
|
|
280 | C<persistent> connections and/or providing a C<sessionid> identifier. |
268 | |
281 | |
269 | There are probably lots of weird uses for this function, starting from |
282 | There are probably lots of weird uses for this function, starting from |
270 | tracing the hosts C<http_request> actually tries to connect, to (inexact |
283 | tracing the hosts C<http_request> actually tries to connect, to (inexact |
271 | but fast) host => IP address caching or even socks protocol support. |
284 | but fast) host => IP address caching or even socks protocol support. |
272 | |
285 | |
… | |
… | |
342 | =item persistent => $boolean |
355 | =item persistent => $boolean |
343 | |
356 | |
344 | Try to create/reuse a persistent connection. When this flag is set |
357 | Try to create/reuse a persistent connection. When this flag is set |
345 | (default: true for idempotent requests, false for all others), then |
358 | (default: true for idempotent requests, false for all others), then |
346 | C<http_request> tries to re-use an existing (previously-created) |
359 | C<http_request> tries to re-use an existing (previously-created) |
347 | persistent connection to the host and, failing that, tries to create a new |
360 | persistent connection to same host (i.e. identical URL scheme, hostname, |
348 | one. |
361 | port and sessionid) and, failing that, tries to create a new one. |
349 | |
362 | |
350 | Requests failing in certain ways will be automatically retried once, which |
363 | Requests failing in certain ways will be automatically retried once, which |
351 | is dangerous for non-idempotent requests, which is why it defaults to off |
364 | is dangerous for non-idempotent requests, which is why it defaults to off |
352 | for them. The reason for this is because the bozos who designed HTTP/1.1 |
365 | for them. The reason for this is because the bozos who designed HTTP/1.1 |
353 | made it impossible to distinguish between a fatal error and a normal |
366 | made it impossible to distinguish between a fatal error and a normal |
354 | connection timeout, so you never know whether there was a problem with |
367 | connection timeout, so you never know whether there was a problem with |
355 | your request or not. |
368 | your request or not. |
356 | |
369 | |
357 | When reusing an existent connection, many parameters (such as TLS context) |
370 | When reusing an existent connection, many parameters (such as TLS context) |
358 | will be ignored. See the C<session> parameter for a workaround. |
371 | will be ignored. See the C<sessionid> parameter for a workaround. |
359 | |
372 | |
360 | =item keepalive => $boolean |
373 | =item keepalive => $boolean |
361 | |
374 | |
362 | Only used when C<persistent> is also true. This parameter decides whether |
375 | Only used when C<persistent> is also true. This parameter decides whether |
363 | C<http_request> tries to handshake a HTTP/1.0-style keep-alive connection |
376 | C<http_request> tries to handshake a HTTP/1.0-style keep-alive connection |
… | |
… | |
454 | |
467 | |
455 | # expire cookies |
468 | # expire cookies |
456 | sub cookie_jar_expire($;$) { |
469 | sub cookie_jar_expire($;$) { |
457 | my ($jar, $session_end) = @_; |
470 | my ($jar, $session_end) = @_; |
458 | |
471 | |
459 | %$jar = () if $jar->{version} != 1; |
472 | %$jar = () if $jar->{version} != 2; |
460 | |
473 | |
461 | my $anow = AE::now; |
474 | my $anow = AE::now; |
462 | |
475 | |
463 | while (my ($chost, $paths) = each %$jar) { |
476 | while (my ($chost, $paths) = each %$jar) { |
464 | next unless ref $paths; |
477 | next unless ref $paths; |
… | |
… | |
484 | |
497 | |
485 | # extract cookies from jar |
498 | # extract cookies from jar |
486 | sub cookie_jar_extract($$$$) { |
499 | sub cookie_jar_extract($$$$) { |
487 | my ($jar, $scheme, $host, $path) = @_; |
500 | my ($jar, $scheme, $host, $path) = @_; |
488 | |
501 | |
489 | %$jar = () if $jar->{version} != 1; |
502 | %$jar = () if $jar->{version} != 2; |
|
|
503 | |
|
|
504 | $host = AnyEvent::Util::idn_to_ascii $host |
|
|
505 | if $host =~ /[^\x00-\x7f]/; |
490 | |
506 | |
491 | my @cookies; |
507 | my @cookies; |
492 | |
508 | |
493 | while (my ($chost, $paths) = each %$jar) { |
509 | while (my ($chost, $paths) = each %$jar) { |
494 | next unless ref $paths; |
510 | next unless ref $paths; |
495 | |
511 | |
496 | if ($chost =~ /^\./) { |
512 | # exact match or suffix including . match |
497 | next unless $chost eq substr $host, -length $chost; |
513 | $chost eq $host or ".$chost" eq substr $host, -1 - length $chost |
498 | } elsif ($chost =~ /\./) { |
|
|
499 | next unless $chost eq $host; |
|
|
500 | } else { |
|
|
501 | next; |
514 | or next; |
502 | } |
|
|
503 | |
515 | |
504 | while (my ($cpath, $cookies) = each %$paths) { |
516 | while (my ($cpath, $cookies) = each %$paths) { |
505 | next unless $cpath eq substr $path, 0, length $cpath; |
517 | next unless $cpath eq substr $path, 0, length $cpath; |
506 | |
518 | |
507 | while (my ($cookie, $kv) = each %$cookies) { |
519 | while (my ($cookie, $kv) = each %$cookies) { |
… | |
… | |
528 | } |
540 | } |
529 | |
541 | |
530 | # parse set_cookie header into jar |
542 | # parse set_cookie header into jar |
531 | sub cookie_jar_set_cookie($$$$) { |
543 | sub cookie_jar_set_cookie($$$$) { |
532 | my ($jar, $set_cookie, $host, $date) = @_; |
544 | my ($jar, $set_cookie, $host, $date) = @_; |
|
|
545 | |
|
|
546 | %$jar = () if $jar->{version} != 2; |
533 | |
547 | |
534 | my $anow = int AE::now; |
548 | my $anow = int AE::now; |
535 | my $snow; # server-now |
549 | my $snow; # server-now |
536 | |
550 | |
537 | for ($set_cookie) { |
551 | for ($set_cookie) { |
… | |
… | |
583 | |
597 | |
584 | my $cdom; |
598 | my $cdom; |
585 | my $cpath = (delete $kv{path}) || "/"; |
599 | my $cpath = (delete $kv{path}) || "/"; |
586 | |
600 | |
587 | if (exists $kv{domain}) { |
601 | if (exists $kv{domain}) { |
588 | $cdom = delete $kv{domain}; |
602 | $cdom = $kv{domain}; |
589 | |
603 | |
590 | $cdom =~ s/^\.?/./; # make sure it starts with a "." |
604 | $cdom =~ s/^\.?/./; # make sure it starts with a "." |
591 | |
605 | |
592 | next if $cdom =~ /\.$/; |
606 | next if $cdom =~ /\.$/; |
593 | |
607 | |
594 | # this is not rfc-like and not netscape-like. go figure. |
608 | # this is not rfc-like and not netscape-like. go figure. |
595 | my $ndots = $cdom =~ y/.//; |
609 | my $ndots = $cdom =~ y/.//; |
596 | next if $ndots < ($cdom =~ /\.[^.][^.]\.[^.][^.]$/ ? 3 : 2); |
610 | next if $ndots < ($cdom =~ /\.[^.][^.]\.[^.][^.]$/ ? 3 : 2); |
|
|
611 | |
|
|
612 | $cdom = substr $cdom, 1; # remove initial . |
597 | } else { |
613 | } else { |
598 | $cdom = $host; |
614 | $cdom = $host; |
599 | } |
615 | } |
600 | |
616 | |
601 | # store it |
617 | # store it |
602 | $jar->{version} = 1; |
618 | $jar->{version} = 2; |
603 | $jar->{lc $cdom}{$cpath}{$name} = \%kv; |
619 | $jar->{lc $cdom}{$cpath}{$name} = \%kv; |
604 | |
620 | |
605 | redo if /\G\s*,/gc; |
621 | redo if /\G\s*,/gc; |
606 | } |
622 | } |
607 | } |
623 | } |
… | |
… | |
895 | # we give our best and fall back to URI if available. |
911 | # we give our best and fall back to URI if available. |
896 | if (exists $hdr{location}) { |
912 | if (exists $hdr{location}) { |
897 | my $loc = $hdr{location}; |
913 | my $loc = $hdr{location}; |
898 | |
914 | |
899 | if ($loc =~ m%^//%) { # // |
915 | if ($loc =~ m%^//%) { # // |
900 | $loc = "$rscheme:$loc"; |
916 | $loc = "$uscheme:$loc"; |
901 | |
917 | |
902 | } elsif ($loc eq "") { |
918 | } elsif ($loc eq "") { |
903 | $loc = $url; |
919 | $loc = $url; |
904 | |
920 | |
905 | } elsif ($loc !~ /^(?: $ | [^:\/?\#]+ : )/x) { # anything "simple" |
921 | } elsif ($loc !~ /^(?: $ | [^:\/?\#]+ : )/x) { # anything "simple" |
906 | $loc =~ s/^\.\/+//; |
922 | $loc =~ s/^\.\/+//; |
907 | |
923 | |
908 | if ($loc !~ m%^[.?#]%) { |
924 | if ($loc !~ m%^[.?#]%) { |
909 | my $prefix = "$rscheme://$uhost:$uport"; |
925 | my $prefix = "$uscheme://$uauthority"; |
910 | |
926 | |
911 | unless ($loc =~ s/^\///) { |
927 | unless ($loc =~ s/^\///) { |
912 | $prefix .= $upath; |
928 | $prefix .= $upath; |
913 | $prefix =~ s/\/[^\/]*$//; |
929 | $prefix =~ s/\/[^\/]*$//; |
914 | } |
930 | } |
… | |
… | |
1031 | $finish->(delete $state{handle}); |
1047 | $finish->(delete $state{handle}); |
1032 | |
1048 | |
1033 | } elsif ($chunked) { |
1049 | } elsif ($chunked) { |
1034 | my $cl = 0; |
1050 | my $cl = 0; |
1035 | my $body = ""; |
1051 | my $body = ""; |
1036 | my $on_body = $arg{on_body} || sub { $body .= shift; 1 }; |
1052 | my $on_body = (!$redirect && $arg{on_body}) || sub { $body .= shift; 1 }; |
1037 | |
1053 | |
1038 | $state{read_chunk} = sub { |
1054 | $state{read_chunk} = sub { |
1039 | $_[1] =~ /^([0-9a-fA-F]+)/ |
1055 | $_[1] =~ /^([0-9a-fA-F]+)/ |
1040 | or return $finish->(undef, $ae_error => "Garbled chunked transfer encoding"); |
1056 | or return $finish->(undef, $ae_error => "Garbled chunked transfer encoding"); |
1041 | |
1057 | |
… | |
… | |
1074 | } |
1090 | } |
1075 | }; |
1091 | }; |
1076 | |
1092 | |
1077 | $_[0]->push_read (line => $state{read_chunk}); |
1093 | $_[0]->push_read (line => $state{read_chunk}); |
1078 | |
1094 | |
1079 | } elsif ($arg{on_body}) { |
1095 | } elsif (!$redirect && $arg{on_body}) { |
1080 | if (defined $len) { |
1096 | if (defined $len) { |
1081 | $_[0]->on_read (sub { |
1097 | $_[0]->on_read (sub { |
1082 | $len -= length $_[0]{rbuf}; |
1098 | $len -= length $_[0]{rbuf}; |
1083 | |
1099 | |
1084 | $arg{on_body}(delete $_[0]{rbuf}, \%hdr) |
1100 | $arg{on_body}(delete $_[0]{rbuf}, \%hdr) |
… | |
… | |
1217 | # on a keepalive request (in theory, this should be a separate config option). |
1233 | # on a keepalive request (in theory, this should be a separate config option). |
1218 | if ($persistent && $KA_CACHE{$ka_key}) { |
1234 | if ($persistent && $KA_CACHE{$ka_key}) { |
1219 | $was_persistent = 1; |
1235 | $was_persistent = 1; |
1220 | |
1236 | |
1221 | $state{handle} = ka_fetch $ka_key; |
1237 | $state{handle} = ka_fetch $ka_key; |
1222 | $state{handle}->destroyed |
1238 | # $state{handle}->destroyed |
1223 | and die "AnyEvent::HTTP: unexpectedly got a destructed handle (1), please report.";#d# |
1239 | # and die "AnyEvent::HTTP: unexpectedly got a destructed handle (1), please report.";#d# |
1224 | $prepare_handle->(); |
1240 | $prepare_handle->(); |
1225 | $state{handle}->destroyed |
1241 | # $state{handle}->destroyed |
1226 | and die "AnyEvent::HTTP: unexpectedly got a destructed handle (2), please report.";#d# |
1242 | # and die "AnyEvent::HTTP: unexpectedly got a destructed handle (2), please report.";#d# |
1227 | $handle_actual_request->(); |
1243 | $handle_actual_request->(); |
1228 | |
1244 | |
1229 | } else { |
1245 | } else { |
1230 | my $tcp_connect = $arg{tcp_connect} |
1246 | my $tcp_connect = $arg{tcp_connect} |
1231 | || do { require AnyEvent::Socket; \&AnyEvent::Socket::tcp_connect }; |
1247 | || do { require AnyEvent::Socket; \&AnyEvent::Socket::tcp_connect }; |
… | |
… | |
1291 | function from time to time. |
1307 | function from time to time. |
1292 | |
1308 | |
1293 | A cookie jar is initially an empty hash-reference that is managed by this |
1309 | A cookie jar is initially an empty hash-reference that is managed by this |
1294 | module. Its format is subject to change, but currently it is as follows: |
1310 | module. Its format is subject to change, but currently it is as follows: |
1295 | |
1311 | |
1296 | The key C<version> has to contain C<1>, otherwise the hash gets |
1312 | The key C<version> has to contain C<2>, otherwise the hash gets |
1297 | emptied. All other keys are hostnames or IP addresses pointing to |
1313 | cleared. All other keys are hostnames or IP addresses pointing to |
1298 | hash-references. The key for these inner hash references is the |
1314 | hash-references. The key for these inner hash references is the |
1299 | server path for which this cookie is meant, and the values are again |
1315 | server path for which this cookie is meant, and the values are again |
1300 | hash-references. Each key of those hash-references is a cookie name, and |
1316 | hash-references. Each key of those hash-references is a cookie name, and |
1301 | the value, you guessed it, is another hash-reference, this time with the |
1317 | the value, you guessed it, is another hash-reference, this time with the |
1302 | key-value pairs from the cookie, except for C<expires> and C<max-age>, |
1318 | key-value pairs from the cookie, except for C<expires> and C<max-age>, |
… | |
… | |
1306 | |
1322 | |
1307 | Here is an example of a cookie jar with a single cookie, so you have a |
1323 | Here is an example of a cookie jar with a single cookie, so you have a |
1308 | chance of understanding the above paragraph: |
1324 | chance of understanding the above paragraph: |
1309 | |
1325 | |
1310 | { |
1326 | { |
1311 | version => 1, |
1327 | version => 2, |
1312 | "10.0.0.1" => { |
1328 | "10.0.0.1" => { |
1313 | "/" => { |
1329 | "/" => { |
1314 | "mythweb_id" => { |
1330 | "mythweb_id" => { |
1315 | _expires => 1293917923, |
1331 | _expires => 1293917923, |
1316 | value => "ooRung9dThee3ooyXooM1Ohm", |
1332 | value => "ooRung9dThee3ooyXooM1Ohm", |
… | |
… | |
1461 | or die "$file: $!"; |
1477 | or die "$file: $!"; |
1462 | |
1478 | |
1463 | my %hdr; |
1479 | my %hdr; |
1464 | my $ofs = 0; |
1480 | my $ofs = 0; |
1465 | |
1481 | |
1466 | warn stat $fh; |
|
|
1467 | warn -s _; |
|
|
1468 | if (stat $fh and -s _) { |
1482 | if (stat $fh and -s _) { |
1469 | $ofs = -s _; |
1483 | $ofs = -s _; |
1470 | warn "-s is ", $ofs; |
1484 | warn "-s is ", $ofs; |
1471 | $hdr{"if-unmodified-since"} = AnyEvent::HTTP::format_date +(stat _)[9]; |
1485 | $hdr{"if-unmodified-since"} = AnyEvent::HTTP::format_date +(stat _)[9]; |
1472 | $hdr{"range"} = "bytes=$ofs-"; |
1486 | $hdr{"range"} = "bytes=$ofs-"; |
… | |
… | |
1500 | my (undef, $hdr) = @_; |
1514 | my (undef, $hdr) = @_; |
1501 | |
1515 | |
1502 | my $status = $hdr->{Status}; |
1516 | my $status = $hdr->{Status}; |
1503 | |
1517 | |
1504 | if (my $time = AnyEvent::HTTP::parse_date $hdr->{"last-modified"}) { |
1518 | if (my $time = AnyEvent::HTTP::parse_date $hdr->{"last-modified"}) { |
1505 | utime $fh, $time, $time; |
1519 | utime $time, $time, $fh; |
1506 | } |
1520 | } |
1507 | |
1521 | |
1508 | if ($status == 200 || $status == 206 || $status == 416) { |
1522 | if ($status == 200 || $status == 206 || $status == 416) { |
1509 | # download ok || resume ok || file already fully downloaded |
1523 | # download ok || resume ok || file already fully downloaded |
1510 | $cb->(1, $hdr); |
1524 | $cb->(1, $hdr); |