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

fileserver: Set "Vary: Accept-Encoding" header (see #5849)

Matthew Holt committed Apr 26, 2024 at 19:38 UTC cabb5d71c493f4502df74f346b9d7e7fe4497c99
1 file changed +11 -9
modules/caddyhttp/fileserver/staticfiles.go
+11 -9
@@ -371,15 +371,17 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
371 }
372
373 var file fs.File
374 + respHeader := w.Header()
375
376 // etag is usually unset, but if the user knows what they're doing, let them override it
376 - etag := w.Header().Get("Etag")
377 + etag := respHeader.Get("Etag")
378
379 // static file responses are often compressed, either on-the-fly
380 // or with precompressed sidecar files; in any case, the headers
381 // should contain "Vary: Accept-Encoding" even when not compressed
382 // so caches can craft a reliable key (according to REDbot results)
382 - w.Header().Add("Vary", "Accept-Encoding")
383 + // see #5849
384 + respHeader.Add("Vary", "Accept-Encoding")
385
386 // check for precompressed files
387 for _, ae := range encode.AcceptedEncodings(r, fsrv.PrecompressedOrder) {
@@ -404,8 +406,8 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
406 continue
407 }
408 defer file.Close()
407 - w.Header().Set("Content-Encoding", ae)
408 - w.Header().Del("Accept-Ranges")
409 + respHeader.Set("Content-Encoding", ae)
410 + respHeader.Del("Accept-Ranges")
411
412 // try to get the etag from pre computed files if an etag suffix list was provided
413 if etag == "" && fsrv.EtagFileExtensions != nil {
@@ -459,7 +461,7 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
461 // to repeat the error; just continue because we're probably
462 // trying to write an error page response (see issue #5703)
463 if _, ok := r.Context().Value(caddyhttp.ErrorCtxKey).(error); !ok {
462 - w.Header().Add("Allow", "GET, HEAD")
464 + respHeader.Add("Allow", "GET, HEAD")
465 return caddyhttp.Error(http.StatusMethodNotAllowed, nil)
466 }
467 }
@@ -467,16 +469,16 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
469 // set the Etag - note that a conditional If-None-Match request is handled
470 // by http.ServeContent below, which checks against this Etag value
471 if etag != "" {
470 - w.Header().Set("Etag", etag)
472 + respHeader.Set("Etag", etag)
473 }
474
473 - if w.Header().Get("Content-Type") == "" {
475 + if respHeader.Get("Content-Type") == "" {
476 mtyp := mime.TypeByExtension(filepath.Ext(filename))
477 if mtyp == "" {
478 // do not allow Go to sniff the content-type; see https://www.youtube.com/watch?v=8t8JYpt0egE
477 - w.Header()["Content-Type"] = nil
479 + respHeader["Content-Type"] = nil
480 } else {
479 - w.Header().Set("Content-Type", mtyp)
481 + respHeader.Set("Content-Type", mtyp)
482 }
483 }
484