Skip to content

Commit 8745db6

Browse files
W. Trevor Kinggitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b7d565e commit 8745db6

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

gitweb/gitweb.perl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7051,6 +7051,10 @@ sub git_snapshot {
70517051

70527052
my ($name, $prefix) = snapshot_name($project, $hash);
70537053
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+
70547058
my $cmd = quote_command(
70557059
git_cmd(), 'archive',
70567060
"--format=$known_snapshot_formats{$format}{'format'}",
@@ -7060,9 +7064,15 @@ sub git_snapshot {
70607064
}
70617065

70627066
$filename =~ s/(["\\])/\\$1/g;
7067+
my %latest_date;
7068+
if (%co) {
7069+
%latest_date = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
7070+
}
7071+
70637072
print $cgi->header(
70647073
-type => $known_snapshot_formats{$format}{'type'},
70657074
-content_disposition => 'inline; filename="' . $filename . '"',
7075+
%co ? (-last_modified => $latest_date{'rfc2822'}) : (),
70667076
-status => '200 OK');
70677077

70687078
open my $fd, "-|", $cmd

t/t9501-gitweb-standalone-http-status.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,39 @@ test_expect_success 'modification: feed if-modified-since (unmodified)' '
138138
'
139139
test_debug 'cat gitweb.headers'
140140

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+
141174
# ----------------------------------------------------------------------
142175
# load checking
143176

0 commit comments

Comments
 (0)