ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-HTTP/README
(Generate patch)

Comparing AnyEvent-HTTP/README (file contents):
Revision 1.15 by root, Tue Jan 4 08:29:28 2011 UTC vs.
Revision 1.16 by root, Tue Jan 11 06:38:47 2011 UTC

451 The number of active connections. This is not the number of 451 The number of active connections. This is not the number of
452 currently running requests, but the number of currently open and 452 currently running requests, but the number of currently open and
453 non-idle TCP connections. This number can be useful for 453 non-idle TCP connections. This number can be useful for
454 load-leveling. 454 load-leveling.
455 455
456 SHOWCASE
457 This section contaisn some more elaborate "real-world" examples or code
458 snippets.
459
460 HTTP/1.1 FILE DOWNLOAD
461 Downloading files with HTTP cna be quite tricky, especially when
462 something goes wrong and you want tor esume.
463
464 Here is a function that initiates and resumes a download. It uses the
465 last modified time to check for file content changes, and works with
466 many HTTP/1.0 servers as well, and usually falls back to a complete
467 re-download on older servers.
468
469 It calls the completion callback with either "undef", which means a
470 nonretryable error occured, 0 when the download was partial and should
471 be retried, and 1 if it was successful.
472
473 use AnyEvent::HTTP;
474
475 sub download($$$) {
476 my ($url, $file, $cb) = @_;
477
478 open my $fh, "+<", $file
479 or die "$file: $!";
480
481 my %hdr;
482 my $ofs = 0;
483
484 warn stat $fh;
485 warn -s _;
486 if (stat $fh and -s _) {
487 $ofs = -s _;
488 warn "-s is ", $ofs;#d#
489 $hdr{"if-unmodified-since"} = AnyEvent::HTTP::format_date +(stat _)[9];
490 $hdr{"range"} = "bytes=$ofs-";
491 }
492
493 http_get $url,
494 headers => \%hdr,
495 on_header => sub {
496 my ($hdr) = @_;
497
498 if ($hdr->{Status} == 200 && $ofs) {
499 # resume failed
500 truncate $fh, $ofs = 0;
501 }
502
503 sysseek $fh, $ofs, 0;
504
505 1
506 },
507 on_body => sub {
508 my ($data, $hdr) = @_;
509
510 if ($hdr->{Status} =~ /^2/) {
511 length $data == syswrite $fh, $data
512 or return; # abort on write errors
513 }
514
515 1
516 },
517 sub {
518 my (undef, $hdr) = @_;
519
520 my $status = $hdr->{Status};
521
522 if (my $time = AnyEvent::HTTP::parse_date $hdr->{"last-modified"}) {
523 utime $fh, $time, $time;
524 }
525
526 if ($status == 200 || $status == 206 || $status == 416) {
527 # download ok || resume ok || file already fully downloaded
528 $cb->(1, $hdr);
529
530 } elsif ($status == 412) {
531 # file has changed while resuming, delete and retry
532 unlink $file;
533 $cb->(0, $hdr);
534
535 } elsif ($status == 500 or $status == 503 or $status =~ /^59/) {
536 # retry later
537 $cb->(0, $hdr);
538
539 } else {
540 $cb->(undef, $hdr);
541 }
542 }
543 ;
544 }
545
546 download "http://server/somelargefile", "/tmp/somelargefile", sub {
547 if ($_[0]) {
548 print "OK!\n";
549 } elsif (defined $_[0]) {
550 print "please retry later\n";
551 } else {
552 print "ERROR\n";
553 }
554 };
555
456 SOCKS PROXIES 556 SOCKS PROXIES
457 Socks proxies are not directly supported by AnyEvent::HTTP. You can 557 Socks proxies are not directly supported by AnyEvent::HTTP. You can
458 compile your perl to support socks, or use an external program such as 558 compile your perl to support socks, or use an external program such as
459 socksify (dante) or tsocks to make your program use a socks proxy 559 socksify (dante) or tsocks to make your program use a socks proxy
460 transparently. 560 transparently.
461 561

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines