… | |
… | |
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.42'; |
53 | our $VERSION = '1.43'; |
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; |
… | |
… | |
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:> (this can be |
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 |
144 | suppressed by using C<undef> for these headers in which case they won't be |
145 | send at all). |
145 | sent at all). |
146 | |
146 | |
147 | =item timeout => $seconds |
147 | =item timeout => $seconds |
148 | |
148 | |
149 | 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 |
150 | 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. |
151 | |
154 | |
152 | =item proxy => [$host, $port[, $scheme]] or undef |
155 | =item proxy => [$host, $port[, $scheme]] or undef |
153 | |
156 | |
154 | 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 |
155 | default proxy (as specified by C<$ENV{http_proxy}>) is used. |
158 | default proxy (as specified by C<$ENV{http_proxy}>) is used. |
… | |
… | |
187 | verification, highest compatibility) and high-security (CA and common-name |
190 | verification, highest compatibility) and high-security (CA and common-name |
188 | verification) TLS context. |
191 | verification) TLS context. |
189 | |
192 | |
190 | 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 |
191 | 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. |
192 | |
204 | |
193 | =item on_header => $callback->($headers) |
205 | =item on_header => $callback->($headers) |
194 | |
206 | |
195 | 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 |
196 | 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 |
… | |
… | |
702 | }); |
714 | }); |
703 | } else { |
715 | } else { |
704 | &$handle_actual_request; |
716 | &$handle_actual_request; |
705 | } |
717 | } |
706 | |
718 | |
707 | }, sub { |
719 | }, $arg{on_prepare} || sub { $timeout }; |
708 | $timeout |
|
|
709 | }; |
|
|
710 | }; |
720 | }; |
711 | |
721 | |
712 | defined wantarray && AnyEvent::Util::guard { %state = () } |
722 | defined wantarray && AnyEvent::Util::guard { %state = () } |
713 | } |
723 | } |
714 | |
724 | |
… | |
… | |
735 | =over 4 |
745 | =over 4 |
736 | |
746 | |
737 | =item AnyEvent::HTTP::set_proxy "proxy-url" |
747 | =item AnyEvent::HTTP::set_proxy "proxy-url" |
738 | |
748 | |
739 | Sets the default proxy server to use. The proxy-url must begin with a |
749 | Sets the default proxy server to use. The proxy-url must begin with a |
740 | string of the form C<http://host:port> (optionally C<https:...>). |
750 | string of the form C<http://host:port> (optionally C<https:...>), croaks |
|
|
751 | otherwise. |
|
|
752 | |
|
|
753 | To clear an already-set proxy, use C<undef>. |
741 | |
754 | |
742 | =item $AnyEvent::HTTP::MAX_RECURSE |
755 | =item $AnyEvent::HTTP::MAX_RECURSE |
743 | |
756 | |
744 | The default value for the C<recurse> request parameter (default: C<10>). |
757 | The default value for the C<recurse> request parameter (default: C<10>). |
745 | |
758 | |
… | |
… | |
766 | =back |
779 | =back |
767 | |
780 | |
768 | =cut |
781 | =cut |
769 | |
782 | |
770 | sub set_proxy($) { |
783 | sub set_proxy($) { |
|
|
784 | if (length $_[0]) { |
771 | $PROXY = [$2, $3 || 3128, $1] if $_[0] =~ m%^(https?):// ([^:/]+) (?: : (\d*) )?%ix; |
785 | $_[0] =~ m%^(https?):// ([^:/]+) (?: : (\d*) )?%ix |
|
|
786 | or Carp::croak "$_[0]: invalid proxy URL"; |
|
|
787 | $PROXY = [$2, $3 || 3128, $1] |
|
|
788 | } else { |
|
|
789 | undef $PROXY; |
|
|
790 | } |
772 | } |
791 | } |
773 | |
792 | |
774 | # initialise proxy from environment |
793 | # initialise proxy from environment |
|
|
794 | eval { |
775 | set_proxy $ENV{http_proxy}; |
795 | set_proxy $ENV{http_proxy}; |
|
|
796 | }; |
776 | |
797 | |
777 | =head1 SEE ALSO |
798 | =head1 SEE ALSO |
778 | |
799 | |
779 | L<AnyEvent>. |
800 | L<AnyEvent>. |
780 | |
801 | |