Permalink
Browse files

Handle Last-modified

  • Loading branch information...
miyagawa committed Apr 3, 2012
1 parent 9cc6ba5 commit 64b4486b32167785d3a3631abfcf31a2079b124a
Showing with 10 additions and 2 deletions.
  1. +10 −2 lib/CPANMetaDB/Dist.pm
View
@@ -18,6 +18,7 @@ use AnyEvent;
use AnyEvent::HTTP;
use File::Temp;
use IO::Uncompress::Gunzip;
+use HTTP::Date ();
sub new {
my $class = shift;
@@ -29,8 +30,9 @@ sub new {
sub register {
my $self = shift;
$self->{tmpdir} = File::Temp::tempdir;
+ $self->{modified} = HTTP::Date::time2str(time - 600);
- $self->{t} = AE::timer 0, 600, sub {
+ $self->{t} = AE::timer 0, 300, sub {
$self->fetch_packages;
};
}
@@ -48,17 +50,23 @@ sub fetch_packages {
warn "----> Start downloading $url\n";
AnyEvent::HTTP::http_get $url,
+ headers => {
+ 'If-Modified-Since' => $self->{modified},
+ },
on_body => sub {
my($data, $hdr) = @_;
print $fh $data;
},
sub {
my (undef, $hdr) = @_;
close $fh;
- warn "----> Download complete!\n";
if ($hdr->{Status} == 200) {
+ warn "----> Download complete!\n";
+ $self->{modified} = $hdr->{'Last-Modified'};
$self->update_packages($file);
+ } elsif ($hdr->{Status} == 302) {
+ warn "----> Not modified\n";
} else {
warn "!!! Error: $hdr->{Status}\n";
}

0 comments on commit 64b4486

Please sign in to comment.