AnyEvent-HTTP
view release on metacpan or search on metacpan
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:
{
version => 2,
"10.0.0.1" => {
"/" => {
"mythweb_id" => {
_expires => 1293917923,
value => "ooRung9dThee3ooyXooM1Ohm",
},
},
},
}
$date = AnyEvent::HTTP::format_date $timestamp
Takes a POSIX timestamp (seconds since the epoch) and formats it as
a HTTP Date (RFC 2616).
$timestamp = AnyEvent::HTTP::parse_date $date
Takes a HTTP Date (RFC 2616) or a Cookie date (netscape cookie spec)
or a bunch of minor variations of those, and returns the
corresponding POSIX timestamp, or "undef" if the date cannot be
parsed.
$AnyEvent::HTTP::MAX_RECURSE
The default value for the "recurse" request parameter (default: 10).
$AnyEvent::HTTP::TIMEOUT
The default timeout for connection operations (default: 300).
$AnyEvent::HTTP::USERAGENT
The default value for the "User-Agent" header (the default is
"Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION;
+http://software.schmorp.de/pkg/AnyEvent)").
$AnyEvent::HTTP::MAX_PER_HOST
The maximum number of concurrent connections to the same host
(identified by the hostname). If the limit is exceeded, then
additional requests are queued until previous connections are
closed. Both persistent and non-persistent connections are counted
in this limit.
The default value for this is 4, and it is highly advisable to not
increase it much.
For comparison: the RFC's recommend 4 non-persistent or 2 persistent
connections, older browsers used 2, newer ones (such as firefox 3)
typically use 6, and Opera uses 8 because like, they have the
fastest browser and give a shit for everybody else on the planet.
$AnyEvent::HTTP::PERSISTENT_TIMEOUT
The time after which idle persistent connections get closed by
AnyEvent::HTTP (default: 3).
( run in 1.353 second using v1.01-cache-2.11-cpan-39bf76dae61 )