… | |
… | |
41 | use strict; |
41 | use strict; |
42 | no warnings; |
42 | no warnings; |
43 | |
43 | |
44 | use Errno (); |
44 | use Errno (); |
45 | |
45 | |
46 | use AnyEvent 4.8 (); |
46 | use AnyEvent 5.0 (); |
47 | use AnyEvent::Util (); |
47 | use AnyEvent::Util (); |
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.41'; |
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; |
… | |
… | |
138 | |
138 | |
139 | =item headers => hashref |
139 | =item headers => hashref |
140 | |
140 | |
141 | The request headers to use. Currently, C<http_request> may provide its |
141 | The request headers to use. Currently, C<http_request> may provide its |
142 | own C<Host:>, C<Content-Length:>, C<Connection:> and C<Cookie:> headers |
142 | own C<Host:>, C<Content-Length:>, C<Connection:> and C<Cookie:> headers |
143 | and will provide defaults for C<User-Agent:> and C<Referer:>. |
143 | and will provide defaults for C<User-Agent:> and C<Referer:> (this can be |
|
|
144 | suppressed by using C<undef> for these headers in which case they won't be |
|
|
145 | sent at all). |
144 | |
146 | |
145 | =item timeout => $seconds |
147 | =item timeout => $seconds |
146 | |
148 | |
147 | The time-out to use for various stages - each connect attempt will reset |
149 | The time-out to use for various stages - each connect attempt will reset |
148 | the timeout, as will read or write activity. Default timeout is 5 minutes. |
150 | the timeout, as will read or write activity, i.e. this is not an overall |
|
|
151 | timeout. |
|
|
152 | |
|
|
153 | Default timeout is 5 minutes. |
149 | |
154 | |
150 | =item proxy => [$host, $port[, $scheme]] or undef |
155 | =item proxy => [$host, $port[, $scheme]] or undef |
151 | |
156 | |
152 | Use the given http proxy for all requests. If not specified, then the |
157 | Use the given http proxy for all requests. If not specified, then the |
153 | default proxy (as specified by C<$ENV{http_proxy}>) is used. |
158 | default proxy (as specified by C<$ENV{http_proxy}>) is used. |
154 | |
159 | |
155 | C<$scheme> must be either missing or C<http> for HTTP, or C<https> for |
160 | C<$scheme> must be either missing, C<http> for HTTP or C<https> for |
156 | HTTPS. |
161 | HTTPS. |
157 | |
162 | |
158 | =item body => $string |
163 | =item body => $string |
159 | |
164 | |
160 | The request body, usually empty. Will be-sent as-is (future versions of |
165 | The request body, usually empty. Will be-sent as-is (future versions of |
… | |
… | |
185 | verification, highest compatibility) and high-security (CA and common-name |
190 | verification, highest compatibility) and high-security (CA and common-name |
186 | verification) TLS context. |
191 | verification) TLS context. |
187 | |
192 | |
188 | The default for this option is C<low>, which could be interpreted as "give |
193 | The default for this option is C<low>, which could be interpreted as "give |
189 | me the page, no matter what". |
194 | me the page, no matter what". |
|
|
195 | |
|
|
196 | =item on_prepare => $callback->($fh) |
|
|
197 | |
|
|
198 | In rare cases you need to "tune" the socket before it is used to |
|
|
199 | connect (for exmaple, to bind it on a given IP address). This parameter |
|
|
200 | overrides the prepare callback passed to C<AnyEvent::Socket::tcp_connect> |
|
|
201 | and behaves exactly the same way (e.g. it has to provide a |
|
|
202 | timeout). See the description for the C<$prepare_cb> argument of |
|
|
203 | C<AnyEvent::Socket::tcp_connect> for details. |
190 | |
204 | |
191 | =item on_header => $callback->($headers) |
205 | =item on_header => $callback->($headers) |
192 | |
206 | |
193 | When specified, this callback will be called with the header hash as soon |
207 | When specified, this callback will be called with the header hash as soon |
194 | as headers have been successfully received from the remote server (not on |
208 | as headers have been successfully received from the remote server (not on |
… | |
… | |
358 | or return $cb->(undef, { Status => 599, Reason => "Unparsable URL", URL => $url }); |
372 | or return $cb->(undef, { Status => 599, Reason => "Unparsable URL", URL => $url }); |
359 | |
373 | |
360 | my $uhost = $1; |
374 | my $uhost = $1; |
361 | $uport = $2 if defined $2; |
375 | $uport = $2 if defined $2; |
362 | |
376 | |
363 | $hdr{host} = defined $2 ? "$uhost:$2" : "$uhost"; |
377 | $hdr{host} = defined $2 ? "$uhost:$2" : "$uhost" |
|
|
378 | unless exists $hdr{host}; |
364 | |
379 | |
365 | $uhost =~ s/^\[(.*)\]$/$1/; |
380 | $uhost =~ s/^\[(.*)\]$/$1/; |
366 | $upath .= "?$query" if length $query; |
381 | $upath .= "?$query" if length $query; |
367 | |
382 | |
368 | $upath =~ s%^/?%/%; |
383 | $upath =~ s%^/?%/%; |
… | |
… | |
401 | my ($rhost, $rport, $rscheme, $rpath); # request host, port, path |
416 | my ($rhost, $rport, $rscheme, $rpath); # request host, port, path |
402 | |
417 | |
403 | if ($proxy) { |
418 | if ($proxy) { |
404 | ($rpath, $rhost, $rport, $rscheme) = ($url, @$proxy); |
419 | ($rpath, $rhost, $rport, $rscheme) = ($url, @$proxy); |
405 | |
420 | |
|
|
421 | $rscheme = "http" unless defined $rscheme; |
|
|
422 | |
406 | # don't support https requests over https-proxy transport, |
423 | # don't support https requests over https-proxy transport, |
407 | # can't be done with tls as spec'ed, unless you double-encrypt. |
424 | # can't be done with tls as spec'ed, unless you double-encrypt. |
408 | $rscheme = "http" if $uscheme eq "https" && $rscheme eq "https"; |
425 | $rscheme = "http" if $uscheme eq "https" && $rscheme eq "https"; |
409 | } else { |
426 | } else { |
410 | ($rhost, $rport, $rscheme, $rpath) = ($uhost, $uport, $uscheme, $upath); |
427 | ($rhost, $rport, $rscheme, $rpath) = ($uhost, $uport, $uscheme, $upath); |
411 | } |
428 | } |
412 | |
429 | |
413 | $hdr{"user-agent"} ||= $USERAGENT; |
430 | # leave out fragment and query string, just a heuristic |
414 | $hdr{referer} ||= "$uscheme://$uauthority$upath"; # leave out fragment and query string, just a heuristic |
431 | $hdr{referer} ||= "$uscheme://$uauthority$upath" unless exists $hdr{referer}; |
|
|
432 | $hdr{"user-agent"} ||= $USERAGENT unless exists $hdr{"user-agent"}; |
415 | |
433 | |
416 | $hdr{"content-length"} = length $arg{body}; |
434 | $hdr{"content-length"} = length $arg{body} |
|
|
435 | if length $arg{body} || $method ne "GET"; |
417 | |
436 | |
418 | my %state = (connect_guard => 1); |
437 | my %state = (connect_guard => 1); |
419 | |
438 | |
420 | _get_slot $uhost, sub { |
439 | _get_slot $uhost, sub { |
421 | $state{slot_guard} = shift; |
440 | $state{slot_guard} = shift; |
… | |
… | |
470 | $state{handle}->starttls ("connect") if $uscheme eq "https" && !exists $state{handle}{tls}; |
489 | $state{handle}->starttls ("connect") if $uscheme eq "https" && !exists $state{handle}{tls}; |
471 | |
490 | |
472 | # send request |
491 | # send request |
473 | $state{handle}->push_write ( |
492 | $state{handle}->push_write ( |
474 | "$method $rpath HTTP/1.0\015\012" |
493 | "$method $rpath HTTP/1.0\015\012" |
475 | . (join "", map "\u$_: $hdr{$_}\015\012", keys %hdr) |
494 | . (join "", map "\u$_: $hdr{$_}\015\012", grep defined $hdr{$_}, keys %hdr) |
476 | . "\015\012" |
495 | . "\015\012" |
477 | . (delete $arg{body}) |
496 | . (delete $arg{body}) |
478 | ); |
497 | ); |
|
|
498 | |
|
|
499 | # return if error occured during push_write() |
|
|
500 | return unless %state; |
479 | |
501 | |
480 | %hdr = (); # reduce memory usage, save a kitten |
502 | %hdr = (); # reduce memory usage, save a kitten |
481 | |
503 | |
482 | # status line |
504 | # status line |
483 | $state{handle}->push_read (line => $qr_nl, sub { |
505 | $state{handle}->push_read (line => $qr_nl, sub { |
… | |
… | |
598 | |
620 | |
599 | redo if /\G\s*,/gc; |
621 | redo if /\G\s*,/gc; |
600 | } |
622 | } |
601 | } |
623 | } |
602 | |
624 | |
603 | if ($redirect) { |
625 | if ($redirect && exists $hdr{location}) { |
604 | # we ignore any errors, as it is very common to receive |
626 | # we ignore any errors, as it is very common to receive |
605 | # Content-Length != 0 but no actual body |
627 | # Content-Length != 0 but no actual body |
606 | # we also access %hdr, as $_[1] might be an erro |
628 | # we also access %hdr, as $_[1] might be an erro |
607 | http_request ($method => $hdr{location}, %arg, recurse => $recurse - 1, $cb); |
629 | http_request ($method => $hdr{location}, %arg, recurse => $recurse - 1, $cb); |
608 | } else { |
630 | } else { |
… | |
… | |
697 | }); |
719 | }); |
698 | } else { |
720 | } else { |
699 | &$handle_actual_request; |
721 | &$handle_actual_request; |
700 | } |
722 | } |
701 | |
723 | |
702 | }, sub { |
724 | }, $arg{on_prepare} || sub { $timeout }; |
703 | $timeout |
|
|
704 | }; |
|
|
705 | }; |
725 | }; |
706 | |
726 | |
707 | defined wantarray && AnyEvent::Util::guard { %state = () } |
727 | defined wantarray && AnyEvent::Util::guard { %state = () } |
708 | } |
728 | } |
709 | |
729 | |
… | |
… | |
730 | =over 4 |
750 | =over 4 |
731 | |
751 | |
732 | =item AnyEvent::HTTP::set_proxy "proxy-url" |
752 | =item AnyEvent::HTTP::set_proxy "proxy-url" |
733 | |
753 | |
734 | 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 |
735 | 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>. |
736 | |
759 | |
737 | =item $AnyEvent::HTTP::MAX_RECURSE |
760 | =item $AnyEvent::HTTP::MAX_RECURSE |
738 | |
761 | |
739 | 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>). |
740 | |
763 | |
… | |
… | |
743 | The default value for the C<User-Agent> header (the default is |
766 | The default value for the C<User-Agent> header (the default is |
744 | C<Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)>). |
767 | C<Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)>). |
745 | |
768 | |
746 | =item $AnyEvent::HTTP::MAX_PER_HOST |
769 | =item $AnyEvent::HTTP::MAX_PER_HOST |
747 | |
770 | |
748 | The maximum number of concurrent conenctions to the same host (identified |
771 | The maximum number of concurrent connections to the same host (identified |
749 | by the hostname). If the limit is exceeded, then the additional requests |
772 | by the hostname). If the limit is exceeded, then the additional requests |
750 | are queued until previous connections are closed. |
773 | are queued until previous connections are closed. |
751 | |
774 | |
752 | The default value for this is C<4>, and it is highly advisable to not |
775 | The default value for this is C<4>, and it is highly advisable to not |
753 | increase it. |
776 | increase it. |
… | |
… | |
761 | =back |
784 | =back |
762 | |
785 | |
763 | =cut |
786 | =cut |
764 | |
787 | |
765 | sub set_proxy($) { |
788 | sub set_proxy($) { |
|
|
789 | if (length $_[0]) { |
766 | $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 | } |
767 | } |
796 | } |
768 | |
797 | |
769 | # initialise proxy from environment |
798 | # initialise proxy from environment |
|
|
799 | eval { |
770 | set_proxy $ENV{http_proxy}; |
800 | set_proxy $ENV{http_proxy}; |
|
|
801 | }; |
771 | |
802 | |
772 | =head1 SEE ALSO |
803 | =head1 SEE ALSO |
773 | |
804 | |
774 | L<AnyEvent>. |
805 | L<AnyEvent>. |
775 | |
806 | |