--- AnyEvent-HTTP/HTTP.pm 2009/07/05 23:50:59 1.41 +++ AnyEvent-HTTP/HTTP.pm 2009/07/07 00:15:32 1.44 @@ -43,14 +43,14 @@ use Errno (); -use AnyEvent 4.452 (); +use AnyEvent 4.8 (); use AnyEvent::Util (); use AnyEvent::Socket (); use AnyEvent::Handle (); use base Exporter::; -our $VERSION = '1.12'; +our $VERSION = '1.4'; our @EXPORT = qw(http_get http_post http_head http_request); @@ -61,7 +61,7 @@ our $TIMEOUT = 300; # changing these is evil -our $MAX_PERSISTENT_PER_HOST = 2; +our $MAX_PERSISTENT_PER_HOST = 0; our $MAX_PER_HOST = 4; our $PROXY; @@ -96,9 +96,9 @@ object at least alive until the callback get called. If the object gets destroyed before the callbakc is called, the request will be cancelled. -The callback will be called with the response data as first argument -(or C if it wasn't available due to errors), and a hash-ref with -response headers as second argument. +The callback will be called with the response body data as first argument +(or C if an error occured), and a hash-ref with response headers as +second argument. All the headers in that hash are lowercased. In addition to the response headers, the "pseudo-headers" C, C and C @@ -188,7 +188,7 @@ The default for this option is C, which could be interpreted as "give me the page, no matter what". -=item on_header => $callback->($hdr) +=item on_header => $callback->($headers) When specified, this callback will be called with the header hash as soon as headers have been successfully received from the remote server (not on @@ -202,18 +202,25 @@ content, which, if it is supposed to be rare, can be faster than first doing a C request. -=item on_body => $callback->($data, $hdr) +Example: cancel the request unless the content-type is "text/html". -When specified, all body data will be "filtered" through this callback. + on_header => sub { + $_[0]{"content-type"} =~ /^text\/html\s*(?:;|$)/ + }, -The callback will incrementally receive body data, and is supposed to -return it or a modified version of it (empty strings are valid returns). +=item on_body => $callback->($partial_body, $headers) -If the callback returns C, then the request will be cancelled. +When specified, all body data will be passed to this callback instead of +to the completion callback. The completion callback will get the empty +string instead of the body data. -This callback is useful when you want to do some processing on the data, -or the data is too large to be held in memory (so the callback writes it -to a file and returns the empty string) and so on. +It has to return either true (in which case AnyEvent::HTTP will continue), +or false, in which case AnyEvent::HTTP will cancel the download (and call +the completion callback with an error code of C<598>). + +This callback is useful when the data is too large to be held in memory +(so the callback writes it to a file) or when only some information should +be extracted, or when the body should be processed incrementally. It is usually preferred over doing your own body handling via C. @@ -350,6 +357,8 @@ my $uhost = $1; $uport = $2 if defined $2; + $hdr{host} = defined $2 ? "$uhost:$2" : "$uhost"; + $uhost =~ s/^\[(.*)\]$/$1/; $upath .= "?$query" if length $query; @@ -401,7 +410,6 @@ $hdr{"user-agent"} ||= $USERAGENT; $hdr{referer} ||= "$uscheme://$uauthority$upath"; # leave out fragment and query string, just a heuristic - $hdr{host} = "$uhost:$uport"; $hdr{"content-length"} = length $arg{body}; my %state = (connect_guard => 1); @@ -413,7 +421,12 @@ $state{connect_guard} = AnyEvent::Socket::tcp_connect $rhost, $rport, sub { $state{fh} = shift - or return (%state = (), $cb->(undef, { Status => 599, Reason => "$!", URL => $url })); + or do { + my $err = "$!"; + %state = (); + return $cb->(undef, { Status => 599, Reason => $err, URL => $url }); + }; + pop; # free memory, save a tree return unless delete $state{connect_guard}; @@ -485,7 +498,7 @@ $hdr{lc $1} .= ",$2" while /\G - ([^:\000-\037]+): + ([^:\000-\037]*): [\011\040]* ((?: [^\012]+ | \012[\011\040] )*) \012 @@ -599,7 +612,7 @@ if (!$redirect && $arg{on_header} && !$arg{on_header}(\%hdr)) { $finish->(undef, { Status => 598, Reason => "Request cancelled by on_header", URL => $url }); } elsif ( - $hdr{Status} =~ /^(?:1..|204|304)$/ + $hdr{Status} =~ /^(?:1..|[23]04)$/ or $method eq "HEAD" or (defined $len && !$len) ) { @@ -649,7 +662,7 @@ }); } else { $_[0]->on_error (sub { - $! == Errno::EPIPE + $! == Errno::EPIPE || !$! ? $finish->(delete $_[0]{rbuf}, \%hdr) : $finish->(undef, { Status => 599, Reason => $_[2], URL => $url }); }); @@ -727,17 +740,14 @@ The default value for the C header (the default is C). -=item $AnyEvent::HTTP::MAX_PERSISTENT - -The maximum number of persistent connections to keep open (default: 8). - -Not implemented currently. - -=item $AnyEvent::HTTP::PERSISTENT_TIMEOUT +=item $AnyEvent::HTTP::MAX_PER_HOST -The maximum time to cache a persistent connection, in seconds (default: 2). +The maximum number of concurrent conenctions to the same host (identified +by the hostname). If the limit is exceeded, then the additional requests +are queued until previous connections are closed. -Not implemented currently. +The default value for this is C<4>, and it is highly advisable to not +increase it. =item $AnyEvent::HTTP::ACTIVE