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