--- AnyEvent-HTTP/HTTP.pm 2008/06/05 15:34:00 1.11 +++ AnyEvent-HTTP/HTTP.pm 2008/06/06 16:23:57 1.18 @@ -6,6 +6,10 @@ use AnyEvent::HTTP; + http_get "http://www.nethype.de/", sub { print $_[1] }; + + # ... do something else here + =head1 DESCRIPTION This module is an L user, you need to make sure that you use and @@ -46,9 +50,9 @@ use base Exporter::; -our $VERSION = '1.0'; +our $VERSION = '1.01'; -our @EXPORT = qw(http_get http_request); +our @EXPORT = qw(http_get http_post http_head http_request); our $USERAGENT = "Mozilla/5.0 (compatible; AnyEvent::HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; our $MAX_RECURSE = 10; @@ -61,6 +65,7 @@ our $MAX_PER_HOST = 4; our $PROXY; +our $ACTIVE = 0; my %KA_COUNT; # number of open keep-alive connections per host my %CO_SLOT; # number of open connections, and wait queue, per host @@ -123,7 +128,9 @@ =item headers => hashref -The request headers to use. +The request headers to use. Currently, C may provide its +own C, C, C and C headers +and will provide defaults for C and C. =item timeout => $seconds @@ -183,22 +190,24 @@ =cut +sub _slot_schedule; sub _slot_schedule($) { my $host = shift; while ($CO_SLOT{$host}[0] < $MAX_PER_HOST) { if (my $cb = shift @{ $CO_SLOT{$host}[1] }) { - # somebody wnats that slot + # somebody wants that slot ++$CO_SLOT{$host}[0]; + ++$ACTIVE; $cb->(AnyEvent::Util::guard { + --$ACTIVE; --$CO_SLOT{$host}[0]; _slot_schedule $host; }); } else { # nobody wants the slot, maybe we can forget about it delete $CO_SLOT{$host} unless $CO_SLOT{$host}[0]; - warn "$host deleted" unless $CO_SLOT{$host}[0];#d# last; } } @@ -211,7 +220,7 @@ _slot_schedule $_[0]; } -sub http_request($$$;@) { +sub http_request($$@) { my $cb = pop; my ($method, $url, %arg) = @_; @@ -244,6 +253,8 @@ : $scheme eq "https" ? 443 : return $cb->(undef, { Status => 599, Reason => "only http and https URL schemes supported" }); + $hdr{referer} ||= "$scheme://$authority$upath"; # leave out fragment and query string, just a heuristic + $authority =~ /^(?: .*\@ )? ([^\@:]+) (?: : (\d+) )?$/x or return $cb->(undef, { Status => 599, Reason => "unparsable URL" }); @@ -322,8 +333,9 @@ # (re-)configure handle $state{handle}->timeout ($timeout); $state{handle}->on_error (sub { + my $errno = "$!"; %state = (); - $cb->(undef, { Status => 599, Reason => "$!" }); + $cb->(undef, { Status => 599, Reason => $errno }); }); $state{handle}->on_eof (sub { %state = (); @@ -398,7 +410,7 @@ } } - if ($_[1]{Status} =~ /^x30[12]$/ && $recurse) { + if ($_[1]{Status} =~ /^30[12]$/ && $recurse) { # microsoft and other assholes don't give a shit for following standards, # try to support a common form of broken Location header. $_[1]{location} =~ s%^/%$scheme://$uhost:$uport/%; @@ -441,17 +453,17 @@ defined wantarray && AnyEvent::Util::guard { %state = () } } -sub http_get($$;@) { +sub http_get($@) { unshift @_, "GET"; &http_request } -sub http_head($$;@) { +sub http_head($@) { unshift @_, "HEAD"; &http_request } -sub http_post($$$;@) { +sub http_post($$@) { unshift @_, "POST", "body"; &http_request } @@ -488,6 +500,12 @@ Not implemented currently. +=item $AnyEvent::HTTP::ACTIVE + +The number of active connections. This is not the number of currently +running requests, but the number of currently open and non-idle TCP +connections. This number of can be useful for load-leveling. + =back =cut @@ -505,8 +523,8 @@ =head1 AUTHOR - Marc Lehmann - http://home.schmorp.de/ + Marc Lehmann + http://home.schmorp.de/ =cut