-
Notifications
You must be signed in to change notification settings - Fork 24.4k
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
Conversation
…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
@sota000 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator. |
There was a problem hiding this 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.
Libraries/Network/XMLHttpRequest.js
Outdated
}); | ||
|
||
// 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) => { |
There was a problem hiding this comment.
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()
?
There was a problem hiding this comment.
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.
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().
There was a problem hiding this 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().
@sota000 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator. |
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