… | |
… | |
34 | our $VERSION = '1.0'; |
34 | our $VERSION = '1.0'; |
35 | |
35 | |
36 | our @EXPORT = qw(http_get http_request); |
36 | our @EXPORT = qw(http_get http_request); |
37 | |
37 | |
38 | our $USERAGENT = "Mozilla/5.0 (compatible; AnyEvent::HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; |
38 | our $USERAGENT = "Mozilla/5.0 (compatible; AnyEvent::HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; |
39 | our $MAX_REDIRECTS = 10; |
39 | our $MAX_RECURSE = 10; |
40 | our $MAX_PERSISTENT = 8; |
40 | our $MAX_PERSISTENT = 8; |
41 | our $PERSISTENT_TIMEOUT = 2; |
41 | our $PERSISTENT_TIMEOUT = 2; |
42 | our $TIMEOUT = 300; |
42 | our $TIMEOUT = 300; |
43 | |
43 | |
44 | # changing these is evil |
44 | # changing these is evil |
… | |
… | |
51 | |
51 | |
52 | =item http_get $url, key => value..., $cb->($data, $headers) |
52 | =item http_get $url, key => value..., $cb->($data, $headers) |
53 | |
53 | |
54 | Executes an HTTP-GET request. See the http_request function for details on |
54 | Executes an HTTP-GET request. See the http_request function for details on |
55 | additional parameters. |
55 | additional parameters. |
|
|
56 | |
|
|
57 | =item http_get $url, $body, key => value..., $cb->($data, $headers) |
|
|
58 | |
|
|
59 | Executes an HTTP-POST request with a requets body of C<$bod>. See the |
|
|
60 | http_request function for details on additional parameters. |
56 | |
61 | |
57 | =item http_request $method => $url, key => value..., $cb->($data, $headers) |
62 | =item http_request $method => $url, key => value..., $cb->($data, $headers) |
58 | |
63 | |
59 | Executes a HTTP request of type C<$method> (e.g. C<GET>, C<POST>). The URL |
64 | Executes a HTTP request of type C<$method> (e.g. C<GET>, C<POST>). The URL |
60 | must be an absolute http or https URL. |
65 | must be an absolute http or https URL. |
… | |
… | |
75 | Additional parameters are key-value pairs, and are fully optional. They |
80 | Additional parameters are key-value pairs, and are fully optional. They |
76 | include: |
81 | include: |
77 | |
82 | |
78 | =over 4 |
83 | =over 4 |
79 | |
84 | |
80 | =item recurse => $boolean (default: true) |
85 | =item recurse => $count (default: $MAX_RECURSE) |
81 | |
86 | |
82 | Whether to recurse requests or not, e.g. on redirects, authentication |
87 | Whether to recurse requests or not, e.g. on redirects, authentication |
83 | retries and so on. |
88 | retries and so on, and how often to do so. |
84 | |
89 | |
85 | =item headers => hashref |
90 | =item headers => hashref |
86 | |
91 | |
87 | The request headers to use. |
92 | The request headers to use. |
88 | |
93 | |
… | |
… | |
96 | Use the given http proxy for all requests. If not specified, then the |
101 | Use the given http proxy for all requests. If not specified, then the |
97 | default proxy (as specified by C<$ENV{http_proxy}>) is used. |
102 | default proxy (as specified by C<$ENV{http_proxy}>) is used. |
98 | |
103 | |
99 | C<$scheme> must be either missing or C<http> for HTTP, or C<https> for |
104 | C<$scheme> must be either missing or C<http> for HTTP, or C<https> for |
100 | HTTPS. |
105 | HTTPS. |
|
|
106 | |
|
|
107 | =item body => $string |
|
|
108 | |
|
|
109 | The request body, usually empty. Will be-sent as-is (future versions of |
|
|
110 | this module might offer more options). |
101 | |
111 | |
102 | =back |
112 | =back |
103 | |
113 | |
104 | =back |
114 | =back |
105 | |
115 | |
… | |
… | |
108 | sub http_request($$$;@) { |
118 | sub http_request($$$;@) { |
109 | my $cb = pop; |
119 | my $cb = pop; |
110 | my ($method, $url, %arg) = @_; |
120 | my ($method, $url, %arg) = @_; |
111 | |
121 | |
112 | my %hdr; |
122 | my %hdr; |
|
|
123 | |
|
|
124 | $method = uc $method; |
113 | |
125 | |
114 | if (my $hdr = delete $arg{headers}) { |
126 | if (my $hdr = delete $arg{headers}) { |
115 | while (my ($k, $v) = each %$hdr) { |
127 | while (my ($k, $v) = each %$hdr) { |
116 | $hdr{lc $k} = $v; |
128 | $hdr{lc $k} = $v; |
117 | } |
129 | } |
118 | } |
130 | } |
119 | |
131 | |
120 | my $proxy = $arg{proxy} || $PROXY; |
132 | my $proxy = $arg{proxy} || $PROXY; |
121 | my $timeout = $arg{timeout} || $TIMEOUT; |
133 | my $timeout = $arg{timeout} || $TIMEOUT; |
|
|
134 | my $recurse = exists $arg{recurse} ? $arg{recurse} : $MAX_RECURSE; |
122 | |
135 | |
123 | $hdr{"user-agent"} ||= $USERAGENT; |
136 | $hdr{"user-agent"} ||= $USERAGENT; |
124 | |
137 | |
125 | my ($host, $port, $path, $scheme); |
138 | my ($host, $port, $path, $scheme); |
126 | |
139 | |
… | |
… | |
151 | |
164 | |
152 | $scheme = lc $scheme; |
165 | $scheme = lc $scheme; |
153 | |
166 | |
154 | my %state; |
167 | my %state; |
155 | |
168 | |
156 | my $body = ""; |
|
|
157 | $state{body} = $body; |
169 | $state{body} = delete $arg{body}; |
158 | |
170 | |
159 | $hdr{"content-length"} = length $body; |
171 | $hdr{"content-length"} = length $state{body}; |
160 | |
172 | |
161 | $state{connect_guard} = AnyEvent::Socket::tcp_connect $host, $port, sub { |
173 | $state{connect_guard} = AnyEvent::Socket::tcp_connect $host, $port, sub { |
162 | $state{fh} = shift |
174 | $state{fh} = shift |
163 | or return $cb->(undef, { Status => 599, Reason => "$!" }); |
175 | or return $cb->(undef, { Status => 599, Reason => "$!" }); |
164 | |
176 | |
… | |
… | |
190 | $cb->(undef, { Status => 599, Reason => "unexpected end-of-file" }); |
202 | $cb->(undef, { Status => 599, Reason => "unexpected end-of-file" }); |
191 | }); |
203 | }); |
192 | |
204 | |
193 | # send request |
205 | # send request |
194 | $state{handle}->push_write ( |
206 | $state{handle}->push_write ( |
195 | "\U$method\E $path HTTP/1.0\015\012" |
207 | "$method $path HTTP/1.0\015\012" |
196 | . (join "", map "$_: $hdr{$_}\015\012", keys %hdr) |
208 | . (join "", map "$_: $hdr{$_}\015\012", keys %hdr) |
197 | . "\015\012" |
209 | . "\015\012" |
198 | . (delete $state{body}) |
210 | . (delete $state{body}) |
199 | ); |
211 | ); |
200 | |
212 | |
… | |
… | |
229 | } |
241 | } |
230 | |
242 | |
231 | substr $_, 0, 1, "" |
243 | substr $_, 0, 1, "" |
232 | for values %hdr; |
244 | for values %hdr; |
233 | |
245 | |
|
|
246 | if ($method ne "HEAD") { |
234 | if (exists $hdr{"content-length"}) { |
247 | if (exists $hdr{"content-length"}) { |
235 | $_[0]->unshift_read (chunk => $hdr{"content-length"}, sub { |
248 | $_[0]->unshift_read (chunk => $hdr{"content-length"}, sub { |
236 | # could cache persistent connection now |
249 | # could cache persistent connection now |
237 | if ($hdr{connection} =~ /\bkeep-alive\b/i) { |
250 | if ($hdr{connection} =~ /\bkeep-alive\b/i) { |
238 | # but we don't, due to misdesigns, this is annoyingly complex |
251 | # but we don't, due to misdesigns, this is annoyingly complex |
|
|
252 | }; |
|
|
253 | |
|
|
254 | %state = (); |
|
|
255 | $cb->($_[1], \%hdr); |
239 | }; |
256 | }); |
240 | |
|
|
241 | %state = (); |
|
|
242 | $cb->($_[1], \%hdr); |
|
|
243 | }); |
|
|
244 | } else { |
257 | } else { |
245 | # too bad, need to read until we get an error or EOF, |
258 | # too bad, need to read until we get an error or EOF, |
246 | # no way to detect winged data. |
259 | # no way to detect winged data. |
247 | $_[0]->on_error (sub { |
260 | $_[0]->on_error (sub { |
248 | %state = (); |
261 | %state = (); |
249 | $cb->($_[0]{rbuf}, \%hdr); |
262 | $cb->($_[0]{rbuf}, \%hdr); |
250 | }); |
263 | }); |
251 | $_[0]->on_eof (undef); |
264 | $_[0]->on_eof (undef); |
252 | $_[0]->on_read (sub { }); |
265 | $_[0]->on_read (sub { }); |
|
|
266 | } |
253 | } |
267 | } |
254 | }); |
268 | }); |
255 | }); |
269 | }); |
256 | }, sub { |
270 | }, sub { |
257 | $timeout |
271 | $timeout |
… | |
… | |
263 | sub http_get($$;@) { |
277 | sub http_get($$;@) { |
264 | unshift @_, "GET"; |
278 | unshift @_, "GET"; |
265 | &http_request |
279 | &http_request |
266 | } |
280 | } |
267 | |
281 | |
|
|
282 | sub http_post($$$;@) { |
|
|
283 | unshift @_, "POST", "body"; |
|
|
284 | &http_request |
|
|
285 | } |
|
|
286 | |
268 | =head2 GLOBAL FUNCTIONS AND VARIABLES |
287 | =head2 GLOBAL FUNCTIONS AND VARIABLES |
269 | |
288 | |
270 | =over 4 |
289 | =over 4 |
271 | |
290 | |
272 | =item AnyEvent::HTTP::set_proxy "proxy-url" |
291 | =item AnyEvent::HTTP::set_proxy "proxy-url" |
273 | |
292 | |
274 | Sets the default proxy server to use. The proxy-url must begin with a |
293 | Sets the default proxy server to use. The proxy-url must begin with a |
275 | string of the form C<http://host:port> (optionally C<https:...>). |
294 | string of the form C<http://host:port> (optionally C<https:...>). |
276 | |
295 | |
277 | =item $AnyEvent::HTTP::MAX_REDIRECTS |
296 | =item $AnyEvent::HTTP::MAX_RECURSE |
278 | |
297 | |
279 | The default value for the C<max_redirects> request parameter |
298 | The default value for the C<recurse> request parameter (default: C<10>). |
280 | (default: C<10>). |
|
|
281 | |
299 | |
282 | =item $AnyEvent::HTTP::USERAGENT |
300 | =item $AnyEvent::HTTP::USERAGENT |
283 | |
301 | |
284 | The default value for the C<User-Agent> header (the default is |
302 | The default value for the C<User-Agent> header (the default is |
285 | C<Mozilla/5.0 (compatible; AnyEvent::HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)>). |
303 | C<Mozilla/5.0 (compatible; AnyEvent::HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)>). |
286 | |
304 | |
287 | =item $AnyEvent::HTTP::MAX_PERSISTENT |
305 | =item $AnyEvent::HTTP::MAX_PERSISTENT |
288 | |
306 | |
289 | The maximum number of persistent connections to keep open (default: 8). |
307 | The maximum number of persistent connections to keep open (default: 8). |
290 | |
308 | |
|
|
309 | Not implemented currently. |
|
|
310 | |
291 | =item $AnyEvent::HTTP::PERSISTENT_TIMEOUT |
311 | =item $AnyEvent::HTTP::PERSISTENT_TIMEOUT |
292 | |
312 | |
293 | The maximum time to cache a persistent connection, in seconds (default: 2). |
313 | The maximum time to cache a persistent connection, in seconds (default: 2). |
|
|
314 | |
|
|
315 | Not implemented currently. |
294 | |
316 | |
295 | =back |
317 | =back |
296 | |
318 | |
297 | =cut |
319 | =cut |
298 | |
320 | |