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.92 by root, Tue Jan 4 08:17:59 2011 UTC vs.
Revision 1.93 by root, Tue Jan 11 06:38:47 2011 UTC

46use AnyEvent::Util (); 46use AnyEvent::Util ();
47use AnyEvent::Handle (); 47use AnyEvent::Handle ();
48 48
49use base Exporter::; 49use base Exporter::;
50 50
51our $VERSION = '2.0'; 51our $VERSION = '2.01';
52 52
53our @EXPORT = qw(http_get http_post http_head http_request); 53our @EXPORT = qw(http_get http_post http_head http_request);
54 54
55our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; 55our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)";
56our $MAX_RECURSE = 10; 56our $MAX_RECURSE = 10;
1325# initialise proxy from environment 1325# initialise proxy from environment
1326eval { 1326eval {
1327 set_proxy $ENV{http_proxy}; 1327 set_proxy $ENV{http_proxy};
1328}; 1328};
1329 1329
1330=head2 SHOWCASE
1331
1332This section contaisn some more elaborate "real-world" examples or code
1333snippets.
1334
1335=head2 HTTP/1.1 FILE DOWNLOAD
1336
1337Downloading files with HTTP cna be quite tricky, especially when something
1338goes wrong and you want tor esume.
1339
1340Here is a function that initiates and resumes a download. It uses the
1341last modified time to check for file content changes, and works with many
1342HTTP/1.0 servers as well, and usually falls back to a complete re-download
1343on older servers.
1344
1345It calls the completion callback with either C<undef>, which means a
1346nonretryable error occured, C<0> when the download was partial and should
1347be retried, and C<1> if it was successful.
1348
1349 use AnyEvent::HTTP;
1350
1351 sub download($$$) {
1352 my ($url, $file, $cb) = @_;
1353
1354 open my $fh, "+<", $file
1355 or die "$file: $!";
1356
1357 my %hdr;
1358 my $ofs = 0;
1359
1360 warn stat $fh;
1361 warn -s _;
1362 if (stat $fh and -s _) {
1363 $ofs = -s _;
1364 warn "-s is ", $ofs;#d#
1365 $hdr{"if-unmodified-since"} = AnyEvent::HTTP::format_date +(stat _)[9];
1366 $hdr{"range"} = "bytes=$ofs-";
1367 }
1368
1369 http_get $url,
1370 headers => \%hdr,
1371 on_header => sub {
1372 my ($hdr) = @_;
1373
1374 if ($hdr->{Status} == 200 && $ofs) {
1375 # resume failed
1376 truncate $fh, $ofs = 0;
1377 }
1378
1379 sysseek $fh, $ofs, 0;
1380
1381 1
1382 },
1383 on_body => sub {
1384 my ($data, $hdr) = @_;
1385
1386 if ($hdr->{Status} =~ /^2/) {
1387 length $data == syswrite $fh, $data
1388 or return; # abort on write errors
1389 }
1390
1391 1
1392 },
1393 sub {
1394 my (undef, $hdr) = @_;
1395
1396 my $status = $hdr->{Status};
1397
1398 if (my $time = AnyEvent::HTTP::parse_date $hdr->{"last-modified"}) {
1399 utime $fh, $time, $time;
1400 }
1401
1402 if ($status == 200 || $status == 206 || $status == 416) {
1403 # download ok || resume ok || file already fully downloaded
1404 $cb->(1, $hdr);
1405
1406 } elsif ($status == 412) {
1407 # file has changed while resuming, delete and retry
1408 unlink $file;
1409 $cb->(0, $hdr);
1410
1411 } elsif ($status == 500 or $status == 503 or $status =~ /^59/) {
1412 # retry later
1413 $cb->(0, $hdr);
1414
1415 } else {
1416 $cb->(undef, $hdr);
1417 }
1418 }
1419 ;
1420 }
1421
1422 download "http://server/somelargefile", "/tmp/somelargefile", sub {
1423 if ($_[0]) {
1424 print "OK!\n";
1425 } elsif (defined $_[0]) {
1426 print "please retry later\n";
1427 } else {
1428 print "ERROR\n";
1429 }
1430 };
1431
1330=head2 SOCKS PROXIES 1432=head3 SOCKS PROXIES
1331 1433
1332Socks proxies are not directly supported by AnyEvent::HTTP. You can 1434Socks proxies are not directly supported by AnyEvent::HTTP. You can
1333compile your perl to support socks, or use an external program such as 1435compile your perl to support socks, or use an external program such as
1334F<socksify> (dante) or F<tsocks> to make your program use a socks proxy 1436F<socksify> (dante) or F<tsocks> to make your program use a socks proxy
1335transparently. 1437transparently.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines