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.102 by root, Sat Feb 19 06:46:14 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.04'; 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;
773 if length $arg{body} || $method ne "GET"; 773 if length $arg{body} || $method ne "GET";
774 774
775 my $idempotent = $method =~ /^(?:GET|HEAD|PUT|DELETE|OPTIONS|TRACE)$/; 775 my $idempotent = $method =~ /^(?:GET|HEAD|PUT|DELETE|OPTIONS|TRACE)$/;
776 776
777 # 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
778 my $keepalive = exists $arg{keepalive} ? !!$arg{keepalive} : $idempotent; 778 my $persistent = exists $arg{persistent} ? !!$arg{persistent} : $idempotent;
779 my $keepalive10 = exists $arg{keepalive10} ? $arg{keepalive10} : !$proxy; 779 my $keepalive = exists $arg{keepalive} ? !!$arg{keepalive} : !$proxy;
780 my $keptalive; # true if this is actually a recycled connection 780 my $was_persistent; # true if this is actually a recycled connection
781 781
782 # the key to use in the keepalive cache 782 # the key to use in the keepalive cache
783 my $ka_key = "$uhost\x00$arg{sessionid}"; 783 my $ka_key = "$uhost\x00$arg{sessionid}";
784 784
785 $hdr{connection} = ($keepalive ? $keepalive10 ? "keep-alive " : "" : "close ") . "Te"; #1.1 785 $hdr{connection} = ($persistent ? $keepalive ? "keep-alive " : "" : "close ") . "Te"; #1.1
786 $hdr{te} = "trailers" unless exists $hdr{te}; #1.1 786 $hdr{te} = "trailers" unless exists $hdr{te}; #1.1
787 787
788 my %state = (connect_guard => 1); 788 my %state = (connect_guard => 1);
789 789
790 my $ae_error = 595; # connecting 790 my $ae_error = 595; # connecting
874 } elsif ($status == 307) { 874 } elsif ($status == 307) {
875 $redirect = 1; 875 $redirect = 1;
876 } 876 }
877 } 877 }
878 878
879 my $finish = sub { # ($data, $err_status, $err_reason[, $keepalive]) 879 my $finish = sub { # ($data, $err_status, $err_reason[, $persistent])
880 if ($state{handle}) { 880 if ($state{handle}) {
881 # handle keepalive 881 # handle keepalive
882 if ( 882 if (
883 $keepalive 883 $persistent
884 && $_[3] 884 && $_[3]
885 && ($hdr{HTTPVersion} < 1.1 885 && ($hdr{HTTPVersion} < 1.1
886 ? $hdr{connection} =~ /\bkeep-?alive\b/i 886 ? $hdr{connection} =~ /\bkeep-?alive\b/i
887 : $hdr{connection} !~ /\bclose\b/i) 887 : $hdr{connection} !~ /\bclose\b/i)
888 ) { 888 ) {
907 907
908 if ($redirect && exists $hdr{location}) { 908 if ($redirect && exists $hdr{location}) {
909 # we ignore any errors, as it is very common to receive 909 # we ignore any errors, as it is very common to receive
910 # Content-Length != 0 but no actual body 910 # Content-Length != 0 but no actual body
911 # we also access %hdr, as $_[1] might be an erro 911 # we also access %hdr, as $_[1] might be an erro
912 $state{recurse} =
912 http_request ( 913 http_request (
913 $method => $hdr{location}, 914 $method => $hdr{location},
914 %arg, 915 %arg,
915 recurse => $recurse - 1, 916 recurse => $recurse - 1,
916 Redirect => [$_[0], \%hdr], 917 Redirect => [$_[0], \%hdr],
918 sub {
919 %state = ();
917 $cb 920 &$cb
921 },
918 ); 922 );
919 } else { 923 } else {
920 $cb->($_[0], \%hdr); 924 $cb->($_[0], \%hdr);
921 } 925 }
922 }; 926 };
923 927
1035 } 1039 }
1036 }; 1040 };
1037 1041
1038 # if keepalive is enabled, then the server closing the connection 1042 # if keepalive is enabled, then the server closing the connection
1039 # before a response can happen legally - we retry on idempotent methods. 1043 # before a response can happen legally - we retry on idempotent methods.
1040 if ($keptalive && $idempotent) { 1044 if ($was_persistent && $idempotent) {
1041 my $old_eof = $hdl->{on_eof}; 1045 my $old_eof = $hdl->{on_eof};
1042 $hdl->{on_eof} = sub { 1046 $hdl->{on_eof} = sub {
1043 _destroy_state %state; 1047 _destroy_state %state;
1044 1048
1049 %state = ();
1050 $state{recurse} =
1045 http_request ( 1051 http_request (
1046 $method => $url, 1052 $method => $url,
1047 %arg, 1053 %arg,
1048 keepalive => 0, 1054 keepalive => 0,
1055 sub {
1056 %state = ();
1049 $cb 1057 &$cb
1058 }
1050 ); 1059 );
1051 }; 1060 };
1052 $hdl->on_read (sub { 1061 $hdl->on_read (sub {
1053 return unless %state; 1062 return unless %state;
1054 1063
1055 # as soon as we receive something, a connection close 1064 # as soon as we receive something, a connection close
1063 }; 1072 };
1064 1073
1065 my $prepare_handle = sub { 1074 my $prepare_handle = sub {
1066 my ($hdl) = $state{handle}; 1075 my ($hdl) = $state{handle};
1067 1076
1068 $hdl->timeout ($timeout);
1069 $hdl->on_error (sub { 1077 $hdl->on_error (sub {
1070 _error %state, $cb, { @pseudo, Status => $ae_error, Reason => $_[2] }; 1078 _error %state, $cb, { @pseudo, Status => $ae_error, Reason => $_[2] };
1071 }); 1079 });
1072 $hdl->on_eof (sub { 1080 $hdl->on_eof (sub {
1073 _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" };
1074 }); 1082 });
1083 $hdl->timeout_reset;
1084 $hdl->timeout ($timeout);
1075 }; 1085 };
1076 1086
1077 # connected to proxy (or origin server) 1087 # connected to proxy (or origin server)
1078 my $connect_cb = sub { 1088 my $connect_cb = sub {
1079 my $fh = shift 1089 my $fh = shift
1120 1130
1121 return unless $state{connect_guard}; 1131 return unless $state{connect_guard};
1122 1132
1123 # 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
1124 # 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).
1125 if ($keepalive && $KA_CACHE{$ka_key}) { 1135 if ($persistent && $KA_CACHE{$ka_key}) {
1126 $keptalive = 1; 1136 $was_persistent = 1;
1137
1127 $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#
1128 $prepare_handle->(); 1141 $prepare_handle->();
1142 $state{handle}->destroyed
1143 and die "got a destructed habndle. pa2\n";#d#
1129 $handle_actual_request->(); 1144 $handle_actual_request->();
1145 $state{handle}->destroyed
1146 and die "got a destructed habndle. pa3\n";#d#
1130 1147
1131 } else { 1148 } else {
1132 my $tcp_connect = $arg{tcp_connect} 1149 my $tcp_connect = $arg{tcp_connect}
1133 || do { require AnyEvent::Socket; \&AnyEvent::Socket::tcp_connect }; 1150 || do { require AnyEvent::Socket; \&AnyEvent::Socket::tcp_connect };
1134 1151

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines