Skip to content

Commit

Permalink
templates: Delete headers on httpError to reset to clean slate (#5905)
Browse files Browse the repository at this point in the history
  • Loading branch information
francislavoie committed Oct 18, 2023
1 parent ac1f20b commit 4e8245d
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions modules/caddyhttp/templates/tplcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 4e8245d

Please sign in to comment.