--- AnyEvent-HTTP/HTTP.pm 2011/01/02 20:57:03 1.88 +++ AnyEvent-HTTP/HTTP.pm 2011/01/03 01:03:29 1.91 @@ -171,6 +171,10 @@ (this can be suppressed by using C for these headers in which case they won't be sent at all). +You really should provide your own C header value that is +appropriate for your program - I wouldn't be surprised if the default +AnyEvent string gets blocked by webservers sooner or later. + =item timeout => $seconds The time-out to use for various stages - each connect attempt will reset @@ -331,6 +335,7 @@ http_request GET => "https://www.google.com", + headers => { "user-agent" => "MySearchClient 1.0" }, timeout => 30, sub { my ($body, $hdr) = @_; @@ -651,7 +656,14 @@ $hdr{"content-length"} = length $arg{body} if length $arg{body} || $method ne "GET"; - $hdr{connection} = "close Te"; #1.1 + my $idempotent = $method =~ /^(?:GET|HEAD|PUT|DELETE|OPTIONS|TRACE)$/; + + # default value for keepalive is true iff the request is for an idempotent method + my $keepalive = exists $arg{keepalive} + ? $arg{keepalive}*1 + : $idempotent ? $PERSISTENT_TIMEOUT : 0; + + $hdr{connection} = ($keepalive ? "" : "close ") . "Te"; #1.1 $hdr{te} = "trailers" unless exists $hdr{te}; #1.1 my %state = (connect_guard => 1); @@ -675,7 +687,8 @@ # return if error occured during push_write() return unless %state; - %hdr = (); # reduce memory usage, save a kitten, also make it possible to re-use + # reduce memory usage, save a kitten, also re-use it for the response headers. + %hdr = (); # status line and headers $state{read_response} = sub { @@ -773,7 +786,9 @@ $ae_error = 597; # body phase - my $len = $hdr{"content-length"}; + my $chunked = $hdr{"transfer-encoding"} =~ /\bchunked\b/i; # not quite correct... + + my $len = $chunked ? undef : $hdr{"content-length"}; # body handling, many different code paths # - no body expected @@ -798,7 +813,7 @@ $finish->(delete $state{handle}); - } elsif ($hdr{"transfer-encoding"} =~ /\bchunked\b/i) { + } elsif ($chunked) { my $cl = 0; my $body = undef; my $on_body = $arg{on_body} || sub { $body .= shift; 1 };