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.61 by root, Thu Dec 30 03:45:01 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
805string of the form C<http://host:port> (optionally C<https:...>), croaks 806string of the form C<http://host:port> (optionally C<https:...>), croaks
806otherwise. 807otherwise.
807 808
808To clear an already-set proxy, use C<undef>. 809To clear an already-set proxy, use C<undef>.
809 810
811=item $date = AnyEvent::HTTP::format_date $timestamp
812
813Takes a POSIX timestamp (seconds since the epoch) and formats it as a HTTP
814Date (RFC 2616).
815
816=item $timestamp = AnyEvent::HTTP::parse_date $date
817
818Takes a HTTP Date (RFC 2616) and returns the corresponding POSIX
819timestamp, or C<undef> if the date cannot be parsed.
820
810=item $AnyEvent::HTTP::MAX_RECURSE 821=item $AnyEvent::HTTP::MAX_RECURSE
811 822
812The default value for the C<recurse> request parameter (default: C<10>). 823The default value for the C<recurse> request parameter (default: C<10>).
813 824
814=item $AnyEvent::HTTP::USERAGENT 825=item $AnyEvent::HTTP::USERAGENT
832connections. This number of can be useful for load-leveling. 843connections. This number of can be useful for load-leveling.
833 844
834=back 845=back
835 846
836=cut 847=cut
848
849our @month = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
850our @weekday = qw(Sun Mon Tue Wed Thu Fri Sat);
851
852sub 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
863sub 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
838sub set_proxy($) { 892sub 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
849eval { 903eval {
850 set_proxy $ENV{http_proxy}; 904 set_proxy $ENV{http_proxy};
851}; 905};
852 906
907=head2 SOCKS PROXIES
908
909Socks proxies are not directly supported by AnyEvent::HTTP. You can
910compile your perl to support socks, or use an external program such as
911F<socksify> (dante) or F<tsocks> to make your program use a socks proxy
912transparently.
913
914Alternatively, for AnyEvent::HTTP only, you can use your own
915C<tcp_connect> function that does the proxy handshake - here is an example
916that 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
953Use C<socks4a_connect> instead of C<tcp_connect> when doing C<http_request>s,
954possibly 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
855L<AnyEvent>. 965L<AnyEvent>.
856 966
857=head1 AUTHOR 967=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines