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

http: allow Content-Length header for 304 responses #34835

Merged
merged 1 commit into from
Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,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) => {
BlackYoup marked this conversation as resolved.
Show resolved Hide resolved
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);
});