… | |
… | |
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 | |
… | |
… | |
848 | # initialise proxy from environment |
849 | # initialise proxy from environment |
849 | eval { |
850 | eval { |
850 | set_proxy $ENV{http_proxy}; |
851 | set_proxy $ENV{http_proxy}; |
851 | }; |
852 | }; |
852 | |
853 | |
|
|
854 | =head2 SOCKS PROXIES |
|
|
855 | |
|
|
856 | Socks proxies are not directly supported by AnyEvent::HTTP. You can |
|
|
857 | compile your perl to support socks, or use an external program such as |
|
|
858 | F<socksify> (dante) or F<tsocks> to make your program use a socks proxy |
|
|
859 | transparently. |
|
|
860 | |
|
|
861 | Alternatively, for AnyEvent::HTTP only, you can use your own |
|
|
862 | C<tcp_connect> function that does the proxy handshake - here is an example |
|
|
863 | that works with socks4a proxies: |
|
|
864 | |
|
|
865 | use Errno; |
|
|
866 | use AnyEvent::Util; |
|
|
867 | use AnyEvent::Socket; |
|
|
868 | use AnyEvent::Handle; |
|
|
869 | |
|
|
870 | # host, port and username of/for your socks4a proxy |
|
|
871 | my $socks_host = "10.0.0.23"; |
|
|
872 | my $socks_port = 9050; |
|
|
873 | my $socks_user = ""; |
|
|
874 | |
|
|
875 | sub socks4a_connect { |
|
|
876 | my ($host, $port, $connect_cb, $prepare_cb) = @_; |
|
|
877 | |
|
|
878 | my $hdl = new AnyEvent::Handle |
|
|
879 | connect => [$socks_host, $socks_port], |
|
|
880 | on_prepare => sub { $prepare_cb->($_[0]{fh}) }, |
|
|
881 | on_error => sub { $connect_cb->() }, |
|
|
882 | ; |
|
|
883 | |
|
|
884 | $hdl->push_write (pack "CCnNZ*Z*", 4, 1, $port, 1, $socks_user, $host); |
|
|
885 | |
|
|
886 | $hdl->push_read (chunk => 8, sub { |
|
|
887 | my ($hdl, $chunk) = @_; |
|
|
888 | my ($status, $port, $ipn) = unpack "xCna4", $chunk; |
|
|
889 | |
|
|
890 | if ($status == 0x5a) { |
|
|
891 | $connect_cb->($hdl->{fh}, (format_address $ipn) . ":$port"); |
|
|
892 | } else { |
|
|
893 | $! = Errno::ENXIO; $connect_cb->(); |
|
|
894 | } |
|
|
895 | }); |
|
|
896 | |
|
|
897 | $hdl |
|
|
898 | } |
|
|
899 | |
|
|
900 | Use C<socks4a_connect> instead of C<tcp_connect> when doing C<http_request>s, |
|
|
901 | possibly after switching off other proxy types: |
|
|
902 | |
|
|
903 | AnyEvent::HTTP::set_proxy undef; # usually you do not want other proxies |
|
|
904 | |
|
|
905 | http_get 'http://www.google.com', tcp_connect => \&socks4a_connect, sub { |
|
|
906 | my ($data, $headers) = @_; |
|
|
907 | ... |
|
|
908 | }; |
|
|
909 | |
853 | =head1 SEE ALSO |
910 | =head1 SEE ALSO |
854 | |
911 | |
855 | L<AnyEvent>. |
912 | L<AnyEvent>. |
856 | |
913 | |
857 | =head1 AUTHOR |
914 | =head1 AUTHOR |