… | |
… | |
48 | use AnyEvent::Socket (); |
48 | use AnyEvent::Socket (); |
49 | use AnyEvent::Handle (); |
49 | use AnyEvent::Handle (); |
50 | |
50 | |
51 | use base Exporter::; |
51 | use base Exporter::; |
52 | |
52 | |
53 | our $VERSION = '1.43'; |
53 | our $VERSION = '1.44'; |
54 | |
54 | |
55 | our @EXPORT = qw(http_get http_post http_head http_request); |
55 | our @EXPORT = qw(http_get http_post http_head http_request); |
56 | |
56 | |
57 | our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; |
57 | our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; |
58 | our $MAX_RECURSE = 10; |
58 | our $MAX_RECURSE = 10; |
… | |
… | |
372 | or return $cb->(undef, { Status => 599, Reason => "Unparsable URL", URL => $url }); |
372 | or return $cb->(undef, { Status => 599, Reason => "Unparsable URL", URL => $url }); |
373 | |
373 | |
374 | my $uhost = $1; |
374 | my $uhost = $1; |
375 | $uport = $2 if defined $2; |
375 | $uport = $2 if defined $2; |
376 | |
376 | |
377 | $hdr{host} = defined $2 ? "$uhost:$2" : "$uhost"; |
377 | $hdr{host} = defined $2 ? "$uhost:$2" : "$uhost" |
|
|
378 | unless exists $hdr{host}; |
378 | |
379 | |
379 | $uhost =~ s/^\[(.*)\]$/$1/; |
380 | $uhost =~ s/^\[(.*)\]$/$1/; |
380 | $upath .= "?$query" if length $query; |
381 | $upath .= "?$query" if length $query; |
381 | |
382 | |
382 | $upath =~ s%^/?%/%; |
383 | $upath =~ s%^/?%/%; |
… | |
… | |
428 | |
429 | |
429 | # leave out fragment and query string, just a heuristic |
430 | # leave out fragment and query string, just a heuristic |
430 | $hdr{referer} ||= "$uscheme://$uauthority$upath" unless exists $hdr{referer}; |
431 | $hdr{referer} ||= "$uscheme://$uauthority$upath" unless exists $hdr{referer}; |
431 | $hdr{"user-agent"} ||= $USERAGENT unless exists $hdr{"user-agent"}; |
432 | $hdr{"user-agent"} ||= $USERAGENT unless exists $hdr{"user-agent"}; |
432 | |
433 | |
433 | $hdr{"content-length"} = length $arg{body}; |
434 | $hdr{"content-length"} = length $arg{body} |
|
|
435 | if length $arg{body} || $method ne "GET"; |
434 | |
436 | |
435 | my %state = (connect_guard => 1); |
437 | my %state = (connect_guard => 1); |
436 | |
438 | |
437 | _get_slot $uhost, sub { |
439 | _get_slot $uhost, sub { |
438 | $state{slot_guard} = shift; |
440 | $state{slot_guard} = shift; |
… | |
… | |
491 | "$method $rpath HTTP/1.0\015\012" |
493 | "$method $rpath HTTP/1.0\015\012" |
492 | . (join "", map "\u$_: $hdr{$_}\015\012", grep defined $hdr{$_}, keys %hdr) |
494 | . (join "", map "\u$_: $hdr{$_}\015\012", grep defined $hdr{$_}, keys %hdr) |
493 | . "\015\012" |
495 | . "\015\012" |
494 | . (delete $arg{body}) |
496 | . (delete $arg{body}) |
495 | ); |
497 | ); |
|
|
498 | |
|
|
499 | # return if error occured during push_write() |
|
|
500 | return unless %state; |
496 | |
501 | |
497 | %hdr = (); # reduce memory usage, save a kitten |
502 | %hdr = (); # reduce memory usage, save a kitten |
498 | |
503 | |
499 | # status line |
504 | # status line |
500 | $state{handle}->push_read (line => $qr_nl, sub { |
505 | $state{handle}->push_read (line => $qr_nl, sub { |
… | |
… | |
745 | =over 4 |
750 | =over 4 |
746 | |
751 | |
747 | =item AnyEvent::HTTP::set_proxy "proxy-url" |
752 | =item AnyEvent::HTTP::set_proxy "proxy-url" |
748 | |
753 | |
749 | Sets the default proxy server to use. The proxy-url must begin with a |
754 | Sets the default proxy server to use. The proxy-url must begin with a |
750 | string of the form C<http://host:port> (optionally C<https:...>). |
755 | string of the form C<http://host:port> (optionally C<https:...>), croaks |
|
|
756 | otherwise. |
|
|
757 | |
|
|
758 | To clear an already-set proxy, use C<undef>. |
751 | |
759 | |
752 | =item $AnyEvent::HTTP::MAX_RECURSE |
760 | =item $AnyEvent::HTTP::MAX_RECURSE |
753 | |
761 | |
754 | The default value for the C<recurse> request parameter (default: C<10>). |
762 | The default value for the C<recurse> request parameter (default: C<10>). |
755 | |
763 | |
… | |
… | |
776 | =back |
784 | =back |
777 | |
785 | |
778 | =cut |
786 | =cut |
779 | |
787 | |
780 | sub set_proxy($) { |
788 | sub set_proxy($) { |
|
|
789 | if (length $_[0]) { |
781 | $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 | } |
782 | } |
796 | } |
783 | |
797 | |
784 | # initialise proxy from environment |
798 | # initialise proxy from environment |
|
|
799 | eval { |
785 | set_proxy $ENV{http_proxy}; |
800 | set_proxy $ENV{http_proxy}; |
|
|
801 | }; |
786 | |
802 | |
787 | =head1 SEE ALSO |
803 | =head1 SEE ALSO |
788 | |
804 | |
789 | L<AnyEvent>. |
805 | L<AnyEvent>. |
790 | |
806 | |