@marcel91200 / caddy Mirror · read-only / commits / 2615c9c5
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 13, 01:45 UTC

fileserver: Only set Etag if not already set (fix #5546) (#5547)

Charles Duffy committed May 20, 2023 at 18:21 UTC 2615c9c52417d6ac01a56841b4684400b33f7db1
1 file changed +12 -4
modules/caddyhttp/fileserver/staticfiles.go
+12 -4
@@ -356,7 +356,9 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
356 }
357
358 var file fs.File
359 - var etag string
359 +
360 + // etag is usually unset, but if the user knows what they're doing, let them override it
361 + etag := w.Header().Get("Etag")
362
363 // check for precompressed files
364 for _, ae := range encode.AcceptedEncodings(r, fsrv.PrecompressedOrder) {
@@ -388,7 +390,9 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
390 // don't assign info = compressedInfo because sidecars are kind
391 // of transparent; however we do need to set the Etag:
392 // https://caddy.community/t/gzipped-sidecar-file-wrong-same-etag/16793
391 - etag = calculateEtag(compressedInfo)
393 + if etag == "" {
394 + etag = calculateEtag(compressedInfo)
395 + }
396
397 break
398 }
@@ -408,7 +412,9 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
412 }
413 defer file.Close()
414
411 - etag = calculateEtag(info)
415 + if etag == "" {
416 + etag = calculateEtag(info)
417 + }
418 }
419
420 // at this point, we're serving a file; Go std lib supports only
@@ -421,7 +427,9 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
427
428 // set the Etag - note that a conditional If-None-Match request is handled
429 // by http.ServeContent below, which checks against this Etag value
424 - w.Header().Set("Etag", etag)
430 + if etag != "" {
431 + w.Header().Set("Etag", etag)
432 + }
433
434 if w.Header().Get("Content-Type") == "" {
435 mtyp := mime.TypeByExtension(filepath.Ext(filename))