Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: retrieving http headers on request object should be case insenstive #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletException;
Expand Down Expand Up @@ -116,8 +117,12 @@ public BufferedReader getReader() throws IOException {
@Override
public Map<String, List<String>> getHeaders() {
return Collections.list(request.getHeaderNames()).stream()
.map(name -> new SimpleEntry<>(name, Collections.list(request.getHeaders(name))))
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
.collect(
toMap(
name -> name,
name -> Collections.list(request.getHeaders(name)),
(a, b) -> b,
() -> new TreeMap<>(String.CASE_INSENSITIVE_ORDER)));
}

private static class HttpPartImpl implements HttpPart {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
import javax.servlet.http.HttpServletResponse;

public class HttpResponseImpl implements HttpResponse {
Expand Down Expand Up @@ -64,8 +64,12 @@ public void appendHeader(String key, String value) {
@Override
public Map<String, List<String>> getHeaders() {
return response.getHeaderNames().stream()
.map(header -> new SimpleEntry<>(header, list(response.getHeaders(header))))
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
.collect(
toMap(
name -> name,
name -> new ArrayList<>(response.getHeaders(name)),
(a, b) -> b,
() -> new TreeMap<>(String.CASE_INSENSITIVE_ORDER)));
}

private static <T> List<T> list(Collection<T> collection) {
Expand All @@ -82,12 +86,17 @@ public OutputStream getOutputStream() throws IOException {
@Override
public synchronized BufferedWriter getWriter() throws IOException {
if (writer == null) {
// Unfortunately this means that we get two intermediate objects between the object we return
// and the underlying Writer that response.getWriter() wraps. We could try accessing the
// PrintWriter.out field via reflection, but that sort of access to non-public fields of
// platform classes is now frowned on and may draw warnings or even fail in subsequent
// Unfortunately this means that we get two intermediate objects between the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure these lines needed to be changed?

// object we return
// and the underlying Writer that response.getWriter() wraps. We could try
// accessing the
// PrintWriter.out field via reflection, but that sort of access to non-public
// fields of
// platform classes is now frowned on and may draw warnings or even fail in
// subsequent
// versions.
// We could instead wrap the OutputStream, but that would require us to deduce the appropriate
// We could instead wrap the OutputStream, but that would require us to deduce
// the appropriate
// Charset, using logic like this:
// https://github.com/eclipse/jetty.project/blob/923ec38adf/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java#L731
// We may end up doing that if performance is an issue.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ private void httpRequestMethods(
assertThat(request.getHeaders()).containsAtLeastEntriesIn(expectedHeaders);
},
request -> assertThat(request.getFirstHeader("foo")).hasValue("bar"),
request -> assertThat(request.getFirstHeader("CaSe-SeNsItIvE")).hasValue("VaLuE"),
request -> assertThat(request.getFirstHeader("case-sensitive")).hasValue("VaLuE"),
request -> {
try {
request.getParts();
Expand All @@ -197,6 +199,7 @@ private void httpRequestMethods(
.header(HttpHeader.CONTENT_TYPE, "text/plain; charset=utf-8")
.header("foo", "bar")
.header("foo", "baz")
.header("CaSe-SeNsItIvE", "VaLuE")
.content(new StringContentProvider(TEST_BODY));
ContentResponse response = request.send();
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200);
Expand Down Expand Up @@ -352,11 +355,10 @@ private void httpResponseSetAndGet(
// So we just check that we can add our own headers.
response.appendHeader("foo", "bar");
response.appendHeader("wibbly", "wobbly");
response.appendHeader("foo", "baz");
Map<String, List<String>> updatedHeaders = new TreeMap<>(response.getHeaders());
updatedHeaders.keySet().removeAll(initialHeaders.keySet());
response.appendHeader("FoO", "baz");
var updatedHeaders = response.getHeaders();
assertThat(updatedHeaders)
.containsExactly("foo", Arrays.asList("bar", "baz"), "wibbly", Arrays.asList("wobbly"));
.containsAtLeast("foo", Arrays.asList("bar", "baz"), "wibbly", Arrays.asList("wobbly"));
},
};
for (HttpResponseTest test : tests) {
Expand Down