Commit b783caaa authored by Gyula Voros's avatar Gyula Voros Committed by Matt Holt

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
parent c972ea39
...@@ -287,12 +287,18 @@ func mutateHeadersByRules(headers, rules http.Header, repl httpserver.Replacer) ...@@ -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)
}
} }
} }
} }
...@@ -407,8 +407,10 @@ func TestUpstreamHeadersUpdate(t *testing.T) { ...@@ -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) { ...@@ -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) { ...@@ -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)
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment