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

Comparing AnyEvent-HTTP/README (file contents):
Revision 1.2 by root, Thu Jun 5 18:42:16 2008 UTC vs.
Revision 1.12 by root, Sat Dec 5 15:37:07 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines