Repository navigation
http: add 'sendingHeaders' event for response header manipulation #63996
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
Open
bjohansebas
wants to merge
4
commits into
nodejs:main
Choose a base branch
from
bjohansebas:on-header-solution
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a2eb627
http: add 'sendingHeaders' event for response header manipulation
bjohansebas ba5196f
http2: add 'sendingHeaders' event for compatibility with HTTP/1
bjohansebas 7ba4efe
http: preserve inline headers with sendingHeaders
bjohansebas fec7059
http: preserve empty status messages from sendingHeaders
bjohansebas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const http = require('http'); | ||
|
|
||
| for (const withListener of [false, true]) { | ||
| for (const withStoredHeaders of [false, true]) { | ||
| for (const frozen of [false, true]) { | ||
| const cookies = ['a=1']; | ||
| if (frozen) Object.freeze(cookies); | ||
| const headers = ['Set-Cookie', cookies, 'set-cookie', 'b=2']; | ||
| const expected = ['a=1', 'b=2']; | ||
| let remaining = 2; | ||
|
|
||
| // Reusing the same inline headers must not accumulate duplicate values | ||
| // across responses or require the caller's arrays to be mutable. | ||
| const server = http.createServer(common.mustCall((req, res) => { | ||
| if (withStoredHeaders) { | ||
| res.setHeader('Set-Cookie', ['old=1']); | ||
| res.setHeader('X-Stored', 'keep'); | ||
| } | ||
| if (withListener) { | ||
| res.on('sendingHeaders', common.mustCall(function() { | ||
| assert.deepStrictEqual(this.getHeader('set-cookie'), expected); | ||
| })); | ||
| } | ||
| res.writeHead(200, headers); | ||
| res.end(); | ||
| assert.deepStrictEqual(headers, [ | ||
| 'Set-Cookie', ['a=1'], 'set-cookie', 'b=2', | ||
| ]); | ||
| }, remaining)); | ||
|
|
||
| function request() { | ||
| http.get({ port: server.address().port }, common.mustCall((res) => { | ||
| assert.deepStrictEqual(res.headers['set-cookie'], expected); | ||
| assert.strictEqual(res.headers['x-stored'], | ||
| withStoredHeaders ? 'keep' : undefined); | ||
| res.resume().on('end', common.mustCall(() => { | ||
| if (--remaining > 0) { | ||
| request(); | ||
| } else { | ||
| server.close(); | ||
| } | ||
| })); | ||
| })); | ||
| } | ||
|
|
||
| server.listen(0, common.mustCall(request)); | ||
| } | ||
| } | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: similar feedback as previous test file. You have a lot of test cases here; a little explanation goes a long way for what exactly this is meant to cover. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const http = require('http'); | ||
|
|
||
| for (const withListener of [false, true]) { | ||
| for (const withStoredHeaders of [false, true]) { | ||
| for (const arrayValues of [false, true]) { | ||
| const headers = { | ||
| 'Set-Cookie': arrayValues ? ['a=1'] : 'a=1', | ||
| 'set-cookie': arrayValues ? ['b=2'] : 'b=2', | ||
| }; | ||
| // Inline headers override stored headers. Without stored headers, both | ||
| // differently cased fields are sent, even when a listener is present. | ||
| const expected = withStoredHeaders ? ['b=2'] : ['a=1', 'b=2']; | ||
| const server = http.createServer(common.mustCall((req, res) => { | ||
| if (withStoredHeaders) { | ||
| res.setHeader('Set-Cookie', 'old=1'); | ||
| res.setHeader('X-Stored', 'keep'); | ||
| } | ||
| if (withListener) { | ||
| res.on('sendingHeaders', common.mustCall(function() { | ||
| const cookies = this.getHeader('set-cookie'); | ||
| assert.deepStrictEqual( | ||
| Array.isArray(cookies) ? cookies : [cookies], expected); | ||
| })); | ||
| } | ||
| res.writeHead(200, headers); | ||
| res.end(); | ||
| assert.deepStrictEqual(headers, { | ||
| 'Set-Cookie': arrayValues ? ['a=1'] : 'a=1', | ||
| 'set-cookie': arrayValues ? ['b=2'] : 'b=2', | ||
| }); | ||
| })); | ||
|
|
||
| server.listen(0, common.mustCall(() => { | ||
| http.get({ port: server.address().port }, common.mustCall((res) => { | ||
| assert.deepStrictEqual(res.headers['set-cookie'], expected); | ||
| assert.strictEqual(res.headers['x-stored'], | ||
| withStoredHeaders ? 'keep' : undefined); | ||
| res.resume().on('end', common.mustCall(() => server.close())); | ||
| })); | ||
| })); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nit: this test file could use a little more explanation. It has a matrix of possible runs, and I believe its specifically testing
writeHeadbehavior only. Fine with the way its written, just would appreciate some explanation for the various cases its covering.