Skip to content

Commit

Permalink
http: allow Content-Length header for 304 responses
Browse files Browse the repository at this point in the history
Fixes: #31037

PR-URL: #34835
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ricky Zhou <0x19951125@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
  • Loading branch information
BlackYoup authored and ruyadorno committed Sep 17, 2020
1 parent df70861 commit 977b0ed
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,11 @@ function parserOnIncomingClient(res, shouldKeepAlive) {
if (method === 'HEAD')
return 1; // Skip body but don't treat as Upgrade.

if (res.statusCode === 304) {
res.complete = true;
return 1; // Skip body as there won't be any
}

return 0; // No special treatment.
}

Expand Down
32 changes: 32 additions & 0 deletions test/parallel/test-http-allow-content-length-304.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';
const common = require('../common');

// This test ensures that the http-parser doesn't expect a body when
// a 304 Not Modified response has a non-zero Content-Length header

const assert = require('assert');
const http = require('http');

const server = http.createServer(common.mustCall((req, res) => {
res.setHeader('Content-Length', 11);
res.statusCode = 304;
res.end(null);
}));

server.listen(0, () => {
const request = http.request({
port: server.address().port,
});

request.on('response', common.mustCall((response) => {
response.on('data', common.mustNotCall());
response.on('aborted', common.mustNotCall());
response.on('end', common.mustCall(() => {
assert.strictEqual(response.headers['content-length'], '11');
assert.strictEqual(response.statusCode, 304);
server.close();
}));
}));

request.end(null);
});

0 comments on commit 977b0ed

Please sign in to comment.