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.43 by root, Mon Jul 6 03:03:12 2009 UTC vs.
Revision 1.50 by root, Tue Aug 11 13:20:42 2009 UTC

41use strict; 41use strict;
42no warnings; 42no warnings;
43 43
44use Errno (); 44use Errno ();
45 45
46use AnyEvent 4.452 (); 46use AnyEvent 4.8 ();
47use AnyEvent::Util (); 47use AnyEvent::Util ();
48use AnyEvent::Socket (); 48use AnyEvent::Socket ();
49use AnyEvent::Handle (); 49use AnyEvent::Handle ();
50 50
51use base Exporter::; 51use base Exporter::;
52 52
53our $VERSION = '1.12'; 53our $VERSION = '1.42';
54 54
55our @EXPORT = qw(http_get http_post http_head http_request); 55our @EXPORT = qw(http_get http_post http_head http_request);
56 56
57our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; 57our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)";
58our $MAX_RECURSE = 10; 58our $MAX_RECURSE = 10;
138 138
139=item headers => hashref 139=item headers => hashref
140 140
141The request headers to use. Currently, C<http_request> may provide its 141The request headers to use. Currently, C<http_request> may provide its
142own C<Host:>, C<Content-Length:>, C<Connection:> and C<Cookie:> headers 142own C<Host:>, C<Content-Length:>, C<Connection:> and C<Cookie:> headers
143and will provide defaults for C<User-Agent:> and C<Referer:>. 143and will provide defaults for C<User-Agent:> and C<Referer:> (this can be
144suppressed by using C<undef> for these headers in which case they won't be
145sent at all).
144 146
145=item timeout => $seconds 147=item timeout => $seconds
146 148
147The time-out to use for various stages - each connect attempt will reset 149The time-out to use for various stages - each connect attempt will reset
148the timeout, as will read or write activity. Default timeout is 5 minutes. 150the 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
152Use the given http proxy for all requests. If not specified, then the 154Use the given http proxy for all requests. If not specified, then the
153default proxy (as specified by C<$ENV{http_proxy}>) is used. 155default proxy (as specified by C<$ENV{http_proxy}>) is used.
154 156
155C<$scheme> must be either missing or C<http> for HTTP, or C<https> for 157C<$scheme> must be either missing, C<http> for HTTP or C<https> for
156HTTPS. 158HTTPS.
157 159
158=item body => $string 160=item body => $string
159 161
160The request body, usually empty. Will be-sent as-is (future versions of 162The request body, usually empty. Will be-sent as-is (future versions of
221This callback is useful when the data is too large to be held in memory 223This 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 224(so the callback writes it to a file) or when only some information should
223be extracted, or when the body should be processed incrementally. 225be extracted, or when the body should be processed incrementally.
224 226
225It is usually preferred over doing your own body handling via 227It is usually preferred over doing your own body handling via
226C<want_body_handle>. 228C<want_body_handle>, but in case of streaming APIs, where HTTP is
229only used to create a connection, C<want_body_handle> is the better
230alternative, as it allows you to install your own event handler, reducing
231resource usage.
227 232
228=item want_body_handle => $enable 233=item want_body_handle => $enable
229 234
230When enabled (default is disabled), the behaviour of AnyEvent::HTTP 235When enabled (default is disabled), the behaviour of AnyEvent::HTTP
231changes considerably: after parsing the headers, and instead of 236changes considerably: after parsing the headers, and instead of
243This is useful with some push-type services, where, after the initial 248This is useful with some push-type services, where, after the initial
244headers, an interactive protocol is used (typical example would be the 249headers, an interactive protocol is used (typical example would be the
245push-style twitter API which starts a JSON/XML stream). 250push-style twitter API which starts a JSON/XML stream).
246 251
247If you think you need this, first have a look at C<on_body>, to see if 252If you think you need this, first have a look at C<on_body>, to see if
248that doesn'T solve your problem in a better way. 253that doesn't solve your problem in a better way.
249 254
250=back 255=back
251 256
252Example: make a simple HTTP GET request for http://www.nethype.de/ 257Example: make a simple HTTP GET request for http://www.nethype.de/
253 258
309 push @{ $CO_SLOT{$_[0]}[1] }, $_[1]; 314 push @{ $CO_SLOT{$_[0]}[1] }, $_[1];
310 315
311 _slot_schedule $_[0]; 316 _slot_schedule $_[0];
312} 317}
313 318
314our $qr_nl = qr<\015?\012>; 319our $qr_nl = qr{\015?\012};
315our $qr_nlnl = qr<\015?\012\015?\012>; 320our $qr_nlnl = qr{(?<![^\012])\015?\012};
316 321
317our $TLS_CTX_LOW = { cache => 1, sslv2 => 1 }; 322our $TLS_CTX_LOW = { cache => 1, sslv2 => 1 };
318our $TLS_CTX_HIGH = { cache => 1, verify => 1, verify_peername => "https" }; 323our $TLS_CTX_HIGH = { cache => 1, verify => 1, verify_peername => "https" };
319 324
320sub http_request($$@) { 325sub http_request($$@) {
398 my ($rhost, $rport, $rscheme, $rpath); # request host, port, path 403 my ($rhost, $rport, $rscheme, $rpath); # request host, port, path
399 404
400 if ($proxy) { 405 if ($proxy) {
401 ($rpath, $rhost, $rport, $rscheme) = ($url, @$proxy); 406 ($rpath, $rhost, $rport, $rscheme) = ($url, @$proxy);
402 407
408 $rscheme = "http" unless defined $rscheme;
409
403 # don't support https requests over https-proxy transport, 410 # don't support https requests over https-proxy transport,
404 # 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.
405 $rscheme = "http" if $uscheme eq "https" && $rscheme eq "https"; 412 $rscheme = "http" if $uscheme eq "https" && $rscheme eq "https";
406 } else { 413 } else {
407 ($rhost, $rport, $rscheme, $rpath) = ($uhost, $uport, $uscheme, $upath); 414 ($rhost, $rport, $rscheme, $rpath) = ($uhost, $uport, $uscheme, $upath);
408 } 415 }
409 416
410 $hdr{"user-agent"} ||= $USERAGENT; 417 # leave out fragment and query string, just a heuristic
411 $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"};
412 420
413 $hdr{"content-length"} = length $arg{body}; 421 $hdr{"content-length"} = length $arg{body};
414 422
415 my %state = (connect_guard => 1); 423 my %state = (connect_guard => 1);
416 424
419 427
420 return unless $state{connect_guard}; 428 return unless $state{connect_guard};
421 429
422 $state{connect_guard} = AnyEvent::Socket::tcp_connect $rhost, $rport, sub { 430 $state{connect_guard} = AnyEvent::Socket::tcp_connect $rhost, $rport, sub {
423 $state{fh} = shift 431 $state{fh} = shift
432 or do {
433 my $err = "$!";
434 %state = ();
424 or return (%state = (), $cb->(undef, { Status => 599, Reason => "$!", URL => $url })); 435 return $cb->(undef, { Status => 599, Reason => $err, URL => $url });
436 };
437
425 pop; # free memory, save a tree 438 pop; # free memory, save a tree
426 439
427 return unless delete $state{connect_guard}; 440 return unless delete $state{connect_guard};
428 441
429 # get handle 442 # get handle
462 $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};
463 476
464 # send request 477 # send request
465 $state{handle}->push_write ( 478 $state{handle}->push_write (
466 "$method $rpath HTTP/1.0\015\012" 479 "$method $rpath HTTP/1.0\015\012"
467 . (join "", map "\u$_: $hdr{$_}\015\012", keys %hdr) 480 . (join "", map "\u$_: $hdr{$_}\015\012", grep defined $hdr{$_}, keys %hdr)
468 . "\015\012" 481 . "\015\012"
469 . (delete $arg{body}) 482 . (delete $arg{body})
470 ); 483 );
471 484
472 %hdr = (); # reduce memory usage, save a kitten 485 %hdr = (); # reduce memory usage, save a kitten
483 URL => ",$url" 496 URL => ",$url"
484 ); 497 );
485 498
486 # headers, could be optimized a bit 499 # headers, could be optimized a bit
487 $state{handle}->unshift_read (line => $qr_nlnl, sub { 500 $state{handle}->unshift_read (line => $qr_nlnl, sub {
488 for ("$_[1]\012") { 501 for ("$_[1]") {
489 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.
490 503
491 # things seen, not parsed: 504 # things seen, not parsed:
492 # p3pP="NON CUR OTPi OUR NOR UNI" 505 # p3pP="NON CUR OTPi OUR NOR UNI"
493 506
590 603
591 redo if /\G\s*,/gc; 604 redo if /\G\s*,/gc;
592 } 605 }
593 } 606 }
594 607
595 if ($redirect) { 608 if ($redirect && exists $hdr{location}) {
596 # we ignore any errors, as it is very common to receive 609 # we ignore any errors, as it is very common to receive
597 # Content-Length != 0 but no actual body 610 # Content-Length != 0 but no actual body
598 # we also access %hdr, as $_[1] might be an erro 611 # we also access %hdr, as $_[1] might be an erro
599 http_request ($method => $hdr{location}, %arg, recurse => $recurse - 1, $cb); 612 http_request ($method => $hdr{location}, %arg, recurse => $recurse - 1, $cb);
600 } else { 613 } else {
689 }); 702 });
690 } else { 703 } else {
691 &$handle_actual_request; 704 &$handle_actual_request;
692 } 705 }
693 706
694 }, sub { 707 }, $arg{on_prepare} || sub { $timeout };
695 $timeout
696 };
697 }; 708 };
698 709
699 defined wantarray && AnyEvent::Util::guard { %state = () } 710 defined wantarray && AnyEvent::Util::guard { %state = () }
700} 711}
701 712
735The default value for the C<User-Agent> header (the default is 746The default value for the C<User-Agent> header (the default is
736C<Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)>). 747C<Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)>).
737 748
738=item $AnyEvent::HTTP::MAX_PER_HOST 749=item $AnyEvent::HTTP::MAX_PER_HOST
739 750
740The maximum number of concurrent conenctions to the same host (identified 751The maximum number of concurrent connections to the same host (identified
741by the hostname). If the limit is exceeded, then the additional requests 752by the hostname). If the limit is exceeded, then the additional requests
742are queued until previous connections are closed. 753are queued until previous connections are closed.
743 754
744The default value for this is C<4>, and it is highly advisable to not 755The default value for this is C<4>, and it is highly advisable to not
745increase it. 756increase it.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines