-
-
Notifications
You must be signed in to change notification settings - Fork 35.7k
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
1
commit 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.
+220
−1
Open
Changes from all commits
Commits
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const http = require('http'); | ||
|
|
||
| // 1) Fires once, before serialization, for an implicit-header response (end()). | ||
| { | ||
| const server = http.createServer(common.mustCall((req, res) => { | ||
| let fired = 0; | ||
| res.on('sendingHeaders', common.mustCall(function() { | ||
| fired++; | ||
| // Headers not yet sent, still mutable. | ||
| assert.strictEqual(this.headersSent, false); | ||
| this.setHeader('X-Added-In-Hook', 'yes'); | ||
| })); | ||
| res.end('body'); | ||
| // Synchronous: hook has already run by the time end() returns. | ||
| assert.strictEqual(fired, 1); | ||
| })); | ||
|
|
||
| server.listen(0, common.mustCall(() => { | ||
| http.get({ port: server.address().port }, common.mustCall((res) => { | ||
| assert.strictEqual(res.headers['x-added-in-hook'], 'yes'); | ||
| res.resume().on('end', common.mustCall(() => server.close())); | ||
| })); | ||
| })); | ||
| } | ||
|
|
||
| // 2) Listener may change the status code, and it is reflected on the wire. | ||
| { | ||
| const server = http.createServer(common.mustCall((req, res) => { | ||
| res.on('sendingHeaders', common.mustCall(function() { | ||
| this.statusCode = 503; | ||
| this.statusMessage = 'Service Unavailable'; | ||
| })); | ||
| res.writeHead(200); | ||
| res.end(); | ||
| })); | ||
|
|
||
| server.listen(0, common.mustCall(() => { | ||
| http.get({ port: server.address().port }, common.mustCall((res) => { | ||
| assert.strictEqual(res.statusCode, 503); | ||
| res.resume().on('end', common.mustCall(() => server.close())); | ||
| })); | ||
| })); | ||
| } | ||
|
|
||
| // 3) Listener sees and can remove headers passed INLINE to writeHead(), | ||
| // including the array form. | ||
| { | ||
| const server = http.createServer(common.mustCall((req, res) => { | ||
| res.on('sendingHeaders', common.mustCall(function() { | ||
| assert.strictEqual(this.getHeader('X-Inline'), 'a'); | ||
| this.removeHeader('X-Inline'); | ||
| this.setHeader('X-From-Hook', 'b'); | ||
| })); | ||
| res.writeHead(200, ['X-Inline', 'a', 'Content-Type', 'text/plain']); | ||
| res.end('ok'); | ||
| })); | ||
|
|
||
| server.listen(0, common.mustCall(() => { | ||
| http.get({ port: server.address().port }, common.mustCall((res) => { | ||
| assert.strictEqual(res.headers['x-inline'], undefined); | ||
| assert.strictEqual(res.headers['x-from-hook'], 'b'); | ||
| assert.strictEqual(res.headers['content-type'], 'text/plain'); | ||
| res.resume().on('end', common.mustCall(() => server.close())); | ||
| })); | ||
| })); | ||
| } | ||
|
|
||
| // 4) Multiple listeners all run (FIFO, EventEmitter semantics). | ||
| { | ||
| const order = []; | ||
| const server = http.createServer(common.mustCall((req, res) => { | ||
| res.on('sendingHeaders', common.mustCall(() => order.push(1))); | ||
| res.on('sendingHeaders', common.mustCall(() => order.push(2))); | ||
| res.end(); | ||
| assert.deepStrictEqual(order, [1, 2]); | ||
| })); | ||
|
|
||
| server.listen(0, common.mustCall(() => { | ||
| http.get({ port: server.address().port }, common.mustCall((res) => { | ||
| res.resume().on('end', common.mustCall(() => server.close())); | ||
| })); | ||
| })); | ||
| } | ||
|
|
||
| // 5) flushHeaders() also triggers the hook exactly once. | ||
| { | ||
| const server = http.createServer(common.mustCall((req, res) => { | ||
| res.on('sendingHeaders', common.mustCall(function() { | ||
| this.setHeader('X-Flushed', '1'); | ||
| })); | ||
| res.flushHeaders(); | ||
| res.end(); | ||
| })); | ||
|
|
||
| server.listen(0, common.mustCall(() => { | ||
| http.get({ port: server.address().port }, common.mustCall((res) => { | ||
| assert.strictEqual(res.headers['x-flushed'], '1'); | ||
| res.resume().on('end', common.mustCall(() => server.close())); | ||
| })); | ||
| })); | ||
| } | ||
|
|
||
| // 6) The hook is synchronous: only changes made before an `await` take effect. | ||
| // Work deferred past `await` runs after the headers are sent, so a later | ||
| // setHeader() throws and a statusCode change is not reflected on the wire. | ||
| { | ||
| const server = http.createServer(common.mustCall((req, res) => { | ||
| res.on('sendingHeaders', common.mustCall(async function() { | ||
| this.setHeader('X-Sync', 'before'); | ||
| await null; | ||
| assert.strictEqual(this.headersSent, true); | ||
| this.statusCode = 503; // No effect: headers already sent. | ||
| assert.throws(() => this.setHeader('X-Async', 'after'), { | ||
| code: 'ERR_HTTP_HEADERS_SENT', | ||
| }); | ||
| server.close(); | ||
| })); | ||
| res.end('hello'); | ||
| })); | ||
|
|
||
| server.listen(0, common.mustCall(() => { | ||
| http.get({ port: server.address().port }, common.mustCall((res) => { | ||
| assert.strictEqual(res.statusCode, 200); | ||
| assert.strictEqual(res.headers['x-sync'], 'before'); | ||
| assert.strictEqual(res.headers['x-async'], undefined); | ||
| res.resume(); | ||
| })); | ||
| })); | ||
| } |
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.
I believe it will be useful to put a sentence as warning when trying to use a promise for a listener.
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.
done, and I just added tests as well