gitweb: refactor If-Modified-Since handling

The current gitweb only generates Last-Modified and handles If-Modified-Since headers for the git_feed action. This patch breaks the Last-Modified and If-Modified-Since handling code out from git_feed into a new function exit_if_unmodified_since. This makes the code easy to reuse for other actions. Only gitweb actions which can easily calculate a modification time should use exit_if_unmodified_since, as the goal is to balance local processing time vs. upload bandwidth. Signed-off-by: W Trevor King <wking@drexel.edu> Acked-by: Jakub Narebski <jnareb@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

W. Trevor King committed Mar 29, 2012 at 08:45 UTC b7d565ea4c2bd6d0c79e512eeca77beb15736f80
2 files changed +55 -29
gitweb/gitweb.perl
+29 -28
@@ -7003,6 +7003,28 @@ sub snapshot_name {
7003 return wantarray ? ($name, $name) : $name;
7004 }
7005
7006 +sub exit_if_unmodified_since {
7007 + my ($latest_epoch) = @_;
7008 + our $cgi;
7009 +
7010 + my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
7011 + if (defined $if_modified) {
7012 + my $since;
7013 + if (eval { require HTTP::Date; 1; }) {
7014 + $since = HTTP::Date::str2time($if_modified);
7015 + } elsif (eval { require Time::ParseDate; 1; }) {
7016 + $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
7017 + }
7018 + if (defined $since && $latest_epoch <= $since) {
7019 + my %latest_date = parse_date($latest_epoch);
7020 + print $cgi->header(
7021 + -last_modified => $latest_date{'rfc2822'},
7022 + -status => '304 Not Modified');
7023 + goto DONE_GITWEB;
7024 + }
7025 + }
7026 +}
7027 +
7028 sub git_snapshot {
7029 my $format = $input_params{'snapshot_format'};
7030 if (!@snapshot_fmts) {
@@ -7820,35 +7842,14 @@ sub git_feed {
7842 if (defined($commitlist[0])) {
7843 %latest_commit = %{$commitlist[0]};
7844 my $latest_epoch = $latest_commit{'committer_epoch'};
7823 - %latest_date = parse_date($latest_epoch, $latest_commit{'comitter_tz'});
7824 - my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
7825 - if (defined $if_modified) {
7826 - my $since;
7827 - if (eval { require HTTP::Date; 1; }) {
7828 - $since = HTTP::Date::str2time($if_modified);
7829 - } elsif (eval { require Time::ParseDate; 1; }) {
7830 - $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
7831 - }
7832 - if (defined $since && $latest_epoch <= $since) {
7833 - print $cgi->header(
7834 - -type => $content_type,
7835 - -charset => 'utf-8',
7836 - -last_modified => $latest_date{'rfc2822'},
7837 - -status => '304 Not Modified');
7838 - return;
7839 - }
7840 - }
7841 - print $cgi->header(
7842 - -type => $content_type,
7843 - -charset => 'utf-8',
7844 - -last_modified => $latest_date{'rfc2822'},
7845 - -status => '200 OK');
7846 - } else {
7847 - print $cgi->header(
7848 - -type => $content_type,
7849 - -charset => 'utf-8',
7850 - -status => '200 OK');
7845 + exit_if_unmodified_since($latest_epoch);
7846 + %latest_date = parse_date($latest_epoch, $latest_commit{'comitter_tz'});
7847 }
7848 + print $cgi->header(
7849 + -type => $content_type,
7850 + -charset => 'utf-8',
7851 + %latest_date ? (-last_modified => $latest_date{'rfc2822'}) : (),
7852 + -status => '200 OK');
7853
7854 # Optimization: skip generating the body if client asks only
7855 # for Last-Modified date.
t/t9501-gitweb-standalone-http-status.sh
+26 -1
@@ -92,7 +92,7 @@ test_debug 'cat gitweb.output'
92 test_expect_success 'snapshots: bad tree-ish id (tagged object)' '
93 echo object > tag-object &&
94 git add tag-object &&
95 - git commit -m "Object to be tagged" &&
95 + test_tick && git commit -m "Object to be tagged" &&
96 git tag tagged-object `git hash-object tag-object` &&
97 gitweb_run "p=.git;a=snapshot;h=tagged-object;sf=tgz" &&
98 grep "400 - Object is not a tree-ish" gitweb.output
@@ -112,6 +112,31 @@ test_expect_success 'snapshots: bad object id' '
112 '
113 test_debug 'cat gitweb.output'
114
115 +# ----------------------------------------------------------------------
116 +# modification times (Last-Modified and If-Modified-Since)
117 +
118 +test_expect_success 'modification: feed last-modified' '
119 + gitweb_run "p=.git;a=atom;h=master" &&
120 + grep "Status: 200 OK" gitweb.headers &&
121 + grep "Last-modified: Thu, 7 Apr 2005 22:14:13 +0000" gitweb.headers
122 +'
123 +test_debug 'cat gitweb.headers'
124 +
125 +test_expect_success 'modification: feed if-modified-since (modified)' '
126 + export HTTP_IF_MODIFIED_SINCE="Wed, 6 Apr 2005 22:14:13 +0000" &&
127 + test_when_finished "unset HTTP_IF_MODIFIED_SINCE" &&
128 + gitweb_run "p=.git;a=atom;h=master" &&
129 + grep "Status: 200 OK" gitweb.headers
130 +'
131 +test_debug 'cat gitweb.headers'
132 +
133 +test_expect_success 'modification: feed if-modified-since (unmodified)' '
134 + export HTTP_IF_MODIFIED_SINCE="Thu, 7 Apr 2005 22:14:13 +0000" &&
135 + test_when_finished "unset HTTP_IF_MODIFIED_SINCE" &&
136 + gitweb_run "p=.git;a=atom;h=master" &&
137 + grep "Status: 304 Not Modified" gitweb.headers
138 +'
139 +test_debug 'cat gitweb.headers'
140
141 # ----------------------------------------------------------------------
142 # load checking