ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-HTTP/HTTP.pm
(Generate patch)

Comparing AnyEvent-HTTP/HTTP.pm (file contents):
Revision 1.59 by root, Wed Dec 29 23:59:36 2010 UTC vs.
Revision 1.60 by root, Thu Dec 30 02:56:28 2010 UTC

216=item tcp_connect => $callback->($host, $service, $connect_cb, $prepare_cb) 216=item tcp_connect => $callback->($host, $service, $connect_cb, $prepare_cb)
217 217
218In even rarer cases you want total control over how AnyEvent::HTTP 218In even rarer cases you want total control over how AnyEvent::HTTP
219establishes connections. Normally it uses L<AnyEvent::Socket::tcp_connect> 219establishes connections. Normally it uses L<AnyEvent::Socket::tcp_connect>
220to do this, but you can provide your own C<tcp_connect> function - 220to do this, but you can provide your own C<tcp_connect> function -
221obviously, it has to follow the same calling conventions. 221obviously, it has to follow the same calling conventions, except that it
222may always return a connection guard object.
222 223
223There are probably lots of weird uses for this function, starting from 224There are probably lots of weird uses for this function, starting from
224tracing the hosts C<http_request> actually tries to connect, to (inexact 225tracing the hosts C<http_request> actually tries to connect, to (inexact
225but fast) host => IP address caching or even socks protocol support. 226but fast) host => IP address caching or even socks protocol support.
226 227
848# initialise proxy from environment 849# initialise proxy from environment
849eval { 850eval {
850 set_proxy $ENV{http_proxy}; 851 set_proxy $ENV{http_proxy};
851}; 852};
852 853
854=head2 SOCKS PROXIES
855
856Socks proxies are not directly supported by AnyEvent::HTTP. You can
857compile your perl to support socks, or use an external program such as
858F<socksify> (dante) or F<tsocks> to make your program use a socks proxy
859transparently.
860
861Alternatively, for AnyEvent::HTTP only, you can use your own
862C<tcp_connect> function that does the proxy handshake - here is an example
863that 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
900Use C<socks4a_connect> instead of C<tcp_connect> when doing C<http_request>s,
901possibly 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
855L<AnyEvent>. 912L<AnyEvent>.
856 913
857=head1 AUTHOR 914=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines