Skip to content

Commit 4e8245d

Browse files
templates: Delete headers on httpError to reset to clean slate (#5905)
1 parent ac1f20b commit 4e8245d

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

modules/caddyhttp/templates/tplcontext.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,14 @@ func (c TemplateContext) funcFileStat(filename string) (fs.FileInfo, error) {
444444
// funcHTTPError returns a structured HTTP handler error. EXPERIMENTAL; SUBJECT TO CHANGE.
445445
// Example usage: `{{if not (fileExists $includeFile)}}{{httpError 404}}{{end}}`
446446
func (c TemplateContext) funcHTTPError(statusCode int) (bool, error) {
447+
// Delete some headers that may have been set by the underlying
448+
// handler (such as file_server) which may break the error response.
449+
c.RespHeader.Header.Del("Content-Length")
450+
c.RespHeader.Header.Del("Content-Type")
451+
c.RespHeader.Header.Del("Etag")
452+
c.RespHeader.Header.Del("Last-Modified")
453+
c.RespHeader.Header.Del("Accept-Ranges")
454+
447455
return false, caddyhttp.Error(statusCode, nil)
448456
}
449457

0 commit comments

Comments
 (0)