ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-HTTP/README
Revision: 1.4
Committed: Thu Jul 24 06:01:10 2008 UTC (16 years, 4 months ago) by root
Branch: MAIN
CVS Tags: rel-1_04, rel-1_05
Changes since 1.3: +1 -1 lines
Log Message:
1.04

File Contents

# Content
1 NAME
2 AnyEvent::HTTP - simple but non-blocking HTTP/HTTPS client
3
4 SYNOPSIS
5 use AnyEvent::HTTP;
6
7 http_get "http://www.nethype.de/", sub { print $_[1] };
8
9 # ... do something else here
10
11 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 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 details on additional parameters.
34
35 http_head $url, key => value..., $cb->($data, $headers)
36 Executes an HTTP-HEAD request. See the http_request function for
37 details on additional parameters.
38
39 http_post $url, $body, key => value..., $cb->($data, $headers)
40 Executes an HTTP-POST request with a request body of $body. See the
41 http_request function for details on additional parameters.
42
43 http_request $method => $url, key => value..., $cb->($data, $headers)
44 Executes a HTTP request of type $method (e.g. "GET", "POST"). The
45 URL must be an absolute http or https URL.
46
47 The callback will be called with the response data as first argument
48 (or "undef" if it wasn't available due to errors), and a hash-ref
49 with response headers as second argument.
50
51 All the headers in that hash are lowercased. In addition to the
52 response headers, the "pseudo-headers" "HTTPVersion", "Status" and
53 "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
55 differ from the requested URL when following redirects).
56
57 If the server sends a header multiple lines, then their contents
58 will be joined together with "\x00".
59
60 If an internal error occurs, such as not being able to resolve a
61 hostname, then $data will be "undef", "$headers->{Status}" will be
62 599 and the "Reason" pseudo-header will contain an error message.
63
64 A typical callback might look like this:
65
66 sub {
67 my ($body, $hdr) = @_;
68
69 if ($hdr->{Status} =~ /^2/) {
70 ... everything should be ok
71 } else {
72 print "error, $hdr->{Status} $hdr->{Reason}\n";
73 }
74 }
75
76 Additional parameters are key-value pairs, and are fully optional.
77 They include:
78
79 recurse => $count (default: $MAX_RECURSE)
80 Whether to recurse requests or not, e.g. on redirects,
81 authentication retries and so on, and how often to do so.
82
83 headers => hashref
84 The request headers to use. Currently, "http_request" may
85 provide its own "Host:", "Content-Length:", "Connection:" and
86 "Cookie:" headers and will provide defaults for "User-Agent:"
87 and "Referer:".
88
89 timeout => $seconds
90 The time-out to use for various stages - each connect attempt
91 will reset the timeout, as will read or write activity. Default
92 timeout is 5 minutes.
93
94 proxy => [$host, $port[, $scheme]] or undef
95 Use the given http proxy for all requests. If not specified,
96 then the default proxy (as specified by $ENV{http_proxy}) is
97 used.
98
99 $scheme must be either missing or "http" for HTTP, or "https"
100 for HTTPS.
101
102 body => $string
103 The request body, usually empty. Will be-sent as-is (future
104 versions of this module might offer more options).
105
106 cookie_jar => $hash_ref
107 Passing this parameter enables (simplified) cookie-processing,
108 loosely based on the original netscape specification.
109
110 The $hash_ref must be an (initially empty) hash reference which
111 will get updated automatically. It is possible to save the
112 cookie_jar to persistent storage with something like JSON or
113 Storable, but this is not recommended, as expire times are
114 currently being ignored.
115
116 Note that this cookie implementation is not of very high
117 quality, nor meant to be complete. If you want complete cookie
118 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.
120 Cookies are a privacy disaster, do not use them unless required
121 to.
122
123 Example: make a simple HTTP GET request for http://www.nethype.de/
124
125 http_request GET => "http://www.nethype.de/", sub {
126 my ($body, $hdr) = @_;
127 print "$body\n";
128 };
129
130 Example: make a HTTP HEAD request on https://www.google.com/, use a
131 timeout of 30 seconds.
132
133 http_request
134 GET => "https://www.google.com",
135 timeout => 30,
136 sub {
137 my ($body, $hdr) = @_;
138 use Data::Dumper;
139 print Dumper $hdr;
140 }
141 ;
142
143 GLOBAL FUNCTIONS AND VARIABLES
144 AnyEvent::HTTP::set_proxy "proxy-url"
145 Sets the default proxy server to use. The proxy-url must begin with
146 a string of the form "http://host:port" (optionally "https:...").
147
148 $AnyEvent::HTTP::MAX_RECURSE
149 The default value for the "recurse" request parameter (default: 10).
150
151 $AnyEvent::HTTP::USERAGENT
152 The default value for the "User-Agent" header (the default is
153 "Mozilla/5.0 (compatible; AnyEvent::HTTP/$VERSION;
154 +http://software.schmorp.de/pkg/AnyEvent)").
155
156 $AnyEvent::HTTP::MAX_PERSISTENT
157 The maximum number of persistent connections to keep open (default:
158 8).
159
160 Not implemented currently.
161
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
168 $AnyEvent::HTTP::ACTIVE
169 The number of active connections. This is not the number of
170 currently running requests, but the number of currently open and
171 non-idle TCP connections. This number of can be useful for
172 load-leveling.
173
174 SEE ALSO
175 AnyEvent.
176
177 AUTHOR
178 Marc Lehmann <schmorp@schmorp.de>
179 http://home.schmorp.de/
180