--- AnyEvent-HTTP/HTTP.pm 2009/07/06 00:08:16 1.42 +++ AnyEvent-HTTP/HTTP.pm 2009/08/05 16:07:48 1.47 @@ -43,14 +43,14 @@ use Errno (); -use AnyEvent 4.452 (); +use AnyEvent 4.8 (); use AnyEvent::Util (); use AnyEvent::Socket (); use AnyEvent::Handle (); use base Exporter::; -our $VERSION = '1.12'; +our $VERSION = '1.41'; our @EXPORT = qw(http_get http_post http_head http_request); @@ -61,7 +61,7 @@ our $TIMEOUT = 300; # changing these is evil -our $MAX_PERSISTENT_PER_HOST = 2; +our $MAX_PERSISTENT_PER_HOST = 0; our $MAX_PER_HOST = 4; our $PROXY; @@ -140,7 +140,9 @@ 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. +and will provide defaults for C and C (this can be +suppressed by using C for these headers in which case they won't be +send at all). =item timeout => $seconds @@ -152,7 +154,7 @@ Use the given http proxy for all requests. If not specified, then the default proxy (as specified by C<$ENV{http_proxy}>) is used. -C<$scheme> must be either missing or C for HTTP, or C for +C<$scheme> must be either missing, C for HTTP or C for HTTPS. =item body => $string @@ -223,7 +225,10 @@ be extracted, or when the body should be processed incrementally. It is usually preferred over doing your own body handling via -C. +C, but in case of streaming APIs, where HTTP is +only used to create a connection, C is the better +alternative, as it allows you to install your own event handler, reducing +resource usage. =item want_body_handle => $enable @@ -245,7 +250,7 @@ push-style twitter API which starts a JSON/XML stream). If you think you need this, first have a look at C, to see if -that doesn'T solve your problem in a better way. +that doesn't solve your problem in a better way. =back @@ -311,8 +316,8 @@ _slot_schedule $_[0]; } -our $qr_nl = qr<\015?\012>; -our $qr_nlnl = qr<\015?\012\015?\012>; +our $qr_nl = qr{\015?\012}; +our $qr_nlnl = qr{(? 1, sslv2 => 1 }; our $TLS_CTX_HIGH = { cache => 1, verify => 1, verify_peername => "https" }; @@ -357,6 +362,8 @@ my $uhost = $1; $uport = $2 if defined $2; + $hdr{host} = defined $2 ? "$uhost:$2" : "$uhost"; + $uhost =~ s/^\[(.*)\]$/$1/; $upath .= "?$query" if length $query; @@ -398,6 +405,8 @@ if ($proxy) { ($rpath, $rhost, $rport, $rscheme) = ($url, @$proxy); + $rscheme = "http" unless defined $rscheme; + # don't support https requests over https-proxy transport, # can't be done with tls as spec'ed, unless you double-encrypt. $rscheme = "http" if $uscheme eq "https" && $rscheme eq "https"; @@ -405,10 +414,10 @@ ($rhost, $rport, $rscheme, $rpath) = ($uhost, $uport, $uscheme, $upath); } - $hdr{"user-agent"} ||= $USERAGENT; - $hdr{referer} ||= "$uscheme://$uauthority$upath"; # leave out fragment and query string, just a heuristic + # leave out fragment and query string, just a heuristic + $hdr{referer} ||= "$uscheme://$uauthority$upath" unless exists $hdr{referer}; + $hdr{"user-agent"} ||= $USERAGENT unless exists $hdr{"user-agent"}; - $hdr{host} = "$uhost:$uport"; $hdr{"content-length"} = length $arg{body}; my %state = (connect_guard => 1); @@ -420,7 +429,12 @@ $state{connect_guard} = AnyEvent::Socket::tcp_connect $rhost, $rport, sub { $state{fh} = shift - or return (%state = (), $cb->(undef, { Status => 599, Reason => "$!", URL => $url })); + or do { + my $err = "$!"; + %state = (); + return $cb->(undef, { Status => 599, Reason => $err, URL => $url }); + }; + pop; # free memory, save a tree return unless delete $state{connect_guard}; @@ -463,7 +477,7 @@ # send request $state{handle}->push_write ( "$method $rpath HTTP/1.0\015\012" - . (join "", map "\u$_: $hdr{$_}\015\012", keys %hdr) + . (join "", map "\u$_: $hdr{$_}\015\012", grep defined $hdr{$_}, keys %hdr) . "\015\012" . (delete $arg{body}) ); @@ -484,7 +498,7 @@ # headers, could be optimized a bit $state{handle}->unshift_read (line => $qr_nlnl, sub { - for ("$_[1]\012") { + for ("$_[1]") { y/\015//d; # weed out any \015, as they show up in the weirdest of places. # things seen, not parsed: @@ -492,7 +506,7 @@ $hdr{lc $1} .= ",$2" while /\G - ([^:\000-\037]+): + ([^:\000-\037]*): [\011\040]* ((?: [^\012]+ | \012[\011\040] )*) \012 @@ -591,7 +605,7 @@ } } - if ($redirect) { + if ($redirect && exists $hdr{location}) { # we ignore any errors, as it is very common to receive # Content-Length != 0 but no actual body # we also access %hdr, as $_[1] might be an erro @@ -656,7 +670,7 @@ }); } else { $_[0]->on_error (sub { - $! == Errno::EPIPE + $! == Errno::EPIPE || !$! ? $finish->(delete $_[0]{rbuf}, \%hdr) : $finish->(undef, { Status => 599, Reason => $_[2], URL => $url }); }); @@ -734,17 +748,14 @@ The default value for the C header (the default is C). -=item $AnyEvent::HTTP::MAX_PERSISTENT - -The maximum number of persistent connections to keep open (default: 8). - -Not implemented currently. - -=item $AnyEvent::HTTP::PERSISTENT_TIMEOUT +=item $AnyEvent::HTTP::MAX_PER_HOST -The maximum time to cache a persistent connection, in seconds (default: 2). +The maximum number of concurrent connections to the same host (identified +by the hostname). If the limit is exceeded, then the additional requests +are queued until previous connections are closed. -Not implemented currently. +The default value for this is C<4>, and it is highly advisable to not +increase it. =item $AnyEvent::HTTP::ACTIVE