@marcel91200 / caddy Mirror · read-only / commits / dd86171d
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 13, 05:30 UTC

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>

黑墨水鱼 committed Mar 28, 2023 at 05:05 UTC dd86171d6723f6ebc0ddef39174b2c8d1f911f64
1 file changed +16
modules/caddyhttp/headers/headers.go
+16
@@ -192,6 +192,19 @@ type RespHeaderOps struct {
192
193 // ApplyTo applies ops to hdr using repl.
194 func (ops HeaderOps) ApplyTo(hdr http.Header, repl *caddy.Replacer) {
195 + // before manipulating headers in other ways, check if there
196 + // is configuration to delete all headers, and do that first
197 + // because if a header is to be added, we don't want to delete
198 + // it also
199 + for _, fieldName := range ops.Delete {
200 + fieldName = repl.ReplaceKnown(fieldName, "")
201 + if fieldName == "*" {
202 + for existingField := range hdr {
203 + delete(hdr, existingField)
204 + }
205 + }
206 + }
207 +
208 // add
209 for fieldName, vals := range ops.Add {
210 fieldName = repl.ReplaceKnown(fieldName, "")
@@ -215,6 +228,9 @@ func (ops HeaderOps) ApplyTo(hdr http.Header, repl *caddy.Replacer) {
228 // delete
229 for _, fieldName := range ops.Delete {
230 fieldName = strings.ToLower(repl.ReplaceKnown(fieldName, ""))
231 + if fieldName == "*" {
232 + continue // handled above
233 + }
234 switch {
235 case strings.HasPrefix(fieldName, "*") && strings.HasSuffix(fieldName, "*"):
236 for existingField := range hdr {