… | |
… | |
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 | |
|
|
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 | |
200 | =item body => $string |
208 | =item body => $string |
201 | |
209 | |
202 | 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 |
203 | this module might offer more options). |
211 | this module might offer more options). |
204 | |
212 | |
… | |
… | |
236 | 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 |
237 | me the page, no matter what". |
245 | me the page, no matter what". |
238 | |
246 | |
239 | See also the C<sessionid> parameter. |
247 | See also the C<sessionid> parameter. |
240 | |
248 | |
241 | =item session => $string |
249 | =item sessionid => $string |
242 | |
250 | |
243 | The module might reuse connections to the same host internally. Sometimes |
251 | The module might reuse connections to the same host internally (regardless |
244 | (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 |
245 | 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 |
246 | 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 |
247 | 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. |
248 | |
258 | |
249 | =item on_prepare => $callback->($fh) |
259 | =item on_prepare => $callback->($fh) |
250 | |
260 | |
251 | 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 |
252 | 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 |
… | |
… | |
260 | 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 |
261 | establishes connections. Normally it uses L<AnyEvent::Socket::tcp_connect> |
271 | establishes connections. Normally it uses L<AnyEvent::Socket::tcp_connect> |
262 | 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 - |
263 | 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 |
264 | 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. |
265 | |
281 | |
266 | 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 |
267 | 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 |
268 | but fast) host => IP address caching or even socks protocol support. |
284 | but fast) host => IP address caching or even socks protocol support. |
269 | |
285 | |
… | |
… | |
339 | =item persistent => $boolean |
355 | =item persistent => $boolean |
340 | |
356 | |
341 | 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 |
342 | (default: true for idempotent requests, false for all others), then |
358 | (default: true for idempotent requests, false for all others), then |
343 | C<http_request> tries to re-use an existing (previously-created) |
359 | 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 |
360 | persistent connection to same host (i.e. identical URL scheme, hostname, |
345 | one. |
361 | port and sessionid) and, failing that, tries to create a new one. |
346 | |
362 | |
347 | Requests failing in certain ways will be automatically retried once, which |
363 | 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 |
364 | 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 |
365 | 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 |
366 | made it impossible to distinguish between a fatal error and a normal |
351 | 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 |
352 | your request or not. |
368 | your request or not. |
353 | |
369 | |
354 | When reusing an existent connection, many parameters (such as TLS context) |
370 | When reusing an existent connection, many parameters (such as TLS context) |
355 | will be ignored. See the C<session> parameter for a workaround. |
371 | will be ignored. See the C<sessionid> parameter for a workaround. |
356 | |
372 | |
357 | =item keepalive => $boolean |
373 | =item keepalive => $boolean |
358 | |
374 | |
359 | 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 |
360 | 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 |
… | |
… | |
451 | |
467 | |
452 | # expire cookies |
468 | # expire cookies |
453 | sub cookie_jar_expire($;$) { |
469 | sub cookie_jar_expire($;$) { |
454 | my ($jar, $session_end) = @_; |
470 | my ($jar, $session_end) = @_; |
455 | |
471 | |
456 | %$jar = () if $jar->{version} != 1; |
472 | %$jar = () if $jar->{version} != 2; |
457 | |
473 | |
458 | my $anow = AE::now; |
474 | my $anow = AE::now; |
459 | |
475 | |
460 | while (my ($chost, $paths) = each %$jar) { |
476 | while (my ($chost, $paths) = each %$jar) { |
461 | next unless ref $paths; |
477 | next unless ref $paths; |
… | |
… | |
481 | |
497 | |
482 | # extract cookies from jar |
498 | # extract cookies from jar |
483 | sub cookie_jar_extract($$$$) { |
499 | sub cookie_jar_extract($$$$) { |
484 | my ($jar, $scheme, $host, $path) = @_; |
500 | my ($jar, $scheme, $host, $path) = @_; |
485 | |
501 | |
486 | %$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]/; |
487 | |
506 | |
488 | my @cookies; |
507 | my @cookies; |
489 | |
508 | |
490 | while (my ($chost, $paths) = each %$jar) { |
509 | while (my ($chost, $paths) = each %$jar) { |
491 | next unless ref $paths; |
510 | next unless ref $paths; |
492 | |
511 | |
493 | if ($chost =~ /^\./) { |
512 | # exact match or suffix including . match |
494 | next unless $chost eq substr $host, -length $chost; |
513 | $chost eq $host or ".$chost" eq substr $host, -1 - length $chost |
495 | } elsif ($chost =~ /\./) { |
|
|
496 | next unless $chost eq $host; |
|
|
497 | } else { |
|
|
498 | next; |
514 | or next; |
499 | } |
|
|
500 | |
515 | |
501 | while (my ($cpath, $cookies) = each %$paths) { |
516 | while (my ($cpath, $cookies) = each %$paths) { |
502 | next unless $cpath eq substr $path, 0, length $cpath; |
517 | next unless $cpath eq substr $path, 0, length $cpath; |
503 | |
518 | |
504 | while (my ($cookie, $kv) = each %$cookies) { |
519 | while (my ($cookie, $kv) = each %$cookies) { |
… | |
… | |
525 | } |
540 | } |
526 | |
541 | |
527 | # parse set_cookie header into jar |
542 | # parse set_cookie header into jar |
528 | sub cookie_jar_set_cookie($$$$) { |
543 | sub cookie_jar_set_cookie($$$$) { |
529 | my ($jar, $set_cookie, $host, $date) = @_; |
544 | my ($jar, $set_cookie, $host, $date) = @_; |
|
|
545 | |
|
|
546 | %$jar = () if $jar->{version} != 2; |
530 | |
547 | |
531 | my $anow = int AE::now; |
548 | my $anow = int AE::now; |
532 | my $snow; # server-now |
549 | my $snow; # server-now |
533 | |
550 | |
534 | for ($set_cookie) { |
551 | for ($set_cookie) { |
… | |
… | |
580 | |
597 | |
581 | my $cdom; |
598 | my $cdom; |
582 | my $cpath = (delete $kv{path}) || "/"; |
599 | my $cpath = (delete $kv{path}) || "/"; |
583 | |
600 | |
584 | if (exists $kv{domain}) { |
601 | if (exists $kv{domain}) { |
585 | $cdom = delete $kv{domain}; |
602 | $cdom = $kv{domain}; |
586 | |
603 | |
587 | $cdom =~ s/^\.?/./; # make sure it starts with a "." |
604 | $cdom =~ s/^\.?/./; # make sure it starts with a "." |
588 | |
605 | |
589 | next if $cdom =~ /\.$/; |
606 | next if $cdom =~ /\.$/; |
590 | |
607 | |
591 | # this is not rfc-like and not netscape-like. go figure. |
608 | # this is not rfc-like and not netscape-like. go figure. |
592 | my $ndots = $cdom =~ y/.//; |
609 | my $ndots = $cdom =~ y/.//; |
593 | next if $ndots < ($cdom =~ /\.[^.][^.]\.[^.][^.]$/ ? 3 : 2); |
610 | next if $ndots < ($cdom =~ /\.[^.][^.]\.[^.][^.]$/ ? 3 : 2); |
|
|
611 | |
|
|
612 | $cdom = substr $cdom, 1; # remove initial . |
594 | } else { |
613 | } else { |
595 | $cdom = $host; |
614 | $cdom = $host; |
596 | } |
615 | } |
597 | |
616 | |
598 | # store it |
617 | # store it |
599 | $jar->{version} = 1; |
618 | $jar->{version} = 2; |
600 | $jar->{lc $cdom}{$cpath}{$name} = \%kv; |
619 | $jar->{lc $cdom}{$cpath}{$name} = \%kv; |
601 | |
620 | |
602 | redo if /\G\s*,/gc; |
621 | redo if /\G\s*,/gc; |
603 | } |
622 | } |
604 | } |
623 | } |
… | |
… | |
770 | |
789 | |
771 | my $uport = $uscheme eq "http" ? 80 |
790 | my $uport = $uscheme eq "http" ? 80 |
772 | : $uscheme eq "https" ? 443 |
791 | : $uscheme eq "https" ? 443 |
773 | : return $cb->(undef, { @pseudo, Status => 599, Reason => "Only http and https URL schemes supported" }); |
792 | : return $cb->(undef, { @pseudo, Status => 599, Reason => "Only http and https URL schemes supported" }); |
774 | |
793 | |
775 | $uauthority =~ /^(?: .*\@ )? ([^\@:]+) (?: : (\d+) )?$/x |
794 | $uauthority =~ /^(?: .*\@ )? ([^\@]+?) (?: : (\d+) )?$/x |
776 | or return $cb->(undef, { @pseudo, Status => 599, Reason => "Unparsable URL" }); |
795 | or return $cb->(undef, { @pseudo, Status => 599, Reason => "Unparsable URL" }); |
777 | |
796 | |
778 | my $uhost = lc $1; |
797 | my $uhost = lc $1; |
779 | $uport = $2 if defined $2; |
798 | $uport = $2 if defined $2; |
780 | |
799 | |
… | |
… | |
892 | # we give our best and fall back to URI if available. |
911 | # we give our best and fall back to URI if available. |
893 | if (exists $hdr{location}) { |
912 | if (exists $hdr{location}) { |
894 | my $loc = $hdr{location}; |
913 | my $loc = $hdr{location}; |
895 | |
914 | |
896 | if ($loc =~ m%^//%) { # // |
915 | if ($loc =~ m%^//%) { # // |
897 | $loc = "$rscheme:$loc"; |
916 | $loc = "$uscheme:$loc"; |
898 | |
917 | |
899 | } elsif ($loc eq "") { |
918 | } elsif ($loc eq "") { |
900 | $loc = $url; |
919 | $loc = $url; |
901 | |
920 | |
902 | } elsif ($loc !~ /^(?: $ | [^:\/?\#]+ : )/x) { # anything "simple" |
921 | } elsif ($loc !~ /^(?: $ | [^:\/?\#]+ : )/x) { # anything "simple" |
903 | $loc =~ s/^\.\/+//; |
922 | $loc =~ s/^\.\/+//; |
904 | |
923 | |
905 | if ($loc !~ m%^[.?#]%) { |
924 | if ($loc !~ m%^[.?#]%) { |
906 | my $prefix = "$rscheme://$uhost:$uport"; |
925 | my $prefix = "$uscheme://$uauthority"; |
907 | |
926 | |
908 | unless ($loc =~ s/^\///) { |
927 | unless ($loc =~ s/^\///) { |
909 | $prefix .= $upath; |
928 | $prefix .= $upath; |
910 | $prefix =~ s/\/[^\/]*$//; |
929 | $prefix =~ s/\/[^\/]*$//; |
911 | } |
930 | } |
… | |
… | |
934 | # industry standard is to redirect POST as GET for |
953 | # industry standard is to redirect POST as GET for |
935 | # 301, 302 and 303, in contrast to HTTP/1.0 and 1.1. |
954 | # 301, 302 and 303, in contrast to HTTP/1.0 and 1.1. |
936 | # also, the UA should ask the user for 301 and 307 and POST, |
955 | # also, the UA should ask the user for 301 and 307 and POST, |
937 | # industry standard seems to be to simply follow. |
956 | # industry standard seems to be to simply follow. |
938 | # we go with the industry standard. 308 is defined |
957 | # we go with the industry standard. 308 is defined |
939 | # by rfc7238 |
958 | # by rfc7538 |
940 | if ($status == 301 or $status == 302 or $status == 303) { |
959 | if ($status == 301 or $status == 302 or $status == 303) { |
|
|
960 | $redirect = 1; |
941 | # HTTP/1.1 is unclear on how to mutate the method |
961 | # HTTP/1.1 is unclear on how to mutate the method |
942 | $method = "GET" unless $method eq "HEAD"; |
962 | unless ($method eq "HEAD") { |
943 | $redirect = 1; |
963 | $method = "GET"; |
|
|
964 | delete $arg{body}; |
|
|
965 | } |
944 | } elsif ($status == 307 or $status == 308) { |
966 | } elsif ($status == 307 or $status == 308) { |
945 | $redirect = 1; |
967 | $redirect = 1; |
946 | } |
968 | } |
947 | } |
969 | } |
948 | |
970 | |
… | |
… | |
1025 | $finish->(delete $state{handle}); |
1047 | $finish->(delete $state{handle}); |
1026 | |
1048 | |
1027 | } elsif ($chunked) { |
1049 | } elsif ($chunked) { |
1028 | my $cl = 0; |
1050 | my $cl = 0; |
1029 | my $body = ""; |
1051 | my $body = ""; |
1030 | my $on_body = $arg{on_body} || sub { $body .= shift; 1 }; |
1052 | my $on_body = (!$redirect && $arg{on_body}) || sub { $body .= shift; 1 }; |
1031 | |
1053 | |
1032 | $state{read_chunk} = sub { |
1054 | $state{read_chunk} = sub { |
1033 | $_[1] =~ /^([0-9a-fA-F]+)/ |
1055 | $_[1] =~ /^([0-9a-fA-F]+)/ |
1034 | or return $finish->(undef, $ae_error => "Garbled chunked transfer encoding"); |
1056 | or return $finish->(undef, $ae_error => "Garbled chunked transfer encoding"); |
1035 | |
1057 | |
… | |
… | |
1068 | } |
1090 | } |
1069 | }; |
1091 | }; |
1070 | |
1092 | |
1071 | $_[0]->push_read (line => $state{read_chunk}); |
1093 | $_[0]->push_read (line => $state{read_chunk}); |
1072 | |
1094 | |
1073 | } elsif ($arg{on_body}) { |
1095 | } elsif (!$redirect && $arg{on_body}) { |
1074 | if (defined $len) { |
1096 | if (defined $len) { |
1075 | $_[0]->on_read (sub { |
1097 | $_[0]->on_read (sub { |
1076 | $len -= length $_[0]{rbuf}; |
1098 | $len -= length $_[0]{rbuf}; |
1077 | |
1099 | |
1078 | $arg{on_body}(delete $_[0]{rbuf}, \%hdr) |
1100 | $arg{on_body}(delete $_[0]{rbuf}, \%hdr) |
… | |
… | |
1117 | _destroy_state %state; |
1139 | _destroy_state %state; |
1118 | |
1140 | |
1119 | %state = (); |
1141 | %state = (); |
1120 | $state{recurse} = |
1142 | $state{recurse} = |
1121 | http_request ( |
1143 | http_request ( |
1122 | $method => $url, |
1144 | $method => $url, |
1123 | %arg, |
1145 | %arg, |
1124 | recurse => $recurse - 1, |
1146 | recurse => $recurse - 1, |
1125 | keepalive => 0, |
1147 | persistent => 0, |
1126 | sub { |
1148 | sub { |
1127 | %state = (); |
1149 | %state = (); |
1128 | &$cb |
1150 | &$cb |
1129 | } |
1151 | } |
1130 | ); |
1152 | ); |
… | |
… | |
1176 | |
1198 | |
1177 | # now handle proxy-CONNECT method |
1199 | # now handle proxy-CONNECT method |
1178 | if ($proxy && $uscheme eq "https") { |
1200 | if ($proxy && $uscheme eq "https") { |
1179 | # oh dear, we have to wrap it into a connect request |
1201 | # oh dear, we have to wrap it into a connect request |
1180 | |
1202 | |
|
|
1203 | my $auth = exists $hdr{"proxy-authorization"} |
|
|
1204 | ? "proxy-authorization: " . (delete $hdr{"proxy-authorization"}) . "\015\012" |
|
|
1205 | : ""; |
|
|
1206 | |
1181 | # maybe re-use $uauthority with patched port? |
1207 | # maybe re-use $uauthority with patched port? |
1182 | $state{handle}->push_write ("CONNECT $uhost:$uport HTTP/1.0\015\012\015\012"); |
1208 | $state{handle}->push_write ("CONNECT $uhost:$uport HTTP/1.0\015\012$auth\015\012"); |
1183 | $state{handle}->push_read (line => $qr_nlnl, sub { |
1209 | $state{handle}->push_read (line => $qr_nlnl, sub { |
1184 | $_[1] =~ /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) (?: \s+ ([^\015\012]*) )?/ix |
1210 | $_[1] =~ /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) (?: \s+ ([^\015\012]*) )?/ix |
1185 | or return _error %state, $cb, { @pseudo, Status => 599, Reason => "Invalid proxy connect response ($_[1])" }; |
1211 | or return _error %state, $cb, { @pseudo, Status => 599, Reason => "Invalid proxy connect response ($_[1])" }; |
1186 | |
1212 | |
1187 | if ($2 == 200) { |
1213 | if ($2 == 200) { |
… | |
… | |
1190 | } else { |
1216 | } else { |
1191 | _error %state, $cb, { @pseudo, Status => $2, Reason => $3 }; |
1217 | _error %state, $cb, { @pseudo, Status => $2, Reason => $3 }; |
1192 | } |
1218 | } |
1193 | }); |
1219 | }); |
1194 | } else { |
1220 | } else { |
|
|
1221 | delete $hdr{"proxy-authorization"} unless $proxy; |
|
|
1222 | |
1195 | $handle_actual_request->(); |
1223 | $handle_actual_request->(); |
1196 | } |
1224 | } |
1197 | }; |
1225 | }; |
1198 | |
1226 | |
1199 | _get_slot $uhost, sub { |
1227 | _get_slot $uhost, sub { |
… | |
… | |
1205 | # 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). |
1206 | if ($persistent && $KA_CACHE{$ka_key}) { |
1234 | if ($persistent && $KA_CACHE{$ka_key}) { |
1207 | $was_persistent = 1; |
1235 | $was_persistent = 1; |
1208 | |
1236 | |
1209 | $state{handle} = ka_fetch $ka_key; |
1237 | $state{handle} = ka_fetch $ka_key; |
1210 | $state{handle}->destroyed |
1238 | # $state{handle}->destroyed |
1211 | 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# |
1212 | $prepare_handle->(); |
1240 | $prepare_handle->(); |
1213 | $state{handle}->destroyed |
1241 | # $state{handle}->destroyed |
1214 | 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# |
1215 | $handle_actual_request->(); |
1243 | $handle_actual_request->(); |
1216 | |
1244 | |
1217 | } else { |
1245 | } else { |
1218 | my $tcp_connect = $arg{tcp_connect} |
1246 | my $tcp_connect = $arg{tcp_connect} |
1219 | || do { require AnyEvent::Socket; \&AnyEvent::Socket::tcp_connect }; |
1247 | || do { require AnyEvent::Socket; \&AnyEvent::Socket::tcp_connect }; |
… | |
… | |
1277 | save cookies to disk, and you should call this function after loading them |
1305 | save cookies to disk, and you should call this function after loading them |
1278 | again. If you have a long-running program you can additionally call this |
1306 | again. If you have a long-running program you can additionally call this |
1279 | function from time to time. |
1307 | function from time to time. |
1280 | |
1308 | |
1281 | 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 |
1282 | module. It's format is subject to change, but currently it is like this: |
1310 | module. Its format is subject to change, but currently it is as follows: |
1283 | |
1311 | |
1284 | 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 |
1285 | emptied. All other keys are hostnames or IP addresses pointing to |
1313 | cleared. All other keys are hostnames or IP addresses pointing to |
1286 | hash-references. The key for these inner hash references is the |
1314 | hash-references. The key for these inner hash references is the |
1287 | 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 |
1288 | 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 |
1289 | 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 |
1290 | 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>, |
… | |
… | |
1294 | |
1322 | |
1295 | 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 |
1296 | chance of understanding the above paragraph: |
1324 | chance of understanding the above paragraph: |
1297 | |
1325 | |
1298 | { |
1326 | { |
1299 | version => 1, |
1327 | version => 2, |
1300 | "10.0.0.1" => { |
1328 | "10.0.0.1" => { |
1301 | "/" => { |
1329 | "/" => { |
1302 | "mythweb_id" => { |
1330 | "mythweb_id" => { |
1303 | _expires => 1293917923, |
1331 | _expires => 1293917923, |
1304 | value => "ooRung9dThee3ooyXooM1Ohm", |
1332 | value => "ooRung9dThee3ooyXooM1Ohm", |
… | |
… | |
1332 | C<Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)>). |
1360 | C<Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)>). |
1333 | |
1361 | |
1334 | =item $AnyEvent::HTTP::MAX_PER_HOST |
1362 | =item $AnyEvent::HTTP::MAX_PER_HOST |
1335 | |
1363 | |
1336 | The maximum number of concurrent connections to the same host (identified |
1364 | The maximum number of concurrent connections to the same host (identified |
1337 | by the hostname). If the limit is exceeded, then the additional requests |
1365 | by the hostname). If the limit is exceeded, then additional requests |
1338 | are queued until previous connections are closed. Both persistent and |
1366 | are queued until previous connections are closed. Both persistent and |
1339 | non-persistent connections are counted in this limit. |
1367 | non-persistent connections are counted in this limit. |
1340 | |
1368 | |
1341 | The default value for this is C<4>, and it is highly advisable to not |
1369 | The default value for this is C<4>, and it is highly advisable to not |
1342 | increase it much. |
1370 | increase it much. |
… | |
… | |
1449 | or die "$file: $!"; |
1477 | or die "$file: $!"; |
1450 | |
1478 | |
1451 | my %hdr; |
1479 | my %hdr; |
1452 | my $ofs = 0; |
1480 | my $ofs = 0; |
1453 | |
1481 | |
1454 | warn stat $fh; |
|
|
1455 | warn -s _; |
|
|
1456 | if (stat $fh and -s _) { |
1482 | if (stat $fh and -s _) { |
1457 | $ofs = -s _; |
1483 | $ofs = -s _; |
1458 | warn "-s is ", $ofs; |
1484 | warn "-s is ", $ofs; |
1459 | $hdr{"if-unmodified-since"} = AnyEvent::HTTP::format_date +(stat _)[9]; |
1485 | $hdr{"if-unmodified-since"} = AnyEvent::HTTP::format_date +(stat _)[9]; |
1460 | $hdr{"range"} = "bytes=$ofs-"; |
1486 | $hdr{"range"} = "bytes=$ofs-"; |
… | |
… | |
1488 | my (undef, $hdr) = @_; |
1514 | my (undef, $hdr) = @_; |
1489 | |
1515 | |
1490 | my $status = $hdr->{Status}; |
1516 | my $status = $hdr->{Status}; |
1491 | |
1517 | |
1492 | if (my $time = AnyEvent::HTTP::parse_date $hdr->{"last-modified"}) { |
1518 | if (my $time = AnyEvent::HTTP::parse_date $hdr->{"last-modified"}) { |
1493 | utime $fh, $time, $time; |
1519 | utime $time, $time, $fh; |
1494 | } |
1520 | } |
1495 | |
1521 | |
1496 | if ($status == 200 || $status == 206 || $status == 416) { |
1522 | if ($status == 200 || $status == 206 || $status == 416) { |
1497 | # download ok || resume ok || file already fully downloaded |
1523 | # download ok || resume ok || file already fully downloaded |
1498 | $cb->(1, $hdr); |
1524 | $cb->(1, $hdr); |