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