… | |
… | |
147 | Whether to recurse requests or not, e.g. on redirects, authentication |
147 | Whether to recurse requests or not, e.g. on redirects, authentication |
148 | retries and so on, and how often to do so. |
148 | retries and so on, and how often to do so. |
149 | |
149 | |
150 | =item headers => hashref |
150 | =item headers => hashref |
151 | |
151 | |
152 | The request headers to use. Currently, C<http_request> may provide its |
152 | The request headers to use, with the header name (I<MUST be in lowercase>) |
153 | own C<Host:>, C<Content-Length:>, C<Connection:> and C<Cookie:> headers |
153 | as key and header value as hash value. |
154 | and will provide defaults for C<User-Agent:> and C<Referer:> (this can be |
154 | |
|
|
155 | Currently, http_request> may provide its own C<host>, C<content-length>, |
|
|
156 | C<connection> and C<cookie> headers and will provide defaults for |
|
|
157 | C<user-agent> and C<referer> (this can be suppressed by using a value of |
155 | suppressed by using C<undef> for these headers in which case they won't be |
158 | C<undef> for these headers in which case they won't be sent at all). |
156 | sent at all). |
|
|
157 | |
159 | |
158 | =item timeout => $seconds |
160 | =item timeout => $seconds |
159 | |
161 | |
160 | The time-out to use for various stages - each connect attempt will reset |
162 | The time-out to use for various stages - each connect attempt will reset |
161 | the timeout, as will read or write activity, i.e. this is not an overall |
163 | the timeout, as will read or write activity, i.e. this is not an overall |
… | |
… | |
216 | =item tcp_connect => $callback->($host, $service, $connect_cb, $prepare_cb) |
218 | =item tcp_connect => $callback->($host, $service, $connect_cb, $prepare_cb) |
217 | |
219 | |
218 | In even rarer cases you want total control over how AnyEvent::HTTP |
220 | In even rarer cases you want total control over how AnyEvent::HTTP |
219 | establishes connections. Normally it uses L<AnyEvent::Socket::tcp_connect> |
221 | establishes connections. Normally it uses L<AnyEvent::Socket::tcp_connect> |
220 | to do this, but you can provide your own C<tcp_connect> function - |
222 | to do this, but you can provide your own C<tcp_connect> function - |
221 | obviously, it has to follow the same calling conventions. |
223 | obviously, it has to follow the same calling conventions, except that it |
|
|
224 | may always return a connection guard object. |
222 | |
225 | |
223 | There are probably lots of weird uses for this function, starting from |
226 | There are probably lots of weird uses for this function, starting from |
224 | tracing the hosts C<http_request> actually tries to connect, to (inexact |
227 | tracing the hosts C<http_request> actually tries to connect, to (inexact |
225 | but fast) host => IP address caching or even socks protocol support. |
228 | but fast) host => IP address caching or even socks protocol support. |
226 | |
229 | |
… | |
… | |
805 | string of the form C<http://host:port> (optionally C<https:...>), croaks |
808 | string of the form C<http://host:port> (optionally C<https:...>), croaks |
806 | otherwise. |
809 | otherwise. |
807 | |
810 | |
808 | To clear an already-set proxy, use C<undef>. |
811 | To clear an already-set proxy, use C<undef>. |
809 | |
812 | |
|
|
813 | =item $date = AnyEvent::HTTP::format_date $timestamp |
|
|
814 | |
|
|
815 | Takes a POSIX timestamp (seconds since the epoch) and formats it as a HTTP |
|
|
816 | Date (RFC 2616). |
|
|
817 | |
|
|
818 | =item $timestamp = AnyEvent::HTTP::parse_date $date |
|
|
819 | |
|
|
820 | Takes a HTTP Date (RFC 2616) and returns the corresponding POSIX |
|
|
821 | timestamp, or C<undef> if the date cannot be parsed. |
|
|
822 | |
810 | =item $AnyEvent::HTTP::MAX_RECURSE |
823 | =item $AnyEvent::HTTP::MAX_RECURSE |
811 | |
824 | |
812 | The default value for the C<recurse> request parameter (default: C<10>). |
825 | The default value for the C<recurse> request parameter (default: C<10>). |
813 | |
826 | |
814 | =item $AnyEvent::HTTP::USERAGENT |
827 | =item $AnyEvent::HTTP::USERAGENT |
… | |
… | |
832 | connections. This number of can be useful for load-leveling. |
845 | connections. This number of can be useful for load-leveling. |
833 | |
846 | |
834 | =back |
847 | =back |
835 | |
848 | |
836 | =cut |
849 | =cut |
|
|
850 | |
|
|
851 | our @month = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); |
|
|
852 | our @weekday = qw(Sun Mon Tue Wed Thu Fri Sat); |
|
|
853 | |
|
|
854 | sub format_date($) { |
|
|
855 | my ($time) = @_; |
|
|
856 | |
|
|
857 | # RFC 822/1123 format |
|
|
858 | my ($S, $M, $H, $mday, $mon, $year, $wday, $yday, undef) = gmtime $time; |
|
|
859 | |
|
|
860 | sprintf "%s, %02d %s %04d %02d:%02d:%02d GMT", |
|
|
861 | $weekday[$wday], $mday, $month[$mon], $year + 1900, |
|
|
862 | $H, $M, $S; |
|
|
863 | } |
|
|
864 | |
|
|
865 | sub parse_date($) { |
|
|
866 | my ($date) = @_; |
|
|
867 | |
|
|
868 | my ($d, $m, $y, $H, $M, $S); |
|
|
869 | |
|
|
870 | if ($date =~ /^[A-Z][a-z][a-z], ([0-9][0-9]) ([A-Z][a-z][a-z]) ([0-9][0-9][0-9][0-9]) ([0-9][0-9]):([0-9][0-9]):([0-9][0-9]) GMT$/) { |
|
|
871 | # RFC 822/1123, required by RFC 2616 |
|
|
872 | ($d, $m, $y, $H, $M, $S) = ($1, $2, $3, $4, $5, $6); |
|
|
873 | |
|
|
874 | } elsif ($date =~ /^[A-Z][a-z]+, ([0-9][0-9])-([A-Z][a-z][a-z])-([0-9][0-9]) ([0-9][0-9]):([0-9][0-9]):([0-9][0-9]) GMT$/) { |
|
|
875 | # RFC 850 |
|
|
876 | ($d, $m, $y, $H, $M, $S) = ($1, $2, $3 < 69 ? $3 + 2000 : $3 + 1900, $4, $5, $6); |
|
|
877 | |
|
|
878 | } elsif ($date =~ /^[A-Z][a-z][a-z] ([A-Z][a-z][a-z]) ([0-9 ][0-9]) ([0-9][0-9]):([0-9][0-9]):([0-9][0-9]) ([0-9][0-9][0-9][0-9])$/) { |
|
|
879 | # ISO C's asctime |
|
|
880 | ($d, $m, $y, $H, $M, $S) = ($2, $1, $6, $3, $4, $5); |
|
|
881 | } |
|
|
882 | # other formats fail in the loop below |
|
|
883 | |
|
|
884 | for (0..11) { |
|
|
885 | if ($m eq $month[$_]) { |
|
|
886 | require Time::Local; |
|
|
887 | return Time::Local::timegm ($S, $M, $H, $d, $_, $y); |
|
|
888 | } |
|
|
889 | } |
|
|
890 | |
|
|
891 | undef |
|
|
892 | } |
837 | |
893 | |
838 | sub set_proxy($) { |
894 | sub set_proxy($) { |
839 | if (length $_[0]) { |
895 | if (length $_[0]) { |
840 | $_[0] =~ m%^(https?):// ([^:/]+) (?: : (\d*) )?%ix |
896 | $_[0] =~ m%^(https?):// ([^:/]+) (?: : (\d*) )?%ix |
841 | or Carp::croak "$_[0]: invalid proxy URL"; |
897 | or Carp::croak "$_[0]: invalid proxy URL"; |
… | |
… | |
848 | # initialise proxy from environment |
904 | # initialise proxy from environment |
849 | eval { |
905 | eval { |
850 | set_proxy $ENV{http_proxy}; |
906 | set_proxy $ENV{http_proxy}; |
851 | }; |
907 | }; |
852 | |
908 | |
|
|
909 | =head2 SOCKS PROXIES |
|
|
910 | |
|
|
911 | Socks proxies are not directly supported by AnyEvent::HTTP. You can |
|
|
912 | compile your perl to support socks, or use an external program such as |
|
|
913 | F<socksify> (dante) or F<tsocks> to make your program use a socks proxy |
|
|
914 | transparently. |
|
|
915 | |
|
|
916 | Alternatively, for AnyEvent::HTTP only, you can use your own |
|
|
917 | C<tcp_connect> function that does the proxy handshake - here is an example |
|
|
918 | that works with socks4a proxies: |
|
|
919 | |
|
|
920 | use Errno; |
|
|
921 | use AnyEvent::Util; |
|
|
922 | use AnyEvent::Socket; |
|
|
923 | use AnyEvent::Handle; |
|
|
924 | |
|
|
925 | # host, port and username of/for your socks4a proxy |
|
|
926 | my $socks_host = "10.0.0.23"; |
|
|
927 | my $socks_port = 9050; |
|
|
928 | my $socks_user = ""; |
|
|
929 | |
|
|
930 | sub socks4a_connect { |
|
|
931 | my ($host, $port, $connect_cb, $prepare_cb) = @_; |
|
|
932 | |
|
|
933 | my $hdl = new AnyEvent::Handle |
|
|
934 | connect => [$socks_host, $socks_port], |
|
|
935 | on_prepare => sub { $prepare_cb->($_[0]{fh}) }, |
|
|
936 | on_error => sub { $connect_cb->() }, |
|
|
937 | ; |
|
|
938 | |
|
|
939 | $hdl->push_write (pack "CCnNZ*Z*", 4, 1, $port, 1, $socks_user, $host); |
|
|
940 | |
|
|
941 | $hdl->push_read (chunk => 8, sub { |
|
|
942 | my ($hdl, $chunk) = @_; |
|
|
943 | my ($status, $port, $ipn) = unpack "xCna4", $chunk; |
|
|
944 | |
|
|
945 | if ($status == 0x5a) { |
|
|
946 | $connect_cb->($hdl->{fh}, (format_address $ipn) . ":$port"); |
|
|
947 | } else { |
|
|
948 | $! = Errno::ENXIO; $connect_cb->(); |
|
|
949 | } |
|
|
950 | }); |
|
|
951 | |
|
|
952 | $hdl |
|
|
953 | } |
|
|
954 | |
|
|
955 | Use C<socks4a_connect> instead of C<tcp_connect> when doing C<http_request>s, |
|
|
956 | possibly after switching off other proxy types: |
|
|
957 | |
|
|
958 | AnyEvent::HTTP::set_proxy undef; # usually you do not want other proxies |
|
|
959 | |
|
|
960 | http_get 'http://www.google.com', tcp_connect => \&socks4a_connect, sub { |
|
|
961 | my ($data, $headers) = @_; |
|
|
962 | ... |
|
|
963 | }; |
|
|
964 | |
853 | =head1 SEE ALSO |
965 | =head1 SEE ALSO |
854 | |
966 | |
855 | L<AnyEvent>. |
967 | L<AnyEvent>. |
856 | |
968 | |
857 | =head1 AUTHOR |
969 | =head1 AUTHOR |