forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: resume kept-alive when no body allowed
In accordance with https://www.rfc-editor.org/rfc/rfc9112#name-message-body-length: HEAD, 1xx, 204, and 304 responses cannot contain a message body. If a socket will be kept-alive, resume the socket during parsing so that it may be returned to the free pool. Fixes nodejs#47228
- Loading branch information
Showing
2 changed files
with
68 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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,52 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const cp = require('child_process'); | ||
const http = require('http'); | ||
|
||
if (process.argv[2] === 'server') { | ||
const server = http.createServer((req, res) => { | ||
if(req.method === "HEAD") { | ||
res.writeHead(200); | ||
res.end(); | ||
} | ||
else if (req.url === '/204') { | ||
res.writeHead(204); | ||
res.end(); | ||
} | ||
else if (req.url === '/304') { | ||
res.writeHead(304); | ||
res.end(); | ||
} | ||
}); | ||
|
||
server.listen(0, () => { | ||
process.send(server.address().port); | ||
}); | ||
} else { | ||
const serverProcess = cp.fork(__filename, ['server'], { | ||
stdio: ['ignore', 'ignore', 'ignore', 'ipc'] | ||
}); | ||
serverProcess.once('message', common.mustCall((port) => { | ||
serverProcess.channel.unref(); | ||
serverProcess.unref(); | ||
const agent = new http.Agent({ keepAlive: true }); | ||
|
||
// Make requests without consuming response | ||
http.get({ method: "HEAD", host: common.localhostIPv4, port, agent }, common.mustCall()); | ||
http.get({ method: "GET", host: common.localhostIPv4, port, agent, path: '/204' }, common.mustCall()); | ||
http.get({ method: "GET", host: common.localhostIPv4, port, agent, path: '/304' }, common.mustCall()); | ||
|
||
// Ensure handlers are called/not called as expected | ||
const cb = (res) => { | ||
res.on('end', common.mustCall()) | ||
res.on('data', common.mustNotCall()) | ||
} | ||
http.get({ method: "HEAD", host: common.localhostIPv4, port, agent }, cb); | ||
http.get({ method: "GET", host: common.localhostIPv4, port, agent, path: '/204' }, cb); | ||
http.get({ method: "GET", host: common.localhostIPv4, port, agent, path: '/304' }, cb); | ||
})); | ||
|
||
// HEAD, 204, and 304 requests should not block script exit | ||
setTimeout(common.mustNotCall(), common.platformTimeout(3000)).unref(); | ||
} |