ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-HTTP/HTTP.pm
(Generate patch)

Comparing AnyEvent-HTTP/HTTP.pm (file contents):
Revision 1.49 by root, Wed Aug 5 16:23:48 2009 UTC vs.
Revision 1.54 by root, Wed Jun 16 18:09:52 2010 UTC

41use strict; 41use strict;
42no warnings; 42no warnings;
43 43
44use Errno (); 44use Errno ();
45 45
46use AnyEvent 4.8 (); 46use AnyEvent 5.0 ();
47use AnyEvent::Util (); 47use AnyEvent::Util ();
48use AnyEvent::Socket (); 48use AnyEvent::Socket ();
49use AnyEvent::Handle (); 49use AnyEvent::Handle ();
50 50
51use base Exporter::; 51use base Exporter::;
52 52
53our $VERSION = '1.42'; 53our $VERSION = '1.44';
54 54
55our @EXPORT = qw(http_get http_post http_head http_request); 55our @EXPORT = qw(http_get http_post http_head http_request);
56 56
57our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; 57our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)";
58our $MAX_RECURSE = 10; 58our $MAX_RECURSE = 10;
145sent at all). 145sent at all).
146 146
147=item timeout => $seconds 147=item timeout => $seconds
148 148
149The time-out to use for various stages - each connect attempt will reset 149The time-out to use for various stages - each connect attempt will reset
150the timeout, as will read or write activity. Default timeout is 5 minutes. 150the timeout, as will read or write activity, i.e. this is not an overall
151timeout.
152
153Default timeout is 5 minutes.
151 154
152=item proxy => [$host, $port[, $scheme]] or undef 155=item proxy => [$host, $port[, $scheme]] or undef
153 156
154Use the given http proxy for all requests. If not specified, then the 157Use the given http proxy for all requests. If not specified, then the
155default proxy (as specified by C<$ENV{http_proxy}>) is used. 158default proxy (as specified by C<$ENV{http_proxy}>) is used.
187verification, highest compatibility) and high-security (CA and common-name 190verification, highest compatibility) and high-security (CA and common-name
188verification) TLS context. 191verification) TLS context.
189 192
190The default for this option is C<low>, which could be interpreted as "give 193The default for this option is C<low>, which could be interpreted as "give
191me the page, no matter what". 194me the page, no matter what".
195
196=item on_prepare => $callback->($fh)
197
198In rare cases you need to "tune" the socket before it is used to
199connect (for exmaple, to bind it on a given IP address). This parameter
200overrides the prepare callback passed to C<AnyEvent::Socket::tcp_connect>
201and behaves exactly the same way (e.g. it has to provide a
202timeout). See the description for the C<$prepare_cb> argument of
203C<AnyEvent::Socket::tcp_connect> for details.
192 204
193=item on_header => $callback->($headers) 205=item on_header => $callback->($headers)
194 206
195When specified, this callback will be called with the header hash as soon 207When specified, this callback will be called with the header hash as soon
196as headers have been successfully received from the remote server (not on 208as headers have been successfully received from the remote server (not on
360 or return $cb->(undef, { Status => 599, Reason => "Unparsable URL", URL => $url }); 372 or return $cb->(undef, { Status => 599, Reason => "Unparsable URL", URL => $url });
361 373
362 my $uhost = $1; 374 my $uhost = $1;
363 $uport = $2 if defined $2; 375 $uport = $2 if defined $2;
364 376
365 $hdr{host} = defined $2 ? "$uhost:$2" : "$uhost"; 377 $hdr{host} = defined $2 ? "$uhost:$2" : "$uhost"
378 unless exists $hdr{host};
366 379
367 $uhost =~ s/^\[(.*)\]$/$1/; 380 $uhost =~ s/^\[(.*)\]$/$1/;
368 $upath .= "?$query" if length $query; 381 $upath .= "?$query" if length $query;
369 382
370 $upath =~ s%^/?%/%; 383 $upath =~ s%^/?%/%;
416 429
417 # leave out fragment and query string, just a heuristic 430 # leave out fragment and query string, just a heuristic
418 $hdr{referer} ||= "$uscheme://$uauthority$upath" unless exists $hdr{referer}; 431 $hdr{referer} ||= "$uscheme://$uauthority$upath" unless exists $hdr{referer};
419 $hdr{"user-agent"} ||= $USERAGENT unless exists $hdr{"user-agent"}; 432 $hdr{"user-agent"} ||= $USERAGENT unless exists $hdr{"user-agent"};
420 433
421 $hdr{"content-length"} = length $arg{body}; 434 $hdr{"content-length"} = length $arg{body}
435 if length $arg{body} || $method ne "GET";
422 436
423 my %state = (connect_guard => 1); 437 my %state = (connect_guard => 1);
424 438
425 _get_slot $uhost, sub { 439 _get_slot $uhost, sub {
426 $state{slot_guard} = shift; 440 $state{slot_guard} = shift;
479 "$method $rpath HTTP/1.0\015\012" 493 "$method $rpath HTTP/1.0\015\012"
480 . (join "", map "\u$_: $hdr{$_}\015\012", grep defined $hdr{$_}, keys %hdr) 494 . (join "", map "\u$_: $hdr{$_}\015\012", grep defined $hdr{$_}, keys %hdr)
481 . "\015\012" 495 . "\015\012"
482 . (delete $arg{body}) 496 . (delete $arg{body})
483 ); 497 );
498
499 # return if error occured during push_write()
500 return unless %state;
484 501
485 %hdr = (); # reduce memory usage, save a kitten 502 %hdr = (); # reduce memory usage, save a kitten
486 503
487 # status line 504 # status line
488 $state{handle}->push_read (line => $qr_nl, sub { 505 $state{handle}->push_read (line => $qr_nl, sub {
702 }); 719 });
703 } else { 720 } else {
704 &$handle_actual_request; 721 &$handle_actual_request;
705 } 722 }
706 723
707 }, sub { 724 }, $arg{on_prepare} || sub { $timeout };
708 $timeout
709 };
710 }; 725 };
711 726
712 defined wantarray && AnyEvent::Util::guard { %state = () } 727 defined wantarray && AnyEvent::Util::guard { %state = () }
713} 728}
714 729
735=over 4 750=over 4
736 751
737=item AnyEvent::HTTP::set_proxy "proxy-url" 752=item AnyEvent::HTTP::set_proxy "proxy-url"
738 753
739Sets the default proxy server to use. The proxy-url must begin with a 754Sets the default proxy server to use. The proxy-url must begin with a
740string of the form C<http://host:port> (optionally C<https:...>). 755string of the form C<http://host:port> (optionally C<https:...>), croaks
756otherwise.
757
758To clear an already-set proxy, use C<undef>.
741 759
742=item $AnyEvent::HTTP::MAX_RECURSE 760=item $AnyEvent::HTTP::MAX_RECURSE
743 761
744The default value for the C<recurse> request parameter (default: C<10>). 762The default value for the C<recurse> request parameter (default: C<10>).
745 763
766=back 784=back
767 785
768=cut 786=cut
769 787
770sub set_proxy($) { 788sub set_proxy($) {
789 if (length $_[0]) {
771 $PROXY = [$2, $3 || 3128, $1] if $_[0] =~ m%^(https?):// ([^:/]+) (?: : (\d*) )?%ix; 790 $_[0] =~ m%^(https?):// ([^:/]+) (?: : (\d*) )?%ix
791 or Carp::croak "$_[0]: invalid proxy URL";
792 $PROXY = [$2, $3 || 3128, $1]
793 } else {
794 undef $PROXY;
795 }
772} 796}
773 797
774# initialise proxy from environment 798# initialise proxy from environment
799eval {
775set_proxy $ENV{http_proxy}; 800 set_proxy $ENV{http_proxy};
801};
776 802
777=head1 SEE ALSO 803=head1 SEE ALSO
778 804
779L<AnyEvent>. 805L<AnyEvent>.
780 806

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines