… | |
… | |
48 | use AnyEvent::Socket (); |
48 | use AnyEvent::Socket (); |
49 | use AnyEvent::Handle (); |
49 | use AnyEvent::Handle (); |
50 | |
50 | |
51 | use base Exporter::; |
51 | use base Exporter::; |
52 | |
52 | |
53 | our $VERSION = '1.03'; |
53 | our $VERSION = '1.05'; |
54 | |
54 | |
55 | our @EXPORT = qw(http_get http_post http_head http_request); |
55 | our @EXPORT = qw(http_get http_post http_head http_request); |
56 | |
56 | |
57 | our $USERAGENT = "Mozilla/5.0 (compatible; AnyEvent::HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; |
57 | our $USERAGENT = "Mozilla/5.0 (compatible; AnyEvent::HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; |
58 | our $MAX_RECURSE = 10; |
58 | our $MAX_RECURSE = 10; |
… | |
… | |
71 | my %CO_SLOT; # number of open connections, and wait queue, per host |
71 | my %CO_SLOT; # number of open connections, and wait queue, per host |
72 | |
72 | |
73 | =item http_get $url, key => value..., $cb->($data, $headers) |
73 | =item http_get $url, key => value..., $cb->($data, $headers) |
74 | |
74 | |
75 | Executes an HTTP-GET request. See the http_request function for details on |
75 | Executes an HTTP-GET request. See the http_request function for details on |
76 | additional parameters. |
76 | additional parameters and the return value. |
77 | |
77 | |
78 | =item http_head $url, key => value..., $cb->($data, $headers) |
78 | =item http_head $url, key => value..., $cb->($data, $headers) |
79 | |
79 | |
80 | Executes an HTTP-HEAD request. See the http_request function for details on |
80 | Executes an HTTP-HEAD request. See the http_request function for details |
81 | additional parameters. |
81 | on additional parameters and the return value. |
82 | |
82 | |
83 | =item http_post $url, $body, key => value..., $cb->($data, $headers) |
83 | =item http_post $url, $body, key => value..., $cb->($data, $headers) |
84 | |
84 | |
85 | Executes an HTTP-POST request with a request body of C<$bod>. See the |
85 | Executes an HTTP-POST request with a request body of C<$body>. See the |
86 | http_request function for details on additional parameters. |
86 | http_request function for details on additional parameters and the return |
|
|
87 | value. |
87 | |
88 | |
88 | =item http_request $method => $url, key => value..., $cb->($data, $headers) |
89 | =item http_request $method => $url, key => value..., $cb->($data, $headers) |
89 | |
90 | |
90 | Executes a HTTP request of type C<$method> (e.g. C<GET>, C<POST>). The URL |
91 | Executes a HTTP request of type C<$method> (e.g. C<GET>, C<POST>). The URL |
91 | must be an absolute http or https URL. |
92 | must be an absolute http or https URL. |
|
|
93 | |
|
|
94 | When called in void context, nothing is returned. In other contexts, |
|
|
95 | C<http_request> returns a "cancellation guard" - you have to keep the |
|
|
96 | object at least alive until the callback get called. If the object gets |
|
|
97 | destroyed before the callbakc is called, the request will be cancelled. |
92 | |
98 | |
93 | The callback will be called with the response data as first argument |
99 | The callback will be called with the response data as first argument |
94 | (or C<undef> if it wasn't available due to errors), and a hash-ref with |
100 | (or C<undef> if it wasn't available due to errors), and a hash-ref with |
95 | response headers as second argument. |
101 | response headers as second argument. |
96 | |
102 | |
… | |
… | |
188 | my ($body, $hdr) = @_; |
194 | my ($body, $hdr) = @_; |
189 | use Data::Dumper; |
195 | use Data::Dumper; |
190 | print Dumper $hdr; |
196 | print Dumper $hdr; |
191 | } |
197 | } |
192 | ; |
198 | ; |
|
|
199 | |
|
|
200 | Example: make another simple HTTP GET request, but immediately try to |
|
|
201 | cancel it. |
|
|
202 | |
|
|
203 | my $request = http_request GET => "http://www.nethype.de/", sub { |
|
|
204 | my ($body, $hdr) = @_; |
|
|
205 | print "$body\n"; |
|
|
206 | }; |
|
|
207 | |
|
|
208 | undef $request; |
193 | |
209 | |
194 | =cut |
210 | =cut |
195 | |
211 | |
196 | sub _slot_schedule; |
212 | sub _slot_schedule; |
197 | sub _slot_schedule($) { |
213 | sub _slot_schedule($) { |
… | |
… | |
235 | while (my ($k, $v) = each %$hdr) { |
251 | while (my ($k, $v) = each %$hdr) { |
236 | $hdr{lc $k} = $v; |
252 | $hdr{lc $k} = $v; |
237 | } |
253 | } |
238 | } |
254 | } |
239 | |
255 | |
240 | my $recurse = exists $arg{recurse} ? $arg{recurse} : $MAX_RECURSE; |
256 | my $recurse = exists $arg{recurse} ? delete $arg{recurse} : $MAX_RECURSE; |
241 | |
257 | |
242 | return $cb->(undef, { Status => 599, Reason => "recursion limit reached", URL => $url }) |
258 | return $cb->(undef, { Status => 599, Reason => "recursion limit reached", URL => $url }) |
243 | if $recurse < 0; |
259 | if $recurse < 0; |
244 | |
260 | |
245 | my $proxy = $arg{proxy} || $PROXY; |
261 | my $proxy = $arg{proxy} || $PROXY; |
… | |
… | |
355 | |
371 | |
356 | %hdr = (); # reduce memory usage, save a kitten |
372 | %hdr = (); # reduce memory usage, save a kitten |
357 | |
373 | |
358 | # status line |
374 | # status line |
359 | $state{handle}->push_read (line => qr/\015?\012/, sub { |
375 | $state{handle}->push_read (line => qr/\015?\012/, sub { |
360 | $_[1] =~ /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) \s+ ([^\015\012]+)/ix |
376 | $_[1] =~ /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) (?: \s+ ([^\015\012]*) )?/ix |
361 | or return (%state = (), $cb->(undef, { Status => 599, Reason => "invalid server response ($_[1])", URL => $url })); |
377 | or return (%state = (), $cb->(undef, { Status => 599, Reason => "invalid server response ($_[1])", URL => $url })); |
362 | |
378 | |
363 | my %hdr = ( # response headers |
379 | my %hdr = ( # response headers |
364 | HTTPVersion => "\x00$1", |
380 | HTTPVersion => "\x00$1", |
365 | Status => "\x00$2", |
381 | Status => "\x00$2", |
… | |
… | |
398 | my %kv = (value => $value, map { split /=/, $_, 2 } @arg); |
414 | my %kv = (value => $value, map { split /=/, $_, 2 } @arg); |
399 | |
415 | |
400 | my $cdom = (delete $kv{domain}) || $uhost; |
416 | my $cdom = (delete $kv{domain}) || $uhost; |
401 | my $cpath = (delete $kv{path}) || "/"; |
417 | my $cpath = (delete $kv{path}) || "/"; |
402 | |
418 | |
403 | $cdom =~ s/^.?/./; # make sure it starts with a "." |
419 | $cdom =~ s/^\.?/./; # make sure it starts with a "." |
404 | |
420 | |
405 | next if $cdom =~ /\.$/; |
421 | next if $cdom =~ /\.$/; |
406 | |
422 | |
407 | # this is not rfc-like and not netscape-like. go figure. |
423 | # this is not rfc-like and not netscape-like. go figure. |
408 | my $ndots = $cdom =~ y/.//; |
424 | my $ndots = $cdom =~ y/.//; |
… | |
… | |
412 | $arg{cookie_jar}{version} = 1; |
428 | $arg{cookie_jar}{version} = 1; |
413 | $arg{cookie_jar}{$cdom}{$cpath}{$name} = \%kv; |
429 | $arg{cookie_jar}{$cdom}{$cpath}{$name} = \%kv; |
414 | } |
430 | } |
415 | } |
431 | } |
416 | |
432 | |
417 | if ($_[1]{Status} =~ /^30[12]$/ && $recurse) { |
|
|
418 | # microsoft and other assholes don't give a shit for following standards, |
433 | # microsoft and other shitheads don't give a shit for following standards, |
419 | # try to support a common form of broken Location header. |
434 | # try to support some common forms of broken Location headers. |
|
|
435 | if ($_[1]{location} !~ /^(?: $ | [^:\/?\#]+ : )/x) { |
|
|
436 | $_[1]{location} =~ s/^\.\/+//; |
|
|
437 | |
420 | $_[1]{location} =~ s%^/%$scheme://$uhost:$uport/%; |
438 | my $url = "$scheme://$uhost:$uport"; |
421 | |
439 | |
|
|
440 | unless ($_[1]{location} =~ s/^\///) { |
|
|
441 | $url .= $upath; |
|
|
442 | $url =~ s/\/[^\/]*$//; |
|
|
443 | } |
|
|
444 | |
|
|
445 | $_[1]{location} = "$url/$_[1]{location}"; |
|
|
446 | } |
|
|
447 | |
|
|
448 | if ($_[1]{Status} =~ /^30[12]$/ && $recurse && $method ne "POST") { |
|
|
449 | # apparently, mozilla et al. just change POST to GET here |
|
|
450 | # more research is needed before we do the same |
422 | http_request ($method, $_[1]{location}, %arg, recurse => $recurse - 1, $cb); |
451 | http_request ($method, $_[1]{location}, %arg, recurse => $recurse - 1, $cb); |
|
|
452 | } elsif ($_[1]{Status} == 303 && $recurse) { |
|
|
453 | # even http/1.1 is unlear on how to mutate the method |
|
|
454 | $method = "GET" unless $method eq "HEAD"; |
|
|
455 | http_request ($method => $_[1]{location}, %arg, recurse => $recurse - 1, $cb); |
|
|
456 | } elsif ($_[1]{Status} == 307 && $recurse && $method =~ /^(?:GET|HEAD)$/) { |
|
|
457 | http_request ($method => $_[1]{location}, %arg, recurse => $recurse - 1, $cb); |
423 | } else { |
458 | } else { |
424 | $cb->($_[0], $_[1]); |
459 | $cb->($_[0], $_[1]); |
425 | } |
460 | } |
426 | }; |
461 | }; |
427 | |
462 | |