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.83 by root, Sun Jan 2 05:02:28 2011 UTC vs.
Revision 1.85 by root, Sun Jan 2 05:31:56 2011 UTC

36 36
37=cut 37=cut
38 38
39package AnyEvent::HTTP; 39package AnyEvent::HTTP;
40 40
41use strict; 41use common::sense;
42no warnings;
43 42
44use Errno (); 43use Errno ();
45 44
46use AnyEvent 5.0 (); 45use AnyEvent 5.0 ();
47use AnyEvent::Util (); 46use AnyEvent::Util ();
778 777
779 $ae_error = 597; # body phase 778 $ae_error = 597; # body phase
780 779
781 my $len = $hdr{"content-length"}; 780 my $len = $hdr{"content-length"};
782 781
782 # body handling, many different code paths
783 # - no body expected
784 # - want_body_handle
785 # - te chunked
786 # - 2x length known (with or without on_body)
787 # - 2x length not known (with or without on_body)
783 if (!$redirect && $arg{on_header} && !$arg{on_header}(\%hdr)) { 788 if (!$redirect && $arg{on_header} && !$arg{on_header}(\%hdr)) {
784 $finish->(undef, 598 => "Request cancelled by on_header"); 789 $finish->(undef, 598 => "Request cancelled by on_header");
785 } elsif ( 790 } elsif (
786 $hdr{Status} =~ /^(?:1..|204|205|304)$/ 791 $hdr{Status} =~ /^(?:1..|204|205|304)$/
787 or $method eq "HEAD" 792 or $method eq "HEAD"
788 or (defined $len && !$len) 793 or (defined $len && $len == 0) # == 0, not !, because "0 " is true
789 ) { 794 ) {
790 # no body 795 # no body
791 $finish->("", undef, undef, 1); 796 $finish->("", undef, undef, 1);
792 } else { 797
793 # body handling, many different code paths
794 # - no body expected
795 # - want_body_handle
796 # - te chunked
797 # - 2x length known (with or without on_body)
798 # - 2x length not known (with or without on_body)
799 if (!$redirect && $arg{want_body_handle}) { 798 } elsif (!$redirect && $arg{want_body_handle}) {
800 $_[0]->on_eof (undef); 799 $_[0]->on_eof (undef);
801 $_[0]->on_error (undef); 800 $_[0]->on_error (undef);
802 $_[0]->on_read (undef); 801 $_[0]->on_read (undef);
803 802
804 $finish->(delete $state{handle}); 803 $finish->(delete $state{handle});
805 804
806 } elsif ($hdr{"transfer-encoding"} =~ /\bchunked\b/i) { 805 } elsif ($hdr{"transfer-encoding"} =~ /\bchunked\b/i) {
807 my $cl = 0; 806 my $cl = 0;
808 my $body = undef; 807 my $body = undef;
809 my $on_body = $arg{on_body} || sub { $body .= shift; 1 }; 808 my $on_body = $arg{on_body} || sub { $body .= shift; 1 };
810 809
811 $state{read_chunk} = sub { 810 $state{read_chunk} = sub {
812 $_[1] =~ /^([0-9a-fA-F]+)/ 811 $_[1] =~ /^([0-9a-fA-F]+)/
813 or $finish->(undef, $ae_error => "Garbled chunked transfer encoding"); 812 or $finish->(undef, $ae_error => "Garbled chunked transfer encoding");
814 813
815 my $len = hex $1; 814 my $len = hex $1;
816 815
817 if ($len) { 816 if ($len) {
818 $cl += $len; 817 $cl += $len;
819 818
820 $_[0]->push_read (chunk => $len, sub { 819 $_[0]->push_read (chunk => $len, sub {
821 $on_body->($_[1], \%hdr) 820 $on_body->($_[1], \%hdr)
822 or return $finish->(undef, 598 => "Request cancelled by on_body"); 821 or return $finish->(undef, 598 => "Request cancelled by on_body");
823 822
824 $_[0]->push_read (line => sub { 823 $_[0]->push_read (line => sub {
825 length $_[1] 824 length $_[1]
826 and return $finish->(undef, $ae_error => "Garbled chunked transfer encoding"); 825 and return $finish->(undef, $ae_error => "Garbled chunked transfer encoding");
827 $_[0]->push_read (line => $state{read_chunk}); 826 $_[0]->push_read (line => $state{read_chunk});
828 });
829 }); 827 });
830 } else {
831 $hdr{"content-length"} ||= $cl;
832
833 $_[0]->push_read (line => $qr_nlnl, sub {
834 if (length $_[1]) {
835 for ("$_[1]") {
836 y/\015//d; # weed out any \015, as they show up in the weirdest of places.
837
838 my $hdr = parse_hdr
839 or return $finish->(undef, $ae_error => "Garbled response trailers");
840
841 %hdr = (%hdr, %$hdr);
842 }
843 }
844
845 $finish->($body, undef, undef, 1);
846 });
847 }
848 };
849
850 $_[0]->push_read (line => $state{read_chunk});
851
852 } elsif ($arg{on_body}) {
853 if ($len) {
854 $_[0]->on_read (sub {
855 $len -= length $_[0]{rbuf};
856
857 $arg{on_body}(delete $_[0]{rbuf}, \%hdr)
858 or return $finish->(undef, 598 => "Request cancelled by on_body");
859
860 $len > 0
861 or $finish->("", undef, undef, 1);
862 }); 828 });
863 } else { 829 } else {
864 $_[0]->on_eof (sub { 830 $hdr{"content-length"} ||= $cl;
865 $finish->(""); 831
832 $_[0]->push_read (line => $qr_nlnl, sub {
833 if (length $_[1]) {
834 for ("$_[1]") {
835 y/\015//d; # weed out any \015, as they show up in the weirdest of places.
836
837 my $hdr = parse_hdr
838 or return $finish->(undef, $ae_error => "Garbled response trailers");
839
840 %hdr = (%hdr, %$hdr);
841 }
866 }); 842 }
867 $_[0]->on_read (sub { 843
868 $arg{on_body}(delete $_[0]{rbuf}, \%hdr) 844 $finish->($body, undef, undef, 1);
869 or $finish->(undef, 598 => "Request cancelled by on_body");
870 }); 845 });
871 } 846 }
847 };
848
849 $_[0]->push_read (line => $state{read_chunk});
850
851 } elsif ($arg{on_body}) {
852 if (defined $len) {
853 $_[0]->on_read (sub {
854 $len -= length $_[0]{rbuf};
855
856 $arg{on_body}(delete $_[0]{rbuf}, \%hdr)
857 or return $finish->(undef, 598 => "Request cancelled by on_body");
858
859 $len > 0
860 or $finish->("", undef, undef, 1);
861 });
872 } else { 862 } else {
873 $_[0]->on_eof (undef); 863 $_[0]->on_eof (sub {
874 864 $finish->("");
875 if ($len) { 865 });
876 $_[0]->on_read (sub { 866 $_[0]->on_read (sub {
867 $arg{on_body}(delete $_[0]{rbuf}, \%hdr)
868 or $finish->(undef, 598 => "Request cancelled by on_body");
869 });
870 }
871 } else {
872 $_[0]->on_eof (undef);
873
874 if (defined $len) {
875 $_[0]->on_read (sub {
877 $finish->((substr delete $_[0]{rbuf}, 0, $len, ""), undef, undef, 1) 876 $finish->((substr delete $_[0]{rbuf}, 0, $len, ""), undef, undef, 1)
878 if $len <= length $_[0]{rbuf}; 877 if $len <= length $_[0]{rbuf};
879 }); 878 });
880 } else { 879 } else {
881 $_[0]->on_error (sub { 880 $_[0]->on_error (sub {
882 ($! == Errno::EPIPE || !$!) 881 ($! == Errno::EPIPE || !$!)
883 ? $finish->(delete $_[0]{rbuf}) 882 ? $finish->(delete $_[0]{rbuf})
884 : $finish->(undef, $ae_error => $_[2]); 883 : $finish->(undef, $ae_error => $_[2]);
885 }); 884 });
886 $_[0]->on_read (sub { }); 885 $_[0]->on_read (sub { });
887 }
888 } 886 }
889 } 887 }
890 }; 888 };
891 889
892 $state{handle}->push_read (line => $qr_nlnl, $state{read_response}); 890 $state{handle}->push_read (line => $qr_nlnl, $state{read_response});

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines