Skip to content

Commit

Permalink
If user sets an etag before request reaches staticfiles, do not overw…
Browse files Browse the repository at this point in the history
…rite it (#5546)
  • Loading branch information
charles-dyfis-net committed May 19, 2023
1 parent 5336bc0 commit 5e93551
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions modules/caddyhttp/fileserver/staticfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,9 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
}

var file fs.File
var etag string

// etag is usually unset, but if the user knows what they're doing, let them override it
etag := w.Header().Get("Etag")

// check for precompressed files
for _, ae := range encode.AcceptedEncodings(r, fsrv.PrecompressedOrder) {
Expand Down Expand Up @@ -388,7 +390,9 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
// don't assign info = compressedInfo because sidecars are kind
// of transparent; however we do need to set the Etag:
// https://caddy.community/t/gzipped-sidecar-file-wrong-same-etag/16793
etag = calculateEtag(compressedInfo)
if etag == "" {
etag = calculateEtag(compressedInfo)
}

break
}
Expand All @@ -408,7 +412,9 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
}
defer file.Close()

etag = calculateEtag(info)
if etag == "" {
etag = calculateEtag(info)
}
}

// at this point, we're serving a file; Go std lib supports only
Expand All @@ -421,7 +427,9 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c

// 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", etag)
if etag != "" {
w.Header().Set("Etag", etag)
}

if w.Header().Get("Content-Type") == "" {
mtyp := mime.TypeByExtension(filepath.Ext(filename))
Expand Down

0 comments on commit 5e93551

Please sign in to comment.