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

fix: undici headers #289

Merged
merged 5 commits into from
Feb 2, 2023
Merged

fix: undici headers #289

merged 5 commits into from
Feb 2, 2023

Conversation

climba03003
Copy link
Member

@climba03003 climba03003 commented Feb 2, 2023

Fixes #287

Finally found the problem is caused by both http and undici.

  1. http is not always treats the header to latin1.
    https://github.com/nodejs/node/blob/2a29df64645a70bbb833298423a29206c4ec6a2e/lib/_http_outgoing.js#L361-L373
  2. undici always perform Buffer.from('').toString('utf8') regardless the header encoding.
    https://github.com/nodejs/undici/blob/2b260c997ad4efe4ed2064b264b4b546a59e7a67/lib/core/util.js#L216-L229

In combine of the above two problem.
The headers will parsed as a completely difference byte when chaining.

In this fix, we always treats the header as latin1.
So, we are always consistence in the encoding.

Checklist

@climba03003 climba03003 requested a review from SimenB February 2, 2023 09:35
@climba03003 climba03003 requested a review from mcollina February 2, 2023 09:55
//
// in order to presist the encoding, always encode it
// back to latin1
function patchUndiciHeaders (headers) {
Copy link
Member

@SimenB SimenB Feb 2, 2023

Choose a reason for hiding this comment

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

should there be an option in undici to leave headers as latin1 encoding?

EDIT: Ah, noticed nodejs/undici#1903 now

Copy link
Member Author

Choose a reason for hiding this comment

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

I think yes. Otherwise, the patch is prone to error.

undici always trans-code the header. Maybe it should really detect the encoding before trans-code. That means the behavior should be identical to http.request.

Actually, I already open a issue on undici.
See nodejs/undici#1903

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

lib/utils.js Outdated Show resolved Hide resolved
lib/utils.js Outdated Show resolved Hide resolved
lib/utils.js Outdated Show resolved Hide resolved
lib/utils.js Outdated Show resolved Hide resolved
Co-authored-by: Simen Bekkhus <[email protected]>
@mcollina mcollina merged commit 09cb8e7 into master Feb 2, 2023
@mcollina mcollina deleted the fix-header-encoding branch February 2, 2023 13:43
@mcollina
Copy link
Member

mcollina commented Feb 2, 2023

Would this need a revert after undici is fixed?

@climba03003
Copy link
Member Author

climba03003 commented Feb 2, 2023

I believe, yes.
We can update the patch function to detect undici version once it fixed.
It can maintain the best compatibility.

@arturkin
Copy link

FYI this causes regression with set-cookie headers in case it's an array of values. Had to revert to the previous version due to this.

@climba03003
Copy link
Member Author

FYI this causes regression with set-cookie headers in case it's an array of values. Had to revert to the previous version due to this.

Can you provide a reproducible code in a separate issue?

@SimenB
Copy link
Member

SimenB commented Feb 13, 2023

The change can probably be reverted together with a bump of the undici dependency?

@climba03003
Copy link
Member Author

climba03003 commented Feb 13, 2023

The change can probably be reverted together with a bump of the undici dependency?

Even with bumping undici in dependencies, I believe it wouldn't catch-up the latest.
I am fine if it would also update undici when fetching new package.

@SimenB
Copy link
Member

SimenB commented Feb 13, 2023

It would

@mcollina
Copy link
Member

Could you open a fresh PR?

climba03003 added a commit that referenced this pull request Feb 13, 2023
Partial revert of 09cb8e7
Keep the test-case and upgrade undici
mcollina pushed a commit that referenced this pull request Feb 13, 2023
Partial revert of 09cb8e7
Keep the test-case and upgrade undici
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.

Header encoding changes, leading to ERR_INVALID_CHAR
5 participants