From a63cb3e3fdea70991a95c3f0bc8f3866a5aec6ef Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Thu, 27 Jun 2019 13:09:10 -0600 Subject: Implement etag; fix related bugs in encode and templates middlewares --- modules/caddyhttp/fileserver/staticfiles.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'modules/caddyhttp/fileserver/staticfiles.go') diff --git a/modules/caddyhttp/fileserver/staticfiles.go b/modules/caddyhttp/fileserver/staticfiles.go index 49c2be4..bcf8cf7 100644 --- a/modules/caddyhttp/fileserver/staticfiles.go +++ b/modules/caddyhttp/fileserver/staticfiles.go @@ -184,7 +184,9 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) error } defer file.Close() - // TODO: Etag + // set the ETag - note that a conditional If-None-Match request is handled + // by http.ServeContent below, which checks against this ETag value + w.Header().Set("ETag", calculateEtag(info)) if w.Header().Get("Content-Type") == "" { mtyp := mime.TypeByExtension(filepath.Ext(filename)) @@ -419,6 +421,17 @@ func fileHidden(filename string, hide []string) bool { return false } +// calculateEtag produces a strong etag by default, although, for +// efficiency reasons, it does not actually consume the contents +// of the file to make a hash of all the bytes. ¯\_(ツ)_/¯ +// Prefix the etag with "W/" to convert it into a weak etag. +// See: https://tools.ietf.org/html/rfc7232#section-2.3 +func calculateEtag(d os.FileInfo) string { + t := strconv.FormatInt(d.ModTime().Unix(), 36) + s := strconv.FormatInt(d.Size(), 36) + return `"` + t + s + `"` +} + var defaultIndexNames = []string{"index.html"} const minBackoff, maxBackoff = 2, 5 -- cgit v1.2.3