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.46 by root, Sat Jul 25 01:29:09 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.41';
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;
221This callback is useful when the data is too large to be held in memory 221This 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 222(so the callback writes it to a file) or when only some information should
223be extracted, or when the body should be processed incrementally. 223be extracted, or when the body should be processed incrementally.
224 224
225It is usually preferred over doing your own body handling via 225It is usually preferred over doing your own body handling via
226C<want_body_handle>. 226C<want_body_handle>, but in case of streaming APIs, where HTTP is
227only used to create a connection, C<want_body_handle> is the better
228alternative, as it allows you to install your own event handler, reducing
229resource usage.
227 230
228=item want_body_handle => $enable 231=item want_body_handle => $enable
229 232
230When enabled (default is disabled), the behaviour of AnyEvent::HTTP 233When enabled (default is disabled), the behaviour of AnyEvent::HTTP
231changes considerably: after parsing the headers, and instead of 234changes considerably: after parsing the headers, and instead of
243This is useful with some push-type services, where, after the initial 246This is useful with some push-type services, where, after the initial
244headers, an interactive protocol is used (typical example would be the 247headers, an interactive protocol is used (typical example would be the
245push-style twitter API which starts a JSON/XML stream). 248push-style twitter API which starts a JSON/XML stream).
246 249
247If you think you need this, first have a look at C<on_body>, to see if 250If 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. 251that doesn't solve your problem in a better way.
249 252
250=back 253=back
251 254
252Example: make a simple HTTP GET request for http://www.nethype.de/ 255Example: make a simple HTTP GET request for http://www.nethype.de/
253 256
309 push @{ $CO_SLOT{$_[0]}[1] }, $_[1]; 312 push @{ $CO_SLOT{$_[0]}[1] }, $_[1];
310 313
311 _slot_schedule $_[0]; 314 _slot_schedule $_[0];
312} 315}
313 316
314our $qr_nl = qr<\015?\012>; 317our $qr_nl = qr{\015?\012};
315our $qr_nlnl = qr<\015?\012\015?\012>; 318our $qr_nlnl = qr{(?<![^\012])\015?\012};
316 319
317our $TLS_CTX_LOW = { cache => 1, sslv2 => 1 }; 320our $TLS_CTX_LOW = { cache => 1, sslv2 => 1 };
318our $TLS_CTX_HIGH = { cache => 1, verify => 1, verify_peername => "https" }; 321our $TLS_CTX_HIGH = { cache => 1, verify => 1, verify_peername => "https" };
319 322
320sub http_request($$@) { 323sub http_request($$@) {
419 422
420 return unless $state{connect_guard}; 423 return unless $state{connect_guard};
421 424
422 $state{connect_guard} = AnyEvent::Socket::tcp_connect $rhost, $rport, sub { 425 $state{connect_guard} = AnyEvent::Socket::tcp_connect $rhost, $rport, sub {
423 $state{fh} = shift 426 $state{fh} = shift
427 or do {
428 my $err = "$!";
429 %state = ();
424 or return (%state = (), $cb->(undef, { Status => 599, Reason => "$!", URL => $url })); 430 return $cb->(undef, { Status => 599, Reason => $err, URL => $url });
431 };
432
425 pop; # free memory, save a tree 433 pop; # free memory, save a tree
426 434
427 return unless delete $state{connect_guard}; 435 return unless delete $state{connect_guard};
428 436
429 # get handle 437 # get handle
483 URL => ",$url" 491 URL => ",$url"
484 ); 492 );
485 493
486 # headers, could be optimized a bit 494 # headers, could be optimized a bit
487 $state{handle}->unshift_read (line => $qr_nlnl, sub { 495 $state{handle}->unshift_read (line => $qr_nlnl, sub {
488 for ("$_[1]\012") { 496 for ("$_[1]") {
489 y/\015//d; # weed out any \015, as they show up in the weirdest of places. 497 y/\015//d; # weed out any \015, as they show up in the weirdest of places.
490 498
491 # things seen, not parsed: 499 # things seen, not parsed:
492 # p3pP="NON CUR OTPi OUR NOR UNI" 500 # p3pP="NON CUR OTPi OUR NOR UNI"
493 501

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines