@marcel91200 / caddy Mirror · read-only / commits / 7ab61f46
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, 15:30 UTC

fileserver: Better fix for Etag of compressed files

Matthew Holt committed Aug 8, 2022 at 13:09 UTC 7ab61f46f0db1ee63c6c691def61ca2a9ebee332
1 file changed +13 -4
modules/caddyhttp/fileserver/staticfiles.go
+13 -4
@@ -351,6 +351,7 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
351 }
352
353 var file fs.File
354 + var etag string
355
356 // check for precompressed files
357 for _, ae := range encode.AcceptedEncodings(r, fsrv.PrecompressedOrder) {
@@ -371,13 +372,19 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
372 if caddyErr, ok := err.(caddyhttp.HandlerError); ok && caddyErr.StatusCode == http.StatusServiceUnavailable {
373 return err
374 }
375 + file = nil
376 continue
377 }
378 defer file.Close()
377 - info = compressedInfo
379 w.Header().Set("Content-Encoding", ae)
380 w.Header().Del("Accept-Ranges")
381 w.Header().Add("Vary", "Accept-Encoding")
382 +
383 + // don't assign info = compressedInfo because sidecars are kind
384 + // of transparent; however we do need to set the Etag:
385 + // https://caddy.community/t/gzipped-sidecar-file-wrong-same-etag/16793
386 + etag = calculateEtag(compressedInfo)
387 +
388 break
389 }
390
@@ -395,11 +402,13 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
402 return err // error is already structured
403 }
404 defer file.Close()
405 +
406 + etag = calculateEtag(info)
407 }
408
400 - // set the ETag - note that a conditional If-None-Match request is handled
401 - // by http.ServeContent below, which checks against this ETag value
402 - w.Header().Set("Etag", calculateEtag(info))
409 + // set the Etag - note that a conditional If-None-Match request is handled
410 + // by http.ServeContent below, which checks against this Etag value
411 + w.Header().Set("Etag", etag)
412
413 if w.Header().Get("Content-Type") == "" {
414 mtyp := mime.TypeByExtension(filepath.Ext(filename))