--- AnyEvent-HTTP/HTTP.pm 2009/08/05 16:23:48 1.49 +++ AnyEvent-HTTP/HTTP.pm 2009/12/05 15:37:07 1.53 @@ -43,14 +43,14 @@ use Errno (); -use AnyEvent 4.8 (); +use AnyEvent 5.0 (); use AnyEvent::Util (); use AnyEvent::Socket (); use AnyEvent::Handle (); use base Exporter::; -our $VERSION = '1.42'; +our $VERSION = '1.44'; our @EXPORT = qw(http_get http_post http_head http_request); @@ -147,7 +147,10 @@ =item timeout => $seconds The time-out to use for various stages - each connect attempt will reset -the timeout, as will read or write activity. Default timeout is 5 minutes. +the timeout, as will read or write activity, i.e. this is not an overall +timeout. + +Default timeout is 5 minutes. =item proxy => [$host, $port[, $scheme]] or undef @@ -190,6 +193,15 @@ The default for this option is C, which could be interpreted as "give me the page, no matter what". +=item on_prepare => $callback->($fh) + +In rare cases you need to "tune" the socket before it is used to +connect (for exmaple, to bind it on a given IP address). This parameter +overrides the prepare callback passed to C +and behaves exactly the same way (e.g. it has to provide a +timeout). See the description for the C<$prepare_cb> argument of +C for details. + =item on_header => $callback->($headers) When specified, this callback will be called with the header hash as soon @@ -362,7 +374,8 @@ my $uhost = $1; $uport = $2 if defined $2; - $hdr{host} = defined $2 ? "$uhost:$2" : "$uhost"; + $hdr{host} = defined $2 ? "$uhost:$2" : "$uhost" + unless exists $hdr{host}; $uhost =~ s/^\[(.*)\]$/$1/; $upath .= "?$query" if length $query; @@ -418,7 +431,8 @@ $hdr{referer} ||= "$uscheme://$uauthority$upath" unless exists $hdr{referer}; $hdr{"user-agent"} ||= $USERAGENT unless exists $hdr{"user-agent"}; - $hdr{"content-length"} = length $arg{body}; + $hdr{"content-length"} = length $arg{body} + if length $arg{body} || $method ne "GET"; my %state = (connect_guard => 1); @@ -704,9 +718,7 @@ &$handle_actual_request; } - }, sub { - $timeout - }; + }, $arg{on_prepare} || sub { $timeout }; }; defined wantarray && AnyEvent::Util::guard { %state = () } @@ -737,7 +749,10 @@ =item AnyEvent::HTTP::set_proxy "proxy-url" Sets the default proxy server to use. The proxy-url must begin with a -string of the form C (optionally C). +string of the form C (optionally C), croaks +otherwise. + +To clear an already-set proxy, use C. =item $AnyEvent::HTTP::MAX_RECURSE @@ -768,11 +783,19 @@ =cut sub set_proxy($) { - $PROXY = [$2, $3 || 3128, $1] if $_[0] =~ m%^(https?):// ([^:/]+) (?: : (\d*) )?%ix; + if (length $_[0]) { + $_[0] =~ m%^(https?):// ([^:/]+) (?: : (\d*) )?%ix + or Carp::croak "$_[0]: invalid proxy URL"; + $PROXY = [$2, $3 || 3128, $1] + } else { + undef $PROXY; + } } # initialise proxy from environment -set_proxy $ENV{http_proxy}; +eval { + set_proxy $ENV{http_proxy}; +}; =head1 SEE ALSO