AnyEvent-HTTP
view release on metacpan or search on metacpan
persistent => $boolean
Try to create/reuse a persistent connection. When this flag is
set (default: true for idempotent requests, false for all
others), then "http_request" tries to re-use an existing
(previously-created) persistent connection to same host (i.e.
identical URL scheme, hostname, port and sessionid) and, failing
that, tries to create a new one.
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 "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
this parameter is only good for setting hints.
Example: set the maximum read size to 4096, to potentially
conserve memory at the cost of speed.
handle_params => {
max_read_size => 4096,
},
Example: do a simple HTTP GET request for http://www.nethype.de/ and
print the response body.
http_request GET => "http://www.nethype.de/", sub {
my ($body, $hdr) = @_;
print "$body\n";
};
Example: do a HTTP HEAD request on https://www.google.com/, use a
timeout of 30 seconds.
http_request
HEAD => "https://www.google.com",
headers => { "user-agent" => "MySearchClient 1.0" },
timeout => 30,
sub {
my ($body, $hdr) = @_;
use Data::Dumper;
print Dumper $hdr;
}
;
Example: do another simple HTTP GET request, but immediately try to
cancel it.
my $request = http_request GET => "http://www.nethype.de/", sub {
my ($body, $hdr) = @_;
print "$body\n";
};
undef $request;
DNS CACHING
AnyEvent::HTTP uses the AnyEvent::Socket::tcp_connect function for the
actual connection, which in turn uses AnyEvent::DNS to resolve
hostnames. The latter is a simple stub resolver and does no caching on
its own. If you want DNS caching, you currently have to provide your own
default resolver (by storing a suitable resolver object in
$AnyEvent::DNS::RESOLVER) or your own "tcp_connect" callback.
GLOBAL FUNCTIONS AND VARIABLES
AnyEvent::HTTP::set_proxy "proxy-url"
Sets the default proxy server to use. The proxy-url must begin with
a string of the form "http://host:port", croaks otherwise.
To clear an already-set proxy, use "undef".
When AnyEvent::HTTP is loaded for the first time it will query the
default proxy from the operating system, currently by looking at
"$ENV{http_proxy"}.
AnyEvent::HTTP::cookie_jar_expire $jar[, $session_end]
Remove all cookies from the cookie jar that have been expired. If
$session_end is given and true, then additionally remove all session
cookies.
You should call this function (with a true $session_end) before you
save cookies to disk, and you should call this function after
loading them again. If you have a long-running program you can
additionally call this function from time to time.
A cookie jar is initially an empty hash-reference that is managed by
this module. Its format is subject to change, but currently it is as
follows:
The key "version" has to contain 2, otherwise the hash gets cleared.
All other keys are hostnames or IP addresses pointing to
hash-references. The key for these inner hash references is the
server path for which this cookie is meant, and the values are again
hash-references. Each key of those hash-references is a cookie name,
and the value, you guessed it, is another hash-reference, this time
with the key-value pairs from the cookie, except for "expires" and
"max-age", which have been replaced by a "_expires" key that
contains the cookie expiry timestamp. Session cookies are indicated
by not having an "_expires" key.
Here is an example of a cookie jar with a single cookie, so you have
a chance of understanding the above paragraph:
( run in 0.853 second using v1.01-cache-2.11-cpan-39bf76dae61 )