… | |
… | |
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.04'; |
51 | our $VERSION = '2.13'; |
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; |
… | |
… | |
169 | |
169 | |
170 | You really should provide your own C<User-Agent:> header value that is |
170 | You really should provide your own C<User-Agent:> header value that is |
171 | appropriate for your program - I wouldn't be surprised if the default |
171 | appropriate for your program - I wouldn't be surprised if the default |
172 | AnyEvent string gets blocked by webservers sooner or later. |
172 | AnyEvent string gets blocked by webservers sooner or later. |
173 | |
173 | |
|
|
174 | Also, make sure that your headers names and values do not contain any |
|
|
175 | embedded newlines. |
|
|
176 | |
174 | =item timeout => $seconds |
177 | =item timeout => $seconds |
175 | |
178 | |
176 | The time-out to use for various stages - each connect attempt will reset |
179 | The time-out to use for various stages - each connect attempt will reset |
177 | the timeout, as will read or write activity, i.e. this is not an overall |
180 | the timeout, as will read or write activity, i.e. this is not an overall |
178 | timeout. |
181 | timeout. |
… | |
… | |
381 | |
384 | |
382 | Example: do a HTTP HEAD request on https://www.google.com/, use a |
385 | Example: do a HTTP HEAD request on https://www.google.com/, use a |
383 | timeout of 30 seconds. |
386 | timeout of 30 seconds. |
384 | |
387 | |
385 | http_request |
388 | http_request |
386 | GET => "https://www.google.com", |
389 | HEAD => "https://www.google.com", |
387 | headers => { "user-agent" => "MySearchClient 1.0" }, |
390 | headers => { "user-agent" => "MySearchClient 1.0" }, |
388 | timeout => 30, |
391 | timeout => 30, |
389 | sub { |
392 | sub { |
390 | my ($body, $hdr) = @_; |
393 | my ($body, $hdr) = @_; |
391 | use Data::Dumper; |
394 | use Data::Dumper; |
… | |
… | |
773 | if length $arg{body} || $method ne "GET"; |
776 | if length $arg{body} || $method ne "GET"; |
774 | |
777 | |
775 | my $idempotent = $method =~ /^(?:GET|HEAD|PUT|DELETE|OPTIONS|TRACE)$/; |
778 | my $idempotent = $method =~ /^(?:GET|HEAD|PUT|DELETE|OPTIONS|TRACE)$/; |
776 | |
779 | |
777 | # default value for keepalive is true iff the request is for an idempotent method |
780 | # default value for keepalive is true iff the request is for an idempotent method |
778 | my $keepalive = exists $arg{keepalive} ? !!$arg{keepalive} : $idempotent; |
781 | my $persistent = exists $arg{persistent} ? !!$arg{persistent} : $idempotent; |
779 | my $keepalive10 = exists $arg{keepalive10} ? $arg{keepalive10} : !$proxy; |
782 | my $keepalive = exists $arg{keepalive} ? !!$arg{keepalive} : !$proxy; |
780 | my $keptalive; # true if this is actually a recycled connection |
783 | my $was_persistent; # true if this is actually a recycled connection |
781 | |
784 | |
782 | # the key to use in the keepalive cache |
785 | # the key to use in the keepalive cache |
783 | my $ka_key = "$uhost\x00$arg{sessionid}"; |
786 | my $ka_key = "$uscheme\x00$uhost\x00$uport\x00$arg{sessionid}"; |
784 | |
787 | |
785 | $hdr{connection} = ($keepalive ? $keepalive10 ? "keep-alive " : "" : "close ") . "Te"; #1.1 |
788 | $hdr{connection} = ($persistent ? $keepalive ? "keep-alive " : "" : "close ") . "Te"; #1.1 |
786 | $hdr{te} = "trailers" unless exists $hdr{te}; #1.1 |
789 | $hdr{te} = "trailers" unless exists $hdr{te}; #1.1 |
787 | |
790 | |
788 | my %state = (connect_guard => 1); |
791 | my %state = (connect_guard => 1); |
789 | |
792 | |
790 | my $ae_error = 595; # connecting |
793 | my $ae_error = 595; # connecting |
… | |
… | |
874 | } elsif ($status == 307) { |
877 | } elsif ($status == 307) { |
875 | $redirect = 1; |
878 | $redirect = 1; |
876 | } |
879 | } |
877 | } |
880 | } |
878 | |
881 | |
879 | my $finish = sub { # ($data, $err_status, $err_reason[, $keepalive]) |
882 | my $finish = sub { # ($data, $err_status, $err_reason[, $persistent]) |
880 | if ($state{handle}) { |
883 | if ($state{handle}) { |
881 | # handle keepalive |
884 | # handle keepalive |
882 | if ( |
885 | if ( |
883 | $keepalive |
886 | $persistent |
884 | && $_[3] |
887 | && $_[3] |
885 | && ($hdr{HTTPVersion} < 1.1 |
888 | && ($hdr{HTTPVersion} < 1.1 |
886 | ? $hdr{connection} =~ /\bkeep-?alive\b/i |
889 | ? $hdr{connection} =~ /\bkeep-?alive\b/i |
887 | : $hdr{connection} !~ /\bclose\b/i) |
890 | : $hdr{connection} !~ /\bclose\b/i) |
888 | ) { |
891 | ) { |
… | |
… | |
907 | |
910 | |
908 | if ($redirect && exists $hdr{location}) { |
911 | if ($redirect && exists $hdr{location}) { |
909 | # we ignore any errors, as it is very common to receive |
912 | # we ignore any errors, as it is very common to receive |
910 | # Content-Length != 0 but no actual body |
913 | # Content-Length != 0 but no actual body |
911 | # we also access %hdr, as $_[1] might be an erro |
914 | # we also access %hdr, as $_[1] might be an erro |
|
|
915 | $state{recurse} = |
912 | http_request ( |
916 | http_request ( |
913 | $method => $hdr{location}, |
917 | $method => $hdr{location}, |
914 | %arg, |
918 | %arg, |
915 | recurse => $recurse - 1, |
919 | recurse => $recurse - 1, |
916 | Redirect => [$_[0], \%hdr], |
920 | Redirect => [$_[0], \%hdr], |
|
|
921 | sub { |
|
|
922 | %state = (); |
917 | $cb |
923 | &$cb |
|
|
924 | }, |
918 | ); |
925 | ); |
919 | } else { |
926 | } else { |
920 | $cb->($_[0], \%hdr); |
927 | $cb->($_[0], \%hdr); |
921 | } |
928 | } |
922 | }; |
929 | }; |
923 | |
930 | |
… | |
… | |
955 | my $body = ""; |
962 | my $body = ""; |
956 | my $on_body = $arg{on_body} || sub { $body .= shift; 1 }; |
963 | my $on_body = $arg{on_body} || sub { $body .= shift; 1 }; |
957 | |
964 | |
958 | $state{read_chunk} = sub { |
965 | $state{read_chunk} = sub { |
959 | $_[1] =~ /^([0-9a-fA-F]+)/ |
966 | $_[1] =~ /^([0-9a-fA-F]+)/ |
960 | or $finish->(undef, $ae_error => "Garbled chunked transfer encoding"); |
967 | or return $finish->(undef, $ae_error => "Garbled chunked transfer encoding"); |
961 | |
968 | |
962 | my $len = hex $1; |
969 | my $len = hex $1; |
963 | |
970 | |
964 | if ($len) { |
971 | if ($len) { |
965 | $cl += $len; |
972 | $cl += $len; |
… | |
… | |
1035 | } |
1042 | } |
1036 | }; |
1043 | }; |
1037 | |
1044 | |
1038 | # if keepalive is enabled, then the server closing the connection |
1045 | # if keepalive is enabled, then the server closing the connection |
1039 | # before a response can happen legally - we retry on idempotent methods. |
1046 | # before a response can happen legally - we retry on idempotent methods. |
1040 | if ($keptalive && $idempotent) { |
1047 | if ($was_persistent && $idempotent) { |
1041 | my $old_eof = $hdl->{on_eof}; |
1048 | my $old_eof = $hdl->{on_eof}; |
1042 | $hdl->{on_eof} = sub { |
1049 | $hdl->{on_eof} = sub { |
1043 | _destroy_state %state; |
1050 | _destroy_state %state; |
1044 | |
1051 | |
|
|
1052 | %state = (); |
|
|
1053 | $state{recurse} = |
1045 | http_request ( |
1054 | http_request ( |
1046 | $method => $url, |
1055 | $method => $url, |
1047 | %arg, |
1056 | %arg, |
1048 | keepalive => 0, |
1057 | keepalive => 0, |
|
|
1058 | sub { |
|
|
1059 | %state = (); |
1049 | $cb |
1060 | &$cb |
|
|
1061 | } |
1050 | ); |
1062 | ); |
1051 | }; |
1063 | }; |
1052 | $hdl->on_read (sub { |
1064 | $hdl->on_read (sub { |
1053 | return unless %state; |
1065 | return unless %state; |
1054 | |
1066 | |
1055 | # as soon as we receive something, a connection close |
1067 | # as soon as we receive something, a connection close |
… | |
… | |
1063 | }; |
1075 | }; |
1064 | |
1076 | |
1065 | my $prepare_handle = sub { |
1077 | my $prepare_handle = sub { |
1066 | my ($hdl) = $state{handle}; |
1078 | my ($hdl) = $state{handle}; |
1067 | |
1079 | |
1068 | $hdl->timeout ($timeout); |
|
|
1069 | $hdl->on_error (sub { |
1080 | $hdl->on_error (sub { |
1070 | _error %state, $cb, { @pseudo, Status => $ae_error, Reason => $_[2] }; |
1081 | _error %state, $cb, { @pseudo, Status => $ae_error, Reason => $_[2] }; |
1071 | }); |
1082 | }); |
1072 | $hdl->on_eof (sub { |
1083 | $hdl->on_eof (sub { |
1073 | _error %state, $cb, { @pseudo, Status => $ae_error, Reason => "Unexpected end-of-file" }; |
1084 | _error %state, $cb, { @pseudo, Status => $ae_error, Reason => "Unexpected end-of-file" }; |
1074 | }); |
1085 | }); |
|
|
1086 | $hdl->timeout_reset; |
|
|
1087 | $hdl->timeout ($timeout); |
1075 | }; |
1088 | }; |
1076 | |
1089 | |
1077 | # connected to proxy (or origin server) |
1090 | # connected to proxy (or origin server) |
1078 | my $connect_cb = sub { |
1091 | my $connect_cb = sub { |
1079 | my $fh = shift |
1092 | my $fh = shift |
… | |
… | |
1120 | |
1133 | |
1121 | return unless $state{connect_guard}; |
1134 | return unless $state{connect_guard}; |
1122 | |
1135 | |
1123 | # try to use an existing keepalive connection, but only if we, ourselves, plan |
1136 | # try to use an existing keepalive connection, but only if we, ourselves, plan |
1124 | # on a keepalive request (in theory, this should be a separate config option). |
1137 | # on a keepalive request (in theory, this should be a separate config option). |
1125 | if ($keepalive && $KA_CACHE{$ka_key}) { |
1138 | if ($persistent && $KA_CACHE{$ka_key}) { |
1126 | $keptalive = 1; |
1139 | $was_persistent = 1; |
|
|
1140 | |
1127 | $state{handle} = ka_fetch $ka_key; |
1141 | $state{handle} = ka_fetch $ka_key; |
|
|
1142 | $state{handle}->destroyed |
|
|
1143 | and die "AnyEvent::HTTP: unexpectedly got a destructed handle (1), please report.";#d# |
1128 | $prepare_handle->(); |
1144 | $prepare_handle->(); |
|
|
1145 | $state{handle}->destroyed |
|
|
1146 | and die "AnyEvent::HTTP: unexpectedly got a destructed handle (2), please report.";#d# |
1129 | $handle_actual_request->(); |
1147 | $handle_actual_request->(); |
1130 | |
1148 | |
1131 | } else { |
1149 | } else { |
1132 | my $tcp_connect = $arg{tcp_connect} |
1150 | my $tcp_connect = $arg{tcp_connect} |
1133 | || do { require AnyEvent::Socket; \&AnyEvent::Socket::tcp_connect }; |
1151 | || do { require AnyEvent::Socket; \&AnyEvent::Socket::tcp_connect }; |
… | |
… | |
1366 | |
1384 | |
1367 | warn stat $fh; |
1385 | warn stat $fh; |
1368 | warn -s _; |
1386 | warn -s _; |
1369 | if (stat $fh and -s _) { |
1387 | if (stat $fh and -s _) { |
1370 | $ofs = -s _; |
1388 | $ofs = -s _; |
1371 | warn "-s is ", $ofs;#d# |
1389 | warn "-s is ", $ofs; |
1372 | $hdr{"if-unmodified-since"} = AnyEvent::HTTP::format_date +(stat _)[9]; |
1390 | $hdr{"if-unmodified-since"} = AnyEvent::HTTP::format_date +(stat _)[9]; |
1373 | $hdr{"range"} = "bytes=$ofs-"; |
1391 | $hdr{"range"} = "bytes=$ofs-"; |
1374 | } |
1392 | } |
1375 | |
1393 | |
1376 | http_get $url, |
1394 | http_get $url, |