header: caddyfile: Defer header operations for deletions or manually

See https://caddy.community/t/caddy-server-that-returns-only-ip-address-as-text/6928/6?u=matt

In most cases, we will want to apply header operations immediately,
rather than waiting until the response is written. The exceptions are
generally going to be if we are deleting a header field or if a field is
to be overwritten. We now automatically defer header ops if deleting a
header field, and allow the user to manually enable deferred mode with
the defer subdirective.
This commit is contained in:
Matthew Holt 2020-02-04 11:05:32 -07:00
parent 3592e59399
commit 9639fe7d28
No known key found for this signature in database
GPG Key ID: 2A349DD577D586A5
2 changed files with 17 additions and 3 deletions

View File

@ -33,10 +33,13 @@ func init() {
// header [<matcher>] [[+|-]<field> [<value|regexp>] [<replacement>]] {
// [+]<field> [<value|regexp> [<replacement>]]
// -<field>
// [defer]
// }
//
// Either a block can be opened or a single header field can be configured
// in the first line, but not both in the same directive.
// in the first line, but not both in the same directive. Header operations
// are deferred to write-time if any headers are being deleted or if the
// 'defer' subdirective is used.
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
hdr := new(Handler)
@ -44,7 +47,6 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
if hdr.Response == nil {
hdr.Response = &RespHeaderOps{
HeaderOps: new(HeaderOps),
Deferred: true,
}
}
}
@ -64,14 +66,21 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
}
makeResponseOps()
CaddyfileHeaderOp(hdr.Response.HeaderOps, field, value, replacement)
if len(hdr.Response.HeaderOps.Delete) > 0 {
hdr.Response.Deferred = true
}
}
// if not, they should be in a block
for h.NextBlock(0) {
field := h.Val()
if field == "defer" {
hdr.Response.Deferred = true
continue
}
if hasArgs {
return nil, h.Err("cannot specify headers in both arguments and block")
}
field := h.Val()
var value, replacement string
if h.NextArg() {
value = h.Val()
@ -81,6 +90,9 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
}
makeResponseOps()
CaddyfileHeaderOp(hdr.Response.HeaderOps, field, value, replacement)
if len(hdr.Response.HeaderOps.Delete) > 0 {
hdr.Response.Deferred = true
}
}
}

View File

@ -178,6 +178,8 @@ type RespHeaderOps struct {
// If true, header operations will be deferred until
// they are written out. Superceded if Require is set.
// Usually you will need to set this to true if any
// fields are being deleted.
Deferred bool `json:"deferred,omitempty"`
}