@@ -371,15 +371,17 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
371
371
}
372
372
373
373
var file fs.File
374
+ respHeader := w .Header ()
374
375
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" )
377
378
378
379
// static file responses are often compressed, either on-the-fly
379
380
// or with precompressed sidecar files; in any case, the headers
380
381
// should contain "Vary: Accept-Encoding" even when not compressed
381
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" )
383
385
384
386
// check for precompressed files
385
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
404
406
continue
405
407
}
406
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" )
409
411
410
412
// try to get the etag from pre computed files if an etag suffix list was provided
411
413
if etag == "" && fsrv .EtagFileExtensions != nil {
@@ -459,24 +461,24 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
459
461
// to repeat the error; just continue because we're probably
460
462
// trying to write an error page response (see issue #5703)
461
463
if _ , ok := r .Context ().Value (caddyhttp .ErrorCtxKey ).(error ); ! ok {
462
- w . Header () .Add ("Allow" , "GET, HEAD" )
464
+ respHeader .Add ("Allow" , "GET, HEAD" )
463
465
return caddyhttp .Error (http .StatusMethodNotAllowed , nil )
464
466
}
465
467
}
466
468
467
469
// set the Etag - note that a conditional If-None-Match request is handled
468
470
// by http.ServeContent below, which checks against this Etag value
469
471
if etag != "" {
470
- w . Header () .Set ("Etag" , etag )
472
+ respHeader .Set ("Etag" , etag )
471
473
}
472
474
473
- if w . Header () .Get ("Content-Type" ) == "" {
475
+ if respHeader .Get ("Content-Type" ) == "" {
474
476
mtyp := mime .TypeByExtension (filepath .Ext (filename ))
475
477
if mtyp == "" {
476
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
478
480
} else {
479
- w . Header () .Set ("Content-Type" , mtyp )
481
+ respHeader .Set ("Content-Type" , mtyp )
480
482
}
481
483
}
482
484
0 commit comments