… | |
… | |
41 | use strict; |
41 | use strict; |
42 | no warnings; |
42 | no warnings; |
43 | |
43 | |
44 | use Errno (); |
44 | use Errno (); |
45 | |
45 | |
46 | use AnyEvent 4.8 (); |
46 | use AnyEvent 5.0 (); |
47 | use AnyEvent::Util (); |
47 | use AnyEvent::Util (); |
48 | use AnyEvent::Socket (); |
48 | use AnyEvent::Socket (); |
49 | use AnyEvent::Handle (); |
49 | use AnyEvent::Handle (); |
50 | |
50 | |
51 | use base Exporter::; |
51 | use base Exporter::; |
52 | |
52 | |
53 | our $VERSION = '1.4'; |
53 | our $VERSION = '1.43'; |
54 | |
54 | |
55 | our @EXPORT = qw(http_get http_post http_head http_request); |
55 | our @EXPORT = qw(http_get http_post http_head http_request); |
56 | |
56 | |
57 | our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; |
57 | our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; |
58 | our $MAX_RECURSE = 10; |
58 | our $MAX_RECURSE = 10; |
… | |
… | |
138 | |
138 | |
139 | =item headers => hashref |
139 | =item headers => hashref |
140 | |
140 | |
141 | The request headers to use. Currently, C<http_request> may provide its |
141 | The request headers to use. Currently, C<http_request> may provide its |
142 | own C<Host:>, C<Content-Length:>, C<Connection:> and C<Cookie:> headers |
142 | own C<Host:>, C<Content-Length:>, C<Connection:> and C<Cookie:> headers |
143 | and will provide defaults for C<User-Agent:> and C<Referer:>. |
143 | and will provide defaults for C<User-Agent:> and C<Referer:> (this can be |
|
|
144 | suppressed by using C<undef> for these headers in which case they won't be |
|
|
145 | sent at all). |
144 | |
146 | |
145 | =item timeout => $seconds |
147 | =item timeout => $seconds |
146 | |
148 | |
147 | The time-out to use for various stages - each connect attempt will reset |
149 | The time-out to use for various stages - each connect attempt will reset |
148 | the timeout, as will read or write activity. Default timeout is 5 minutes. |
150 | the timeout, as will read or write activity, i.e. this is not an overall |
|
|
151 | timeout. |
|
|
152 | |
|
|
153 | Default timeout is 5 minutes. |
149 | |
154 | |
150 | =item proxy => [$host, $port[, $scheme]] or undef |
155 | =item proxy => [$host, $port[, $scheme]] or undef |
151 | |
156 | |
152 | Use the given http proxy for all requests. If not specified, then the |
157 | Use the given http proxy for all requests. If not specified, then the |
153 | default proxy (as specified by C<$ENV{http_proxy}>) is used. |
158 | default proxy (as specified by C<$ENV{http_proxy}>) is used. |
154 | |
159 | |
155 | C<$scheme> must be either missing or C<http> for HTTP, or C<https> for |
160 | C<$scheme> must be either missing, C<http> for HTTP or C<https> for |
156 | HTTPS. |
161 | HTTPS. |
157 | |
162 | |
158 | =item body => $string |
163 | =item body => $string |
159 | |
164 | |
160 | The request body, usually empty. Will be-sent as-is (future versions of |
165 | The request body, usually empty. Will be-sent as-is (future versions of |
… | |
… | |
186 | verification) TLS context. |
191 | verification) TLS context. |
187 | |
192 | |
188 | The default for this option is C<low>, which could be interpreted as "give |
193 | The default for this option is C<low>, which could be interpreted as "give |
189 | me the page, no matter what". |
194 | me the page, no matter what". |
190 | |
195 | |
|
|
196 | =item on_prepare => $callback->($fh) |
|
|
197 | |
|
|
198 | In rare cases you need to "tune" the socket before it is used to |
|
|
199 | connect (for exmaple, to bind it on a given IP address). This parameter |
|
|
200 | overrides the prepare callback passed to C<AnyEvent::Socket::tcp_connect> |
|
|
201 | and behaves exactly the same way (e.g. it has to provide a |
|
|
202 | timeout). See the description for the C<$prepare_cb> argument of |
|
|
203 | C<AnyEvent::Socket::tcp_connect> for details. |
|
|
204 | |
191 | =item on_header => $callback->($headers) |
205 | =item on_header => $callback->($headers) |
192 | |
206 | |
193 | When specified, this callback will be called with the header hash as soon |
207 | When specified, this callback will be called with the header hash as soon |
194 | as headers have been successfully received from the remote server (not on |
208 | as headers have been successfully received from the remote server (not on |
195 | locally-generated errors). |
209 | locally-generated errors). |
… | |
… | |
221 | This callback is useful when the data is too large to be held in memory |
235 | This callback is useful when the data is too large to be held in memory |
222 | (so the callback writes it to a file) or when only some information should |
236 | (so the callback writes it to a file) or when only some information should |
223 | be extracted, or when the body should be processed incrementally. |
237 | be extracted, or when the body should be processed incrementally. |
224 | |
238 | |
225 | It is usually preferred over doing your own body handling via |
239 | It is usually preferred over doing your own body handling via |
226 | C<want_body_handle>. |
240 | C<want_body_handle>, but in case of streaming APIs, where HTTP is |
|
|
241 | only used to create a connection, C<want_body_handle> is the better |
|
|
242 | alternative, as it allows you to install your own event handler, reducing |
|
|
243 | resource usage. |
227 | |
244 | |
228 | =item want_body_handle => $enable |
245 | =item want_body_handle => $enable |
229 | |
246 | |
230 | When enabled (default is disabled), the behaviour of AnyEvent::HTTP |
247 | When enabled (default is disabled), the behaviour of AnyEvent::HTTP |
231 | changes considerably: after parsing the headers, and instead of |
248 | changes considerably: after parsing the headers, and instead of |
… | |
… | |
243 | This is useful with some push-type services, where, after the initial |
260 | This is useful with some push-type services, where, after the initial |
244 | headers, an interactive protocol is used (typical example would be the |
261 | headers, an interactive protocol is used (typical example would be the |
245 | push-style twitter API which starts a JSON/XML stream). |
262 | push-style twitter API which starts a JSON/XML stream). |
246 | |
263 | |
247 | If you think you need this, first have a look at C<on_body>, to see if |
264 | If you think you need this, first have a look at C<on_body>, to see if |
248 | that doesn'T solve your problem in a better way. |
265 | that doesn't solve your problem in a better way. |
249 | |
266 | |
250 | =back |
267 | =back |
251 | |
268 | |
252 | Example: make a simple HTTP GET request for http://www.nethype.de/ |
269 | Example: make a simple HTTP GET request for http://www.nethype.de/ |
253 | |
270 | |
… | |
… | |
309 | push @{ $CO_SLOT{$_[0]}[1] }, $_[1]; |
326 | push @{ $CO_SLOT{$_[0]}[1] }, $_[1]; |
310 | |
327 | |
311 | _slot_schedule $_[0]; |
328 | _slot_schedule $_[0]; |
312 | } |
329 | } |
313 | |
330 | |
314 | our $qr_nl = qr<\015?\012>; |
331 | our $qr_nl = qr{\015?\012}; |
315 | our $qr_nlnl = qr<\015?\012\015?\012>; |
332 | our $qr_nlnl = qr{(?<![^\012])\015?\012}; |
316 | |
333 | |
317 | our $TLS_CTX_LOW = { cache => 1, sslv2 => 1 }; |
334 | our $TLS_CTX_LOW = { cache => 1, sslv2 => 1 }; |
318 | our $TLS_CTX_HIGH = { cache => 1, verify => 1, verify_peername => "https" }; |
335 | our $TLS_CTX_HIGH = { cache => 1, verify => 1, verify_peername => "https" }; |
319 | |
336 | |
320 | sub http_request($$@) { |
337 | sub http_request($$@) { |
… | |
… | |
398 | my ($rhost, $rport, $rscheme, $rpath); # request host, port, path |
415 | my ($rhost, $rport, $rscheme, $rpath); # request host, port, path |
399 | |
416 | |
400 | if ($proxy) { |
417 | if ($proxy) { |
401 | ($rpath, $rhost, $rport, $rscheme) = ($url, @$proxy); |
418 | ($rpath, $rhost, $rport, $rscheme) = ($url, @$proxy); |
402 | |
419 | |
|
|
420 | $rscheme = "http" unless defined $rscheme; |
|
|
421 | |
403 | # don't support https requests over https-proxy transport, |
422 | # don't support https requests over https-proxy transport, |
404 | # can't be done with tls as spec'ed, unless you double-encrypt. |
423 | # can't be done with tls as spec'ed, unless you double-encrypt. |
405 | $rscheme = "http" if $uscheme eq "https" && $rscheme eq "https"; |
424 | $rscheme = "http" if $uscheme eq "https" && $rscheme eq "https"; |
406 | } else { |
425 | } else { |
407 | ($rhost, $rport, $rscheme, $rpath) = ($uhost, $uport, $uscheme, $upath); |
426 | ($rhost, $rport, $rscheme, $rpath) = ($uhost, $uport, $uscheme, $upath); |
408 | } |
427 | } |
409 | |
428 | |
410 | $hdr{"user-agent"} ||= $USERAGENT; |
429 | # leave out fragment and query string, just a heuristic |
411 | $hdr{referer} ||= "$uscheme://$uauthority$upath"; # leave out fragment and query string, just a heuristic |
430 | $hdr{referer} ||= "$uscheme://$uauthority$upath" unless exists $hdr{referer}; |
|
|
431 | $hdr{"user-agent"} ||= $USERAGENT unless exists $hdr{"user-agent"}; |
412 | |
432 | |
413 | $hdr{"content-length"} = length $arg{body}; |
433 | $hdr{"content-length"} = length $arg{body}; |
414 | |
434 | |
415 | my %state = (connect_guard => 1); |
435 | my %state = (connect_guard => 1); |
416 | |
436 | |
… | |
… | |
467 | $state{handle}->starttls ("connect") if $uscheme eq "https" && !exists $state{handle}{tls}; |
487 | $state{handle}->starttls ("connect") if $uscheme eq "https" && !exists $state{handle}{tls}; |
468 | |
488 | |
469 | # send request |
489 | # send request |
470 | $state{handle}->push_write ( |
490 | $state{handle}->push_write ( |
471 | "$method $rpath HTTP/1.0\015\012" |
491 | "$method $rpath HTTP/1.0\015\012" |
472 | . (join "", map "\u$_: $hdr{$_}\015\012", keys %hdr) |
492 | . (join "", map "\u$_: $hdr{$_}\015\012", grep defined $hdr{$_}, keys %hdr) |
473 | . "\015\012" |
493 | . "\015\012" |
474 | . (delete $arg{body}) |
494 | . (delete $arg{body}) |
475 | ); |
495 | ); |
476 | |
496 | |
477 | %hdr = (); # reduce memory usage, save a kitten |
497 | %hdr = (); # reduce memory usage, save a kitten |
… | |
… | |
488 | URL => ",$url" |
508 | URL => ",$url" |
489 | ); |
509 | ); |
490 | |
510 | |
491 | # headers, could be optimized a bit |
511 | # headers, could be optimized a bit |
492 | $state{handle}->unshift_read (line => $qr_nlnl, sub { |
512 | $state{handle}->unshift_read (line => $qr_nlnl, sub { |
493 | for ("$_[1]\012") { |
513 | for ("$_[1]") { |
494 | y/\015//d; # weed out any \015, as they show up in the weirdest of places. |
514 | y/\015//d; # weed out any \015, as they show up in the weirdest of places. |
495 | |
515 | |
496 | # things seen, not parsed: |
516 | # things seen, not parsed: |
497 | # p3pP="NON CUR OTPi OUR NOR UNI" |
517 | # p3pP="NON CUR OTPi OUR NOR UNI" |
498 | |
518 | |
… | |
… | |
595 | |
615 | |
596 | redo if /\G\s*,/gc; |
616 | redo if /\G\s*,/gc; |
597 | } |
617 | } |
598 | } |
618 | } |
599 | |
619 | |
600 | if ($redirect) { |
620 | if ($redirect && exists $hdr{location}) { |
601 | # we ignore any errors, as it is very common to receive |
621 | # we ignore any errors, as it is very common to receive |
602 | # Content-Length != 0 but no actual body |
622 | # Content-Length != 0 but no actual body |
603 | # we also access %hdr, as $_[1] might be an erro |
623 | # we also access %hdr, as $_[1] might be an erro |
604 | http_request ($method => $hdr{location}, %arg, recurse => $recurse - 1, $cb); |
624 | http_request ($method => $hdr{location}, %arg, recurse => $recurse - 1, $cb); |
605 | } else { |
625 | } else { |
… | |
… | |
694 | }); |
714 | }); |
695 | } else { |
715 | } else { |
696 | &$handle_actual_request; |
716 | &$handle_actual_request; |
697 | } |
717 | } |
698 | |
718 | |
699 | }, sub { |
719 | }, $arg{on_prepare} || sub { $timeout }; |
700 | $timeout |
|
|
701 | }; |
|
|
702 | }; |
720 | }; |
703 | |
721 | |
704 | defined wantarray && AnyEvent::Util::guard { %state = () } |
722 | defined wantarray && AnyEvent::Util::guard { %state = () } |
705 | } |
723 | } |
706 | |
724 | |
… | |
… | |
727 | =over 4 |
745 | =over 4 |
728 | |
746 | |
729 | =item AnyEvent::HTTP::set_proxy "proxy-url" |
747 | =item AnyEvent::HTTP::set_proxy "proxy-url" |
730 | |
748 | |
731 | Sets the default proxy server to use. The proxy-url must begin with a |
749 | Sets the default proxy server to use. The proxy-url must begin with a |
732 | string of the form C<http://host:port> (optionally C<https:...>). |
750 | string of the form C<http://host:port> (optionally C<https:...>), croaks |
|
|
751 | otherwise. |
|
|
752 | |
|
|
753 | To clear an already-set proxy, use C<undef>. |
733 | |
754 | |
734 | =item $AnyEvent::HTTP::MAX_RECURSE |
755 | =item $AnyEvent::HTTP::MAX_RECURSE |
735 | |
756 | |
736 | The default value for the C<recurse> request parameter (default: C<10>). |
757 | The default value for the C<recurse> request parameter (default: C<10>). |
737 | |
758 | |
… | |
… | |
740 | The default value for the C<User-Agent> header (the default is |
761 | The default value for the C<User-Agent> header (the default is |
741 | C<Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)>). |
762 | C<Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)>). |
742 | |
763 | |
743 | =item $AnyEvent::HTTP::MAX_PER_HOST |
764 | =item $AnyEvent::HTTP::MAX_PER_HOST |
744 | |
765 | |
745 | The maximum number of concurrent conenctions to the same host (identified |
766 | The maximum number of concurrent connections to the same host (identified |
746 | by the hostname). If the limit is exceeded, then the additional requests |
767 | by the hostname). If the limit is exceeded, then the additional requests |
747 | are queued until previous connections are closed. |
768 | are queued until previous connections are closed. |
748 | |
769 | |
749 | The default value for this is C<4>, and it is highly advisable to not |
770 | The default value for this is C<4>, and it is highly advisable to not |
750 | increase it. |
771 | increase it. |
… | |
… | |
758 | =back |
779 | =back |
759 | |
780 | |
760 | =cut |
781 | =cut |
761 | |
782 | |
762 | sub set_proxy($) { |
783 | sub set_proxy($) { |
|
|
784 | if (length $_[0]) { |
763 | $PROXY = [$2, $3 || 3128, $1] if $_[0] =~ m%^(https?):// ([^:/]+) (?: : (\d*) )?%ix; |
785 | $_[0] =~ m%^(https?):// ([^:/]+) (?: : (\d*) )?%ix |
|
|
786 | or Carp::croak "$_[0]: invalid proxy URL"; |
|
|
787 | $PROXY = [$2, $3 || 3128, $1] |
|
|
788 | } else { |
|
|
789 | undef $PROXY; |
|
|
790 | } |
764 | } |
791 | } |
765 | |
792 | |
766 | # initialise proxy from environment |
793 | # initialise proxy from environment |
|
|
794 | eval { |
767 | set_proxy $ENV{http_proxy}; |
795 | set_proxy $ENV{http_proxy}; |
|
|
796 | }; |
768 | |
797 | |
769 | =head1 SEE ALSO |
798 | =head1 SEE ALSO |
770 | |
799 | |
771 | L<AnyEvent>. |
800 | L<AnyEvent>. |
772 | |
801 | |