Skip to content

Commit

Permalink
headers: Add > Caddyfile shortcut for enabling defer (#5535)
Browse files Browse the repository at this point in the history
  • Loading branch information
francislavoie authored May 16, 2023
1 parent 36546cd commit e8352ae
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
12 changes: 12 additions & 0 deletions caddytest/integration/caddyfile_adapt/header.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
+Link "Foo"
+Link "Bar"
}
header >Set Defer
}
----------
{
Expand Down Expand Up @@ -136,6 +137,17 @@
]
}
}
},
{
"handler": "headers",
"response": {
"deferred": true,
"set": {
"Set": [
"Defer"
]
}
}
}
]
}
Expand Down
24 changes: 17 additions & 7 deletions modules/caddyhttp/headers/caddyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,20 @@ func init() {
// parseCaddyfile sets up the handler for response headers from
// Caddyfile tokens. Syntax:
//
// header [<matcher>] [[+|-|?]<field> [<value|regexp>] [<replacement>]] {
// [+]<field> [<value|regexp> [<replacement>]]
// ?<field> <default_value>
// -<field>
// [defer]
// header [<matcher>] [[+|-|?|>]<field> [<value|regexp>] [<replacement>]] {
// [+]<field> [<value|regexp> [<replacement>]]
// ?<field> <default_value>
// -<field>
// ><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. Header operations
// 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,
// and ? conditionally sets a value only if the header field is not already
// set.
// ? conditionally sets a value only if the header field is not already set,
// and > sets a field with defer enabled.
func parseCaddyfile(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error) {
if !h.Next() {
return nil, h.ArgErr()
Expand Down Expand Up @@ -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)
if ops.Set == nil {
ops.Set = make(http.Header)
Expand Down

0 comments on commit e8352ae

Please sign in to comment.