Skip to content

Commit

Permalink
Treating response header names as case-insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
masseyke committed Sep 20, 2023
1 parent 32baf87 commit ce8a5b3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -274,17 +275,22 @@ public HttpResponse execute(HttpRequest request) throws IOException {
// headers
Header[] headers = response.getAllHeaders();
Map<String, String[]> responseHeaders = Maps.newMapWithExpectedSize(headers.length);
/*
* Headers are not case sensitive, so in the following loop we lowercase all of them. We also roll up all values for the same
* case-insensitive header into a list.
*/
for (Header header : headers) {
if (responseHeaders.containsKey(header.getName())) {
String[] old = responseHeaders.get(header.getName());
String lowerCaseHeaderName = header.getName().toLowerCase(Locale.ROOT);
if (responseHeaders.containsKey(lowerCaseHeaderName)) {
String[] old = responseHeaders.get(lowerCaseHeaderName);
String[] values = new String[old.length + 1];

System.arraycopy(old, 0, values, 0, old.length);
values[values.length - 1] = header.getValue();

responseHeaders.put(header.getName(), values);
responseHeaders.put(lowerCaseHeaderName, values);
} else {
responseHeaders.put(header.getName(), new String[] { header.getValue() });
responseHeaders.put(lowerCaseHeaderName, new String[] { header.getValue() });
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ public void testThatDuplicateHeaderKeysAreReturned() throws Exception {
.setBody("foo")
.addHeader("foo", "bar")
.addHeader("foo", "baz")
.addHeader("Foo", "bam")
.addHeader("Content-Length", "3");
webServer.enqueue(mockResponse);

Expand All @@ -509,7 +510,7 @@ public void testThatDuplicateHeaderKeysAreReturned() throws Exception {
assertThat(webServer.requests(), hasSize(1));

assertThat(httpResponse.headers(), hasKey("foo"));
assertThat(httpResponse.headers().get("foo"), containsInAnyOrder("bar", "baz"));
assertThat(httpResponse.headers().get("foo"), containsInAnyOrder("bar", "baz", "bam"));
}

// finally fixing https://github.com/elastic/x-plugins/issues/1141 - yay! Fixed due to switching to apache http client internally!
Expand Down

0 comments on commit ce8a5b3

Please sign in to comment.