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, 16:45 UTC
headers: Support wildcards for delete ops (close #4830) (#4831)
Matt Holt committed
Jun 15, 2022 at 09:57 UTC
0bcd02d5f669869b0c79c00ea3c5a3f6a7072b20
2 files changed
+51
-3
modules/caddyhttp/headers/headers.go
+31
-3
@@ -118,10 +118,16 @@ type HeaderOps struct {
118
// Sets HTTP headers; replaces existing header fields.
119
Set http.Header `json:"set,omitempty"`
120
121
- // Names of HTTP header fields to delete.
121
+ // Names of HTTP header fields to delete. Basic wildcards are supported:
122
+ //
123
+ // - Start with `*` for all field names with the given suffix;
124
+ // - End with `*` for all field names with the given prefix;
125
+ // - Start and end with `*` for all field names containing a substring.
126
Delete []string `json:"delete,omitempty"`
127
124
- // Performs substring replacements of HTTP headers in-situ.
128
+ // Performs in-situ substring replacements of HTTP headers.
129
+ // Keys are the field names on which to perform the associated replacements.
130
+ // If the field name is `*`, the replacements are performed on all header fields.
131
Replace map[string][]Replacement `json:"replace,omitempty"`
132
}
133
@@ -208,7 +214,29 @@ func (ops HeaderOps) ApplyTo(hdr http.Header, repl *caddy.Replacer) {
214
215
// delete
216
for _, fieldName := range ops.Delete {
211
- hdr.Del(repl.ReplaceAll(fieldName, ""))
217
+ fieldName = strings.ToLower(repl.ReplaceAll(fieldName, ""))
218
+ switch {
219
+ case strings.HasPrefix(fieldName, "*") && strings.HasSuffix(fieldName, "*"):
220
+ for existingField := range hdr {
221
+ if strings.Contains(strings.ToLower(existingField), fieldName[1:len(fieldName)-1]) {
222
+ delete(hdr, existingField)
223
+ }
224
+ }
225
+ case strings.HasPrefix(fieldName, "*"):
226
+ for existingField := range hdr {
227
+ if strings.HasSuffix(strings.ToLower(existingField), fieldName[1:]) {
228
+ delete(hdr, existingField)
229
+ }
230
+ }
231
+ case strings.HasSuffix(fieldName, "*"):
232
+ for existingField := range hdr {
233
+ if strings.HasPrefix(strings.ToLower(existingField), fieldName[:len(fieldName)-1]) {
234
+ delete(hdr, existingField)
235
+ }
236
+ }
237
+ default:
238
+ hdr.Del(fieldName)
239
+ }
240
}
241
242
// replace
modules/caddyhttp/headers/headers_test.go
+20
@@ -79,6 +79,26 @@ func TestHandler(t *testing.T) {
79
"Keep-Me": []string{"i swear i'm innocent"},
80
},
81
},
82
+ {
83
+ handler: Handler{
84
+ Request: &HeaderOps{
85
+ Delete: []string{
86
+ "*-suffix",
87
+ "prefix-*",
88
+ "*_*",
89
+ },
90
+ },
91
+ },
92
+ reqHeader: http.Header{
93
+ "Header-Suffix": []string{"lalala"},
94
+ "Prefix-Test": []string{"asdf"},
95
+ "Host_Header": []string{"silly django... sigh"}, // see issue #4830
96
+ "Keep-Me": []string{"foofoofoo"},
97
+ },
98
+ expectedReqHeader: http.Header{
99
+ "Keep-Me": []string{"foofoofoo"},
100
+ },
101
+ },
102
{
103
handler: Handler{
104
Request: &HeaderOps{