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.95 by root, Wed Jan 12 03:30:05 2011 UTC vs.
Revision 1.103 by root, Thu Feb 24 12:13:11 2011 UTC

46use AnyEvent::Util (); 46use AnyEvent::Util ();
47use AnyEvent::Handle (); 47use AnyEvent::Handle ();
48 48
49use base Exporter::; 49use base Exporter::;
50 50
51our $VERSION = '2.02'; 51our $VERSION = '2.1';
52 52
53our @EXPORT = qw(http_get http_post http_head http_request); 53our @EXPORT = qw(http_get http_post http_head http_request);
54 54
55our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; 55our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)";
56our $MAX_RECURSE = 10; 56our $MAX_RECURSE = 10;
179 179
180Default timeout is 5 minutes. 180Default timeout is 5 minutes.
181 181
182=item proxy => [$host, $port[, $scheme]] or undef 182=item proxy => [$host, $port[, $scheme]] or undef
183 183
184Use the given http proxy for all requests. If not specified, then the 184Use the given http proxy for all requests, or no proxy if C<undef> is
185default proxy (as specified by C<$ENV{http_proxy}>) is used. 185used.
186 186
187C<$scheme> must be either missing or must be C<http> for HTTP. 187C<$scheme> must be either missing or must be C<http> for HTTP.
188
189If not specified, then the default proxy is used (see
190C<AnyEvent::HTTP::set_proxy>).
188 191
189=item body => $string 192=item body => $string
190 193
191The request body, usually empty. Will be sent as-is (future versions of 194The request body, usually empty. Will be sent as-is (future versions of
192this module might offer more options). 195this module might offer more options).
529 while ( 532 while (
530 m{ 533 m{
531 \G\s* 534 \G\s*
532 (?: 535 (?:
533 expires \s*=\s* ([A-Z][a-z][a-z]+,\ [^,;]+) 536 expires \s*=\s* ([A-Z][a-z][a-z]+,\ [^,;]+)
534 | ([^=;,[:space:]]+) (?: \s*=\s* (?: "((?:[^\\"]+|\\.)*)" | ([^=;,[:space:]]*) ) )? 537 | ([^=;,[:space:]]+) (?: \s*=\s* (?: "((?:[^\\"]+|\\.)*)" | ([^;,[:space:]]*) ) )?
535 ) 538 )
536 }gcxsi 539 }gcxsi
537 ) { 540 ) {
538 my $name = $2; 541 my $name = $2;
539 my $value = $4; 542 my $value = $4;
709 my $recurse = exists $arg{recurse} ? delete $arg{recurse} : $MAX_RECURSE; 712 my $recurse = exists $arg{recurse} ? delete $arg{recurse} : $MAX_RECURSE;
710 713
711 return $cb->(undef, { @pseudo, Status => 599, Reason => "Too many redirections" }) 714 return $cb->(undef, { @pseudo, Status => 599, Reason => "Too many redirections" })
712 if $recurse < 0; 715 if $recurse < 0;
713 716
714 my $proxy = $arg{proxy} || $PROXY; 717 my $proxy = exists $arg{proxy} ? $arg{proxy} : $PROXY;
715 my $timeout = $arg{timeout} || $TIMEOUT; 718 my $timeout = $arg{timeout} || $TIMEOUT;
716 719
717 my ($uscheme, $uauthority, $upath, $query, undef) = # ignore fragment 720 my ($uscheme, $uauthority, $upath, $query, undef) = # ignore fragment
718 $url =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:(\?[^#]*))?(?:#(.*))?|; 721 $url =~ m|^([^:]+):(?://([^/?#]*))?([^?#]*)(?:(\?[^#]*))?(?:#(.*))?$|;
719 722
720 $uscheme = lc $uscheme; 723 $uscheme = lc $uscheme;
721 724
722 my $uport = $uscheme eq "http" ? 80 725 my $uport = $uscheme eq "http" ? 80
723 : $uscheme eq "https" ? 443 726 : $uscheme eq "https" ? 443
770 if length $arg{body} || $method ne "GET"; 773 if length $arg{body} || $method ne "GET";
771 774
772 my $idempotent = $method =~ /^(?:GET|HEAD|PUT|DELETE|OPTIONS|TRACE)$/; 775 my $idempotent = $method =~ /^(?:GET|HEAD|PUT|DELETE|OPTIONS|TRACE)$/;
773 776
774 # default value for keepalive is true iff the request is for an idempotent method 777 # default value for keepalive is true iff the request is for an idempotent method
775 my $keepalive = exists $arg{keepalive} ? !!$arg{keepalive} : $idempotent; 778 my $persistent = exists $arg{persistent} ? !!$arg{persistent} : $idempotent;
776 my $keepalive10 = exists $arg{keepalive10} ? $arg{keepalive10} : !$proxy; 779 my $keepalive = exists $arg{keepalive} ? !!$arg{keepalive} : !$proxy;
777 my $keptalive; # true if this is actually a recycled connection 780 my $was_persistent; # true if this is actually a recycled connection
778 781
779 # the key to use in the keepalive cache 782 # the key to use in the keepalive cache
780 my $ka_key = "$uhost\x00$arg{sessionid}"; 783 my $ka_key = "$uhost\x00$arg{sessionid}";
781 784
782 $hdr{connection} = ($keepalive ? $keepalive10 ? "keep-alive " : "" : "close ") . "Te"; #1.1 785 $hdr{connection} = ($persistent ? $keepalive ? "keep-alive " : "" : "close ") . "Te"; #1.1
783 $hdr{te} = "trailers" unless exists $hdr{te}; #1.1 786 $hdr{te} = "trailers" unless exists $hdr{te}; #1.1
784 787
785 my %state = (connect_guard => 1); 788 my %state = (connect_guard => 1);
786 789
787 my $ae_error = 595; # connecting 790 my $ae_error = 595; # connecting
871 } elsif ($status == 307) { 874 } elsif ($status == 307) {
872 $redirect = 1; 875 $redirect = 1;
873 } 876 }
874 } 877 }
875 878
876 my $finish = sub { # ($data, $err_status, $err_reason[, $keepalive]) 879 my $finish = sub { # ($data, $err_status, $err_reason[, $persistent])
877 if ($state{handle}) { 880 if ($state{handle}) {
878 # handle keepalive 881 # handle keepalive
879 if ( 882 if (
880 $keepalive 883 $persistent
881 && $_[3] 884 && $_[3]
882 && ($hdr{HTTPVersion} < 1.1 885 && ($hdr{HTTPVersion} < 1.1
883 ? $hdr{connection} =~ /\bkeep-?alive\b/i 886 ? $hdr{connection} =~ /\bkeep-?alive\b/i
884 : $hdr{connection} !~ /\bclose\b/i) 887 : $hdr{connection} !~ /\bclose\b/i)
885 ) { 888 ) {
904 907
905 if ($redirect && exists $hdr{location}) { 908 if ($redirect && exists $hdr{location}) {
906 # we ignore any errors, as it is very common to receive 909 # we ignore any errors, as it is very common to receive
907 # Content-Length != 0 but no actual body 910 # Content-Length != 0 but no actual body
908 # we also access %hdr, as $_[1] might be an erro 911 # we also access %hdr, as $_[1] might be an erro
912 $state{recurse} =
909 http_request ( 913 http_request (
910 $method => $hdr{location}, 914 $method => $hdr{location},
911 %arg, 915 %arg,
912 recurse => $recurse - 1, 916 recurse => $recurse - 1,
913 Redirect => [$_[0], \%hdr], 917 Redirect => [$_[0], \%hdr],
918 sub {
919 %state = ();
914 $cb 920 &$cb
921 },
915 ); 922 );
916 } else { 923 } else {
917 $cb->($_[0], \%hdr); 924 $cb->($_[0], \%hdr);
918 } 925 }
919 }; 926 };
920 927
1032 } 1039 }
1033 }; 1040 };
1034 1041
1035 # if keepalive is enabled, then the server closing the connection 1042 # if keepalive is enabled, then the server closing the connection
1036 # before a response can happen legally - we retry on idempotent methods. 1043 # before a response can happen legally - we retry on idempotent methods.
1037 if ($keptalive && $idempotent) { 1044 if ($was_persistent && $idempotent) {
1038 my $old_eof = $hdl->{on_eof}; 1045 my $old_eof = $hdl->{on_eof};
1039 $hdl->{on_eof} = sub { 1046 $hdl->{on_eof} = sub {
1040 _destroy_state %state; 1047 _destroy_state %state;
1041 1048
1049 %state = ();
1050 $state{recurse} =
1042 http_request ( 1051 http_request (
1043 $method => $url, 1052 $method => $url,
1044 %arg, 1053 %arg,
1045 keepalive => 0, 1054 keepalive => 0,
1055 sub {
1056 %state = ();
1046 $cb 1057 &$cb
1058 }
1047 ); 1059 );
1048 }; 1060 };
1049 $hdl->on_read (sub { 1061 $hdl->on_read (sub {
1050 return unless %state; 1062 return unless %state;
1051 1063
1052 # as soon as we receive something, a connection close 1064 # as soon as we receive something, a connection close
1060 }; 1072 };
1061 1073
1062 my $prepare_handle = sub { 1074 my $prepare_handle = sub {
1063 my ($hdl) = $state{handle}; 1075 my ($hdl) = $state{handle};
1064 1076
1065 $hdl->timeout ($timeout);
1066 $hdl->on_error (sub { 1077 $hdl->on_error (sub {
1067 _error %state, $cb, { @pseudo, Status => $ae_error, Reason => $_[2] }; 1078 _error %state, $cb, { @pseudo, Status => $ae_error, Reason => $_[2] };
1068 }); 1079 });
1069 $hdl->on_eof (sub { 1080 $hdl->on_eof (sub {
1070 _error %state, $cb, { @pseudo, Status => $ae_error, Reason => "Unexpected end-of-file" }; 1081 _error %state, $cb, { @pseudo, Status => $ae_error, Reason => "Unexpected end-of-file" };
1071 }); 1082 });
1083 $hdl->timeout_reset;
1084 $hdl->timeout ($timeout);
1072 }; 1085 };
1073 1086
1074 # connected to proxy (or origin server) 1087 # connected to proxy (or origin server)
1075 my $connect_cb = sub { 1088 my $connect_cb = sub {
1076 my $fh = shift 1089 my $fh = shift
1117 1130
1118 return unless $state{connect_guard}; 1131 return unless $state{connect_guard};
1119 1132
1120 # try to use an existing keepalive connection, but only if we, ourselves, plan 1133 # try to use an existing keepalive connection, but only if we, ourselves, plan
1121 # on a keepalive request (in theory, this should be a separate config option). 1134 # on a keepalive request (in theory, this should be a separate config option).
1122 if ($keepalive && $KA_CACHE{$ka_key}) { 1135 if ($persistent && $KA_CACHE{$ka_key}) {
1123 $keptalive = 1; 1136 $was_persistent = 1;
1137
1124 $state{handle} = ka_fetch $ka_key; 1138 $state{handle} = ka_fetch $ka_key;
1139 $state{handle}->destroyed
1140 and die "got a destructed habndle. pah\n";#d#
1125 $prepare_handle->(); 1141 $prepare_handle->();
1142 $state{handle}->destroyed
1143 and die "got a destructed habndle. pa2\n";#d#
1126 $handle_actual_request->(); 1144 $handle_actual_request->();
1145 $state{handle}->destroyed
1146 and die "got a destructed habndle. pa3\n";#d#
1127 1147
1128 } else { 1148 } else {
1129 my $tcp_connect = $arg{tcp_connect} 1149 my $tcp_connect = $arg{tcp_connect}
1130 || do { require AnyEvent::Socket; \&AnyEvent::Socket::tcp_connect }; 1150 || do { require AnyEvent::Socket; \&AnyEvent::Socket::tcp_connect };
1131 1151
1171 1191
1172Sets the default proxy server to use. The proxy-url must begin with a 1192Sets the default proxy server to use. The proxy-url must begin with a
1173string of the form C<http://host:port>, croaks otherwise. 1193string of the form C<http://host:port>, croaks otherwise.
1174 1194
1175To clear an already-set proxy, use C<undef>. 1195To clear an already-set proxy, use C<undef>.
1196
1197When AnyEvent::HTTP is laoded for the first time it will query the
1198default proxy from the operating system, currently by looking at
1199C<$ENV{http_proxy>}.
1176 1200
1177=item AnyEvent::HTTP::cookie_jar_expire $jar[, $session_end] 1201=item AnyEvent::HTTP::cookie_jar_expire $jar[, $session_end]
1178 1202
1179Remove all cookies from the cookie jar that have been expired. If 1203Remove all cookies from the cookie jar that have been expired. If
1180C<$session_end> is given and true, then additionally remove all session 1204C<$session_end> is given and true, then additionally remove all session
1332This section contaisn some more elaborate "real-world" examples or code 1356This section contaisn some more elaborate "real-world" examples or code
1333snippets. 1357snippets.
1334 1358
1335=head2 HTTP/1.1 FILE DOWNLOAD 1359=head2 HTTP/1.1 FILE DOWNLOAD
1336 1360
1337Downloading files with HTTP cna be quite tricky, especially when something 1361Downloading files with HTTP can be quite tricky, especially when something
1338goes wrong and you want tor esume. 1362goes wrong and you want to resume.
1339 1363
1340Here is a function that initiates and resumes a download. It uses the 1364Here is a function that initiates and resumes a download. It uses the
1341last modified time to check for file content changes, and works with many 1365last modified time to check for file content changes, and works with many
1342HTTP/1.0 servers as well, and usually falls back to a complete re-download 1366HTTP/1.0 servers as well, and usually falls back to a complete re-download
1343on older servers. 1367on older servers.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines