… | |
… | |
806 | 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 |
807 | otherwise. |
807 | otherwise. |
808 | |
808 | |
809 | To clear an already-set proxy, use C<undef>. |
809 | To clear an already-set proxy, use C<undef>. |
810 | |
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 | |
811 | =item $AnyEvent::HTTP::MAX_RECURSE |
821 | =item $AnyEvent::HTTP::MAX_RECURSE |
812 | |
822 | |
813 | 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>). |
814 | |
824 | |
815 | =item $AnyEvent::HTTP::USERAGENT |
825 | =item $AnyEvent::HTTP::USERAGENT |
… | |
… | |
833 | connections. This number of can be useful for load-leveling. |
843 | connections. This number of can be useful for load-leveling. |
834 | |
844 | |
835 | =back |
845 | =back |
836 | |
846 | |
837 | =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 | } |
838 | |
891 | |
839 | sub set_proxy($) { |
892 | sub set_proxy($) { |
840 | if (length $_[0]) { |
893 | if (length $_[0]) { |
841 | $_[0] =~ m%^(https?):// ([^:/]+) (?: : (\d*) )?%ix |
894 | $_[0] =~ m%^(https?):// ([^:/]+) (?: : (\d*) )?%ix |
842 | or Carp::croak "$_[0]: invalid proxy URL"; |
895 | or Carp::croak "$_[0]: invalid proxy URL"; |