… | |
… | |
54 | The callback will be called with the response body data as first |
54 | The callback will be called with the response body data as first |
55 | argument (or "undef" if an error occured), and a hash-ref with |
55 | argument (or "undef" if an error occured), and a hash-ref with |
56 | response headers as second argument. |
56 | response headers as second argument. |
57 | |
57 | |
58 | All the headers in that hash are lowercased. In addition to the |
58 | All the headers in that hash are lowercased. In addition to the |
59 | response headers, the "pseudo-headers" "HTTPVersion", "Status" and |
59 | response headers, the "pseudo-headers" (uppercase to avoid clashing |
|
|
60 | with possible response headers) "HTTPVersion", "Status" and "Reason" |
60 | "Reason" contain the three parts of the HTTP Status-Line of the same |
61 | contain the three parts of the HTTP Status-Line of the same name. |
|
|
62 | |
61 | name. The pseudo-header "URL" contains the original URL (which can |
63 | The pseudo-header "URL" contains the actual URL (which can differ |
62 | differ from the requested URL when following redirects). |
64 | from the requested URL when following redirects - for example, you |
|
|
65 | might get an error that your URL scheme is not supported even though |
|
|
66 | your URL is a valid http URL because it redirected to an ftp URL, in |
|
|
67 | which case you can look at the URL pseudo header). |
|
|
68 | |
|
|
69 | The pseudo-header "Redirect" only exists when the request was a |
|
|
70 | result of an internal redirect. In that case it is an array |
|
|
71 | reference with the "($data, $headers)" from the redirect response. |
|
|
72 | Note that this response could in turn be the result of a redirect |
|
|
73 | itself, and "$headers->{Redirect}[1]{Redirect}" will then contain |
|
|
74 | the original response, and so on. |
63 | |
75 | |
64 | If the server sends a header multiple times, then their contents |
76 | If the server sends a header multiple times, then their contents |
65 | will be joined together with a comma (","), as per the HTTP spec. |
77 | will be joined together with a comma (","), as per the HTTP spec. |
66 | |
78 | |
67 | If an internal error occurs, such as not being able to resolve a |
79 | If an internal error occurs, such as not being able to resolve a |
… | |
… | |
90 | |
102 | |
91 | headers => hashref |
103 | headers => hashref |
92 | The request headers to use. Currently, "http_request" may |
104 | The request headers to use. Currently, "http_request" may |
93 | provide its own "Host:", "Content-Length:", "Connection:" and |
105 | provide its own "Host:", "Content-Length:", "Connection:" and |
94 | "Cookie:" headers and will provide defaults for "User-Agent:" |
106 | "Cookie:" headers and will provide defaults for "User-Agent:" |
95 | and "Referer:". |
107 | and "Referer:" (this can be suppressed by using "undef" for |
|
|
108 | these headers in which case they won't be sent at all). |
96 | |
109 | |
97 | timeout => $seconds |
110 | timeout => $seconds |
98 | The time-out to use for various stages - each connect attempt |
111 | The time-out to use for various stages - each connect attempt |
99 | will reset the timeout, as will read or write activity. Default |
112 | will reset the timeout, as will read or write activity, i.e. |
|
|
113 | this is not an overall timeout. |
|
|
114 | |
100 | timeout is 5 minutes. |
115 | Default timeout is 5 minutes. |
101 | |
116 | |
102 | proxy => [$host, $port[, $scheme]] or undef |
117 | proxy => [$host, $port[, $scheme]] or undef |
103 | Use the given http proxy for all requests. If not specified, |
118 | Use the given http proxy for all requests. If not specified, |
104 | then the default proxy (as specified by $ENV{http_proxy}) is |
119 | then the default proxy (as specified by $ENV{http_proxy}) is |
105 | used. |
120 | used. |
106 | |
121 | |
107 | $scheme must be either missing or "http" for HTTP, or "https" |
122 | $scheme must be either missing, "http" for HTTP or "https" for |
108 | for HTTPS. |
123 | HTTPS. |
109 | |
124 | |
110 | body => $string |
125 | body => $string |
111 | The request body, usually empty. Will be-sent as-is (future |
126 | The request body, usually empty. Will be-sent as-is (future |
112 | versions of this module might offer more options). |
127 | versions of this module might offer more options). |
113 | |
128 | |
… | |
… | |
137 | and high-security (CA and common-name verification) TLS context. |
152 | and high-security (CA and common-name verification) TLS context. |
138 | |
153 | |
139 | The default for this option is "low", which could be interpreted |
154 | The default for this option is "low", which could be interpreted |
140 | as "give me the page, no matter what". |
155 | as "give me the page, no matter what". |
141 | |
156 | |
|
|
157 | on_prepare => $callback->($fh) |
|
|
158 | In rare cases you need to "tune" the socket before it is used to |
|
|
159 | connect (for exmaple, to bind it on a given IP address). This |
|
|
160 | parameter overrides the prepare callback passed to |
|
|
161 | "AnyEvent::Socket::tcp_connect" and behaves exactly the same way |
|
|
162 | (e.g. it has to provide a timeout). See the description for the |
|
|
163 | $prepare_cb argument of "AnyEvent::Socket::tcp_connect" for |
|
|
164 | details. |
|
|
165 | |
142 | on_header => $callback->($headers) |
166 | on_header => $callback->($headers) |
143 | When specified, this callback will be called with the header |
167 | When specified, this callback will be called with the header |
144 | hash as soon as headers have been successfully received from the |
168 | hash as soon as headers have been successfully received from the |
145 | remote server (not on locally-generated errors). |
169 | remote server (not on locally-generated errors). |
146 | |
170 | |
… | |
… | |
174 | memory (so the callback writes it to a file) or when only some |
198 | memory (so the callback writes it to a file) or when only some |
175 | information should be extracted, or when the body should be |
199 | information should be extracted, or when the body should be |
176 | processed incrementally. |
200 | processed incrementally. |
177 | |
201 | |
178 | It is usually preferred over doing your own body handling via |
202 | It is usually preferred over doing your own body handling via |
179 | "want_body_handle". |
203 | "want_body_handle", but in case of streaming APIs, where HTTP is |
|
|
204 | only used to create a connection, "want_body_handle" is the |
|
|
205 | better alternative, as it allows you to install your own event |
|
|
206 | handler, reducing resource usage. |
180 | |
207 | |
181 | want_body_handle => $enable |
208 | want_body_handle => $enable |
182 | When enabled (default is disabled), the behaviour of |
209 | When enabled (default is disabled), the behaviour of |
183 | AnyEvent::HTTP changes considerably: after parsing the headers, |
210 | AnyEvent::HTTP changes considerably: after parsing the headers, |
184 | and instead of downloading the body (if any), the completion |
211 | and instead of downloading the body (if any), the completion |
… | |
… | |
197 | initial headers, an interactive protocol is used (typical |
224 | initial headers, an interactive protocol is used (typical |
198 | example would be the push-style twitter API which starts a |
225 | example would be the push-style twitter API which starts a |
199 | JSON/XML stream). |
226 | JSON/XML stream). |
200 | |
227 | |
201 | If you think you need this, first have a look at "on_body", to |
228 | If you think you need this, first have a look at "on_body", to |
202 | see if that doesn'T solve your problem in a better way. |
229 | see if that doesn't solve your problem in a better way. |
203 | |
230 | |
204 | Example: make a simple HTTP GET request for http://www.nethype.de/ |
231 | Example: make a simple HTTP GET request for http://www.nethype.de/ |
205 | |
232 | |
206 | http_request GET => "http://www.nethype.de/", sub { |
233 | http_request GET => "http://www.nethype.de/", sub { |
207 | my ($body, $hdr) = @_; |
234 | my ($body, $hdr) = @_; |
… | |
… | |
229 | print "$body\n"; |
256 | print "$body\n"; |
230 | }; |
257 | }; |
231 | |
258 | |
232 | undef $request; |
259 | undef $request; |
233 | |
260 | |
|
|
261 | DNS CACHING |
|
|
262 | AnyEvent::HTTP uses the AnyEvent::Socket::tcp_connect function for the |
|
|
263 | actual connection, which in turn uses AnyEvent::DNS to resolve |
|
|
264 | hostnames. The latter is a simple stub resolver and does no caching on |
|
|
265 | its own. If you want DNS caching, you currently have to provide your own |
|
|
266 | default resolver (by storing a suitable resolver object in |
|
|
267 | $AnyEvent::DNS::RESOLVER). |
|
|
268 | |
234 | GLOBAL FUNCTIONS AND VARIABLES |
269 | GLOBAL FUNCTIONS AND VARIABLES |
235 | AnyEvent::HTTP::set_proxy "proxy-url" |
270 | AnyEvent::HTTP::set_proxy "proxy-url" |
236 | Sets the default proxy server to use. The proxy-url must begin with |
271 | Sets the default proxy server to use. The proxy-url must begin with |
237 | a string of the form "http://host:port" (optionally "https:..."). |
272 | a string of the form "http://host:port" (optionally "https:..."), |
|
|
273 | croaks otherwise. |
|
|
274 | |
|
|
275 | To clear an already-set proxy, use "undef". |
238 | |
276 | |
239 | $AnyEvent::HTTP::MAX_RECURSE |
277 | $AnyEvent::HTTP::MAX_RECURSE |
240 | The default value for the "recurse" request parameter (default: 10). |
278 | The default value for the "recurse" request parameter (default: 10). |
241 | |
279 | |
242 | $AnyEvent::HTTP::USERAGENT |
280 | $AnyEvent::HTTP::USERAGENT |
243 | The default value for the "User-Agent" header (the default is |
281 | The default value for the "User-Agent" header (the default is |
244 | "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; |
282 | "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; |
245 | +http://software.schmorp.de/pkg/AnyEvent)"). |
283 | +http://software.schmorp.de/pkg/AnyEvent)"). |
246 | |
284 | |
247 | $AnyEvent::HTTP::MAX_PER_HOST |
285 | $AnyEvent::HTTP::MAX_PER_HOST |
248 | The maximum number of concurrent conenctions to the same host |
286 | The maximum number of concurrent connections to the same host |
249 | (identified by the hostname). If the limit is exceeded, then the |
287 | (identified by the hostname). If the limit is exceeded, then the |
250 | additional requests are queued until previous connections are |
288 | additional requests are queued until previous connections are |
251 | closed. |
289 | closed. |
252 | |
290 | |
253 | The default value for this is 4, and it is highly advisable to not |
291 | The default value for this is 4, and it is highly advisable to not |