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

Comparing AnyEvent-HTTP/HTTP.pm (file contents):
Revision 1.2 by root, Wed Jun 4 11:37:41 2008 UTC vs.
Revision 1.5 by root, Wed Jun 4 12:03:47 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines