824
825
my $request = HTTP::Request->new('GET', $url);
827
my($mtime) = (stat($file))[9];
829
$request->header('If-Modified-Since' =>
830
HTTP::Date::time2str($mtime));
827
# If the file exists, add a cache-related header
829
my ($mtime) = ( stat($file) )[9];
831
$request->header( 'If-Modified-Since' => HTTP::Date::time2str($mtime) );
833
834
my $tmpfile = "$file-$$";
835
836
my $response = $self->request($request, $tmpfile);
836
if ($response->is_success) {
838
my $file_length = (stat($tmpfile))[7];
839
my($content_length) = $response->header('Content-length');
841
if (defined $content_length and $file_length < $content_length) {
843
die "Transfer truncated: " .
844
"only $file_length out of $content_length bytes received\n";
846
elsif (defined $content_length and $file_length > $content_length) {
848
die "Content-length mismatch: " .
849
"expected $content_length bytes, got $file_length\n";
854
# Some dosish systems fail to rename if the target exists
858
rename($tmpfile, $file) or
859
die "Cannot rename '$tmpfile' to '$file': $!\n";
861
if (my $lm = $response->last_modified) {
862
# make sure the file has the same last modification time
863
utime $lm, $lm, $file;
838
# Only fetching a fresh copy of the would be considered success.
839
# If the file was not modified, "304" would returned, which
840
# is considered by HTTP::Status to be a "redirect", /not/ "success"
841
if ( $response->is_success ) {
842
my $file_length = ( stat($tmpfile) )[7];
843
my ($content_length) = $response->header('Content-length');
845
if ( defined $content_length and $file_length < $content_length ) {
847
die "Transfer truncated: " . "only $file_length out of $content_length bytes received\n";
849
elsif ( defined $content_length and $file_length > $content_length ) {
851
die "Content-length mismatch: " . "expected $content_length bytes, got $file_length\n";
853
# The file was the expected length.
855
# Replace the stale file with a fresh copy
857
# Some dosish systems fail to rename if the target exists
861
rename( $tmpfile, $file )
862
or die "Cannot rename '$tmpfile' to '$file': $!\n";
864
# make sure the file has the same last modification time
865
if ( my $lm = $response->last_modified ) {
866
utime $lm, $lm, $file;
870
# The local copy is fresh enough, so just delete the temp file
868
872
unlink($tmpfile);