Skip to content

Commit cabb5d7

Browse files
committed
fileserver: Set "Vary: Accept-Encoding" header (see #5849)
1 parent ba58114 commit cabb5d7

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

modules/caddyhttp/fileserver/staticfiles.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -371,15 +371,17 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
371371
}
372372

373373
var file fs.File
374+
respHeader := w.Header()
374375

375376
// 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")
377378

378379
// static file responses are often compressed, either on-the-fly
379380
// or with precompressed sidecar files; in any case, the headers
380381
// should contain "Vary: Accept-Encoding" even when not compressed
381382
// 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")
383385

384386
// check for precompressed files
385387
for _, ae := range encode.AcceptedEncodings(r, fsrv.PrecompressedOrder) {
@@ -404,8 +406,8 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
404406
continue
405407
}
406408
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")
409411

410412
// try to get the etag from pre computed files if an etag suffix list was provided
411413
if etag == "" && fsrv.EtagFileExtensions != nil {
@@ -459,24 +461,24 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
459461
// to repeat the error; just continue because we're probably
460462
// trying to write an error page response (see issue #5703)
461463
if _, ok := r.Context().Value(caddyhttp.ErrorCtxKey).(error); !ok {
462-
w.Header().Add("Allow", "GET, HEAD")
464+
respHeader.Add("Allow", "GET, HEAD")
463465
return caddyhttp.Error(http.StatusMethodNotAllowed, nil)
464466
}
465467
}
466468

467469
// set the Etag - note that a conditional If-None-Match request is handled
468470
// by http.ServeContent below, which checks against this Etag value
469471
if etag != "" {
470-
w.Header().Set("Etag", etag)
472+
respHeader.Set("Etag", etag)
471473
}
472474

473-
if w.Header().Get("Content-Type") == "" {
475+
if respHeader.Get("Content-Type") == "" {
474476
mtyp := mime.TypeByExtension(filepath.Ext(filename))
475477
if mtyp == "" {
476478
// 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
478480
} else {
479-
w.Header().Set("Content-Type", mtyp)
481+
respHeader.Set("Content-Type", mtyp)
480482
}
481483
}
482484

0 commit comments

Comments
 (0)