… | |
… | |
43 | |
43 | |
44 | use Errno (); |
44 | use Errno (); |
45 | |
45 | |
46 | use AnyEvent 5.0 (); |
46 | use AnyEvent 5.0 (); |
47 | use AnyEvent::Util (); |
47 | use AnyEvent::Util (); |
48 | use AnyEvent::Socket (); |
|
|
49 | use AnyEvent::Handle (); |
48 | use AnyEvent::Handle (); |
50 | |
49 | |
51 | use base Exporter::; |
50 | use base Exporter::; |
52 | |
51 | |
53 | our $VERSION = '1.46'; |
52 | our $VERSION = '1.5'; |
54 | |
53 | |
55 | our @EXPORT = qw(http_get http_post http_head http_request); |
54 | our @EXPORT = qw(http_get http_post http_head http_request); |
56 | |
55 | |
57 | our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; |
56 | our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; |
58 | our $MAX_RECURSE = 10; |
57 | our $MAX_RECURSE = 10; |
… | |
… | |
101 | second argument. |
100 | second argument. |
102 | |
101 | |
103 | All the headers in that hash are lowercased. In addition to the response |
102 | All the headers in that hash are lowercased. In addition to the response |
104 | headers, the "pseudo-headers" (uppercase to avoid clashing with possible |
103 | headers, the "pseudo-headers" (uppercase to avoid clashing with possible |
105 | response headers) C<HTTPVersion>, C<Status> and C<Reason> contain the |
104 | response headers) C<HTTPVersion>, C<Status> and C<Reason> contain the |
106 | three parts of the HTTP Status-Line of the same name. |
105 | three parts of the HTTP Status-Line of the same name. If an error occurs |
|
|
106 | during the body phase of a request, then the original C<Status> and |
|
|
107 | C<Reason> values from the header are available as C<OrigStatus> and |
|
|
108 | C<OrigReason>. |
107 | |
109 | |
108 | The pseudo-header C<URL> contains the actual URL (which can differ from |
110 | The pseudo-header C<URL> contains the actual URL (which can differ from |
109 | the requested URL when following redirects - for example, you might get |
111 | the requested URL when following redirects - for example, you might get |
110 | an error that your URL scheme is not supported even though your URL is a |
112 | an error that your URL scheme is not supported even though your URL is a |
111 | valid http URL because it redirected to an ftp URL, in which case you can |
113 | valid http URL because it redirected to an ftp URL, in which case you can |
… | |
… | |
211 | connect (for exmaple, to bind it on a given IP address). This parameter |
213 | connect (for exmaple, to bind it on a given IP address). This parameter |
212 | overrides the prepare callback passed to C<AnyEvent::Socket::tcp_connect> |
214 | overrides the prepare callback passed to C<AnyEvent::Socket::tcp_connect> |
213 | and behaves exactly the same way (e.g. it has to provide a |
215 | and behaves exactly the same way (e.g. it has to provide a |
214 | timeout). See the description for the C<$prepare_cb> argument of |
216 | timeout). See the description for the C<$prepare_cb> argument of |
215 | C<AnyEvent::Socket::tcp_connect> for details. |
217 | C<AnyEvent::Socket::tcp_connect> for details. |
|
|
218 | |
|
|
219 | =item tcp_connect => $callback->($host, $service, $connect_cb, $prepare_cb) |
|
|
220 | |
|
|
221 | In even rarer cases you want total control over how AnyEvent::HTTP |
|
|
222 | establishes connections. Normally it uses L<AnyEvent::Socket::tcp_connect> |
|
|
223 | to do this, but you can provide your own C<tcp_connect> function - |
|
|
224 | obviously, it has to follow the same calling conventions, except that it |
|
|
225 | may always return a connection guard object. |
|
|
226 | |
|
|
227 | There are probably lots of weird uses for this function, starting from |
|
|
228 | tracing the hosts C<http_request> actually tries to connect, to (inexact |
|
|
229 | but fast) host => IP address caching or even socks protocol support. |
216 | |
230 | |
217 | =item on_header => $callback->($headers) |
231 | =item on_header => $callback->($headers) |
218 | |
232 | |
219 | When specified, this callback will be called with the header hash as soon |
233 | When specified, this callback will be called with the header hash as soon |
220 | as headers have been successfully received from the remote server (not on |
234 | as headers have been successfully received from the remote server (not on |
… | |
… | |
366 | my @pseudo = (URL => $url); |
380 | my @pseudo = (URL => $url); |
367 | push @pseudo, Redirect => delete $arg{Redirect} if exists $arg{Redirect}; |
381 | push @pseudo, Redirect => delete $arg{Redirect} if exists $arg{Redirect}; |
368 | |
382 | |
369 | my $recurse = exists $arg{recurse} ? delete $arg{recurse} : $MAX_RECURSE; |
383 | my $recurse = exists $arg{recurse} ? delete $arg{recurse} : $MAX_RECURSE; |
370 | |
384 | |
371 | return $cb->(undef, { Status => 599, Reason => "Too many redirections", @pseudo }) |
385 | return $cb->(undef, { @pseudo, Status => 599, Reason => "Too many redirections" }) |
372 | if $recurse < 0; |
386 | if $recurse < 0; |
373 | |
387 | |
374 | my $proxy = $arg{proxy} || $PROXY; |
388 | my $proxy = $arg{proxy} || $PROXY; |
375 | my $timeout = $arg{timeout} || $TIMEOUT; |
389 | my $timeout = $arg{timeout} || $TIMEOUT; |
376 | |
390 | |
… | |
… | |
379 | |
393 | |
380 | $uscheme = lc $uscheme; |
394 | $uscheme = lc $uscheme; |
381 | |
395 | |
382 | my $uport = $uscheme eq "http" ? 80 |
396 | my $uport = $uscheme eq "http" ? 80 |
383 | : $uscheme eq "https" ? 443 |
397 | : $uscheme eq "https" ? 443 |
384 | : return $cb->(undef, { Status => 599, Reason => "Only http and https URL schemes supported", @pseudo }); |
398 | : return $cb->(undef, { @pseudo, Status => 599, Reason => "Only http and https URL schemes supported" }); |
385 | |
399 | |
386 | $uauthority =~ /^(?: .*\@ )? ([^\@:]+) (?: : (\d+) )?$/x |
400 | $uauthority =~ /^(?: .*\@ )? ([^\@:]+) (?: : (\d+) )?$/x |
387 | or return $cb->(undef, { Status => 599, Reason => "Unparsable URL", @pseudo }); |
401 | or return $cb->(undef, { @pseudo, Status => 599, Reason => "Unparsable URL" }); |
388 | |
402 | |
389 | my $uhost = $1; |
403 | my $uhost = $1; |
390 | $uport = $2 if defined $2; |
404 | $uport = $2 if defined $2; |
391 | |
405 | |
392 | $hdr{host} = defined $2 ? "$uhost:$2" : "$uhost" |
406 | $hdr{host} = defined $2 ? "$uhost:$2" : "$uhost" |
… | |
… | |
454 | _get_slot $uhost, sub { |
468 | _get_slot $uhost, sub { |
455 | $state{slot_guard} = shift; |
469 | $state{slot_guard} = shift; |
456 | |
470 | |
457 | return unless $state{connect_guard}; |
471 | return unless $state{connect_guard}; |
458 | |
472 | |
459 | $state{connect_guard} = AnyEvent::Socket::tcp_connect $rhost, $rport, sub { |
473 | my $connect_cb = sub { |
460 | $state{fh} = shift |
474 | $state{fh} = shift |
461 | or do { |
475 | or do { |
462 | my $err = "$!"; |
476 | my $err = "$!"; |
463 | %state = (); |
477 | %state = (); |
464 | return $cb->(undef, { Status => 599, Reason => $err, @pseudo }); |
478 | return $cb->(undef, { @pseudo, Status => 599, Reason => $err }); |
465 | }; |
479 | }; |
466 | |
480 | |
467 | pop; # free memory, save a tree |
481 | pop; # free memory, save a tree |
468 | |
482 | |
469 | return unless delete $state{connect_guard}; |
483 | return unless delete $state{connect_guard}; |
… | |
… | |
475 | tls_ctx => $arg{tls_ctx}, |
489 | tls_ctx => $arg{tls_ctx}, |
476 | # these need to be reconfigured on keepalive handles |
490 | # these need to be reconfigured on keepalive handles |
477 | timeout => $timeout, |
491 | timeout => $timeout, |
478 | on_error => sub { |
492 | on_error => sub { |
479 | %state = (); |
493 | %state = (); |
480 | $cb->(undef, { Status => 599, Reason => $_[2], @pseudo }); |
494 | $cb->(undef, { @pseudo, Status => 599, Reason => $_[2] }); |
481 | }, |
495 | }, |
482 | on_eof => sub { |
496 | on_eof => sub { |
483 | %state = (); |
497 | %state = (); |
484 | $cb->(undef, { Status => 599, Reason => "Unexpected end-of-file", @pseudo }); |
498 | $cb->(undef, { @pseudo, Status => 599, Reason => "Unexpected end-of-file" }); |
485 | }, |
499 | }, |
486 | ; |
500 | ; |
487 | |
501 | |
488 | # limit the number of persistent connections |
502 | # limit the number of persistent connections |
489 | # keepalive not yet supported |
503 | # keepalive not yet supported |
… | |
… | |
516 | |
530 | |
517 | %hdr = (); # reduce memory usage, save a kitten, also make it possible to re-use |
531 | %hdr = (); # reduce memory usage, save a kitten, also make it possible to re-use |
518 | |
532 | |
519 | # status line and headers |
533 | # status line and headers |
520 | $state{handle}->push_read (line => $qr_nlnl, sub { |
534 | $state{handle}->push_read (line => $qr_nlnl, sub { |
|
|
535 | my $keepalive = pop; |
|
|
536 | |
521 | for ("$_[1]") { |
537 | for ("$_[1]") { |
522 | y/\015//d; # weed out any \015, as they show up in the weirdest of places. |
538 | y/\015//d; # weed out any \015, as they show up in the weirdest of places. |
523 | |
539 | |
524 | /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) (?: \s+ ([^\015\012]*) )? \015?\012/igxc |
540 | /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) (?: \s+ ([^\015\012]*) )? \015?\012/igxc |
525 | or return (%state = (), $cb->(undef, { Status => 599, Reason => "Invalid server response", @pseudo })); |
541 | or return (%state = (), $cb->(undef, { @pseudo, Status => 599, Reason => "Invalid server response" })); |
526 | |
542 | |
527 | push @pseudo, |
543 | push @pseudo, |
528 | HTTPVersion => $1, |
544 | HTTPVersion => $1, |
529 | Status => $2, |
545 | Status => $2, |
530 | Reason => $3, |
546 | Reason => $3, |
… | |
… | |
540 | ((?: [^\012]+ | \012[\011\040] )*) |
556 | ((?: [^\012]+ | \012[\011\040] )*) |
541 | \012 |
557 | \012 |
542 | /gxc; |
558 | /gxc; |
543 | |
559 | |
544 | /\G$/ |
560 | /\G$/ |
545 | or return (%state = (), $cb->(undef, { Status => 599, Reason => "Garbled response headers", @pseudo })); |
561 | or return (%state = (), $cb->(undef, { @pseudo, Status => 599, Reason => "Garbled response headers" })); |
546 | } |
562 | } |
547 | |
563 | |
548 | # remove the "," prefix we added to all headers above |
564 | # remove the "," prefix we added to all headers above |
549 | substr $_, 0, 1, "" |
565 | substr $_, 0, 1, "" |
550 | for values %hdr; |
566 | for values %hdr; |
… | |
… | |
585 | } elsif ($status == 307) { |
601 | } elsif ($status == 307) { |
586 | $redirect = 1; |
602 | $redirect = 1; |
587 | } |
603 | } |
588 | } |
604 | } |
589 | |
605 | |
590 | my $finish = sub { |
606 | my $finish = sub { # ($data, $err_status, $err_reason[, $keepalive]) |
591 | $state{handle}->destroy if $state{handle}; |
607 | $state{handle}->destroy if $state{handle}; |
592 | %state = (); |
608 | %state = (); |
593 | |
609 | |
|
|
610 | if (defined $_[1]) { |
|
|
611 | $hdr{OrigStatus} = $hdr{Status}; $hdr{Status} = $_[1]; |
|
|
612 | $hdr{OrigReason} = $hdr{Reason}; $hdr{Reason} = $_[2]; |
|
|
613 | } |
|
|
614 | |
594 | # set-cookie processing |
615 | # set-cookie processing |
595 | if ($arg{cookie_jar}) { |
616 | if ($arg{cookie_jar}) { |
596 | for ($_[1]{"set-cookie"}) { |
617 | for ($hdr{"set-cookie"}) { |
597 | # parse NAME=VALUE |
618 | # parse NAME=VALUE |
598 | my @kv; |
619 | my @kv; |
599 | |
620 | |
600 | while (/\G\s* ([^=;,[:space:]]+) \s*=\s* (?: "((?:[^\\"]+|\\.)*)" | ([^=;,[:space:]]*) )/gcxs) { |
621 | while (/\G\s* ([^=;,[:space:]]+) \s*=\s* (?: "((?:[^\\"]+|\\.)*)" | ([^=;,[:space:]]*) )/gcxs) { |
601 | my $name = $1; |
622 | my $name = $1; |
… | |
… | |
647 | # we also access %hdr, as $_[1] might be an erro |
668 | # we also access %hdr, as $_[1] might be an erro |
648 | http_request ( |
669 | http_request ( |
649 | $method => $hdr{location}, |
670 | $method => $hdr{location}, |
650 | %arg, |
671 | %arg, |
651 | recurse => $recurse - 1, |
672 | recurse => $recurse - 1, |
652 | Redirect => \@_, |
673 | Redirect => [$_[0], \%hdr], |
653 | $cb); |
674 | $cb); |
654 | } else { |
675 | } else { |
655 | $cb->($_[0], $_[1]); |
676 | $cb->($_[0], \%hdr); |
656 | } |
677 | } |
657 | }; |
678 | }; |
658 | |
679 | |
659 | my $len = $hdr{"content-length"}; |
680 | my $len = $hdr{"content-length"}; |
660 | |
681 | |
661 | if (!$redirect && $arg{on_header} && !$arg{on_header}(\%hdr)) { |
682 | if (!$redirect && $arg{on_header} && !$arg{on_header}(\%hdr)) { |
662 | $finish->(undef, { Status => 598, Reason => "Request cancelled by on_header", @pseudo }); |
683 | $finish->(undef, 598 => "Request cancelled by on_header"); |
663 | } elsif ( |
684 | } elsif ( |
664 | $hdr{Status} =~ /^(?:1..|[23]04)$/ |
685 | $hdr{Status} =~ /^(?:1..|204|205|304)$/ |
665 | or $method eq "HEAD" |
686 | or $method eq "HEAD" |
666 | or (defined $len && !$len) |
687 | or (defined $len && !$len) |
667 | ) { |
688 | ) { |
668 | # no body |
689 | # no body |
669 | $finish->("", \%hdr); |
690 | $finish->("", undef, undef, 1); |
670 | } else { |
691 | } else { |
671 | # body handling, four different code paths |
692 | # body handling, four different code paths |
672 | # for want_body_handle, on_body (2x), normal (2x) |
693 | # for want_body_handle, on_body (2x), normal (2x) |
673 | # we might read too much here, but it does not matter yet (no pers. connections) |
694 | # we might read too much here, but it does not matter yet (no pipelining) |
674 | if (!$redirect && $arg{want_body_handle}) { |
695 | if (!$redirect && $arg{want_body_handle}) { |
675 | $_[0]->on_eof (undef); |
696 | $_[0]->on_eof (undef); |
676 | $_[0]->on_error (undef); |
697 | $_[0]->on_error (undef); |
677 | $_[0]->on_read (undef); |
698 | $_[0]->on_read (undef); |
678 | |
699 | |
679 | $finish->(delete $state{handle}, \%hdr); |
700 | $finish->(delete $state{handle}); |
680 | |
701 | |
681 | } elsif ($arg{on_body}) { |
702 | } elsif ($arg{on_body}) { |
682 | $_[0]->on_error (sub { $finish->(undef, { Status => 599, Reason => $_[2], @pseudo }) }); |
703 | $_[0]->on_error (sub { $finish->(undef, 599 => $_[2]) }); |
683 | if ($len) { |
704 | if ($len) { |
684 | $_[0]->on_eof (undef); |
|
|
685 | $_[0]->on_read (sub { |
705 | $_[0]->on_read (sub { |
686 | $len -= length $_[0]{rbuf}; |
706 | $len -= length $_[0]{rbuf}; |
687 | |
707 | |
688 | $arg{on_body}(delete $_[0]{rbuf}, \%hdr) |
708 | $arg{on_body}(delete $_[0]{rbuf}, \%hdr) |
689 | or $finish->(undef, { Status => 598, Reason => "Request cancelled by on_body", @pseudo }); |
709 | or $finish->(undef, 598 => "Request cancelled by on_body"); |
690 | |
710 | |
691 | $len > 0 |
711 | $len > 0 |
692 | or $finish->("", \%hdr); |
712 | or $finish->("", undef, undef, 1); |
693 | }); |
713 | }); |
694 | } else { |
714 | } else { |
695 | $_[0]->on_eof (sub { |
715 | $_[0]->on_eof (sub { |
696 | $finish->("", \%hdr); |
716 | $finish->(""); |
697 | }); |
717 | }); |
698 | $_[0]->on_read (sub { |
718 | $_[0]->on_read (sub { |
699 | $arg{on_body}(delete $_[0]{rbuf}, \%hdr) |
719 | $arg{on_body}(delete $_[0]{rbuf}, \%hdr) |
700 | or $finish->(undef, { Status => 598, Reason => "Request cancelled by on_body", @pseudo }); |
720 | or $finish->(undef, 598 => "Request cancelled by on_body"); |
701 | }); |
721 | }); |
702 | } |
722 | } |
703 | } else { |
723 | } else { |
704 | $_[0]->on_eof (undef); |
724 | $_[0]->on_eof (undef); |
705 | |
725 | |
706 | if ($len) { |
726 | if ($len) { |
707 | $_[0]->on_error (sub { $finish->(undef, { Status => 599, Reason => $_[2], @pseudo }) }); |
727 | $_[0]->on_error (sub { $finish->(undef, 599 => $_[2]) }); |
708 | $_[0]->on_read (sub { |
728 | $_[0]->on_read (sub { |
709 | $finish->((substr delete $_[0]{rbuf}, 0, $len, ""), \%hdr) |
729 | $finish->((substr delete $_[0]{rbuf}, 0, $len, ""), undef, undef, 1) |
710 | if $len <= length $_[0]{rbuf}; |
730 | if $len <= length $_[0]{rbuf}; |
711 | }); |
731 | }); |
712 | } else { |
732 | } else { |
713 | $_[0]->on_error (sub { |
733 | $_[0]->on_error (sub { |
714 | ($! == Errno::EPIPE || !$!) |
734 | ($! == Errno::EPIPE || !$!) |
715 | ? $finish->(delete $_[0]{rbuf}, \%hdr) |
735 | ? $finish->(delete $_[0]{rbuf}) |
716 | : $finish->(undef, { Status => 599, Reason => $_[2], @pseudo }); |
736 | : $finish->(undef, 599 => $_[2]); |
717 | }); |
737 | }); |
718 | $_[0]->on_read (sub { }); |
738 | $_[0]->on_read (sub { }); |
719 | } |
739 | } |
720 | } |
740 | } |
721 | } |
741 | } |
… | |
… | |
728 | |
748 | |
729 | # maybe re-use $uauthority with patched port? |
749 | # maybe re-use $uauthority with patched port? |
730 | $state{handle}->push_write ("CONNECT $uhost:$uport HTTP/1.0\015\012Host: $uhost\015\012\015\012"); |
750 | $state{handle}->push_write ("CONNECT $uhost:$uport HTTP/1.0\015\012Host: $uhost\015\012\015\012"); |
731 | $state{handle}->push_read (line => $qr_nlnl, sub { |
751 | $state{handle}->push_read (line => $qr_nlnl, sub { |
732 | $_[1] =~ /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) (?: \s+ ([^\015\012]*) )?/ix |
752 | $_[1] =~ /^HTTP\/([0-9\.]+) \s+ ([0-9]{3}) (?: \s+ ([^\015\012]*) )?/ix |
733 | or return (%state = (), $cb->(undef, { Status => 599, Reason => "Invalid proxy connect response ($_[1])", @pseudo })); |
753 | or return (%state = (), $cb->(undef, { @pseudo, Status => 599, Reason => "Invalid proxy connect response ($_[1])" })); |
734 | |
754 | |
735 | if ($2 == 200) { |
755 | if ($2 == 200) { |
736 | $rpath = $upath; |
756 | $rpath = $upath; |
737 | &$handle_actual_request; |
757 | &$handle_actual_request; |
738 | } else { |
758 | } else { |
739 | %state = (); |
759 | %state = (); |
740 | $cb->(undef, { Status => $2, Reason => $3, @pseudo }); |
760 | $cb->(undef, { @pseudo, Status => $2, Reason => $3 }); |
741 | } |
761 | } |
742 | }); |
762 | }); |
743 | } else { |
763 | } else { |
744 | &$handle_actual_request; |
764 | &$handle_actual_request; |
745 | } |
765 | } |
|
|
766 | }; |
746 | |
767 | |
747 | }, $arg{on_prepare} || sub { $timeout }; |
768 | my $tcp_connect = $arg{tcp_connect} |
|
|
769 | || do { require AnyEvent::Socket; \&AnyEvent::Socket::tcp_connect }; |
|
|
770 | |
|
|
771 | $state{connect_guard} = $tcp_connect->($rhost, $rport, $connect_cb, $arg{on_prepare} || sub { $timeout }); |
|
|
772 | |
748 | }; |
773 | }; |
749 | |
774 | |
750 | defined wantarray && AnyEvent::Util::guard { %state = () } |
775 | defined wantarray && AnyEvent::Util::guard { %state = () } |
751 | } |
776 | } |
752 | |
777 | |
… | |
… | |
787 | string of the form C<http://host:port> (optionally C<https:...>), croaks |
812 | string of the form C<http://host:port> (optionally C<https:...>), croaks |
788 | otherwise. |
813 | otherwise. |
789 | |
814 | |
790 | To clear an already-set proxy, use C<undef>. |
815 | To clear an already-set proxy, use C<undef>. |
791 | |
816 | |
|
|
817 | =item $date = AnyEvent::HTTP::format_date $timestamp |
|
|
818 | |
|
|
819 | Takes a POSIX timestamp (seconds since the epoch) and formats it as a HTTP |
|
|
820 | Date (RFC 2616). |
|
|
821 | |
|
|
822 | =item $timestamp = AnyEvent::HTTP::parse_date $date |
|
|
823 | |
|
|
824 | Takes a HTTP Date (RFC 2616) and returns the corresponding POSIX |
|
|
825 | timestamp, or C<undef> if the date cannot be parsed. |
|
|
826 | |
792 | =item $AnyEvent::HTTP::MAX_RECURSE |
827 | =item $AnyEvent::HTTP::MAX_RECURSE |
793 | |
828 | |
794 | The default value for the C<recurse> request parameter (default: C<10>). |
829 | The default value for the C<recurse> request parameter (default: C<10>). |
795 | |
830 | |
796 | =item $AnyEvent::HTTP::USERAGENT |
831 | =item $AnyEvent::HTTP::USERAGENT |
… | |
… | |
814 | connections. This number of can be useful for load-leveling. |
849 | connections. This number of can be useful for load-leveling. |
815 | |
850 | |
816 | =back |
851 | =back |
817 | |
852 | |
818 | =cut |
853 | =cut |
|
|
854 | |
|
|
855 | our @month = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); |
|
|
856 | our @weekday = qw(Sun Mon Tue Wed Thu Fri Sat); |
|
|
857 | |
|
|
858 | sub format_date($) { |
|
|
859 | my ($time) = @_; |
|
|
860 | |
|
|
861 | # RFC 822/1123 format |
|
|
862 | my ($S, $M, $H, $mday, $mon, $year, $wday, $yday, undef) = gmtime $time; |
|
|
863 | |
|
|
864 | sprintf "%s, %02d %s %04d %02d:%02d:%02d GMT", |
|
|
865 | $weekday[$wday], $mday, $month[$mon], $year + 1900, |
|
|
866 | $H, $M, $S; |
|
|
867 | } |
|
|
868 | |
|
|
869 | sub parse_date($) { |
|
|
870 | my ($date) = @_; |
|
|
871 | |
|
|
872 | my ($d, $m, $y, $H, $M, $S); |
|
|
873 | |
|
|
874 | 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$/) { |
|
|
875 | # RFC 822/1123, required by RFC 2616 |
|
|
876 | ($d, $m, $y, $H, $M, $S) = ($1, $2, $3, $4, $5, $6); |
|
|
877 | |
|
|
878 | } 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$/) { |
|
|
879 | # RFC 850 |
|
|
880 | ($d, $m, $y, $H, $M, $S) = ($1, $2, $3 < 69 ? $3 + 2000 : $3 + 1900, $4, $5, $6); |
|
|
881 | |
|
|
882 | } 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])$/) { |
|
|
883 | # ISO C's asctime |
|
|
884 | ($d, $m, $y, $H, $M, $S) = ($2, $1, $6, $3, $4, $5); |
|
|
885 | } |
|
|
886 | # other formats fail in the loop below |
|
|
887 | |
|
|
888 | for (0..11) { |
|
|
889 | if ($m eq $month[$_]) { |
|
|
890 | require Time::Local; |
|
|
891 | return Time::Local::timegm ($S, $M, $H, $d, $_, $y); |
|
|
892 | } |
|
|
893 | } |
|
|
894 | |
|
|
895 | undef |
|
|
896 | } |
819 | |
897 | |
820 | sub set_proxy($) { |
898 | sub set_proxy($) { |
821 | if (length $_[0]) { |
899 | if (length $_[0]) { |
822 | $_[0] =~ m%^(https?):// ([^:/]+) (?: : (\d*) )?%ix |
900 | $_[0] =~ m%^(https?):// ([^:/]+) (?: : (\d*) )?%ix |
823 | or Carp::croak "$_[0]: invalid proxy URL"; |
901 | or Carp::croak "$_[0]: invalid proxy URL"; |
… | |
… | |
830 | # initialise proxy from environment |
908 | # initialise proxy from environment |
831 | eval { |
909 | eval { |
832 | set_proxy $ENV{http_proxy}; |
910 | set_proxy $ENV{http_proxy}; |
833 | }; |
911 | }; |
834 | |
912 | |
|
|
913 | =head2 SOCKS PROXIES |
|
|
914 | |
|
|
915 | Socks proxies are not directly supported by AnyEvent::HTTP. You can |
|
|
916 | compile your perl to support socks, or use an external program such as |
|
|
917 | F<socksify> (dante) or F<tsocks> to make your program use a socks proxy |
|
|
918 | transparently. |
|
|
919 | |
|
|
920 | Alternatively, for AnyEvent::HTTP only, you can use your own |
|
|
921 | C<tcp_connect> function that does the proxy handshake - here is an example |
|
|
922 | that works with socks4a proxies: |
|
|
923 | |
|
|
924 | use Errno; |
|
|
925 | use AnyEvent::Util; |
|
|
926 | use AnyEvent::Socket; |
|
|
927 | use AnyEvent::Handle; |
|
|
928 | |
|
|
929 | # host, port and username of/for your socks4a proxy |
|
|
930 | my $socks_host = "10.0.0.23"; |
|
|
931 | my $socks_port = 9050; |
|
|
932 | my $socks_user = ""; |
|
|
933 | |
|
|
934 | sub socks4a_connect { |
|
|
935 | my ($host, $port, $connect_cb, $prepare_cb) = @_; |
|
|
936 | |
|
|
937 | my $hdl = new AnyEvent::Handle |
|
|
938 | connect => [$socks_host, $socks_port], |
|
|
939 | on_prepare => sub { $prepare_cb->($_[0]{fh}) }, |
|
|
940 | on_error => sub { $connect_cb->() }, |
|
|
941 | ; |
|
|
942 | |
|
|
943 | $hdl->push_write (pack "CCnNZ*Z*", 4, 1, $port, 1, $socks_user, $host); |
|
|
944 | |
|
|
945 | $hdl->push_read (chunk => 8, sub { |
|
|
946 | my ($hdl, $chunk) = @_; |
|
|
947 | my ($status, $port, $ipn) = unpack "xCna4", $chunk; |
|
|
948 | |
|
|
949 | if ($status == 0x5a) { |
|
|
950 | $connect_cb->($hdl->{fh}, (format_address $ipn) . ":$port"); |
|
|
951 | } else { |
|
|
952 | $! = Errno::ENXIO; $connect_cb->(); |
|
|
953 | } |
|
|
954 | }); |
|
|
955 | |
|
|
956 | $hdl |
|
|
957 | } |
|
|
958 | |
|
|
959 | Use C<socks4a_connect> instead of C<tcp_connect> when doing C<http_request>s, |
|
|
960 | possibly after switching off other proxy types: |
|
|
961 | |
|
|
962 | AnyEvent::HTTP::set_proxy undef; # usually you do not want other proxies |
|
|
963 | |
|
|
964 | http_get 'http://www.google.com', tcp_connect => \&socks4a_connect, sub { |
|
|
965 | my ($data, $headers) = @_; |
|
|
966 | ... |
|
|
967 | }; |
|
|
968 | |
835 | =head1 SEE ALSO |
969 | =head1 SEE ALSO |
836 | |
970 | |
837 | L<AnyEvent>. |
971 | L<AnyEvent>. |
838 | |
972 | |
839 | =head1 AUTHOR |
973 | =head1 AUTHOR |