]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: clean up checkIfModifiedSince and checkIfUnmodifiedSince
authorAnmol Sethi <hi@nhooyr.io>
Thu, 19 Oct 2017 01:27:48 +0000 (21:27 -0400)
committerBrad Fitzpatrick <bradfitz@golang.org>
Mon, 14 Oct 2019 21:10:00 +0000 (21:10 +0000)
The comment in both functions referred to the wrong header and I made
the checks easier to read.

Change-Id: Ifb46729cee631a3305f557863818e3487b8eed71
Reviewed-on: https://go-review.googlesource.com/c/go/+/71753
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/net/http/fs.go

index 80c391d1c308a7d61cea1a5263f48c93d8540f1a..d2144857e84300a54ba347aaead4285e0f666c26 100644 (file)
@@ -384,15 +384,18 @@ func checkIfUnmodifiedSince(r *Request, modtime time.Time) condResult {
        if ius == "" || isZeroTime(modtime) {
                return condNone
        }
-       if t, err := ParseTime(ius); err == nil {
-               // The Date-Modified header truncates sub-second precision, so
-               // use mtime < t+1s instead of mtime <= t to check for unmodified.
-               if modtime.Before(t.Add(1 * time.Second)) {
-                       return condTrue
-               }
-               return condFalse
+       t, err := ParseTime(ius)
+       if err != nil {
+               return condNone
        }
-       return condNone
+
+       // The Last-Modified header truncates sub-second precision so
+       // the modtime needs to be truncated too.
+       modtime = modtime.Truncate(time.Second)
+       if modtime.Before(t) || modtime.Equal(t) {
+               return condTrue
+       }
+       return condFalse
 }
 
 func checkIfNoneMatch(w ResponseWriter, r *Request) condResult {
@@ -436,9 +439,10 @@ func checkIfModifiedSince(r *Request, modtime time.Time) condResult {
        if err != nil {
                return condNone
        }
-       // The Date-Modified header truncates sub-second precision, so
-       // use mtime < t+1s instead of mtime <= t to check for unmodified.
-       if modtime.Before(t.Add(1 * time.Second)) {
+       // The Last-Modified header truncates sub-second precision so
+       // the modtime needs to be truncated too.
+       modtime = modtime.Truncate(time.Second)
+       if modtime.Before(t) || modtime.Equal(t) {
                return condFalse
        }
        return condTrue