summaryrefslogtreecommitdiff
path: root/modules/caddyhttp
diff options
context:
space:
mode:
author黑墨水鱼 <heimoshuiyu@gmail.com>2023-03-28 05:05:18 +0800
committerGitHub <noreply@github.com>2023-03-27 21:05:18 +0000
commitdd86171d6723f6ebc0ddef39174b2c8d1f911f64 (patch)
treeee77ca726d2bd3f7630c1776691606b6290432dc /modules/caddyhttp
parentf5a13a4ab4e1dd1066fa81fb044127ae18c4f001 (diff)
headers: Support deleting all headers as first op (#5464)
* Delete all existing fields when fieldName is `*` * Rearrange deletion before addition in headers * Revert "Rearrange deletion before addition in headers" This reverts commit 1b50eeeccc92ccd660c7896d8283c7d9e5d1fcb0. * Treat deleting all headers as a special case * Apply suggestions from code review Co-authored-by: Matt Holt <mholt@users.noreply.github.com> --------- Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
Diffstat (limited to 'modules/caddyhttp')
-rw-r--r--modules/caddyhttp/headers/headers.go16
1 files changed, 16 insertions, 0 deletions
diff --git a/modules/caddyhttp/headers/headers.go b/modules/caddyhttp/headers/headers.go
index f8d3fdc..ac1ab94 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 {