summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/fileserver
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2019-06-27 13:09:10 -0600
committerMatthew Holt <mholt@users.noreply.github.com>2019-06-27 13:09:10 -0600
commita63cb3e3fdea70991a95c3f0bc8f3866a5aec6ef (patch)
treedbb40f0f0edb26ab3e16b6671b155f8cd0785728 /modules/caddyhttp/fileserver
parent2b22d2e6ea7ffd17ae769bd8a2adae60e5a7d0bf (diff)
Implement etag; fix related bugs in encode and templates middlewares
Diffstat (limited to 'modules/caddyhttp/fileserver')
-rw-r--r--modules/caddyhttp/fileserver/staticfiles.go15
1 files changed, 14 insertions, 1 deletions
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