… | |
… | |
28 | limited support. |
28 | limited support. |
29 | |
29 | |
30 | METHODS |
30 | METHODS |
31 | http_get $url, key => value..., $cb->($data, $headers) |
31 | http_get $url, key => value..., $cb->($data, $headers) |
32 | Executes an HTTP-GET request. See the http_request function for |
32 | Executes an HTTP-GET request. See the http_request function for |
33 | details on additional parameters. |
33 | details on additional parameters and the return value. |
34 | |
34 | |
35 | http_head $url, key => value..., $cb->($data, $headers) |
35 | http_head $url, key => value..., $cb->($data, $headers) |
36 | Executes an HTTP-HEAD request. See the http_request function for |
36 | Executes an HTTP-HEAD request. See the http_request function for |
37 | details on additional parameters. |
37 | details on additional parameters and the return value. |
38 | |
38 | |
39 | http_post $url, $body, key => value..., $cb->($data, $headers) |
39 | http_post $url, $body, key => value..., $cb->($data, $headers) |
40 | Executes an HTTP-POST request with a request body of $body. See the |
40 | Executes an HTTP-POST request with a request body of $body. See the |
41 | http_request function for details on additional parameters. |
41 | http_request function for details on additional parameters and the |
|
|
42 | return value. |
42 | |
43 | |
43 | http_request $method => $url, key => value..., $cb->($data, $headers) |
44 | http_request $method => $url, key => value..., $cb->($data, $headers) |
44 | Executes a HTTP request of type $method (e.g. "GET", "POST"). The |
45 | Executes a HTTP request of type $method (e.g. "GET", "POST"). The |
45 | URL must be an absolute http or https URL. |
46 | URL must be an absolute http or https URL. |
46 | |
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 | |
47 | The callback will be called with the response data as first argument |
54 | The callback will be called with the response body data as first |
48 | (or "undef" if it wasn't available due to errors), and a hash-ref |
55 | argument (or "undef" if an error occured), and a hash-ref with |
49 | with response headers as second argument. |
56 | response headers as second argument. |
50 | |
57 | |
51 | 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 |
52 | response headers, the "pseudo-headers" "HTTPVersion", "Status" and |
59 | response headers, the "pseudo-headers" "HTTPVersion", "Status" and |
53 | "Reason" contain the three parts of the HTTP Status-Line of the same |
60 | "Reason" contain the three parts of the HTTP Status-Line of the same |
54 | name. The pseudo-header "URL" contains the original URL (which can |
61 | name. The pseudo-header "URL" contains the original URL (which can |
55 | differ from the requested URL when following redirects). |
62 | differ from the requested URL when following redirects). |
56 | |
63 | |
57 | If the server sends a header multiple lines, then their contents |
64 | If the server sends a header multiple times, then their contents |
58 | will be joined together with "\x00". |
65 | will be joined together with a comma (","), as per the HTTP spec. |
59 | |
66 | |
60 | If an internal error occurs, such as not being able to resolve a |
67 | If an internal error occurs, such as not being able to resolve a |
61 | hostname, then $data will be "undef", "$headers->{Status}" will be |
68 | hostname, then $data will be "undef", "$headers->{Status}" will be |
62 | 599 and the "Reason" pseudo-header will contain an error message. |
69 | "59x" (usually 599) and the "Reason" pseudo-header will contain an |
|
|
70 | error message. |
63 | |
71 | |
64 | A typical callback might look like this: |
72 | A typical callback might look like this: |
65 | |
73 | |
66 | sub { |
74 | sub { |
67 | my ($body, $hdr) = @_; |
75 | my ($body, $hdr) = @_; |
… | |
… | |
82 | |
90 | |
83 | headers => hashref |
91 | headers => hashref |
84 | The request headers to use. Currently, "http_request" may |
92 | The request headers to use. Currently, "http_request" may |
85 | provide its own "Host:", "Content-Length:", "Connection:" and |
93 | provide its own "Host:", "Content-Length:", "Connection:" and |
86 | "Cookie:" headers and will provide defaults for "User-Agent:" |
94 | "Cookie:" headers and will provide defaults for "User-Agent:" |
87 | and "Referer:". |
95 | and "Referer:" (this can be suppressed by using "undef" for |
|
|
96 | these headers in which case they won't be sent at all). |
88 | |
97 | |
89 | timeout => $seconds |
98 | timeout => $seconds |
90 | The time-out to use for various stages - each connect attempt |
99 | The time-out to use for various stages - each connect attempt |
91 | will reset the timeout, as will read or write activity. Default |
100 | will reset the timeout, as will read or write activity. Default |
92 | timeout is 5 minutes. |
101 | timeout is 5 minutes. |
… | |
… | |
94 | proxy => [$host, $port[, $scheme]] or undef |
103 | proxy => [$host, $port[, $scheme]] or undef |
95 | Use the given http proxy for all requests. If not specified, |
104 | Use the given http proxy for all requests. If not specified, |
96 | then the default proxy (as specified by $ENV{http_proxy}) is |
105 | then the default proxy (as specified by $ENV{http_proxy}) is |
97 | used. |
106 | used. |
98 | |
107 | |
99 | $scheme must be either missing or "http" for HTTP, or "https" |
108 | $scheme must be either missing, "http" for HTTP or "https" for |
100 | for HTTPS. |
109 | HTTPS. |
101 | |
110 | |
102 | body => $string |
111 | body => $string |
103 | The request body, usually empty. Will be-sent as-is (future |
112 | The request body, usually empty. Will be-sent as-is (future |
104 | versions of this module might offer more options). |
113 | versions of this module might offer more options). |
105 | |
114 | |
… | |
… | |
108 | loosely based on the original netscape specification. |
117 | loosely based on the original netscape specification. |
109 | |
118 | |
110 | The $hash_ref must be an (initially empty) hash reference which |
119 | The $hash_ref must be an (initially empty) hash reference which |
111 | will get updated automatically. It is possible to save the |
120 | will get updated automatically. It is possible to save the |
112 | cookie_jar to persistent storage with something like JSON or |
121 | cookie_jar to persistent storage with something like JSON or |
113 | Storable, but this is not recommended, as expire times are |
122 | Storable, but this is not recommended, as expiry times are |
114 | currently being ignored. |
123 | currently being ignored. |
115 | |
124 | |
116 | Note that this cookie implementation is not of very high |
125 | Note that this cookie implementation is not of very high |
117 | quality, nor meant to be complete. If you want complete cookie |
126 | quality, nor meant to be complete. If you want complete cookie |
118 | management you have to do that on your own. "cookie_jar" is |
127 | management you have to do that on your own. "cookie_jar" is |
119 | meant as a quick fix to get some cookie-using sites working. |
128 | meant as a quick fix to get some cookie-using sites working. |
120 | Cookies are a privacy disaster, do not use them unless required |
129 | Cookies are a privacy disaster, do not use them unless required |
121 | to. |
130 | to. |
|
|
131 | |
|
|
132 | tls_ctx => $scheme | $tls_ctx |
|
|
133 | Specifies the AnyEvent::TLS context to be used for https |
|
|
134 | connections. This parameter follows the same rules as the |
|
|
135 | "tls_ctx" parameter to AnyEvent::Handle, but additionally, the |
|
|
136 | two strings "low" or "high" can be specified, which give you a |
|
|
137 | predefined low-security (no verification, highest compatibility) |
|
|
138 | and high-security (CA and common-name verification) TLS context. |
|
|
139 | |
|
|
140 | The default for this option is "low", which could be interpreted |
|
|
141 | as "give me the page, no matter what". |
|
|
142 | |
|
|
143 | on_header => $callback->($headers) |
|
|
144 | When specified, this callback will be called with the header |
|
|
145 | hash as soon as headers have been successfully received from the |
|
|
146 | remote server (not on locally-generated errors). |
|
|
147 | |
|
|
148 | It has to return either true (in which case AnyEvent::HTTP will |
|
|
149 | continue), or false, in which case AnyEvent::HTTP will cancel |
|
|
150 | the download (and call the finish callback with an error code of |
|
|
151 | 598). |
|
|
152 | |
|
|
153 | This callback is useful, among other things, to quickly reject |
|
|
154 | unwanted content, which, if it is supposed to be rare, can be |
|
|
155 | faster than first doing a "HEAD" request. |
|
|
156 | |
|
|
157 | Example: cancel the request unless the content-type is |
|
|
158 | "text/html". |
|
|
159 | |
|
|
160 | on_header => sub { |
|
|
161 | $_[0]{"content-type"} =~ /^text\/html\s*(?:;|$)/ |
|
|
162 | }, |
|
|
163 | |
|
|
164 | on_body => $callback->($partial_body, $headers) |
|
|
165 | When specified, all body data will be passed to this callback |
|
|
166 | instead of to the completion callback. The completion callback |
|
|
167 | will get the empty string instead of the body data. |
|
|
168 | |
|
|
169 | It has to return either true (in which case AnyEvent::HTTP will |
|
|
170 | continue), or false, in which case AnyEvent::HTTP will cancel |
|
|
171 | the download (and call the completion callback with an error |
|
|
172 | code of 598). |
|
|
173 | |
|
|
174 | This callback is useful when the data is too large to be held in |
|
|
175 | memory (so the callback writes it to a file) or when only some |
|
|
176 | information should be extracted, or when the body should be |
|
|
177 | processed incrementally. |
|
|
178 | |
|
|
179 | It is usually preferred over doing your own body handling via |
|
|
180 | "want_body_handle", but in case of streaming APIs, where HTTP is |
|
|
181 | only used to create a connection, "want_body_handle" is the |
|
|
182 | better alternative, as it allows you to install your own event |
|
|
183 | handler, reducing resource usage. |
|
|
184 | |
|
|
185 | want_body_handle => $enable |
|
|
186 | When enabled (default is disabled), the behaviour of |
|
|
187 | AnyEvent::HTTP changes considerably: after parsing the headers, |
|
|
188 | and instead of downloading the body (if any), the completion |
|
|
189 | callback will be called. Instead of the $body argument |
|
|
190 | containing the body data, the callback will receive the |
|
|
191 | AnyEvent::Handle object associated with the connection. In error |
|
|
192 | cases, "undef" will be passed. When there is no body (e.g. |
|
|
193 | status 304), the empty string will be passed. |
|
|
194 | |
|
|
195 | The handle object might or might not be in TLS mode, might be |
|
|
196 | connected to a proxy, be a persistent connection etc., and |
|
|
197 | configured in unspecified ways. The user is responsible for this |
|
|
198 | handle (it will not be used by this module anymore). |
|
|
199 | |
|
|
200 | This is useful with some push-type services, where, after the |
|
|
201 | initial headers, an interactive protocol is used (typical |
|
|
202 | example would be the push-style twitter API which starts a |
|
|
203 | JSON/XML stream). |
|
|
204 | |
|
|
205 | If you think you need this, first have a look at "on_body", to |
|
|
206 | see if that doesn't solve your problem in a better way. |
122 | |
207 | |
123 | Example: make a simple HTTP GET request for http://www.nethype.de/ |
208 | Example: make a simple HTTP GET request for http://www.nethype.de/ |
124 | |
209 | |
125 | http_request GET => "http://www.nethype.de/", sub { |
210 | http_request GET => "http://www.nethype.de/", sub { |
126 | my ($body, $hdr) = @_; |
211 | my ($body, $hdr) = @_; |
… | |
… | |
138 | use Data::Dumper; |
223 | use Data::Dumper; |
139 | print Dumper $hdr; |
224 | print Dumper $hdr; |
140 | } |
225 | } |
141 | ; |
226 | ; |
142 | |
227 | |
|
|
228 | Example: make another simple HTTP GET request, but immediately try |
|
|
229 | to cancel it. |
|
|
230 | |
|
|
231 | my $request = http_request GET => "http://www.nethype.de/", sub { |
|
|
232 | my ($body, $hdr) = @_; |
|
|
233 | print "$body\n"; |
|
|
234 | }; |
|
|
235 | |
|
|
236 | undef $request; |
|
|
237 | |
143 | GLOBAL FUNCTIONS AND VARIABLES |
238 | GLOBAL FUNCTIONS AND VARIABLES |
144 | AnyEvent::HTTP::set_proxy "proxy-url" |
239 | AnyEvent::HTTP::set_proxy "proxy-url" |
145 | Sets the default proxy server to use. The proxy-url must begin with |
240 | Sets the default proxy server to use. The proxy-url must begin with |
146 | a string of the form "http://host:port" (optionally "https:..."). |
241 | a string of the form "http://host:port" (optionally "https:..."). |
147 | |
242 | |
148 | $AnyEvent::HTTP::MAX_RECURSE |
243 | $AnyEvent::HTTP::MAX_RECURSE |
149 | The default value for the "recurse" request parameter (default: 10). |
244 | The default value for the "recurse" request parameter (default: 10). |
150 | |
245 | |
151 | $AnyEvent::HTTP::USERAGENT |
246 | $AnyEvent::HTTP::USERAGENT |
152 | The default value for the "User-Agent" header (the default is |
247 | The default value for the "User-Agent" header (the default is |
153 | "Mozilla/5.0 (compatible; AnyEvent::HTTP/$VERSION; |
248 | "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; |
154 | +http://software.schmorp.de/pkg/AnyEvent)"). |
249 | +http://software.schmorp.de/pkg/AnyEvent)"). |
155 | |
250 | |
156 | $AnyEvent::HTTP::MAX_PERSISTENT |
251 | $AnyEvent::HTTP::MAX_PER_HOST |
157 | The maximum number of persistent connections to keep open (default: |
252 | The maximum number of concurrent connections to the same host |
158 | 8). |
253 | (identified by the hostname). If the limit is exceeded, then the |
|
|
254 | additional requests are queued until previous connections are |
|
|
255 | closed. |
159 | |
256 | |
160 | Not implemented currently. |
257 | The default value for this is 4, and it is highly advisable to not |
161 | |
258 | increase it. |
162 | $AnyEvent::HTTP::PERSISTENT_TIMEOUT |
|
|
163 | The maximum time to cache a persistent connection, in seconds |
|
|
164 | (default: 2). |
|
|
165 | |
|
|
166 | Not implemented currently. |
|
|
167 | |
259 | |
168 | $AnyEvent::HTTP::ACTIVE |
260 | $AnyEvent::HTTP::ACTIVE |
169 | The number of active connections. This is not the number of |
261 | The number of active connections. This is not the number of |
170 | currently running requests, but the number of currently open and |
262 | currently running requests, but the number of currently open and |
171 | non-idle TCP connections. This number of can be useful for |
263 | non-idle TCP connections. This number of can be useful for |
… | |
… | |
176 | |
268 | |
177 | AUTHOR |
269 | AUTHOR |
178 | Marc Lehmann <schmorp@schmorp.de> |
270 | Marc Lehmann <schmorp@schmorp.de> |
179 | http://home.schmorp.de/ |
271 | http://home.schmorp.de/ |
180 | |
272 | |
|
|
273 | With many thanks to Дмитрий Шалашов, who provided |
|
|
274 | countless testcases and bugreports. |
|
|
275 | |