Skip to content

Commit

Permalink
Avoid adding a Last-Modified header when mtime is 1
Browse files Browse the repository at this point in the history
Patch by Francis Lavoie, taken from github comment at caddyserver#5556 (comment); committed by Charles Duffy
  • Loading branch information
francislavoie authored and charles-dyfis-net committed May 23, 2023
1 parent 0609137 commit 19df75f
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion modules/caddyhttp/fileserver/staticfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,11 +484,23 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
w = statusOverrideResponseWriter{ResponseWriter: w, code: statusCodeOverride}
}

// Nix uses modtime=1 for files in its store, and it's useless to set
// the Last-Modified header in that case, so we need to check if the
// header was set by anything else first (i.e. user config or another
// module) so that we can later check if http.ServeContent set the header.
modTime := info.ModTime()
beforeLastModified := w.Header().Get("Last-Modified")

// let the standard library do what it does best; note, however,
// that errors generated by ServeContent are written immediately
// to the response, so we cannot handle them (but errors there
// are rare)
http.ServeContent(w, r, info.Name(), info.ModTime(), file.(io.ReadSeeker))
http.ServeContent(w, r, info.Name(), modTime, file.(io.ReadSeeker))

// Delete the Last-Modified header if it was changed and modtime=1 (i.e. Nix store)
if beforeLastModified != w.Header().Get("Last-Modified") && modTime.Unix() == 1 {
w.Header().Del("Last-Modified")
}

return nil
}
Expand Down

0 comments on commit 19df75f

Please sign in to comment.