| 1 |
NAME |
| 2 |
AnyEvent::HTTP - simple but non-blocking HTTP/HTTPS client |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
use AnyEvent::HTTP; |
| 6 |
|
| 7 |
http_get "http://www.nethype.de/", sub { print $_[1] }; |
| 8 |
|
| 9 |
# ... do something else here |
| 10 |
|
| 11 |
DESCRIPTION |
| 12 |
This module is an AnyEvent user, you need to make sure that you use and |
| 13 |
run a supported event loop. |
| 14 |
|
| 15 |
This module implements a simple, stateless and non-blocking HTTP client. |
| 16 |
It supports GET, POST and other request methods, cookies and more, all |
| 17 |
on a very low level. It can follow redirects supports proxies and |
| 18 |
automatically limits the number of connections to the values specified |
| 19 |
in the RFC. |
| 20 |
|
| 21 |
It should generally be a "good client" that is enough for most HTTP |
| 22 |
tasks. Simple tasks should be simple, but complex tasks should still be |
| 23 |
possible as the user retains control over request and response headers. |
| 24 |
|
| 25 |
The caller is responsible for authentication management, cookies (if the |
| 26 |
simplistic implementation in this module doesn't suffice), referer and |
| 27 |
other high-level protocol details for which this module offers only |
| 28 |
limited support. |
| 29 |
|
| 30 |
METHODS |
| 31 |
http_get $url, key => value..., $cb->($data, $headers) |
| 32 |
Executes an HTTP-GET request. See the http_request function for |
| 33 |
details on additional parameters and the return value. |
| 34 |
|
| 35 |
http_head $url, key => value..., $cb->($data, $headers) |
| 36 |
Executes an HTTP-HEAD request. See the http_request function for |
| 37 |
details on additional parameters and the return value. |
| 38 |
|
| 39 |
http_post $url, $body, key => value..., $cb->($data, $headers) |
| 40 |
Executes an HTTP-POST request with a request body of $body. See the |
| 41 |
http_request function for details on additional parameters and the |
| 42 |
return value. |
| 43 |
|
| 44 |
http_request $method => $url, key => value..., $cb->($data, $headers) |
| 45 |
Executes a HTTP request of type $method (e.g. "GET", "POST"). The |
| 46 |
URL must be an absolute http or https URL. |
| 47 |
|
| 48 |
When called in void context, nothing is returned. In other contexts, |
| 49 |
"http_request" returns a "cancellation guard" - you have to keep the |
| 50 |
object at least alive until the callback get called. If the object |
| 51 |
gets destroyed before the callbakc is called, the request will be |
| 52 |
cancelled. |
| 53 |
|
| 54 |
The callback will be called with the response data as first argument |
| 55 |
(or "undef" if it wasn't available due to errors), and a hash-ref |
| 56 |
with response headers as second argument. |
| 57 |
|
| 58 |
All the headers in that hash are lowercased. In addition to the |
| 59 |
response headers, the "pseudo-headers" "HTTPVersion", "Status" and |
| 60 |
"Reason" contain the three parts of the HTTP Status-Line of the same |
| 61 |
name. The pseudo-header "URL" contains the original URL (which can |
| 62 |
differ from the requested URL when following redirects). |
| 63 |
|
| 64 |
If the server sends a header multiple times, then their contents |
| 65 |
will be joined together with a comma (","), as per the HTTP spec. |
| 66 |
|
| 67 |
If an internal error occurs, such as not being able to resolve a |
| 68 |
hostname, then $data will be "undef", "$headers->{Status}" will be |
| 69 |
599 and the "Reason" pseudo-header will contain an error message. |
| 70 |
|
| 71 |
A typical callback might look like this: |
| 72 |
|
| 73 |
sub { |
| 74 |
my ($body, $hdr) = @_; |
| 75 |
|
| 76 |
if ($hdr->{Status} =~ /^2/) { |
| 77 |
... everything should be ok |
| 78 |
} else { |
| 79 |
print "error, $hdr->{Status} $hdr->{Reason}\n"; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
Additional parameters are key-value pairs, and are fully optional. |
| 84 |
They include: |
| 85 |
|
| 86 |
recurse => $count (default: $MAX_RECURSE) |
| 87 |
Whether to recurse requests or not, e.g. on redirects, |
| 88 |
authentication retries and so on, and how often to do so. |
| 89 |
|
| 90 |
headers => hashref |
| 91 |
The request headers to use. Currently, "http_request" may |
| 92 |
provide its own "Host:", "Content-Length:", "Connection:" and |
| 93 |
"Cookie:" headers and will provide defaults for "User-Agent:" |
| 94 |
and "Referer:". |
| 95 |
|
| 96 |
timeout => $seconds |
| 97 |
The time-out to use for various stages - each connect attempt |
| 98 |
will reset the timeout, as will read or write activity. Default |
| 99 |
timeout is 5 minutes. |
| 100 |
|
| 101 |
proxy => [$host, $port[, $scheme]] or undef |
| 102 |
Use the given http proxy for all requests. If not specified, |
| 103 |
then the default proxy (as specified by $ENV{http_proxy}) is |
| 104 |
used. |
| 105 |
|
| 106 |
$scheme must be either missing or "http" for HTTP, or "https" |
| 107 |
for HTTPS. |
| 108 |
|
| 109 |
body => $string |
| 110 |
The request body, usually empty. Will be-sent as-is (future |
| 111 |
versions of this module might offer more options). |
| 112 |
|
| 113 |
cookie_jar => $hash_ref |
| 114 |
Passing this parameter enables (simplified) cookie-processing, |
| 115 |
loosely based on the original netscape specification. |
| 116 |
|
| 117 |
The $hash_ref must be an (initially empty) hash reference which |
| 118 |
will get updated automatically. It is possible to save the |
| 119 |
cookie_jar to persistent storage with something like JSON or |
| 120 |
Storable, but this is not recommended, as expire times are |
| 121 |
currently being ignored. |
| 122 |
|
| 123 |
Note that this cookie implementation is not of very high |
| 124 |
quality, nor meant to be complete. If you want complete cookie |
| 125 |
management you have to do that on your own. "cookie_jar" is |
| 126 |
meant as a quick fix to get some cookie-using sites working. |
| 127 |
Cookies are a privacy disaster, do not use them unless required |
| 128 |
to. |
| 129 |
|
| 130 |
Example: make a simple HTTP GET request for http://www.nethype.de/ |
| 131 |
|
| 132 |
http_request GET => "http://www.nethype.de/", sub { |
| 133 |
my ($body, $hdr) = @_; |
| 134 |
print "$body\n"; |
| 135 |
}; |
| 136 |
|
| 137 |
Example: make a HTTP HEAD request on https://www.google.com/, use a |
| 138 |
timeout of 30 seconds. |
| 139 |
|
| 140 |
http_request |
| 141 |
GET => "https://www.google.com", |
| 142 |
timeout => 30, |
| 143 |
sub { |
| 144 |
my ($body, $hdr) = @_; |
| 145 |
use Data::Dumper; |
| 146 |
print Dumper $hdr; |
| 147 |
} |
| 148 |
; |
| 149 |
|
| 150 |
Example: make another simple HTTP GET request, but immediately try |
| 151 |
to cancel it. |
| 152 |
|
| 153 |
my $request = http_request GET => "http://www.nethype.de/", sub { |
| 154 |
my ($body, $hdr) = @_; |
| 155 |
print "$body\n"; |
| 156 |
}; |
| 157 |
|
| 158 |
undef $request; |
| 159 |
|
| 160 |
GLOBAL FUNCTIONS AND VARIABLES |
| 161 |
AnyEvent::HTTP::set_proxy "proxy-url" |
| 162 |
Sets the default proxy server to use. The proxy-url must begin with |
| 163 |
a string of the form "http://host:port" (optionally "https:..."). |
| 164 |
|
| 165 |
$AnyEvent::HTTP::MAX_RECURSE |
| 166 |
The default value for the "recurse" request parameter (default: 10). |
| 167 |
|
| 168 |
$AnyEvent::HTTP::USERAGENT |
| 169 |
The default value for the "User-Agent" header (the default is |
| 170 |
"Mozilla/5.0 (compatible; AnyEvent::HTTP/$VERSION; |
| 171 |
+http://software.schmorp.de/pkg/AnyEvent)"). |
| 172 |
|
| 173 |
$AnyEvent::HTTP::MAX_PERSISTENT |
| 174 |
The maximum number of persistent connections to keep open (default: |
| 175 |
8). |
| 176 |
|
| 177 |
Not implemented currently. |
| 178 |
|
| 179 |
$AnyEvent::HTTP::PERSISTENT_TIMEOUT |
| 180 |
The maximum time to cache a persistent connection, in seconds |
| 181 |
(default: 2). |
| 182 |
|
| 183 |
Not implemented currently. |
| 184 |
|
| 185 |
$AnyEvent::HTTP::ACTIVE |
| 186 |
The number of active connections. This is not the number of |
| 187 |
currently running requests, but the number of currently open and |
| 188 |
non-idle TCP connections. This number of can be useful for |
| 189 |
load-leveling. |
| 190 |
|
| 191 |
SEE ALSO |
| 192 |
AnyEvent. |
| 193 |
|
| 194 |
AUTHOR |
| 195 |
Marc Lehmann <schmorp@schmorp.de> |
| 196 |
http://home.schmorp.de/ |
| 197 |
|
| 198 |
With many thanks to Дмитрий Шалашов, who provided |
| 199 |
countless testcases and bugreports. |
| 200 |
|