… | |
… | |
169 | C<Host:>, C<Content-Length:>, C<Connection:> and C<Cookie:> headers and |
169 | C<Host:>, C<Content-Length:>, C<Connection:> and C<Cookie:> headers and |
170 | will provide defaults at least for C<TE:>, C<Referer:> and C<User-Agent:> |
170 | will provide defaults at least for C<TE:>, C<Referer:> and C<User-Agent:> |
171 | (this can be suppressed by using C<undef> for these headers in which case |
171 | (this can be suppressed by using C<undef> for these headers in which case |
172 | they won't be sent at all). |
172 | they won't be sent at all). |
173 | |
173 | |
|
|
174 | You really should provide your own C<User-Agent:> header value that is |
|
|
175 | appropriate for your program - I wouldn't be surprised if the default |
|
|
176 | AnyEvent string gets blocked by webservers sooner or later. |
|
|
177 | |
174 | =item timeout => $seconds |
178 | =item timeout => $seconds |
175 | |
179 | |
176 | The time-out to use for various stages - each connect attempt will reset |
180 | The time-out to use for various stages - each connect attempt will reset |
177 | the timeout, as will read or write activity, i.e. this is not an overall |
181 | the timeout, as will read or write activity, i.e. this is not an overall |
178 | timeout. |
182 | timeout. |
… | |
… | |
329 | Example: do a HTTP HEAD request on https://www.google.com/, use a |
333 | Example: do a HTTP HEAD request on https://www.google.com/, use a |
330 | timeout of 30 seconds. |
334 | timeout of 30 seconds. |
331 | |
335 | |
332 | http_request |
336 | http_request |
333 | GET => "https://www.google.com", |
337 | GET => "https://www.google.com", |
|
|
338 | headers => { "user-agent" => "MySearchClient 1.0" }, |
334 | timeout => 30, |
339 | timeout => 30, |
335 | sub { |
340 | sub { |
336 | my ($body, $hdr) = @_; |
341 | my ($body, $hdr) = @_; |
337 | use Data::Dumper; |
342 | use Data::Dumper; |
338 | print Dumper $hdr; |
343 | print Dumper $hdr; |
… | |
… | |
649 | $hdr{"user-agent"} = $USERAGENT unless exists $hdr{"user-agent"}; |
654 | $hdr{"user-agent"} = $USERAGENT unless exists $hdr{"user-agent"}; |
650 | |
655 | |
651 | $hdr{"content-length"} = length $arg{body} |
656 | $hdr{"content-length"} = length $arg{body} |
652 | if length $arg{body} || $method ne "GET"; |
657 | if length $arg{body} || $method ne "GET"; |
653 | |
658 | |
|
|
659 | my $idempotent = $method =~ /^(?:GET|HEAD|PUT|DELETE|OPTIONS|TRACE)$/; |
|
|
660 | |
|
|
661 | # default value for keepalive is true iff the request is for an idempotent method |
|
|
662 | my $keepalive = exists $arg{keepalive} |
|
|
663 | ? $arg{keepalive}*1 |
|
|
664 | : $idempotent ? $PERSISTENT_TIMEOUT : 0; |
|
|
665 | |
654 | $hdr{connection} = "close Te"; #1.1 |
666 | $hdr{connection} = ($keepalive ? "" : "close ") . "Te"; #1.1 |
655 | $hdr{te} = "trailers" unless exists $hdr{te}; #1.1 |
667 | $hdr{te} = "trailers" unless exists $hdr{te}; #1.1 |
656 | |
668 | |
657 | my %state = (connect_guard => 1); |
669 | my %state = (connect_guard => 1); |
658 | |
670 | |
659 | my $ae_error = 595; # connecting |
671 | my $ae_error = 595; # connecting |
… | |
… | |
673 | ); |
685 | ); |
674 | |
686 | |
675 | # return if error occured during push_write() |
687 | # return if error occured during push_write() |
676 | return unless %state; |
688 | return unless %state; |
677 | |
689 | |
678 | %hdr = (); # reduce memory usage, save a kitten, also make it possible to re-use |
690 | # reduce memory usage, save a kitten, also re-use it for the response headers. |
|
|
691 | %hdr = (); |
679 | |
692 | |
680 | # status line and headers |
693 | # status line and headers |
681 | $state{read_response} = sub { |
694 | $state{read_response} = sub { |
682 | for ("$_[1]") { |
695 | for ("$_[1]") { |
683 | y/\015//d; # weed out any \015, as they show up in the weirdest of places. |
696 | y/\015//d; # weed out any \015, as they show up in the weirdest of places. |
… | |
… | |
771 | } |
784 | } |
772 | }; |
785 | }; |
773 | |
786 | |
774 | $ae_error = 597; # body phase |
787 | $ae_error = 597; # body phase |
775 | |
788 | |
|
|
789 | my $chunked = $hdr{"transfer-encoding"} =~ /\bchunked\b/i; # not quite correct... |
|
|
790 | |
776 | my $len = $hdr{"content-length"}; |
791 | my $len = $chunked ? undef : $hdr{"content-length"}; |
777 | |
792 | |
778 | # body handling, many different code paths |
793 | # body handling, many different code paths |
779 | # - no body expected |
794 | # - no body expected |
780 | # - want_body_handle |
795 | # - want_body_handle |
781 | # - te chunked |
796 | # - te chunked |
… | |
… | |
796 | $_[0]->on_error (undef); |
811 | $_[0]->on_error (undef); |
797 | $_[0]->on_read (undef); |
812 | $_[0]->on_read (undef); |
798 | |
813 | |
799 | $finish->(delete $state{handle}); |
814 | $finish->(delete $state{handle}); |
800 | |
815 | |
801 | } elsif ($hdr{"transfer-encoding"} =~ /\bchunked\b/i) { |
816 | } elsif ($chunked) { |
802 | my $cl = 0; |
817 | my $cl = 0; |
803 | my $body = undef; |
818 | my $body = undef; |
804 | my $on_body = $arg{on_body} || sub { $body .= shift; 1 }; |
819 | my $on_body = $arg{on_body} || sub { $body .= shift; 1 }; |
805 | |
820 | |
806 | $state{read_chunk} = sub { |
821 | $state{read_chunk} = sub { |