0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00

Filter empty headers (#1239)

* Filter empty headers

Some web servers (e.g. Jetty 9.3) don’t like HTTP headers with empty values. This commit filters header replacements with zero length.

* Extend tests to verify removal of empty headers

* Handle add-header case

* Change - Use short variable assignment
This commit is contained in:
Gyula Voros 2016-11-17 05:41:53 +01:00 committed by Matt Holt
parent c972ea39c8
commit b783caaaed
2 changed files with 20 additions and 2 deletions

View file

@ -287,12 +287,18 @@ func mutateHeadersByRules(headers, rules http.Header, repl httpserver.Replacer)
for ruleField, ruleValues := range rules { for ruleField, ruleValues := range rules {
if strings.HasPrefix(ruleField, "+") { if strings.HasPrefix(ruleField, "+") {
for _, ruleValue := range ruleValues { for _, ruleValue := range ruleValues {
headers.Add(strings.TrimPrefix(ruleField, "+"), repl.Replace(ruleValue)) replacement := repl.Replace(ruleValue)
if len(replacement) > 0 {
headers.Add(strings.TrimPrefix(ruleField, "+"), replacement)
}
} }
} else if strings.HasPrefix(ruleField, "-") { } else if strings.HasPrefix(ruleField, "-") {
headers.Del(strings.TrimPrefix(ruleField, "-")) headers.Del(strings.TrimPrefix(ruleField, "-"))
} else if len(ruleValues) > 0 { } else if len(ruleValues) > 0 {
headers.Set(ruleField, repl.Replace(ruleValues[len(ruleValues)-1])) replacement := repl.Replace(ruleValues[len(ruleValues)-1])
if len(replacement) > 0 {
headers.Set(ruleField, replacement)
}
} }
} }
} }

View file

@ -407,8 +407,10 @@ func TestUpstreamHeadersUpdate(t *testing.T) {
"Upgrade": {"{>Upgrade}"}, "Upgrade": {"{>Upgrade}"},
"+Merge-Me": {"Merge-Value"}, "+Merge-Me": {"Merge-Value"},
"+Add-Me": {"Add-Value"}, "+Add-Me": {"Add-Value"},
"+Add-Empty": {"{}"},
"-Remove-Me": {""}, "-Remove-Me": {""},
"Replace-Me": {"{hostname}"}, "Replace-Me": {"{hostname}"},
"Clear-Me": {""},
"Host": {"{>Host}"}, "Host": {"{>Host}"},
} }
// set up proxy // set up proxy
@ -448,6 +450,11 @@ func TestUpstreamHeadersUpdate(t *testing.T) {
headerKey, expect, got) headerKey, expect, got)
} }
headerKey = "Add-Empty"
if _, ok := actualHeaders[headerKey]; ok {
t.Errorf("Request sent to upstream backend should not contain empty %v header", headerKey)
}
headerKey = "Remove-Me" headerKey = "Remove-Me"
if _, ok := actualHeaders[headerKey]; ok { if _, ok := actualHeaders[headerKey]; ok {
t.Errorf("Request sent to upstream backend should not contain %v header", headerKey) t.Errorf("Request sent to upstream backend should not contain %v header", headerKey)
@ -461,6 +468,11 @@ func TestUpstreamHeadersUpdate(t *testing.T) {
headerKey, expect, got) headerKey, expect, got)
} }
headerKey = "Clear-Me"
if _, ok := actualHeaders[headerKey]; ok {
t.Errorf("Request sent to upstream backend should not contain empty %v header", headerKey)
}
if actualHost != expectHost { if actualHost != expectHost {
t.Errorf("Request sent to upstream backend should have value of Host with %s, but got %s", expectHost, actualHost) t.Errorf("Request sent to upstream backend should have value of Host with %s, but got %s", expectHost, actualHost)
} }