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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines