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>
This commit is contained in:
黑墨水鱼 2023-03-28 05:05:18 +08:00 committed by GitHub
parent f5a13a4ab4
commit dd86171d67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -192,6 +192,19 @@ type RespHeaderOps struct {
// ApplyTo applies ops to hdr using repl. // ApplyTo applies ops to hdr using repl.
func (ops HeaderOps) ApplyTo(hdr http.Header, repl *caddy.Replacer) { 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 // add
for fieldName, vals := range ops.Add { for fieldName, vals := range ops.Add {
fieldName = repl.ReplaceKnown(fieldName, "") fieldName = repl.ReplaceKnown(fieldName, "")
@ -215,6 +228,9 @@ func (ops HeaderOps) ApplyTo(hdr http.Header, repl *caddy.Replacer) {
// delete // delete
for _, fieldName := range ops.Delete { for _, fieldName := range ops.Delete {
fieldName = strings.ToLower(repl.ReplaceKnown(fieldName, "")) fieldName = strings.ToLower(repl.ReplaceKnown(fieldName, ""))
if fieldName == "*" {
continue // handled above
}
switch { switch {
case strings.HasPrefix(fieldName, "*") && strings.HasSuffix(fieldName, "*"): case strings.HasPrefix(fieldName, "*") && strings.HasSuffix(fieldName, "*"):
for existingField := range hdr { for existingField := range hdr {