AnyEvent-HTTP
view release on metacpan or search on metacpan
2.13 Wed Jul 27 17:53:58 CEST 2011
- garbled chunked responses caused AnyEvent::HTTP to malfunction
(patch by Dmitri Melikyan).
- fix GET => HEAD in one case in the documentation (James Bromberger).
2.12 Tue Jun 14 07:22:54 CEST 2011
- fix a possible 'Can't call method "destroyed"' error (which would
have been reported by Carl Chambers).
2.11 Tue May 10 14:33:28 CEST 2011
- the keepalive session cache wouldn't take port and scheme into account
when reusing connection - potentially causing information leaks
(reported by Nick Kostirya).
- bump AnyEvent dependency version (reported by Richard Harris).
2.1 Thu Feb 24 13:11:51 CET 2011
- the keepalive and persistent parameters were actually named
differently in the code - they now work as documented.
- fix a bug where callbacks would sometimes never be called when
the request timeout is near or below the persistent connection
timeout (testcase by Cindy Wang).
- destroying the guard would have no effect when a request was
recursing or being retired.
2.04 Sat Feb 19 07:45:24 CET 2011
- "proxy => undef" now overrides any global proxy when specified.
- require scheme in urls, also use a stricter match to match urls,
2.02 Wed Jan 12 04:29:37 CET 2011
- do not lowercase cookie names, only parameter names.
2.01 Tue Jan 11 07:38:15 CET 2011
- add missing dependency on common::sense.
- add a resume download example.
2.0 Tue Jan 4 09:16:56 CET 2011
- hopefully fully upgraded to HTTP/1.1.
- support HTTP/1.1 persistent and HTTP/1.0 keep-alive connections.
- drop https-proxy-connection support. seems unused and ill-specified.
- use more differentiated 59x status codes.
- properly use url (not proxy) hostname to verify server certificate.
- much improved cookie implementation:
- properly implement cookie expiry (for new cookies).
- new function to expire cookies and sessions: cookie_jar_expire.
- add special exception to parse broken expires= keys in
set-cookie headers.
- do not quote cookie values when not strictly necessary, to
improve compatibility with broken servers.
http_request function for details on additional parameters and the return
value.
=item http_request $method => $url, key => value..., $cb->($data, $headers)
Executes a HTTP request of type C<$method> (e.g. C<GET>, C<POST>). The URL
must be an absolute http or https URL.
When called in void context, nothing is returned. In other contexts,
C<http_request> returns a "cancellation guard" - you have to keep the
object at least alive until the callback get called. If the object gets
destroyed before the callback is called, the request will be cancelled.
The callback will be called with the response body data as first argument
(or C<undef> if an error occurred), and a hash-ref with response headers
(and trailers) as second argument.
All the headers in that hash are lowercased. In addition to the response
headers, the "pseudo-headers" (uppercase to avoid clashing with possible
response headers) C<HTTPVersion>, C<Status> and C<Reason> contain the
three parts of the HTTP Status-Line of the same name. If an error occurs
Requests failing in certain ways will be automatically retried once, which
is dangerous for non-idempotent requests, which is why it defaults to off
for them. The reason for this is because the bozos who designed HTTP/1.1
made it impossible to distinguish between a fatal error and a normal
connection timeout, so you never know whether there was a problem with
your request or not.
When reusing an existent connection, many parameters (such as TLS context)
will be ignored. See the C<sessionid> parameter for a workaround.
=item keepalive => $boolean
Only used when C<persistent> is also true. This parameter decides whether
C<http_request> tries to handshake a HTTP/1.0-style keep-alive connection
(as opposed to only a HTTP/1.1 persistent connection).
The default is true, except when using a proxy, in which case it defaults
to false, as HTTP/1.0 proxies cannot support this in a meaningful way.
=item handle_params => { key => value ... }
The key-value pairs in this hash will be passed to any L<AnyEvent::Handle>
constructor that is called - not all requests will create a handle, and
sometimes more than one is created, so this parameter is only good for
# store it
$jar->{version} = 2;
$jar->{lc $cdom}{$cpath}{$name} = \%kv;
redo if /\G\s*,/gc;
}
}
#############################################################################
# keepalive/persistent connection cache
# fetch a connection from the keepalive cache
sub ka_fetch($) {
my $ka_key = shift;
my $hdl = pop @{ $KA_CACHE{$ka_key} }; # currently we reuse the MOST RECENTLY USED connection
delete $KA_CACHE{$ka_key}
unless @{ $KA_CACHE{$ka_key} };
$hdl
}
# leave out fragment and query string, just a heuristic
$hdr{referer} = "$uscheme://$uauthority$upath" unless exists $hdr{referer};
$hdr{"user-agent"} = $USERAGENT unless exists $hdr{"user-agent"};
$hdr{"content-length"} = length $arg{body}
if length $arg{body} || $method ne "GET";
my $idempotent = $IDEMPOTENT{$method};
# default value for keepalive is true iff the request is for an idempotent method
my $persistent = exists $arg{persistent} ? !!$arg{persistent} : $idempotent;
my $keepalive = exists $arg{keepalive} ? !!$arg{keepalive} : !$proxy;
my $was_persistent; # true if this is actually a recycled connection
# the key to use in the keepalive cache
my $ka_key = "$uscheme\x00$uhost\x00$uport\x00$arg{sessionid}";
$hdr{connection} = ($persistent ? $keepalive ? "keep-alive, " : "" : "close, ") . "Te"; #1.1
$hdr{te} = "trailers" unless exists $hdr{te}; #1.1
my %state = (connect_guard => 1);
my $ae_error = 595; # connecting
# handle actual, non-tunneled, request
my $handle_actual_request = sub {
$ae_error = 596; # request phase
$method = "GET";
delete $arg{body};
}
} elsif ($status == 307 or $status == 308) {
$redirect = 1;
}
}
my $finish = sub { # ($data, $err_status, $err_reason[, $persistent])
if ($state{handle}) {
# handle keepalive
if (
$persistent
&& $_[3]
&& ($hdr{HTTPVersion} < 1.1
? $hdr{connection} =~ /\bkeep-?alive\b/i
: $hdr{connection} !~ /\bclose\b/i)
) {
ka_store $ka_key, delete $state{handle};
} else {
# no keepalive, destroy the handle
$state{handle}->destroy;
}
}
%state = ();
if (defined $_[1]) {
$hdr{OrigStatus} = $hdr{Status}; $hdr{Status} = $_[1];
$hdr{OrigReason} = $hdr{Reason}; $hdr{Reason} = $_[2];
}
$_[0]->on_error (sub {
($! == Errno::EPIPE || !$!)
? $finish->(delete $_[0]{rbuf})
: $finish->(undef, $ae_error => $_[2]);
});
$_[0]->on_read (sub { });
}
}
};
# if keepalive is enabled, then the server closing the connection
# before a response can happen legally - we retry on idempotent methods.
if ($was_persistent && $idempotent) {
my $old_eof = $hdl->{on_eof};
$hdl->{on_eof} = sub {
_destroy_state %state;
%state = ();
$state{recurse} =
http_request (
$method => $url,
$handle_actual_request->();
}
};
_get_slot $uhost, sub {
$state{slot_guard} = shift;
return unless $state{connect_guard};
# try to use an existing keepalive connection, but only if we, ourselves, plan
# on a keepalive request (in theory, this should be a separate config option).
if ($persistent && $KA_CACHE{$ka_key}) {
$was_persistent = 1;
$state{handle} = ka_fetch $ka_key;
# $state{handle}->destroyed
# and die "AnyEvent::HTTP: unexpectedly got a destructed handle (1), please report.";#d#
$prepare_handle->();
# $state{handle}->destroyed
# and die "AnyEvent::HTTP: unexpectedly got a destructed handle (2), please report.";#d#
$rpath = $upath;
Executes an HTTP-POST request with a request body of $body. See the
http_request function for details on additional parameters and the
return value.
http_request $method => $url, key => value..., $cb->($data, $headers)
Executes a HTTP request of type $method (e.g. "GET", "POST"). The
URL must be an absolute http or https URL.
When called in void context, nothing is returned. In other contexts,
"http_request" returns a "cancellation guard" - you have to keep the
object at least alive until the callback get called. If the object
gets destroyed before the callback is called, the request will be
cancelled.
The callback will be called with the response body data as first
argument (or "undef" if an error occurred), and a hash-ref with
response headers (and trailers) as second argument.
All the headers in that hash are lowercased. In addition to the
response headers, the "pseudo-headers" (uppercase to avoid clashing
with possible response headers) "HTTPVersion", "Status" and "Reason"
why it defaults to off for them. The reason for this is because
the bozos who designed HTTP/1.1 made it impossible to
distinguish between a fatal error and a normal connection
timeout, so you never know whether there was a problem with your
request or not.
When reusing an existent connection, many parameters (such as
TLS context) will be ignored. See the "sessionid" parameter for
a workaround.
keepalive => $boolean
Only used when "persistent" is also true. This parameter decides
whether "http_request" tries to handshake a HTTP/1.0-style
keep-alive connection (as opposed to only a HTTP/1.1 persistent
connection).
The default is true, except when using a proxy, in which case it
defaults to false, as HTTP/1.0 proxies cannot support this in a
meaningful way.
handle_params => { key => value ... }
The key-value pairs in this hash will be passed to any
AnyEvent::Handle constructor that is called - not all requests
will create a handle, and sometimes more than one is created, so
( run in 1.357 second using v1.01-cache-2.11-cpan-df04353d9ac )