Subject: | PATCH: tidy and document the internals of mirror() better |
The following patch to mirror() makes no functional changes, but makes
the function easier to review for those who need to. The changes include:
- Running perltidy on parts of it to clarify the indentation meaning.
- Adding internal comments to clarify the code flow.
Mark
Subject: | lwp_mirror_docs.patch |
--- /usr/local/lib/perl5/site_perl/5.8.0/LWP/UserAgent.pm Fri Nov 12 06:12:04 2004
+++ /home/mark/tmp/UserAgent.pm Fri Nov 17 13:33:42 2006
@@ -670,49 +670,52 @@
LWP::Debug::trace('()');
my $request = HTTP::Request->new('GET', $url);
- if (-e $file) {
- my($mtime) = (stat($file))[9];
- if($mtime) {
- $request->header('If-Modified-Since' =>
- HTTP::Date::time2str($mtime));
- }
+ # If the file exists, add a cache-related header
+ if ( -e $file ) {
+ my ($mtime) = ( stat($file) )[9];
+ if ($mtime) {
+ $request->header( 'If-Modified-Since' => HTTP::Date::time2str($mtime) );
+ }
}
my $tmpfile = "$file-$$";
my $response = $self->request($request, $tmpfile);
- if ($response->is_success) {
- my $file_length = (stat($tmpfile))[7];
- my($content_length) = $response->header('Content-length');
+ # Only fetching a fresh copy of the would be considered success.
+ # If the file was not modified, "304" would returned, which
+ # is considered by HTTP::Status to be a "redirect", /not/ "success"
+ if ( $response->is_success ) {
+ my $file_length = ( stat($tmpfile) )[7];
+ my ($content_length) = $response->header('Content-length');
- if (defined $content_length and $file_length < $content_length) {
- unlink($tmpfile);
- die "Transfer truncated: " .
- "only $file_length out of $content_length bytes received\n";
- }
- elsif (defined $content_length and $file_length > $content_length) {
- unlink($tmpfile);
- die "Content-length mismatch: " .
- "expected $content_length bytes, got $file_length\n";
- }
- else {
- # OK
- if (-e $file) {
- # Some dosish systems fail to rename if the target exists
- chmod 0777, $file;
- unlink $file;
- }
- rename($tmpfile, $file) or
- die "Cannot rename '$tmpfile' to '$file': $!\n";
+ if ( defined $content_length and $file_length < $content_length ) {
+ unlink($tmpfile);
+ die "Transfer truncated: " . "only $file_length out of $content_length bytes received\n";
+ }
+ elsif ( defined $content_length and $file_length > $content_length ) {
+ unlink($tmpfile);
+ die "Content-length mismatch: " . "expected $content_length bytes, got $file_length\n";
+ }
+ # The file was the expected length.
+ else {
+ # Replace the stale file with a fresh copy
+ if ( -e $file ) {
+ # Some dosish systems fail to rename if the target exists
+ chmod 0777, $file;
+ unlink $file;
+ }
+ rename( $tmpfile, $file )
+ or die "Cannot rename '$tmpfile' to '$file': $!\n";
- if (my $lm = $response->last_modified) {
- # make sure the file has the same last modification time
- utime $lm, $lm, $file;
- }
- }
+ # make sure the file has the same last modification time
+ if ( my $lm = $response->last_modified ) {
+ utime $lm, $lm, $file;
+ }
+ }
}
+ # The local copy is fresh enough, so just delete the temp file
else {
- unlink($tmpfile);
+ unlink($tmpfile);
}
return $response;
}