1
0
mirror of https://github.com/golang/go synced 2024-10-01 16:18:32 -06:00

net/http: ignore the Unix epoch time in ServeContent

ServeContent ignored zero time.Time{} values when generating
Last-Modified response headers and checking If-Modified-Since request
headers. Do the same for a time.Time representing the Unix epoch zero
value, as this is a common bogus value. Callers who really want to
send that value (incredibly unlikely) can add a nanosecond to it and
it will be truncated to second granularity anyway.

Fixes #9842

Change-Id: I69f697bfc4017404a92a34e3fe57e2711c1e299d
Reviewed-on: https://go-review.googlesource.com/7915
Reviewed-by: David Symonds <dsymonds@golang.org>
This commit is contained in:
Brad Fitzpatrick 2015-02-12 08:42:11 -08:00
parent 25bf792197
commit a0fb8f8cce
2 changed files with 16 additions and 5 deletions

View File

@ -102,10 +102,10 @@ func dirList(w ResponseWriter, f File) {
// The name is otherwise unused; in particular it can be empty and is // The name is otherwise unused; in particular it can be empty and is
// never sent in the response. // never sent in the response.
// //
// If modtime is not the zero time, ServeContent includes it in a // If modtime is not the zero time or Unix epoch, ServeContent
// Last-Modified header in the response. If the request includes an // includes it in a Last-Modified header in the response. If the
// If-Modified-Since header, ServeContent uses modtime to decide // request includes an If-Modified-Since header, ServeContent uses
// whether the content needs to be sent at all. // modtime to decide whether the content needs to be sent at all.
// //
// The content's Seek method must work: ServeContent uses // The content's Seek method must work: ServeContent uses
// a seek to the end of the content to determine its size. // a seek to the end of the content to determine its size.
@ -258,10 +258,15 @@ func serveContent(w ResponseWriter, r *Request, name string, modtime time.Time,
} }
} }
var unixEpochTime = time.Unix(0, 0)
// modtime is the modification time of the resource to be served, or IsZero(). // modtime is the modification time of the resource to be served, or IsZero().
// return value is whether this request is now complete. // return value is whether this request is now complete.
func checkLastModified(w ResponseWriter, r *Request, modtime time.Time) bool { func checkLastModified(w ResponseWriter, r *Request, modtime time.Time) bool {
if modtime.IsZero() { if modtime.IsZero() || modtime.Equal(unixEpochTime) {
// If the file doesn't have a modtime (IsZero), or the modtime
// is obviously garbage (Unix time == 0), then ignore modtimes
// and don't process the If-Modified-Since header.
return false return false
} }

View File

@ -751,6 +751,12 @@ func TestServeContent(t *testing.T) {
wantContentType: "text/css; charset=utf-8", wantContentType: "text/css; charset=utf-8",
wantLastMod: "Wed, 25 Jun 2014 17:12:18 GMT", wantLastMod: "Wed, 25 Jun 2014 17:12:18 GMT",
}, },
"unix_zero_modtime": {
content: strings.NewReader("<html>foo"),
modtime: time.Unix(0, 0),
wantStatus: StatusOK,
wantContentType: "text/html; charset=utf-8",
},
} }
for testName, tt := range tests { for testName, tt := range tests {
var content io.ReadSeeker var content io.ReadSeeker