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.117 by root, Mon Sep 9 21:41:43 2013 UTC vs.
Revision 1.140 by root, Wed Mar 6 19:29:18 2024 UTC

4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use AnyEvent::HTTP; 7 use AnyEvent::HTTP;
8 8
9 http_get "http://www.nethype.de/", sub { print $_[1] }; 9 http_get "http://www.nethype.de/", sub {
10 my ($body, $hdr) = @_;
11 print "$hdr->{URL} Status: $hdr->{Status}\n";
12 print $body;
13 };
10 14
11 # ... do something else here 15 # ... do something else here
12 16
13=head1 DESCRIPTION 17=head1 DESCRIPTION
14 18
46use AnyEvent::Util (); 50use AnyEvent::Util ();
47use AnyEvent::Handle (); 51use AnyEvent::Handle ();
48 52
49use base Exporter::; 53use base Exporter::;
50 54
51our $VERSION = '2.15'; 55our $VERSION = 2.25;
52 56
53our @EXPORT = qw(http_get http_post http_head http_request); 57our @EXPORT = qw(http_get http_post http_head http_request);
54 58
55our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; 59our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)";
56our $MAX_RECURSE = 10; 60our $MAX_RECURSE = 10;
157=item recurse => $count (default: $MAX_RECURSE) 161=item recurse => $count (default: $MAX_RECURSE)
158 162
159Whether to recurse requests or not, e.g. on redirects, authentication and 163Whether to recurse requests or not, e.g. on redirects, authentication and
160other retries and so on, and how often to do so. 164other retries and so on, and how often to do so.
161 165
166Only redirects to http and https URLs are supported. While most common
167redirection forms are handled entirely within this module, some require
168the use of the optional L<URI> module. If it is required but missing, then
169the request will fail with an error.
170
162=item headers => hashref 171=item headers => hashref
163 172
164The request headers to use. Currently, C<http_request> may provide its own 173The request headers to use. Currently, C<http_request> may provide its own
165C<Host:>, C<Content-Length:>, C<Connection:> and C<Cookie:> headers and 174C<Host:>, C<Content-Length:>, C<Connection:> and C<Cookie:> headers and
166will provide defaults at least for C<TE:>, C<Referer:> and C<User-Agent:> 175will provide defaults at least for C<TE:>, C<Referer:> and C<User-Agent:>
189 198
190C<$scheme> must be either missing or must be C<http> for HTTP. 199C<$scheme> must be either missing or must be C<http> for HTTP.
191 200
192If not specified, then the default proxy is used (see 201If not specified, then the default proxy is used (see
193C<AnyEvent::HTTP::set_proxy>). 202C<AnyEvent::HTTP::set_proxy>).
203
204Currently, if your proxy requires authorization, you have to specify an
205appropriate "Proxy-Authorization" header in every request.
206
207Note that this module will prefer an existing persistent connection,
208even if that connection was made using another proxy. If you need to
209ensure that a new connection is made in this case, you can either force
210C<persistent> to false or e.g. use the proxy address in your C<sessionid>.
194 211
195=item body => $string 212=item body => $string
196 213
197The request body, usually empty. Will be sent as-is (future versions of 214The request body, usually empty. Will be sent as-is (future versions of
198this module might offer more options). 215this module might offer more options).
231The default for this option is C<low>, which could be interpreted as "give 248The default for this option is C<low>, which could be interpreted as "give
232me the page, no matter what". 249me the page, no matter what".
233 250
234See also the C<sessionid> parameter. 251See also the C<sessionid> parameter.
235 252
236=item session => $string 253=item sessionid => $string
237 254
238The module might reuse connections to the same host internally. Sometimes 255The module might reuse connections to the same host internally (regardless
239(e.g. when using TLS), you do not want to reuse connections from other 256of other settings, such as C<tcp_connect> or C<proxy>). Sometimes (e.g.
257when using TLS or a specfic proxy), you do not want to reuse connections
240sessions. This can be achieved by setting this parameter to some unique 258from other sessions. This can be achieved by setting this parameter to
241ID (such as the address of an object storing your state data, or the TLS 259some unique ID (such as the address of an object storing your state data
242context) - only connections using the same unique ID will be reused. 260or the TLS context, or the proxy IP) - only connections using the same
261unique ID will be reused.
243 262
244=item on_prepare => $callback->($fh) 263=item on_prepare => $callback->($fh)
245 264
246In rare cases you need to "tune" the socket before it is used to 265In rare cases you need to "tune" the socket before it is used to
247connect (for example, to bind it on a given IP address). This parameter 266connect (for example, to bind it on a given IP address). This parameter
255In even rarer cases you want total control over how AnyEvent::HTTP 274In even rarer cases you want total control over how AnyEvent::HTTP
256establishes connections. Normally it uses L<AnyEvent::Socket::tcp_connect> 275establishes connections. Normally it uses L<AnyEvent::Socket::tcp_connect>
257to do this, but you can provide your own C<tcp_connect> function - 276to do this, but you can provide your own C<tcp_connect> function -
258obviously, it has to follow the same calling conventions, except that it 277obviously, it has to follow the same calling conventions, except that it
259may always return a connection guard object. 278may always return a connection guard object.
279
280The connections made by this hook will be treated as equivalent to
281connections made the built-in way, specifically, they will be put into
282and taken from the persistent connection cache. If your C<$tcp_connect>
283function is incompatible with this kind of re-use, consider switching off
284C<persistent> connections and/or providing a C<sessionid> identifier.
260 285
261There are probably lots of weird uses for this function, starting from 286There are probably lots of weird uses for this function, starting from
262tracing the hosts C<http_request> actually tries to connect, to (inexact 287tracing the hosts C<http_request> actually tries to connect, to (inexact
263but fast) host => IP address caching or even socks protocol support. 288but fast) host => IP address caching or even socks protocol support.
264 289
334=item persistent => $boolean 359=item persistent => $boolean
335 360
336Try to create/reuse a persistent connection. When this flag is set 361Try to create/reuse a persistent connection. When this flag is set
337(default: true for idempotent requests, false for all others), then 362(default: true for idempotent requests, false for all others), then
338C<http_request> tries to re-use an existing (previously-created) 363C<http_request> tries to re-use an existing (previously-created)
339persistent connection to the host and, failing that, tries to create a new 364persistent connection to same host (i.e. identical URL scheme, hostname,
340one. 365port and sessionid) and, failing that, tries to create a new one.
341 366
342Requests failing in certain ways will be automatically retried once, which 367Requests failing in certain ways will be automatically retried once, which
343is dangerous for non-idempotent requests, which is why it defaults to off 368is dangerous for non-idempotent requests, which is why it defaults to off
344for them. The reason for this is because the bozos who designed HTTP/1.1 369for them. The reason for this is because the bozos who designed HTTP/1.1
345made it impossible to distinguish between a fatal error and a normal 370made it impossible to distinguish between a fatal error and a normal
346connection timeout, so you never know whether there was a problem with 371connection timeout, so you never know whether there was a problem with
347your request or not. 372your request or not.
348 373
349When reusing an existent connection, many parameters (such as TLS context) 374When reusing an existent connection, many parameters (such as TLS context)
350will be ignored. See the C<session> parameter for a workaround. 375will be ignored. See the C<sessionid> parameter for a workaround.
351 376
352=item keepalive => $boolean 377=item keepalive => $boolean
353 378
354Only used when C<persistent> is also true. This parameter decides whether 379Only used when C<persistent> is also true. This parameter decides whether
355C<http_request> tries to handshake a HTTP/1.0-style keep-alive connection 380C<http_request> tries to handshake a HTTP/1.0-style keep-alive connection
446 471
447# expire cookies 472# expire cookies
448sub cookie_jar_expire($;$) { 473sub cookie_jar_expire($;$) {
449 my ($jar, $session_end) = @_; 474 my ($jar, $session_end) = @_;
450 475
451 %$jar = () if $jar->{version} != 1; 476 %$jar = () if $jar->{version} != 2;
452 477
453 my $anow = AE::now; 478 my $anow = AE::now;
454 479
455 while (my ($chost, $paths) = each %$jar) { 480 while (my ($chost, $paths) = each %$jar) {
456 next unless ref $paths; 481 next unless ref $paths;
476 501
477# extract cookies from jar 502# extract cookies from jar
478sub cookie_jar_extract($$$$) { 503sub cookie_jar_extract($$$$) {
479 my ($jar, $scheme, $host, $path) = @_; 504 my ($jar, $scheme, $host, $path) = @_;
480 505
481 %$jar = () if $jar->{version} != 1; 506 %$jar = () if $jar->{version} != 2;
507
508 $host = AnyEvent::Util::idn_to_ascii $host
509 if $host =~ /[^\x00-\x7f]/;
482 510
483 my @cookies; 511 my @cookies;
484 512
485 while (my ($chost, $paths) = each %$jar) { 513 while (my ($chost, $paths) = each %$jar) {
486 next unless ref $paths; 514 next unless ref $paths;
487 515
488 if ($chost =~ /^\./) { 516 # exact match or suffix including . match
489 next unless $chost eq substr $host, -length $chost; 517 $chost eq $host or ".$chost" eq substr $host, -1 - length $chost
490 } elsif ($chost =~ /\./) {
491 next unless $chost eq $host;
492 } else {
493 next; 518 or next;
494 }
495 519
496 while (my ($cpath, $cookies) = each %$paths) { 520 while (my ($cpath, $cookies) = each %$paths) {
497 next unless $cpath eq substr $path, 0, length $cpath; 521 next unless $cpath eq substr $path, 0, length $cpath;
498 522
499 while (my ($cookie, $kv) = each %$cookies) { 523 while (my ($cookie, $kv) = each %$cookies) {
520} 544}
521 545
522# parse set_cookie header into jar 546# parse set_cookie header into jar
523sub cookie_jar_set_cookie($$$$) { 547sub cookie_jar_set_cookie($$$$) {
524 my ($jar, $set_cookie, $host, $date) = @_; 548 my ($jar, $set_cookie, $host, $date) = @_;
549
550 %$jar = () if $jar->{version} != 2;
525 551
526 my $anow = int AE::now; 552 my $anow = int AE::now;
527 my $snow; # server-now 553 my $snow; # server-now
528 554
529 for ($set_cookie) { 555 for ($set_cookie) {
575 601
576 my $cdom; 602 my $cdom;
577 my $cpath = (delete $kv{path}) || "/"; 603 my $cpath = (delete $kv{path}) || "/";
578 604
579 if (exists $kv{domain}) { 605 if (exists $kv{domain}) {
580 $cdom = delete $kv{domain}; 606 $cdom = $kv{domain};
581 607
582 $cdom =~ s/^\.?/./; # make sure it starts with a "." 608 $cdom =~ s/^\.?/./; # make sure it starts with a "."
583 609
584 next if $cdom =~ /\.$/; 610 next if $cdom =~ /\.$/;
585 611
586 # this is not rfc-like and not netscape-like. go figure. 612 # this is not rfc-like and not netscape-like. go figure.
587 my $ndots = $cdom =~ y/.//; 613 my $ndots = $cdom =~ y/.//;
588 next if $ndots < ($cdom =~ /\.[^.][^.]\.[^.][^.]$/ ? 3 : 2); 614 next if $ndots < ($cdom =~ /\.[^.][^.]\.[^.][^.]$/ ? 3 : 2);
615
616 $cdom = substr $cdom, 1; # remove initial .
589 } else { 617 } else {
590 $cdom = $host; 618 $cdom = $host;
591 } 619 }
592 620
593 # store it 621 # store it
594 $jar->{version} = 1; 622 $jar->{version} = 2;
595 $jar->{lc $cdom}{$cpath}{$name} = \%kv; 623 $jar->{lc $cdom}{$cpath}{$name} = \%kv;
596 624
597 redo if /\G\s*,/gc; 625 redo if /\G\s*,/gc;
598 } 626 }
599} 627}
692} 720}
693 721
694our %IDEMPOTENT = ( 722our %IDEMPOTENT = (
695 DELETE => 1, 723 DELETE => 1,
696 GET => 1, 724 GET => 1,
725 QUERY => 1,
697 HEAD => 1, 726 HEAD => 1,
698 OPTIONS => 1, 727 OPTIONS => 1,
699 PUT => 1, 728 PUT => 1,
700 TRACE => 1, 729 TRACE => 1,
701 730
713 MKCOL => 1, 742 MKCOL => 1,
714 MKREDIRECTREF => 1, 743 MKREDIRECTREF => 1,
715 MKWORKSPACE => 1, 744 MKWORKSPACE => 1,
716 MOVE => 1, 745 MOVE => 1,
717 ORDERPATCH => 1, 746 ORDERPATCH => 1,
747 PRI => 1,
718 PROPFIND => 1, 748 PROPFIND => 1,
719 PROPPATCH => 1, 749 PROPPATCH => 1,
720 REBIND => 1, 750 REBIND => 1,
721 REPORT => 1, 751 REPORT => 1,
722 SEARCH => 1, 752 SEARCH => 1,
765 795
766 my $uport = $uscheme eq "http" ? 80 796 my $uport = $uscheme eq "http" ? 80
767 : $uscheme eq "https" ? 443 797 : $uscheme eq "https" ? 443
768 : return $cb->(undef, { @pseudo, Status => 599, Reason => "Only http and https URL schemes supported" }); 798 : return $cb->(undef, { @pseudo, Status => 599, Reason => "Only http and https URL schemes supported" });
769 799
770 $uauthority =~ /^(?: .*\@ )? ([^\@:]+) (?: : (\d+) )?$/x 800 $uauthority =~ /^(?: .*\@ )? ([^\@]+?) (?: : (\d+) )?$/x
771 or return $cb->(undef, { @pseudo, Status => 599, Reason => "Unparsable URL" }); 801 or return $cb->(undef, { @pseudo, Status => 599, Reason => "Unparsable URL" });
772 802
773 my $uhost = lc $1; 803 my $uhost = lc $1;
774 $uport = $2 if defined $2; 804 $uport = $2 if defined $2;
775 805
821 my $was_persistent; # true if this is actually a recycled connection 851 my $was_persistent; # true if this is actually a recycled connection
822 852
823 # the key to use in the keepalive cache 853 # the key to use in the keepalive cache
824 my $ka_key = "$uscheme\x00$uhost\x00$uport\x00$arg{sessionid}"; 854 my $ka_key = "$uscheme\x00$uhost\x00$uport\x00$arg{sessionid}";
825 855
826 $hdr{connection} = ($persistent ? $keepalive ? "keep-alive " : "" : "close ") . "Te"; #1.1 856 $hdr{connection} = ($persistent ? $keepalive ? "keep-alive, " : "" : "close, ") . "Te"; #1.1
827 $hdr{te} = "trailers" unless exists $hdr{te}; #1.1 857 $hdr{te} = "trailers" unless exists $hdr{te}; #1.1
828 858
829 my %state = (connect_guard => 1); 859 my %state = (connect_guard => 1);
830 860
831 my $ae_error = 595; # connecting 861 my $ae_error = 595; # connecting
841 # send request 871 # send request
842 $hdl->push_write ( 872 $hdl->push_write (
843 "$method $rpath HTTP/1.1\015\012" 873 "$method $rpath HTTP/1.1\015\012"
844 . (join "", map "\u$_: $hdr{$_}\015\012", grep defined $hdr{$_}, keys %hdr) 874 . (join "", map "\u$_: $hdr{$_}\015\012", grep defined $hdr{$_}, keys %hdr)
845 . "\015\012" 875 . "\015\012"
846 . (delete $arg{body}) 876 . $arg{body}
847 ); 877 );
848 878
849 # return if error occurred during push_write() 879 # return if error occurred during push_write()
850 return unless %state; 880 return unless %state;
851 881
881 911
882 %hdr = (%$hdr, @pseudo); 912 %hdr = (%$hdr, @pseudo);
883 } 913 }
884 914
885 # redirect handling 915 # redirect handling
886 # microsoft and other shitheads don't give a shit for following standards, 916 # relative uri handling forced by microsoft and other shitheads.
887 # try to support some common forms of broken Location headers. 917 # we give our best and fall back to URI if available.
888 if ($hdr{location} !~ /^(?: $ | [^:\/?\#]+ : )/x) { 918 if (exists $hdr{location}) {
919 my $loc = $hdr{location};
920
921 if ($loc =~ m%^//%) { # //
922 $loc = "$uscheme:$loc";
923
924 } elsif ($loc eq "") {
925 $loc = $url;
926
927 } elsif ($loc !~ /^(?: $ | [^:\/?\#]+ : )/x) { # anything "simple"
889 $hdr{location} =~ s/^\.\/+//; 928 $loc =~ s/^\.\/+//;
890 929
891 my $url = "$rscheme://$uhost:$uport"; 930 if ($loc !~ m%^[.?#]%) {
931 my $prefix = "$uscheme://$uauthority";
892 932
893 unless ($hdr{location} =~ s/^\///) { 933 unless ($loc =~ s/^\///) {
894 $url .= $upath; 934 $prefix .= $upath;
895 $url =~ s/\/[^\/]*$//; 935 $prefix =~ s/\/[^\/]*$//;
936 }
937
938 $loc = "$prefix/$loc";
939
940 } elsif (eval { require URI }) { # uri
941 $loc = URI->new_abs ($loc, $url)->as_string;
942
943 } else {
944 return _error %state, $cb, { @pseudo, Status => 599, Reason => "Cannot parse Location (URI module missing)" };
945 #$hdr{Status} = 599;
946 #$hdr{Reason} = "Unparsable Redirect (URI module missing)";
947 #$recurse = 0;
948 }
896 } 949 }
897 950
898 $hdr{location} = "$url/$hdr{location}"; 951 $hdr{location} = $loc;
899 } 952 }
900 953
901 my $redirect; 954 my $redirect;
902 955
903 if ($recurse) { 956 if ($recurse) {
905 958
906 # industry standard is to redirect POST as GET for 959 # industry standard is to redirect POST as GET for
907 # 301, 302 and 303, in contrast to HTTP/1.0 and 1.1. 960 # 301, 302 and 303, in contrast to HTTP/1.0 and 1.1.
908 # also, the UA should ask the user for 301 and 307 and POST, 961 # also, the UA should ask the user for 301 and 307 and POST,
909 # industry standard seems to be to simply follow. 962 # industry standard seems to be to simply follow.
910 # we go with the industry standard. 963 # we go with the industry standard. 308 is defined
964 # by rfc7538
911 if ($status == 301 or $status == 302 or $status == 303) { 965 if ($status == 301 or $status == 302 or $status == 303) {
966 $redirect = 1;
912 # HTTP/1.1 is unclear on how to mutate the method 967 # HTTP/1.1 is unclear on how to mutate the method
913 $method = "GET" unless $method eq "HEAD"; 968 unless ($method eq "HEAD") {
914 $redirect = 1; 969 $method = "GET";
970 delete $arg{body};
971 }
915 } elsif ($status == 307) { 972 } elsif ($status == 307 or $status == 308) {
916 $redirect = 1; 973 $redirect = 1;
917 } 974 }
918 } 975 }
919 976
920 my $finish = sub { # ($data, $err_status, $err_reason[, $persistent]) 977 my $finish = sub { # ($data, $err_status, $err_reason[, $persistent])
996 $finish->(delete $state{handle}); 1053 $finish->(delete $state{handle});
997 1054
998 } elsif ($chunked) { 1055 } elsif ($chunked) {
999 my $cl = 0; 1056 my $cl = 0;
1000 my $body = ""; 1057 my $body = "";
1001 my $on_body = $arg{on_body} || sub { $body .= shift; 1 }; 1058 my $on_body = (!$redirect && $arg{on_body}) || sub { $body .= shift; 1 };
1002 1059
1003 $state{read_chunk} = sub { 1060 $state{read_chunk} = sub {
1004 $_[1] =~ /^([0-9a-fA-F]+)/ 1061 $_[1] =~ /^([0-9a-fA-F]+)/
1005 or return $finish->(undef, $ae_error => "Garbled chunked transfer encoding"); 1062 or return $finish->(undef, $ae_error => "Garbled chunked transfer encoding");
1006 1063
1039 } 1096 }
1040 }; 1097 };
1041 1098
1042 $_[0]->push_read (line => $state{read_chunk}); 1099 $_[0]->push_read (line => $state{read_chunk});
1043 1100
1044 } elsif ($arg{on_body}) { 1101 } elsif (!$redirect && $arg{on_body}) {
1045 if (defined $len) { 1102 if (defined $len) {
1046 $_[0]->on_read (sub { 1103 $_[0]->on_read (sub {
1047 $len -= length $_[0]{rbuf}; 1104 $len -= length $_[0]{rbuf};
1048 1105
1049 $arg{on_body}(delete $_[0]{rbuf}, \%hdr) 1106 $arg{on_body}(delete $_[0]{rbuf}, \%hdr)
1088 _destroy_state %state; 1145 _destroy_state %state;
1089 1146
1090 %state = (); 1147 %state = ();
1091 $state{recurse} = 1148 $state{recurse} =
1092 http_request ( 1149 http_request (
1093 $method => $url, 1150 $method => $url,
1094 %arg, 1151 %arg,
1095 recurse => $recurse - 1, 1152 recurse => $recurse - 1,
1096 keepalive => 0, 1153 persistent => 0,
1097 sub { 1154 sub {
1098 %state = (); 1155 %state = ();
1099 &$cb 1156 &$cb
1100 } 1157 }
1101 ); 1158 );
1147 1204
1148 # now handle proxy-CONNECT method 1205 # now handle proxy-CONNECT method
1149 if ($proxy && $uscheme eq "https") { 1206 if ($proxy && $uscheme eq "https") {
1150 # oh dear, we have to wrap it into a connect request 1207 # oh dear, we have to wrap it into a connect request
1151 1208
1209 my $auth = exists $hdr{"proxy-authorization"}
1210 ? "proxy-authorization: " . (delete $hdr{"proxy-authorization"}) . "\015\012"
1211 : "";
1212
1152 # maybe re-use $uauthority with patched port? 1213 # maybe re-use $uauthority with patched port?
1153 $state{handle}->push_write ("CONNECT $uhost:$uport HTTP/1.0\015\012\015\012"); 1214 $state{handle}->push_write ("CONNECT $uhost:$uport HTTP/1.0\015\012$auth\015\012");
1154 $state{handle}->push_read (line => $qr_nlnl, sub { 1215 $state{handle}->push_read (line => $qr_nlnl, sub {
1155 $_[1] =~ /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) (?: \s+ ([^\015\012]*) )?/ix 1216 $_[1] =~ /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) (?: \s+ ([^\015\012]*) )?/ix
1156 or return _error %state, $cb, { @pseudo, Status => 599, Reason => "Invalid proxy connect response ($_[1])" }; 1217 or return _error %state, $cb, { @pseudo, Status => 599, Reason => "Invalid proxy connect response ($_[1])" };
1157 1218
1158 if ($2 == 200) { 1219 if ($2 == 200) {
1161 } else { 1222 } else {
1162 _error %state, $cb, { @pseudo, Status => $2, Reason => $3 }; 1223 _error %state, $cb, { @pseudo, Status => $2, Reason => $3 };
1163 } 1224 }
1164 }); 1225 });
1165 } else { 1226 } else {
1227 delete $hdr{"proxy-authorization"} unless $proxy;
1228
1166 $handle_actual_request->(); 1229 $handle_actual_request->();
1167 } 1230 }
1168 }; 1231 };
1169 1232
1170 _get_slot $uhost, sub { 1233 _get_slot $uhost, sub {
1176 # on a keepalive request (in theory, this should be a separate config option). 1239 # on a keepalive request (in theory, this should be a separate config option).
1177 if ($persistent && $KA_CACHE{$ka_key}) { 1240 if ($persistent && $KA_CACHE{$ka_key}) {
1178 $was_persistent = 1; 1241 $was_persistent = 1;
1179 1242
1180 $state{handle} = ka_fetch $ka_key; 1243 $state{handle} = ka_fetch $ka_key;
1181 $state{handle}->destroyed 1244# $state{handle}->destroyed
1182 and die "AnyEvent::HTTP: unexpectedly got a destructed handle (1), please report.";#d# 1245# and die "AnyEvent::HTTP: unexpectedly got a destructed handle (1), please report.";#d#
1183 $prepare_handle->(); 1246 $prepare_handle->();
1184 $state{handle}->destroyed 1247# $state{handle}->destroyed
1185 and die "AnyEvent::HTTP: unexpectedly got a destructed handle (2), please report.";#d# 1248# and die "AnyEvent::HTTP: unexpectedly got a destructed handle (2), please report.";#d#
1249 $rpath = $upath;
1186 $handle_actual_request->(); 1250 $handle_actual_request->();
1187 1251
1188 } else { 1252 } else {
1189 my $tcp_connect = $arg{tcp_connect} 1253 my $tcp_connect = $arg{tcp_connect}
1190 || do { require AnyEvent::Socket; \&AnyEvent::Socket::tcp_connect }; 1254 || do { require AnyEvent::Socket; \&AnyEvent::Socket::tcp_connect };
1248save cookies to disk, and you should call this function after loading them 1312save cookies to disk, and you should call this function after loading them
1249again. If you have a long-running program you can additionally call this 1313again. If you have a long-running program you can additionally call this
1250function from time to time. 1314function from time to time.
1251 1315
1252A cookie jar is initially an empty hash-reference that is managed by this 1316A cookie jar is initially an empty hash-reference that is managed by this
1253module. It's format is subject to change, but currently it is like this: 1317module. Its format is subject to change, but currently it is as follows:
1254 1318
1255The key C<version> has to contain C<1>, otherwise the hash gets 1319The key C<version> has to contain C<2>, otherwise the hash gets
1256emptied. All other keys are hostnames or IP addresses pointing to 1320cleared. All other keys are hostnames or IP addresses pointing to
1257hash-references. The key for these inner hash references is the 1321hash-references. The key for these inner hash references is the
1258server path for which this cookie is meant, and the values are again 1322server path for which this cookie is meant, and the values are again
1259hash-references. Each key of those hash-references is a cookie name, and 1323hash-references. Each key of those hash-references is a cookie name, and
1260the value, you guessed it, is another hash-reference, this time with the 1324the value, you guessed it, is another hash-reference, this time with the
1261key-value pairs from the cookie, except for C<expires> and C<max-age>, 1325key-value pairs from the cookie, except for C<expires> and C<max-age>,
1265 1329
1266Here is an example of a cookie jar with a single cookie, so you have a 1330Here is an example of a cookie jar with a single cookie, so you have a
1267chance of understanding the above paragraph: 1331chance of understanding the above paragraph:
1268 1332
1269 { 1333 {
1270 version => 1, 1334 version => 2,
1271 "10.0.0.1" => { 1335 "10.0.0.1" => {
1272 "/" => { 1336 "/" => {
1273 "mythweb_id" => { 1337 "mythweb_id" => {
1274 _expires => 1293917923, 1338 _expires => 1293917923,
1275 value => "ooRung9dThee3ooyXooM1Ohm", 1339 value => "ooRung9dThee3ooyXooM1Ohm",
1303C<Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)>). 1367C<Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)>).
1304 1368
1305=item $AnyEvent::HTTP::MAX_PER_HOST 1369=item $AnyEvent::HTTP::MAX_PER_HOST
1306 1370
1307The maximum number of concurrent connections to the same host (identified 1371The maximum number of concurrent connections to the same host (identified
1308by the hostname). If the limit is exceeded, then the additional requests 1372by the hostname). If the limit is exceeded, then additional requests
1309are queued until previous connections are closed. Both persistent and 1373are queued until previous connections are closed. Both persistent and
1310non-persistent connections are counted in this limit. 1374non-persistent connections are counted in this limit.
1311 1375
1312The default value for this is C<4>, and it is highly advisable to not 1376The default value for this is C<4>, and it is highly advisable to not
1313increase it much. 1377increase it much.
1420 or die "$file: $!"; 1484 or die "$file: $!";
1421 1485
1422 my %hdr; 1486 my %hdr;
1423 my $ofs = 0; 1487 my $ofs = 0;
1424 1488
1425 warn stat $fh;
1426 warn -s _;
1427 if (stat $fh and -s _) { 1489 if (stat $fh and -s _) {
1428 $ofs = -s _; 1490 $ofs = -s _;
1429 warn "-s is ", $ofs; 1491 warn "-s is ", $ofs;
1430 $hdr{"if-unmodified-since"} = AnyEvent::HTTP::format_date +(stat _)[9]; 1492 $hdr{"if-unmodified-since"} = AnyEvent::HTTP::format_date +(stat _)[9];
1431 $hdr{"range"} = "bytes=$ofs-"; 1493 $hdr{"range"} = "bytes=$ofs-";
1459 my (undef, $hdr) = @_; 1521 my (undef, $hdr) = @_;
1460 1522
1461 my $status = $hdr->{Status}; 1523 my $status = $hdr->{Status};
1462 1524
1463 if (my $time = AnyEvent::HTTP::parse_date $hdr->{"last-modified"}) { 1525 if (my $time = AnyEvent::HTTP::parse_date $hdr->{"last-modified"}) {
1464 utime $fh, $time, $time; 1526 utime $time, $time, $fh;
1465 } 1527 }
1466 1528
1467 if ($status == 200 || $status == 206 || $status == 416) { 1529 if ($status == 200 || $status == 206 || $status == 416) {
1468 # download ok || resume ok || file already fully downloaded 1530 # download ok || resume ok || file already fully downloaded
1469 $cb->(1, $hdr); 1531 $cb->(1, $hdr);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines