headers: Add > Caddyfile shortcut for enabling defer (#5535)

This commit is contained in:
Francis Lavoie 2023-05-16 01:18:13 -04:00 committed by GitHub
parent 36546cd8b9
commit e8352aef38
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 7 deletions

View file

@ -17,6 +17,7 @@
+Link "Foo" +Link "Foo"
+Link "Bar" +Link "Bar"
} }
header >Set Defer
} }
---------- ----------
{ {
@ -136,6 +137,17 @@
] ]
} }
} }
},
{
"handler": "headers",
"response": {
"deferred": true,
"set": {
"Set": [
"Defer"
]
}
}
} }
] ]
} }

View file

@ -32,19 +32,20 @@ func init() {
// parseCaddyfile sets up the handler for response headers from // parseCaddyfile sets up the handler for response headers from
// Caddyfile tokens. Syntax: // Caddyfile tokens. Syntax:
// //
// header [<matcher>] [[+|-|?]<field> [<value|regexp>] [<replacement>]] { // header [<matcher>] [[+|-|?|>]<field> [<value|regexp>] [<replacement>]] {
// [+]<field> [<value|regexp> [<replacement>]] // [+]<field> [<value|regexp> [<replacement>]]
// ?<field> <default_value> // ?<field> <default_value>
// -<field> // -<field>
// [defer] // ><field>
// [defer]
// } // }
// //
// Either a block can be opened or a single header field can be configured // 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. Header operations // 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 // are deferred to write-time if any headers are being deleted or if the
// 'defer' subdirective is used. + appends a header value, - deletes a field, // 'defer' subdirective is used. + appends a header value, - deletes a field,
// and ? conditionally sets a value only if the header field is not already // ? conditionally sets a value only if the header field is not already set,
// set. // and > sets a field with defer enabled.
func parseCaddyfile(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error) { func parseCaddyfile(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error) {
if !h.Next() { if !h.Next() {
return nil, h.ArgErr() return nil, h.ArgErr()
@ -258,6 +259,15 @@ func applyHeaderOp(ops *HeaderOps, respHeaderOps *RespHeaderOps, field, value, r
}, },
) )
case strings.HasPrefix(field, ">"): // set (overwrite) with defer
if ops.Set == nil {
ops.Set = make(http.Header)
}
ops.Set.Set(field[1:], value)
if respHeaderOps != nil {
respHeaderOps.Deferred = true
}
default: // set (overwrite) default: // set (overwrite)
if ops.Set == nil { if ops.Set == nil {
ops.Set = make(http.Header) ops.Set = make(http.Header)