ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-HTTP/README
(Generate patch)

Comparing AnyEvent-HTTP/README (file contents):
Revision 1.14 by root, Fri Dec 31 03:47:32 2010 UTC vs.
Revision 1.15 by root, Tue Jan 4 08:29:28 2011 UTC

51 gets destroyed before the callback is called, the request will be 51 gets destroyed before the callback is called, the request will be
52 cancelled. 52 cancelled.
53 53
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 (and trailers) 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" (uppercase to avoid clashing 59 response headers, the "pseudo-headers" (uppercase to avoid clashing
60 with possible response headers) "HTTPVersion", "Status" and "Reason" 60 with possible response headers) "HTTPVersion", "Status" and "Reason"
61 contain the three parts of the HTTP Status-Line of the same name. If 61 contain the three parts of the HTTP Status-Line of the same name. If
79 If the server sends a header multiple times, then their contents 79 If the server sends a header multiple times, then their contents
80 will be joined together with a comma (","), as per the HTTP spec. 80 will be joined together with a comma (","), as per the HTTP spec.
81 81
82 If an internal error occurs, such as not being able to resolve a 82 If an internal error occurs, such as not being able to resolve a
83 hostname, then $data will be "undef", "$headers->{Status}" will be 83 hostname, then $data will be "undef", "$headers->{Status}" will be
84 "59x" (usually 599) and the "Reason" pseudo-header will contain an 84 590-599 and the "Reason" pseudo-header will contain an error
85 error message. 85 message. Currently the following status codes are used:
86
87 595 - errors during connection etsbalishment, proxy handshake.
88 596 - errors during TLS negotiation, request sending and header
89 processing.
90 597 - errors during body receiving or processing.
91 598 - user aborted request via "on_header" or "on_body".
92 599 - other, usually nonretryable, errors (garbled URL etc.).
86 93
87 A typical callback might look like this: 94 A typical callback might look like this:
88 95
89 sub { 96 sub {
90 my ($body, $hdr) = @_; 97 my ($body, $hdr) = @_;
104 authentication retries and so on, and how often to do so. 111 authentication retries and so on, and how often to do so.
105 112
106 headers => hashref 113 headers => hashref
107 The request headers to use. Currently, "http_request" may 114 The request headers to use. Currently, "http_request" may
108 provide its own "Host:", "Content-Length:", "Connection:" and 115 provide its own "Host:", "Content-Length:", "Connection:" and
109 "Cookie:" headers and will provide defaults for "User-Agent:" 116 "Cookie:" headers and will provide defaults at least for "TE:",
110 and "Referer:" (this can be suppressed by using "undef" for 117 "Referer:" and "User-Agent:" (this can be suppressed by using
111 these headers in which case they won't be sent at all). 118 "undef" for these headers in which case they won't be sent at
119 all).
120
121 You really should provide your own "User-Agent:" header value
122 that is appropriate for your program - I wouldn't be surprised
123 if the default AnyEvent string gets blocked by webservers sooner
124 or later.
112 125
113 timeout => $seconds 126 timeout => $seconds
114 The time-out to use for various stages - each connect attempt 127 The time-out to use for various stages - each connect attempt
115 will reset the timeout, as will read or write activity, i.e. 128 will reset the timeout, as will read or write activity, i.e.
116 this is not an overall timeout. 129 this is not an overall timeout.
120 proxy => [$host, $port[, $scheme]] or undef 133 proxy => [$host, $port[, $scheme]] or undef
121 Use the given http proxy for all requests. If not specified, 134 Use the given http proxy for all requests. If not specified,
122 then the default proxy (as specified by $ENV{http_proxy}) is 135 then the default proxy (as specified by $ENV{http_proxy}) is
123 used. 136 used.
124 137
125 $scheme must be either missing, "http" for HTTP or "https" for 138 $scheme must be either missing or must be "http" for HTTP.
126 HTTPS.
127 139
128 body => $string 140 body => $string
129 The request body, usually empty. Will be-sent as-is (future 141 The request body, usually empty. Will be sent as-is (future
130 versions of this module might offer more options). 142 versions of this module might offer more options).
131 143
132 cookie_jar => $hash_ref 144 cookie_jar => $hash_ref
133 Passing this parameter enables (simplified) cookie-processing, 145 Passing this parameter enables (simplified) cookie-processing,
134 loosely based on the original netscape specification. 146 loosely based on the original netscape specification.
135 147
136 The $hash_ref must be an (initially empty) hash reference which 148 The $hash_ref must be an (initially empty) hash reference which
137 will get updated automatically. It is possible to save the 149 will get updated automatically. It is possible to save the
138 cookie_jar to persistent storage with something like JSON or 150 cookie jar to persistent storage with something like JSON or
139 Storable, but this is not recommended, as expiry times are 151 Storable - see the "AnyEvent::HTTP::cookie_jar_expire" function
140 currently being ignored. 152 if you wish to remove expired or session-only cookies, and also
153 for documentation on the format of the cookie jar.
141 154
142 Note that this cookie implementation is not of very high 155 Note that this cookie implementation is not meant to be
143 quality, nor meant to be complete. If you want complete cookie 156 complete. If you want complete cookie management you have to do
144 management you have to do that on your own. "cookie_jar" is 157 that on your own. "cookie_jar" is meant as a quick fix to get
145 meant as a quick fix to get some cookie-using sites working. 158 most cookie-using sites working. Cookies are a privacy disaster,
146 Cookies are a privacy disaster, do not use them unless required 159 do not use them unless required to.
147 to. 160
161 When cookie processing is enabled, the "Cookie:" and
162 "Set-Cookie:" headers will be set and handled by this module,
163 otherwise they will be left untouched.
148 164
149 tls_ctx => $scheme | $tls_ctx 165 tls_ctx => $scheme | $tls_ctx
150 Specifies the AnyEvent::TLS context to be used for https 166 Specifies the AnyEvent::TLS context to be used for https
151 connections. This parameter follows the same rules as the 167 connections. This parameter follows the same rules as the
152 "tls_ctx" parameter to AnyEvent::Handle, but additionally, the 168 "tls_ctx" parameter to AnyEvent::Handle, but additionally, the
154 predefined low-security (no verification, highest compatibility) 170 predefined low-security (no verification, highest compatibility)
155 and high-security (CA and common-name verification) TLS context. 171 and high-security (CA and common-name verification) TLS context.
156 172
157 The default for this option is "low", which could be interpreted 173 The default for this option is "low", which could be interpreted
158 as "give me the page, no matter what". 174 as "give me the page, no matter what".
175
176 See also the "sessionid" parameter.
177
178 session => $string
179 The module might reuse connections to the same host internally.
180 Sometimes (e.g. when using TLS), you do not want to reuse
181 connections from other sessions. This can be achieved by setting
182 this parameter to some unique ID (such as the address of an
183 object storing your state data, or the TLS context) - only
184 connections using the same unique ID will be reused.
159 185
160 on_prepare => $callback->($fh) 186 on_prepare => $callback->($fh)
161 In rare cases you need to "tune" the socket before it is used to 187 In rare cases you need to "tune" the socket before it is used to
162 connect (for exmaple, to bind it on a given IP address). This 188 connect (for exmaple, to bind it on a given IP address). This
163 parameter overrides the prepare callback passed to 189 parameter overrides the prepare callback passed to
192 218
193 This callback is useful, among other things, to quickly reject 219 This callback is useful, among other things, to quickly reject
194 unwanted content, which, if it is supposed to be rare, can be 220 unwanted content, which, if it is supposed to be rare, can be
195 faster than first doing a "HEAD" request. 221 faster than first doing a "HEAD" request.
196 222
223 The downside is that cancelling the request makes it impossible
224 to re-use the connection. Also, the "on_header" callback will
225 not receive any trailer (headers sent after the response body).
226
197 Example: cancel the request unless the content-type is 227 Example: cancel the request unless the content-type is
198 "text/html". 228 "text/html".
199 229
200 on_header => sub { 230 on_header => sub {
201 $_[0]{"content-type"} =~ /^text\/html\s*(?:;|$)/ 231 $_[0]{"content-type"} =~ /^text\/html\s*(?:;|$)/
208 238
209 It has to return either true (in which case AnyEvent::HTTP will 239 It has to return either true (in which case AnyEvent::HTTP will
210 continue), or false, in which case AnyEvent::HTTP will cancel 240 continue), or false, in which case AnyEvent::HTTP will cancel
211 the download (and call the completion callback with an error 241 the download (and call the completion callback with an error
212 code of 598). 242 code of 598).
243
244 The downside to cancelling the request is that it makes it
245 impossible to re-use the connection.
213 246
214 This callback is useful when the data is too large to be held in 247 This callback is useful when the data is too large to be held in
215 memory (so the callback writes it to a file) or when only some 248 memory (so the callback writes it to a file) or when only some
216 information should be extracted, or when the body should be 249 information should be extracted, or when the body should be
217 processed incrementally. 250 processed incrementally.
231 AnyEvent::Handle object associated with the connection. In error 264 AnyEvent::Handle object associated with the connection. In error
232 cases, "undef" will be passed. When there is no body (e.g. 265 cases, "undef" will be passed. When there is no body (e.g.
233 status 304), the empty string will be passed. 266 status 304), the empty string will be passed.
234 267
235 The handle object might or might not be in TLS mode, might be 268 The handle object might or might not be in TLS mode, might be
236 connected to a proxy, be a persistent connection etc., and 269 connected to a proxy, be a persistent connection, use chunked
237 configured in unspecified ways. The user is responsible for this 270 transfer encoding etc., and configured in unspecified ways. The
238 handle (it will not be used by this module anymore). 271 user is responsible for this handle (it will not be used by this
272 module anymore).
239 273
240 This is useful with some push-type services, where, after the 274 This is useful with some push-type services, where, after the
241 initial headers, an interactive protocol is used (typical 275 initial headers, an interactive protocol is used (typical
242 example would be the push-style twitter API which starts a 276 example would be the push-style twitter API which starts a
243 JSON/XML stream). 277 JSON/XML stream).
244 278
245 If you think you need this, first have a look at "on_body", to 279 If you think you need this, first have a look at "on_body", to
246 see if that doesn't solve your problem in a better way. 280 see if that doesn't solve your problem in a better way.
247 281
282 persistent => $boolean
283 Try to create/reuse a persistent connection. When this flag is
284 set (default: true for idempotent requests, false for all
285 others), then "http_request" tries to re-use an existing
286 (previously-created) persistent connection to the host and,
287 failing that, tries to create a new one.
288
289 Requests failing in certain ways will be automatically retried
290 once, which is dangerous for non-idempotent requests, which is
291 why it defaults to off for them. The reason for this is because
292 the bozos who designed HTTP/1.1 made it impossible to
293 distinguish between a fatal error and a normal connection
294 timeout, so you never know whether there was a problem with your
295 request or not.
296
297 When reusing an existent connection, many parameters (such as
298 TLS context) will be ignored. See the "session" parameter for a
299 workaround.
300
301 keepalive => $boolean
302 Only used when "persistent" is also true. This parameter decides
303 whether "http_request" tries to handshake a HTTP/1.0-style
304 keep-alive connection (as opposed to only a HTTP/1.1 persistent
305 connection).
306
307 The default is true, except when using a proxy, in which case it
308 defaults to false, as HTTP/1.0 proxies cannot support this in a
309 meaningful way.
310
311 handle_params => { key => value ... }
312 The key-value pairs in this hash will be passed to any
313 AnyEvent::Handle constructor that is called - not all requests
314 will create a handle, and sometimes more than one is created, so
315 this parameter is only good for setting hints.
316
317 Example: set the maximum read size to 4096, to potentially
318 conserve memory at the cost of speed.
319
320 handle_params => {
321 max_read_size => 4096,
322 },
323
248 Example: make a simple HTTP GET request for http://www.nethype.de/ 324 Example: do a simple HTTP GET request for http://www.nethype.de/ and
325 print the response body.
249 326
250 http_request GET => "http://www.nethype.de/", sub { 327 http_request GET => "http://www.nethype.de/", sub {
251 my ($body, $hdr) = @_; 328 my ($body, $hdr) = @_;
252 print "$body\n"; 329 print "$body\n";
253 }; 330 };
254 331
255 Example: make a HTTP HEAD request on https://www.google.com/, use a 332 Example: do a HTTP HEAD request on https://www.google.com/, use a
256 timeout of 30 seconds. 333 timeout of 30 seconds.
257 334
258 http_request 335 http_request
259 GET => "https://www.google.com", 336 GET => "https://www.google.com",
337 headers => { "user-agent" => "MySearchClient 1.0" },
260 timeout => 30, 338 timeout => 30,
261 sub { 339 sub {
262 my ($body, $hdr) = @_; 340 my ($body, $hdr) = @_;
263 use Data::Dumper; 341 use Data::Dumper;
264 print Dumper $hdr; 342 print Dumper $hdr;
265 } 343 }
266 ; 344 ;
267 345
268 Example: make another simple HTTP GET request, but immediately try 346 Example: do another simple HTTP GET request, but immediately try to
269 to cancel it. 347 cancel it.
270 348
271 my $request = http_request GET => "http://www.nethype.de/", sub { 349 my $request = http_request GET => "http://www.nethype.de/", sub {
272 my ($body, $hdr) = @_; 350 my ($body, $hdr) = @_;
273 print "$body\n"; 351 print "$body\n";
274 }; 352 };
279 AnyEvent::HTTP uses the AnyEvent::Socket::tcp_connect function for the 357 AnyEvent::HTTP uses the AnyEvent::Socket::tcp_connect function for the
280 actual connection, which in turn uses AnyEvent::DNS to resolve 358 actual connection, which in turn uses AnyEvent::DNS to resolve
281 hostnames. The latter is a simple stub resolver and does no caching on 359 hostnames. The latter is a simple stub resolver and does no caching on
282 its own. If you want DNS caching, you currently have to provide your own 360 its own. If you want DNS caching, you currently have to provide your own
283 default resolver (by storing a suitable resolver object in 361 default resolver (by storing a suitable resolver object in
284 $AnyEvent::DNS::RESOLVER). 362 $AnyEvent::DNS::RESOLVER) or your own "tcp_connect" callback.
285 363
286 GLOBAL FUNCTIONS AND VARIABLES 364 GLOBAL FUNCTIONS AND VARIABLES
287 AnyEvent::HTTP::set_proxy "proxy-url" 365 AnyEvent::HTTP::set_proxy "proxy-url"
288 Sets the default proxy server to use. The proxy-url must begin with 366 Sets the default proxy server to use. The proxy-url must begin with
289 a string of the form "http://host:port" (optionally "https:..."), 367 a string of the form "http://host:port", croaks otherwise.
290 croaks otherwise.
291 368
292 To clear an already-set proxy, use "undef". 369 To clear an already-set proxy, use "undef".
370
371 AnyEvent::HTTP::cookie_jar_expire $jar[, $session_end]
372 Remove all cookies from the cookie jar that have been expired. If
373 $session_end is given and true, then additionally remove all session
374 cookies.
375
376 You should call this function (with a true $session_end) before you
377 save cookies to disk, and you should call this function after
378 loading them again. If you have a long-running program you can
379 additonally call this function from time to time.
380
381 A cookie jar is initially an empty hash-reference that is managed by
382 this module. It's format is subject to change, but currently it is
383 like this:
384
385 The key "version" has to contain 1, otherwise the hash gets emptied.
386 All other keys are hostnames or IP addresses pointing to
387 hash-references. The key for these inner hash references is the
388 server path for which this cookie is meant, and the values are again
389 hash-references. The keys of those hash-references is the cookie
390 name, and the value, you guessed it, is another hash-reference, this
391 time with the key-value pairs from the cookie, except for "expires"
392 and "max-age", which have been replaced by a "_expires" key that
393 contains the cookie expiry timestamp.
394
395 Here is an example of a cookie jar with a single cookie, so you have
396 a chance of understanding the above paragraph:
397
398 {
399 version => 1,
400 "10.0.0.1" => {
401 "/" => {
402 "mythweb_id" => {
403 _expires => 1293917923,
404 value => "ooRung9dThee3ooyXooM1Ohm",
405 },
406 },
407 },
408 }
293 409
294 $date = AnyEvent::HTTP::format_date $timestamp 410 $date = AnyEvent::HTTP::format_date $timestamp
295 Takes a POSIX timestamp (seconds since the epoch) and formats it as 411 Takes a POSIX timestamp (seconds since the epoch) and formats it as
296 a HTTP Date (RFC 2616). 412 a HTTP Date (RFC 2616).
297 413
298 $timestamp = AnyEvent::HTTP::parse_date $date 414 $timestamp = AnyEvent::HTTP::parse_date $date
299 Takes a HTTP Date (RFC 2616) and returns the corresponding POSIX 415 Takes a HTTP Date (RFC 2616) or a Cookie date (netscape cookie spec)
416 or a bunch of minor variations of those, and returns the
300 timestamp, or "undef" if the date cannot be parsed. 417 corresponding POSIX timestamp, or "undef" if the date cannot be
418 parsed.
301 419
302 $AnyEvent::HTTP::MAX_RECURSE 420 $AnyEvent::HTTP::MAX_RECURSE
303 The default value for the "recurse" request parameter (default: 10). 421 The default value for the "recurse" request parameter (default: 10).
422
423 $AnyEvent::HTTP::TIMEOUT
424 The default timeout for conenction operations (default: 300).
304 425
305 $AnyEvent::HTTP::USERAGENT 426 $AnyEvent::HTTP::USERAGENT
306 The default value for the "User-Agent" header (the default is 427 The default value for the "User-Agent" header (the default is
307 "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; 428 "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION;
308 +http://software.schmorp.de/pkg/AnyEvent)"). 429 +http://software.schmorp.de/pkg/AnyEvent)").
309 430
310 $AnyEvent::HTTP::MAX_PER_HOST 431 $AnyEvent::HTTP::MAX_PER_HOST
311 The maximum number of concurrent connections to the same host 432 The maximum number of concurrent connections to the same host
312 (identified by the hostname). If the limit is exceeded, then the 433 (identified by the hostname). If the limit is exceeded, then the
313 additional requests are queued until previous connections are 434 additional requests are queued until previous connections are
314 closed. 435 closed. Both persistent and non-persistent connections are counted
436 in this limit.
315 437
316 The default value for this is 4, and it is highly advisable to not 438 The default value for this is 4, and it is highly advisable to not
317 increase it. 439 increase it much.
440
441 For comparison: the RFC's recommend 4 non-persistent or 2 persistent
442 connections, older browsers used 2, newers (such as firefox 3)
443 typically use 6, and Opera uses 8 because like, they have the
444 fastest browser and give a shit for everybody else on the planet.
445
446 $AnyEvent::HTTP::PERSISTENT_TIMEOUT
447 The time after which idle persistent conenctions get closed by
448 AnyEvent::HTTP (default: 3).
318 449
319 $AnyEvent::HTTP::ACTIVE 450 $AnyEvent::HTTP::ACTIVE
320 The number of active connections. This is not the number of 451 The number of active connections. This is not the number of
321 currently running requests, but the number of currently open and 452 currently running requests, but the number of currently open and
322 non-idle TCP connections. This number of can be useful for 453 non-idle TCP connections. This number can be useful for
323 load-leveling. 454 load-leveling.
324 455
325 SOCKS PROXIES 456 SOCKS PROXIES
326 Socks proxies are not directly supported by AnyEvent::HTTP. You can 457 Socks proxies are not directly supported by AnyEvent::HTTP. You can
327 compile your perl to support socks, or use an external program such as 458 compile your perl to support socks, or use an external program such as

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines