ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-HTTP/README
Revision: 1.10
Committed: Wed Aug 5 16:43:47 2009 UTC (14 years, 9 months ago) by root
Branch: MAIN
CVS Tags: rel-1_42
Changes since 1.9: +5 -4 lines
Log Message:
1.42

File Contents

# User Rev Content
1 root 1.1 NAME
2 root 1.2 AnyEvent::HTTP - simple but non-blocking HTTP/HTTPS client
3 root 1.1
4     SYNOPSIS
5 root 1.2 use AnyEvent::HTTP;
6 root 1.1
7 root 1.3 http_get "http://www.nethype.de/", sub { print $_[1] };
8    
9     # ... do something else here
10    
11 root 1.1 DESCRIPTION
12     This module is an AnyEvent user, you need to make sure that you use and
13     run a supported event loop.
14    
15 root 1.2 This module implements a simple, stateless and non-blocking HTTP client.
16     It supports GET, POST and other request methods, cookies and more, all
17     on a very low level. It can follow redirects supports proxies and
18     automatically limits the number of connections to the values specified
19     in the RFC.
20    
21     It should generally be a "good client" that is enough for most HTTP
22     tasks. Simple tasks should be simple, but complex tasks should still be
23     possible as the user retains control over request and response headers.
24    
25     The caller is responsible for authentication management, cookies (if the
26     simplistic implementation in this module doesn't suffice), referer and
27     other high-level protocol details for which this module offers only
28     limited support.
29    
30     METHODS
31     http_get $url, key => value..., $cb->($data, $headers)
32     Executes an HTTP-GET request. See the http_request function for
33 root 1.5 details on additional parameters and the return value.
34 root 1.2
35     http_head $url, key => value..., $cb->($data, $headers)
36     Executes an HTTP-HEAD request. See the http_request function for
37 root 1.5 details on additional parameters and the return value.
38 root 1.2
39     http_post $url, $body, key => value..., $cb->($data, $headers)
40 root 1.4 Executes an HTTP-POST request with a request body of $body. See the
41 root 1.5 http_request function for details on additional parameters and the
42     return value.
43 root 1.2
44     http_request $method => $url, key => value..., $cb->($data, $headers)
45     Executes a HTTP request of type $method (e.g. "GET", "POST"). The
46     URL must be an absolute http or https URL.
47    
48 root 1.5 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    
54 root 1.8 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
56     response headers as second argument.
57 root 1.2
58     All the headers in that hash are lowercased. In addition to the
59 root 1.3 response headers, the "pseudo-headers" "HTTPVersion", "Status" and
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    
64 root 1.6 If the server sends a header multiple times, then their contents
65     will be joined together with a comma (","), as per the HTTP spec.
66 root 1.2
67     If an internal error occurs, such as not being able to resolve a
68     hostname, then $data will be "undef", "$headers->{Status}" will be
69 root 1.8 "59x" (usually 599) and the "Reason" pseudo-header will contain an
70     error message.
71 root 1.2
72     A typical callback might look like this:
73    
74     sub {
75     my ($body, $hdr) = @_;
76    
77     if ($hdr->{Status} =~ /^2/) {
78     ... everything should be ok
79     } else {
80     print "error, $hdr->{Status} $hdr->{Reason}\n";
81     }
82     }
83    
84     Additional parameters are key-value pairs, and are fully optional.
85     They include:
86    
87     recurse => $count (default: $MAX_RECURSE)
88     Whether to recurse requests or not, e.g. on redirects,
89     authentication retries and so on, and how often to do so.
90    
91     headers => hashref
92     The request headers to use. Currently, "http_request" may
93     provide its own "Host:", "Content-Length:", "Connection:" and
94     "Cookie:" headers and will provide defaults for "User-Agent:"
95 root 1.10 and "Referer:" (this can be suppressed by using "undef" for
96     these headers in which case they won't be sent at all).
97 root 1.2
98     timeout => $seconds
99     The time-out to use for various stages - each connect attempt
100     will reset the timeout, as will read or write activity. Default
101     timeout is 5 minutes.
102    
103     proxy => [$host, $port[, $scheme]] or undef
104     Use the given http proxy for all requests. If not specified,
105     then the default proxy (as specified by $ENV{http_proxy}) is
106     used.
107    
108 root 1.10 $scheme must be either missing, "http" for HTTP or "https" for
109     HTTPS.
110 root 1.2
111     body => $string
112     The request body, usually empty. Will be-sent as-is (future
113     versions of this module might offer more options).
114    
115     cookie_jar => $hash_ref
116     Passing this parameter enables (simplified) cookie-processing,
117     loosely based on the original netscape specification.
118    
119     The $hash_ref must be an (initially empty) hash reference which
120     will get updated automatically. It is possible to save the
121     cookie_jar to persistent storage with something like JSON or
122 root 1.8 Storable, but this is not recommended, as expiry times are
123 root 1.2 currently being ignored.
124    
125     Note that this cookie implementation is not of very high
126     quality, nor meant to be complete. If you want complete cookie
127     management you have to do that on your own. "cookie_jar" is
128     meant as a quick fix to get some cookie-using sites working.
129     Cookies are a privacy disaster, do not use them unless required
130     to.
131    
132 root 1.8 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 root 1.9 "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 root 1.8
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 root 1.9 see if that doesn't solve your problem in a better way.
207 root 1.8
208 root 1.2 Example: make a simple HTTP GET request for http://www.nethype.de/
209    
210     http_request GET => "http://www.nethype.de/", sub {
211     my ($body, $hdr) = @_;
212     print "$body\n";
213     };
214    
215     Example: make a HTTP HEAD request on https://www.google.com/, use a
216     timeout of 30 seconds.
217    
218     http_request
219     GET => "https://www.google.com",
220     timeout => 30,
221     sub {
222     my ($body, $hdr) = @_;
223     use Data::Dumper;
224     print Dumper $hdr;
225     }
226     ;
227    
228 root 1.5 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    
238 root 1.2 GLOBAL FUNCTIONS AND VARIABLES
239     AnyEvent::HTTP::set_proxy "proxy-url"
240     Sets the default proxy server to use. The proxy-url must begin with
241     a string of the form "http://host:port" (optionally "https:...").
242    
243     $AnyEvent::HTTP::MAX_RECURSE
244     The default value for the "recurse" request parameter (default: 10).
245    
246     $AnyEvent::HTTP::USERAGENT
247     The default value for the "User-Agent" header (the default is
248 root 1.8 "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION;
249 root 1.2 +http://software.schmorp.de/pkg/AnyEvent)").
250    
251 root 1.8 $AnyEvent::HTTP::MAX_PER_HOST
252 root 1.10 The maximum number of concurrent connections to the same host
253 root 1.8 (identified by the hostname). If the limit is exceeded, then the
254     additional requests are queued until previous connections are
255     closed.
256 root 1.2
257 root 1.8 The default value for this is 4, and it is highly advisable to not
258     increase it.
259 root 1.2
260     $AnyEvent::HTTP::ACTIVE
261     The number of active connections. This is not the number of
262     currently running requests, but the number of currently open and
263     non-idle TCP connections. This number of can be useful for
264     load-leveling.
265 root 1.1
266     SEE ALSO
267 root 1.2 AnyEvent.
268 root 1.1
269     AUTHOR
270 root 1.3 Marc Lehmann <schmorp@schmorp.de>
271     http://home.schmorp.de/
272 root 1.1
273 root 1.7 With many thanks to Дмитрий Шалашов, who provided
274     countless testcases and bugreports.
275