… | |
… | |
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.41'; |
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 | send 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. Default timeout is 5 minutes. |
… | |
… | |
150 | =item proxy => [$host, $port[, $scheme]] or undef |
152 | =item proxy => [$host, $port[, $scheme]] or undef |
151 | |
153 | |
152 | Use the given http proxy for all requests. If not specified, then the |
154 | 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. |
155 | default proxy (as specified by C<$ENV{http_proxy}>) is used. |
154 | |
156 | |
155 | C<$scheme> must be either missing or C<http> for HTTP, or C<https> for |
157 | C<$scheme> must be either missing, C<http> for HTTP or C<https> for |
156 | HTTPS. |
158 | HTTPS. |
157 | |
159 | |
158 | =item body => $string |
160 | =item body => $string |
159 | |
161 | |
160 | The request body, usually empty. Will be-sent as-is (future versions of |
162 | The request body, usually empty. Will be-sent as-is (future versions of |
… | |
… | |
312 | push @{ $CO_SLOT{$_[0]}[1] }, $_[1]; |
314 | push @{ $CO_SLOT{$_[0]}[1] }, $_[1]; |
313 | |
315 | |
314 | _slot_schedule $_[0]; |
316 | _slot_schedule $_[0]; |
315 | } |
317 | } |
316 | |
318 | |
317 | our $qr_nl = qr<\015?\012>; |
319 | our $qr_nl = qr{\015?\012}; |
318 | our $qr_nlnl = qr<\015?\012\015?\012>; |
320 | our $qr_nlnl = qr{(?<![^\012])\015?\012}; |
319 | |
321 | |
320 | our $TLS_CTX_LOW = { cache => 1, sslv2 => 1 }; |
322 | our $TLS_CTX_LOW = { cache => 1, sslv2 => 1 }; |
321 | our $TLS_CTX_HIGH = { cache => 1, verify => 1, verify_peername => "https" }; |
323 | our $TLS_CTX_HIGH = { cache => 1, verify => 1, verify_peername => "https" }; |
322 | |
324 | |
323 | sub http_request($$@) { |
325 | sub http_request($$@) { |
… | |
… | |
401 | my ($rhost, $rport, $rscheme, $rpath); # request host, port, path |
403 | my ($rhost, $rport, $rscheme, $rpath); # request host, port, path |
402 | |
404 | |
403 | if ($proxy) { |
405 | if ($proxy) { |
404 | ($rpath, $rhost, $rport, $rscheme) = ($url, @$proxy); |
406 | ($rpath, $rhost, $rport, $rscheme) = ($url, @$proxy); |
405 | |
407 | |
|
|
408 | $rscheme = "http" unless defined $rscheme; |
|
|
409 | |
406 | # don't support https requests over https-proxy transport, |
410 | # don't support https requests over https-proxy transport, |
407 | # can't be done with tls as spec'ed, unless you double-encrypt. |
411 | # can't be done with tls as spec'ed, unless you double-encrypt. |
408 | $rscheme = "http" if $uscheme eq "https" && $rscheme eq "https"; |
412 | $rscheme = "http" if $uscheme eq "https" && $rscheme eq "https"; |
409 | } else { |
413 | } else { |
410 | ($rhost, $rport, $rscheme, $rpath) = ($uhost, $uport, $uscheme, $upath); |
414 | ($rhost, $rport, $rscheme, $rpath) = ($uhost, $uport, $uscheme, $upath); |
411 | } |
415 | } |
412 | |
416 | |
413 | $hdr{"user-agent"} ||= $USERAGENT; |
417 | # leave out fragment and query string, just a heuristic |
414 | $hdr{referer} ||= "$uscheme://$uauthority$upath"; # leave out fragment and query string, just a heuristic |
418 | $hdr{referer} ||= "$uscheme://$uauthority$upath" unless exists $hdr{referer}; |
|
|
419 | $hdr{"user-agent"} ||= $USERAGENT unless exists $hdr{"user-agent"}; |
415 | |
420 | |
416 | $hdr{"content-length"} = length $arg{body}; |
421 | $hdr{"content-length"} = length $arg{body}; |
417 | |
422 | |
418 | my %state = (connect_guard => 1); |
423 | my %state = (connect_guard => 1); |
419 | |
424 | |
… | |
… | |
470 | $state{handle}->starttls ("connect") if $uscheme eq "https" && !exists $state{handle}{tls}; |
475 | $state{handle}->starttls ("connect") if $uscheme eq "https" && !exists $state{handle}{tls}; |
471 | |
476 | |
472 | # send request |
477 | # send request |
473 | $state{handle}->push_write ( |
478 | $state{handle}->push_write ( |
474 | "$method $rpath HTTP/1.0\015\012" |
479 | "$method $rpath HTTP/1.0\015\012" |
475 | . (join "", map "\u$_: $hdr{$_}\015\012", keys %hdr) |
480 | . (join "", map "\u$_: $hdr{$_}\015\012", grep defined $hdr{$_}, keys %hdr) |
476 | . "\015\012" |
481 | . "\015\012" |
477 | . (delete $arg{body}) |
482 | . (delete $arg{body}) |
478 | ); |
483 | ); |
479 | |
484 | |
480 | %hdr = (); # reduce memory usage, save a kitten |
485 | %hdr = (); # reduce memory usage, save a kitten |
… | |
… | |
491 | URL => ",$url" |
496 | URL => ",$url" |
492 | ); |
497 | ); |
493 | |
498 | |
494 | # headers, could be optimized a bit |
499 | # headers, could be optimized a bit |
495 | $state{handle}->unshift_read (line => $qr_nlnl, sub { |
500 | $state{handle}->unshift_read (line => $qr_nlnl, sub { |
496 | for ("$_[1]\012") { |
501 | for ("$_[1]") { |
497 | y/\015//d; # weed out any \015, as they show up in the weirdest of places. |
502 | y/\015//d; # weed out any \015, as they show up in the weirdest of places. |
498 | |
503 | |
499 | # things seen, not parsed: |
504 | # things seen, not parsed: |
500 | # p3pP="NON CUR OTPi OUR NOR UNI" |
505 | # p3pP="NON CUR OTPi OUR NOR UNI" |
501 | |
506 | |
… | |
… | |
598 | |
603 | |
599 | redo if /\G\s*,/gc; |
604 | redo if /\G\s*,/gc; |
600 | } |
605 | } |
601 | } |
606 | } |
602 | |
607 | |
603 | if ($redirect) { |
608 | if ($redirect && exists $hdr{location}) { |
604 | # we ignore any errors, as it is very common to receive |
609 | # we ignore any errors, as it is very common to receive |
605 | # Content-Length != 0 but no actual body |
610 | # Content-Length != 0 but no actual body |
606 | # we also access %hdr, as $_[1] might be an erro |
611 | # we also access %hdr, as $_[1] might be an erro |
607 | http_request ($method => $hdr{location}, %arg, recurse => $recurse - 1, $cb); |
612 | http_request ($method => $hdr{location}, %arg, recurse => $recurse - 1, $cb); |
608 | } else { |
613 | } else { |
… | |
… | |
743 | The default value for the C<User-Agent> header (the default is |
748 | The default value for the C<User-Agent> header (the default is |
744 | C<Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)>). |
749 | C<Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)>). |
745 | |
750 | |
746 | =item $AnyEvent::HTTP::MAX_PER_HOST |
751 | =item $AnyEvent::HTTP::MAX_PER_HOST |
747 | |
752 | |
748 | The maximum number of concurrent conenctions to the same host (identified |
753 | The maximum number of concurrent connections to the same host (identified |
749 | by the hostname). If the limit is exceeded, then the additional requests |
754 | by the hostname). If the limit is exceeded, then the additional requests |
750 | are queued until previous connections are closed. |
755 | are queued until previous connections are closed. |
751 | |
756 | |
752 | The default value for this is C<4>, and it is highly advisable to not |
757 | The default value for this is C<4>, and it is highly advisable to not |
753 | increase it. |
758 | increase it. |