From 4eab3db936e1067af6896cdd9c5c8d8ff12fb1ed Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Wed, 18 Oct 2023 18:10:09 -0400 Subject: [PATCH] templates: Delete headers on `httpError` to reset to clean slate --- modules/caddyhttp/templates/tplcontext.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/modules/caddyhttp/templates/tplcontext.go b/modules/caddyhttp/templates/tplcontext.go index 63e645d37f8..8b3d6bfc486 100644 --- a/modules/caddyhttp/templates/tplcontext.go +++ b/modules/caddyhttp/templates/tplcontext.go @@ -444,6 +444,14 @@ func (c TemplateContext) funcFileStat(filename string) (fs.FileInfo, error) { // funcHTTPError returns a structured HTTP handler error. EXPERIMENTAL; SUBJECT TO CHANGE. // Example usage: `{{if not (fileExists $includeFile)}}{{httpError 404}}{{end}}` func (c TemplateContext) funcHTTPError(statusCode int) (bool, error) { + // Delete some headers that may have been set by the underlying + // handler (such as file_server) which may break the error response. + c.RespHeader.Header.Del("Content-Length") + c.RespHeader.Header.Del("Content-Type") + c.RespHeader.Header.Del("Etag") + c.RespHeader.Header.Del("Last-Modified") + c.RespHeader.Header.Del("Accept-Ranges") + return false, caddyhttp.Error(statusCode, nil) }