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.4 by root, Wed Jun 4 11:59:22 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_get $url, $body, key => value..., $cb->($data, $headers)
58
59Executes an HTTP-POST request with a requets body of C<$bod>. See the
60http_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
59Executes a HTTP request of type C<$method> (e.g. C<GET>, C<POST>). The URL 64Executes a HTTP request of type C<$method> (e.g. C<GET>, C<POST>). The URL
60must be an absolute http or https URL. 65must be an absolute http or https URL.
75Additional parameters are key-value pairs, and are fully optional. They 80Additional parameters are key-value pairs, and are fully optional. They
76include: 81include:
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
82Whether to recurse requests or not, e.g. on redirects, authentication 87Whether to recurse requests or not, e.g. on redirects, authentication
83retries and so on. 88retries and so on, and how often to do so.
84 89
85=item headers => hashref 90=item headers => hashref
86 91
87The request headers to use. 92The request headers to use.
88 93
96Use the given http proxy for all requests. If not specified, then the 101Use the given http proxy for all requests. If not specified, then the
97default proxy (as specified by C<$ENV{http_proxy}>) is used. 102default proxy (as specified by C<$ENV{http_proxy}>) is used.
98 103
99C<$scheme> must be either missing or C<http> for HTTP, or C<https> for 104C<$scheme> must be either missing or C<http> for HTTP, or C<https> for
100HTTPS. 105HTTPS.
106
107=item body => $string
108
109The request body, usually empty. Will be-sent as-is (future versions of
110this module might offer more options).
101 111
102=back 112=back
103 113
104=back 114=back
105 115
108sub http_request($$$;@) { 118sub 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
234 if (exists $hdr{"content-length"}) { 246 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 = (); 247 %state = ();
242 $cb->($_[1], \%hdr); 248 $cb->(undef, \%hdr);
243 });
244 } else { 249 } else {
250 if (exists $hdr{"content-length"}) {
251 $_[0]->unshift_read (chunk => $hdr{"content-length"}, sub {
252 # could cache persistent connection now
253 if ($hdr{connection} =~ /\bkeep-alive\b/i) {
254 # but we don't, due to misdesigns, this is annoyingly complex
255 };
256
257 %state = ();
258 $cb->($_[1], \%hdr);
259 });
260 } else {
245 # too bad, need to read until we get an error or EOF, 261 # too bad, need to read until we get an error or EOF,
246 # no way to detect winged data. 262 # no way to detect winged data.
247 $_[0]->on_error (sub { 263 $_[0]->on_error (sub {
248 %state = (); 264 %state = ();
249 $cb->($_[0]{rbuf}, \%hdr); 265 $cb->($_[0]{rbuf}, \%hdr);
250 }); 266 });
251 $_[0]->on_eof (undef); 267 $_[0]->on_eof (undef);
252 $_[0]->on_read (sub { }); 268 $_[0]->on_read (sub { });
269 }
253 } 270 }
254 }); 271 });
255 }); 272 });
256 }, sub { 273 }, sub {
257 $timeout 274 $timeout
263sub http_get($$;@) { 280sub http_get($$;@) {
264 unshift @_, "GET"; 281 unshift @_, "GET";
265 &http_request 282 &http_request
266} 283}
267 284
285sub http_head($$;@) {
286 unshift @_, "HEAD";
287 &http_request
288}
289
290sub http_post($$$;@) {
291 unshift @_, "POST", "body";
292 &http_request
293}
294
268=head2 GLOBAL FUNCTIONS AND VARIABLES 295=head2 GLOBAL FUNCTIONS AND VARIABLES
269 296
270=over 4 297=over 4
271 298
272=item AnyEvent::HTTP::set_proxy "proxy-url" 299=item AnyEvent::HTTP::set_proxy "proxy-url"
273 300
274Sets the default proxy server to use. The proxy-url must begin with a 301Sets the default proxy server to use. The proxy-url must begin with a
275string of the form C<http://host:port> (optionally C<https:...>). 302string of the form C<http://host:port> (optionally C<https:...>).
276 303
277=item $AnyEvent::HTTP::MAX_REDIRECTS 304=item $AnyEvent::HTTP::MAX_RECURSE
278 305
279The default value for the C<max_redirects> request parameter 306The default value for the C<recurse> request parameter (default: C<10>).
280(default: C<10>).
281 307
282=item $AnyEvent::HTTP::USERAGENT 308=item $AnyEvent::HTTP::USERAGENT
283 309
284The default value for the C<User-Agent> header (the default is 310The 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)>). 311C<Mozilla/5.0 (compatible; AnyEvent::HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)>).
286 312
287=item $AnyEvent::HTTP::MAX_PERSISTENT 313=item $AnyEvent::HTTP::MAX_PERSISTENT
288 314
289The maximum number of persistent connections to keep open (default: 8). 315The maximum number of persistent connections to keep open (default: 8).
290 316
317Not implemented currently.
318
291=item $AnyEvent::HTTP::PERSISTENT_TIMEOUT 319=item $AnyEvent::HTTP::PERSISTENT_TIMEOUT
292 320
293The maximum time to cache a persistent connection, in seconds (default: 2). 321The maximum time to cache a persistent connection, in seconds (default: 2).
322
323Not implemented currently.
294 324
295=back 325=back
296 326
297=cut 327=cut
298 328

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines