--- AnyEvent-HTTP/HTTP.pm 2008/06/04 12:05:45 1.6 +++ AnyEvent-HTTP/HTTP.pm 2008/06/04 13:51:53 1.9 @@ -61,7 +61,7 @@ =item http_post $url, $body, key => value..., $cb->($data, $headers) -Executes an HTTP-POST request with a requets body of C<$bod>. See the +Executes an HTTP-POST request with a request body of C<$bod>. See the http_request function for details on additional parameters. =item http_request $method => $url, key => value..., $cb->($data, $headers) @@ -73,7 +73,7 @@ (or C if it wasn't available due to errors), and a hash-ref with response headers as second argument. -All the headers in that has are lowercased. In addition to the response +All the headers in that hash are lowercased. In addition to the response headers, the three "pseudo-headers" C, C and C contain the three parts of the HTTP Status-Line of the same name. @@ -128,7 +128,25 @@ =back -=back +Example: make a simple HTTP GET request for http://www.nethype.de/ + + http_request GET => "http://www.nethype.de/", sub { + my ($body, $hdr) = @_; + print "$body\n"; + }; + +Example: make a HTTP HEAD request on https://www.google.com/, use a +timeout of 30 seconds. + + http_request + GET => "https://www.google.com", + timeout => 30, + sub { + my ($body, $hdr) = @_; + use Data::Dumper; + print Dumper $hdr; + } + ; =cut @@ -140,15 +158,19 @@ $method = uc $method; - if (my $hdr = delete $arg{headers}) { + if (my $hdr = $arg{headers}) { while (my ($k, $v) = each %$hdr) { $hdr{lc $k} = $v; } } + my $recurse = exists $arg{recurse} ? $arg{recurse} : $MAX_RECURSE; + + return $cb->(undef, { Status => 599, Reason => "recursion limit reached" }) + if $recurse < 0; + my $proxy = $arg{proxy} || $PROXY; my $timeout = $arg{timeout} || $TIMEOUT; - my $recurse = exists $arg{recurse} ? $arg{recurse} : $MAX_RECURSE; $hdr{"user-agent"} ||= $USERAGENT; @@ -163,10 +185,10 @@ $port = $scheme eq "http" ? 80 : $scheme eq "https" ? 443 - : croak "$url: only http and https URLs supported"; + : return $cb->(undef, { Status => 599, Reason => "$url: only http and https URLs supported" }); $authority =~ /^(?: .*\@ )? ([^\@:]+) (?: : (\d+) )?$/x - or croak "$authority: unparsable URL"; + or return $cb->(undef, { Status => 599, Reason => "$url: unparsable URL" }); $host = $1; $port = $2 if defined $2; @@ -183,9 +205,7 @@ my %state; - $state{body} = delete $arg{body}; - - $hdr{"content-length"} = length $state{body}; + $hdr{"content-length"} = length $arg{body}; $state{connect_guard} = AnyEvent::Socket::tcp_connect $host, $port, sub { $state{fh} = shift @@ -224,7 +244,7 @@ "$method $path HTTP/1.0\015\012" . (join "", map "$_: $hdr{$_}\015\012", keys %hdr) . "\015\012" - . (delete $state{body}) + . (delete $arg{body}) ); %hdr = (); # reduce memory usage, save a kitten @@ -260,9 +280,17 @@ substr $_, 0, 1, "" for values %hdr; - if ($method eq "HEAD") { + my $finish = sub { + if ($_[1]{Status} =~ /^30[12]$/ && $recurse) { + http_request ($method, $_[1]{location}, %arg, recurse => $recurse - 1, $cb); + } else { + $cb->($_[0], $_[1]); + } + }; + + if ($hdr{Status} =~ /^(?:1..|204|304)$/ or $method eq "HEAD") { %state = (); - $cb->(undef, \%hdr); + $finish->(undef, \%hdr); } else { if (exists $hdr{"content-length"}) { $_[0]->unshift_read (chunk => $hdr{"content-length"}, sub { @@ -272,14 +300,14 @@ }; %state = (); - $cb->($_[1], \%hdr); + $finish->($_[1], \%hdr); }); } else { # too bad, need to read until we get an error or EOF, # no way to detect winged data. $_[0]->on_error (sub { %state = (); - $cb->($_[0]{rbuf}, \%hdr); + $finish->($_[0]{rbuf}, \%hdr); }); $_[0]->on_eof (undef); $_[0]->on_read (sub { }); @@ -309,6 +337,8 @@ &http_request } +=back + =head2 GLOBAL FUNCTIONS AND VARIABLES =over 4