gitweb: add If-Modified-Since handling to git_snapshot().

Because snapshots can be large, you can save some bandwidth by supporting caching via If-Modified-Since. This patch adds support for the i-m-s request to git_snapshot() if the request is a commit. Requests for snapshots of trees, which lack well defined timestamps, are still handled as they were before. Signed-off-by: W Trevor King <wking@drexel.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

W. Trevor King committed Mar 29, 2012 at 08:45 UTC 8745db63ca65c28221229df3d945cb10045c29dc
2 files changed +43
gitweb/gitweb.perl
+10
@@ -7051,6 +7051,10 @@ sub git_snapshot {
7051
7052 my ($name, $prefix) = snapshot_name($project, $hash);
7053 my $filename = "$name$known_snapshot_formats{$format}{'suffix'}";
7054 +
7055 + my %co = parse_commit($hash);
7056 + exit_if_unmodified_since($co{'committer_epoch'}) if %co;
7057 +
7058 my $cmd = quote_command(
7059 git_cmd(), 'archive',
7060 "--format=$known_snapshot_formats{$format}{'format'}",
@@ -7060,9 +7064,15 @@ sub git_snapshot {
7064 }
7065
7066 $filename =~ s/(["\\])/\\$1/g;
7067 + my %latest_date;
7068 + if (%co) {
7069 + %latest_date = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
7070 + }
7071 +
7072 print $cgi->header(
7073 -type => $known_snapshot_formats{$format}{'type'},
7074 -content_disposition => 'inline; filename="' . $filename . '"',
7075 + %co ? (-last_modified => $latest_date{'rfc2822'}) : (),
7076 -status => '200 OK');
7077
7078 open my $fd, "-|", $cmd
t/t9501-gitweb-standalone-http-status.sh
+33
@@ -138,6 +138,39 @@ test_expect_success 'modification: feed if-modified-since (unmodified)' '
138 '
139 test_debug 'cat gitweb.headers'
140
141 +test_expect_success 'modification: snapshot last-modified' '
142 + gitweb_run "p=.git;a=snapshot;h=master;sf=tgz" &&
143 + grep "Status: 200 OK" gitweb.headers &&
144 + grep "Last-modified: Thu, 7 Apr 2005 22:14:13 +0000" gitweb.headers
145 +'
146 +test_debug 'cat gitweb.headers'
147 +
148 +test_expect_success 'modification: snapshot if-modified-since (modified)' '
149 + export HTTP_IF_MODIFIED_SINCE="Wed, 6 Apr 2005 22:14:13 +0000" &&
150 + test_when_finished "unset HTTP_IF_MODIFIED_SINCE" &&
151 + gitweb_run "p=.git;a=snapshot;h=master;sf=tgz" &&
152 + grep "Status: 200 OK" gitweb.headers
153 +'
154 +test_debug 'cat gitweb.headers'
155 +
156 +test_expect_success 'modification: snapshot if-modified-since (unmodified)' '
157 + export HTTP_IF_MODIFIED_SINCE="Thu, 7 Apr 2005 22:14:13 +0000" &&
158 + test_when_finished "unset HTTP_IF_MODIFIED_SINCE" &&
159 + gitweb_run "p=.git;a=snapshot;h=master;sf=tgz" &&
160 + grep "Status: 304 Not Modified" gitweb.headers
161 +'
162 +test_debug 'cat gitweb.headers'
163 +
164 +test_expect_success 'modification: tree snapshot' '
165 + ID=`git rev-parse --verify HEAD^{tree}` &&
166 + export HTTP_IF_MODIFIED_SINCE="Wed, 6 Apr 2005 22:14:13 +0000" &&
167 + test_when_finished "unset HTTP_IF_MODIFIED_SINCE" &&
168 + gitweb_run "p=.git;a=snapshot;h=$ID;sf=tgz" &&
169 + grep "Status: 200 OK" gitweb.headers &&
170 + ! grep -i "last-modified" gitweb.headers
171 +'
172 +test_debug 'cat gitweb.headers'
173 +
174 # ----------------------------------------------------------------------
175 # load checking
176