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

Conversation

garethgeorge
Copy link
Contributor

Makes the HttpRequest.getFirstHeader operation case insensitive (and offers a slight performance improvement) by using the underlying HttpServletRequest.getHeader on the underlying request object.

@garethgeorge garethgeorge requested a review from anniefu February 24, 2023 23:27
@@ -120,6 +120,11 @@ public Map<String, List<String>> getHeaders() {
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
Copy link
Member

Choose a reason for hiding this comment

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

Kind of a drive-by comment here. I wrote this code originally and I admit the case-sensitivity aspect didn't occur to me. An alternative fix would be to change this method like this:

return Collections.list(request.getHeaderNames()).stream()
    .collect(
        toMap(
            name -> name,
            name -> Collections.list(request.getHeaders(name)),
            (a, b) -> b,
            () -> new TreeMap<>(String.CASE_INSENSITIVE_ORDER));

That also has the advantage of a predictable iteration order, and it means the getHeaders() map is also case-insensitive, not just getFirstHeader.

Regardless of whether you choose this alternative, I think it would be good to remove the .map step in favour of doing the transformation in .toMap. Also, even though HttpResponse.getFirstHeader is presumably called much less often, I think it should also be case-insensitive.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for jumping in on this, I like the solution of moving to TreeMap. I've gone ahead and pulled your code snippet into the PR and updated the HttpResponseImpl definitions.

WDYT?

@garethgeorge garethgeorge force-pushed the garethgeorge/http-casesensitive-test branch from 4220728 to 3cc8236 Compare March 2, 2023 00:20
// 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?

@garethgeorge garethgeorge force-pushed the garethgeorge/http-casesensitive-test branch from 3cc8236 to b3df3d9 Compare March 2, 2023 18:21
@garethgeorge garethgeorge merged commit 44da871 into master Mar 2, 2023
@garethgeorge garethgeorge deleted the garethgeorge/http-casesensitive-test branch March 2, 2023 18:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants