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.19 by root, Sat Feb 19 06:46:14 2011 UTC

12 This module is an AnyEvent user, you need to make sure that you use and 12 This module is an AnyEvent user, you need to make sure that you use and
13 run a supported event loop. 13 run a supported event loop.
14 14
15 This module implements a simple, stateless and non-blocking HTTP client. 15 This module implements a simple, stateless and non-blocking HTTP client.
16 It supports GET, POST and other request methods, cookies and more, all 16 It supports GET, POST and other request methods, cookies and more, all
17 on a very low level. It can follow redirects supports proxies and 17 on a very low level. It can follow redirects, supports proxies, and
18 automatically limits the number of connections to the values specified 18 automatically limits the number of connections to the values specified
19 in the RFC. 19 in the RFC.
20 20
21 It should generally be a "good client" that is enough for most HTTP 21 It should generally be a "good client" that is enough for most HTTP
22 tasks. Simple tasks should be simple, but complex tasks should still be 22 tasks. Simple tasks should be simple, but complex tasks should still be
129 this is not an overall timeout. 129 this is not an overall timeout.
130 130
131 Default timeout is 5 minutes. 131 Default timeout is 5 minutes.
132 132
133 proxy => [$host, $port[, $scheme]] or undef 133 proxy => [$host, $port[, $scheme]] or undef
134 Use the given http proxy for all requests. If not specified, 134 Use the given http proxy for all requests, or no proxy if
135 then the default proxy (as specified by $ENV{http_proxy}) is
136 used. 135 "undef" is used.
137 136
138 $scheme must be either missing or must be "http" for HTTP. 137 $scheme must be either missing or must be "http" for HTTP.
138
139 If not specified, then the default proxy is used (see
140 "AnyEvent::HTTP::set_proxy").
139 141
140 body => $string 142 body => $string
141 The request body, usually empty. Will be sent as-is (future 143 The request body, usually empty. Will be sent as-is (future
142 versions of this module might offer more options). 144 versions of this module might offer more options).
143 145
365 AnyEvent::HTTP::set_proxy "proxy-url" 367 AnyEvent::HTTP::set_proxy "proxy-url"
366 Sets the default proxy server to use. The proxy-url must begin with 368 Sets the default proxy server to use. The proxy-url must begin with
367 a string of the form "http://host:port", croaks otherwise. 369 a string of the form "http://host:port", croaks otherwise.
368 370
369 To clear an already-set proxy, use "undef". 371 To clear an already-set proxy, use "undef".
372
373 When AnyEvent::HTTP is laoded for the first time it will query the
374 default proxy from the operating system, currently by looking at
375 "$ENV{http_proxy"}.
370 376
371 AnyEvent::HTTP::cookie_jar_expire $jar[, $session_end] 377 AnyEvent::HTTP::cookie_jar_expire $jar[, $session_end]
372 Remove all cookies from the cookie jar that have been expired. If 378 Remove all cookies from the cookie jar that have been expired. If
373 $session_end is given and true, then additionally remove all session 379 $session_end is given and true, then additionally remove all session
374 cookies. 380 cookies.
451 The number of active connections. This is not the number of 457 The number of active connections. This is not the number of
452 currently running requests, but the number of currently open and 458 currently running requests, but the number of currently open and
453 non-idle TCP connections. This number can be useful for 459 non-idle TCP connections. This number can be useful for
454 load-leveling. 460 load-leveling.
455 461
462 SHOWCASE
463 This section contaisn some more elaborate "real-world" examples or code
464 snippets.
465
466 HTTP/1.1 FILE DOWNLOAD
467 Downloading files with HTTP can be quite tricky, especially when
468 something goes wrong and you want to resume.
469
470 Here is a function that initiates and resumes a download. It uses the
471 last modified time to check for file content changes, and works with
472 many HTTP/1.0 servers as well, and usually falls back to a complete
473 re-download on older servers.
474
475 It calls the completion callback with either "undef", which means a
476 nonretryable error occured, 0 when the download was partial and should
477 be retried, and 1 if it was successful.
478
479 use AnyEvent::HTTP;
480
481 sub download($$$) {
482 my ($url, $file, $cb) = @_;
483
484 open my $fh, "+<", $file
485 or die "$file: $!";
486
487 my %hdr;
488 my $ofs = 0;
489
490 warn stat $fh;
491 warn -s _;
492 if (stat $fh and -s _) {
493 $ofs = -s _;
494 warn "-s is ", $ofs;#d#
495 $hdr{"if-unmodified-since"} = AnyEvent::HTTP::format_date +(stat _)[9];
496 $hdr{"range"} = "bytes=$ofs-";
497 }
498
499 http_get $url,
500 headers => \%hdr,
501 on_header => sub {
502 my ($hdr) = @_;
503
504 if ($hdr->{Status} == 200 && $ofs) {
505 # resume failed
506 truncate $fh, $ofs = 0;
507 }
508
509 sysseek $fh, $ofs, 0;
510
511 1
512 },
513 on_body => sub {
514 my ($data, $hdr) = @_;
515
516 if ($hdr->{Status} =~ /^2/) {
517 length $data == syswrite $fh, $data
518 or return; # abort on write errors
519 }
520
521 1
522 },
523 sub {
524 my (undef, $hdr) = @_;
525
526 my $status = $hdr->{Status};
527
528 if (my $time = AnyEvent::HTTP::parse_date $hdr->{"last-modified"}) {
529 utime $fh, $time, $time;
530 }
531
532 if ($status == 200 || $status == 206 || $status == 416) {
533 # download ok || resume ok || file already fully downloaded
534 $cb->(1, $hdr);
535
536 } elsif ($status == 412) {
537 # file has changed while resuming, delete and retry
538 unlink $file;
539 $cb->(0, $hdr);
540
541 } elsif ($status == 500 or $status == 503 or $status =~ /^59/) {
542 # retry later
543 $cb->(0, $hdr);
544
545 } else {
546 $cb->(undef, $hdr);
547 }
548 }
549 ;
550 }
551
552 download "http://server/somelargefile", "/tmp/somelargefile", sub {
553 if ($_[0]) {
554 print "OK!\n";
555 } elsif (defined $_[0]) {
556 print "please retry later\n";
557 } else {
558 print "ERROR\n";
559 }
560 };
561
456 SOCKS PROXIES 562 SOCKS PROXIES
457 Socks proxies are not directly supported by AnyEvent::HTTP. You can 563 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 564 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 565 socksify (dante) or tsocks to make your program use a socks proxy
460 transparently. 566 transparently.
461 567

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines