Skip to content
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

Update XMLHttpRequest.getAllResponseHeaders() implementation (#32353) #32363

Closed
wants to merge 3 commits into from

Conversation

ascherkus
Copy link
Contributor

Summary

As per the XMLHttpRequest specification [1], getAllResponseHeaders() should return a string of headers with lowercased names and sorted by their uppercase representation, with each header ending with '\r\n'.

[1] https://xhr.spec.whatwg.org/#the-getallresponseheaders()-method

Changelog

[General] [Changed] XMLHttpRequest.getAllResponseHeaders() now returns headers with names lowercased and sorted in ascending order, as per specification

Test Plan

Test derived from Web Platform Test repository:
https://github.com/web-platform-tests/wpt/tree/master/xhr

…eHeaders() (facebook#32353)

As per the XMLHttpRequest specification [1], getAllResponseHeaders() should
return a string of headers with lowercased names and sorted by their uppercase
representation, with each header ending with '\r\n'.

[1] https://xhr.spec.whatwg.org/#the-getallresponseheaders()-method
@react-native-bot react-native-bot added the No CLA Authors need to sign the CLA before a PR can be reviewed. label Oct 9, 2021
@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. labels Oct 13, 2021
@react-native-bot react-native-bot removed the No CLA Authors need to sign the CLA before a PR can be reviewed. label Oct 13, 2021
@facebook-github-bot
Copy link
Contributor

@sota000 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@yungsters yungsters left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this. I provided some suggestions inline.

});

// Sort in ascending order, with a being less than b if a's name is legacy-uppercased-byte less than b's name.
combinedHeaders.sort((a, b) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why can't you use headerA.toUpperCase() < headerB.toUppercase()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Totally did not realize that'd accomplish the same thing!

When I was initially writing this I was worried about repeatedly uppercasing the whole string during each comparison. I did a quick benchmark and looks like your suggestion performs better, especially if we pre-compute the upper cased header once.

Libraries/Network/XMLHttpRequest.js Outdated Show resolved Hide resolved
Libraries/Network/XMLHttpRequest.js Outdated Show resolved Hide resolved
ascherkus and others added 2 commits October 14, 2021 11:58
Co-authored-by: Timothy Yung <yungsters@gmail.com>
Create an object that contains the lower cased, upper cased, and value
for each header during the combination step. The upper cased value is
computed once to avoid re-computing it during sort().
Copy link
Contributor Author

@ascherkus ascherkus left a comment

Choose a reason for hiding this comment

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

Thanks for the review! I decided to precompute the toUpperCase() call during the combination step, which I think cleans things up and avoids needless recomputation during sort().

@facebook-github-bot
Copy link
Contributor

@sota000 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Oct 26, 2021
@facebook-github-bot
Copy link
Contributor

@sota000 merged this pull request in b2415c4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants