summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/headers
diff options
context:
space:
mode:
authorTom Barrett <tom@tombarrett.xyz>2023-11-01 17:57:48 +0100
committerTom Barrett <tom@tombarrett.xyz>2023-11-01 18:11:33 +0100
commit240c3d1338415e5d82ef7ca0e52c4284be6441bd (patch)
tree4b0ee5d208c2cdffa78d65f1b0abe0ec85f15652 /modules/caddyhttp/headers
parent73e78ab226f21e6c6c68961af88c4ab9c746f4f4 (diff)
parent0e204b730aa2b1fa0835336b1117eff8c420f713 (diff)
vbump to v2.7.5
Diffstat (limited to 'modules/caddyhttp/headers')
-rw-r--r--modules/caddyhttp/headers/caddyfile.go30
-rw-r--r--modules/caddyhttp/headers/headers.go18
2 files changed, 39 insertions, 9 deletions
diff --git a/modules/caddyhttp/headers/caddyfile.go b/modules/caddyhttp/headers/caddyfile.go
index a6bec95..2b06910 100644
--- a/modules/caddyhttp/headers/caddyfile.go
+++ b/modules/caddyhttp/headers/caddyfile.go
@@ -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()
@@ -246,10 +247,14 @@ func applyHeaderOp(ops *HeaderOps, respHeaderOps *RespHeaderOps, field, value, r
respHeaderOps.Set.Set(field, value)
case replacement != "": // replace
+ // allow defer shortcut for replace syntax
+ if strings.HasPrefix(field, ">") && respHeaderOps != nil {
+ respHeaderOps.Deferred = true
+ }
if ops.Replace == nil {
ops.Replace = make(map[string][]Replacement)
}
- field = strings.TrimLeft(field, "+-?")
+ field = strings.TrimLeft(field, "+-?>")
ops.Replace[field] = append(
ops.Replace[field],
Replacement{
@@ -258,6 +263,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)
diff --git a/modules/caddyhttp/headers/headers.go b/modules/caddyhttp/headers/headers.go
index f8d3fdc..ed503ef 100644
--- a/modules/caddyhttp/headers/headers.go
+++ b/modules/caddyhttp/headers/headers.go
@@ -192,6 +192,19 @@ type RespHeaderOps struct {
// ApplyTo applies ops to hdr using repl.
func (ops HeaderOps) ApplyTo(hdr http.Header, repl *caddy.Replacer) {
+ // before manipulating headers in other ways, check if there
+ // is configuration to delete all headers, and do that first
+ // because if a header is to be added, we don't want to delete
+ // it also
+ for _, fieldName := range ops.Delete {
+ fieldName = repl.ReplaceKnown(fieldName, "")
+ if fieldName == "*" {
+ for existingField := range hdr {
+ delete(hdr, existingField)
+ }
+ }
+ }
+
// add
for fieldName, vals := range ops.Add {
fieldName = repl.ReplaceKnown(fieldName, "")
@@ -215,6 +228,9 @@ func (ops HeaderOps) ApplyTo(hdr http.Header, repl *caddy.Replacer) {
// delete
for _, fieldName := range ops.Delete {
fieldName = strings.ToLower(repl.ReplaceKnown(fieldName, ""))
+ if fieldName == "*" {
+ continue // handled above
+ }
switch {
case strings.HasPrefix(fieldName, "*") && strings.HasSuffix(fieldName, "*"):
for existingField := range hdr {
@@ -355,5 +371,5 @@ func (rww *responseWriterWrapper) Write(d []byte) (int, error) {
var (
_ caddy.Provisioner = (*Handler)(nil)
_ caddyhttp.MiddlewareHandler = (*Handler)(nil)
- _ caddyhttp.HTTPInterfaces = (*responseWriterWrapper)(nil)
+ _ http.ResponseWriter = (*responseWriterWrapper)(nil)
)