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

http2: fix Http2Response.sendDate #34850

Closed
Closed
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
8 changes: 8 additions & 0 deletions lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,13 @@ class Http2ServerResponse extends Stream {
throw new ERR_HTTP2_HEADERS_SENT();

name = name.trim().toLowerCase();

if (name === 'date') {
this[kState].sendDate = false;

return;
}

delete this[kHeaders][name];
}

Expand Down Expand Up @@ -787,6 +794,7 @@ class Http2ServerResponse extends Stream {
const options = {
endStream: state.ending,
waitForTrailers: true,
sendDate: state.sendDate
};
this[kStream].respond(headers, options);
}
Expand Down
17 changes: 10 additions & 7 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -2274,7 +2274,7 @@ function callStreamClose(stream) {
stream.close();
}

function processHeaders(oldHeaders) {
function processHeaders(oldHeaders, options) {
assertIsObject(oldHeaders, 'headers');
const headers = ObjectCreate(null);

Expand All @@ -2292,9 +2292,12 @@ function processHeaders(oldHeaders) {
headers[HTTP2_HEADER_STATUS] =
headers[HTTP2_HEADER_STATUS] | 0 || HTTP_STATUS_OK;

if (headers[HTTP2_HEADER_DATE] === null ||
headers[HTTP2_HEADER_DATE] === undefined)
headers[HTTP2_HEADER_DATE] = utcDate();
if (options.sendDate == null || options.sendDate) {
if (headers[HTTP2_HEADER_DATE] === null ||
headers[HTTP2_HEADER_DATE] === undefined) {
headers[HTTP2_HEADER_DATE] = utcDate();
}
}

// This is intentionally stricter than the HTTP/1 implementation, which
// allows values between 100 and 999 (inclusive) in order to allow for
Expand Down Expand Up @@ -2624,7 +2627,7 @@ class ServerHttp2Stream extends Http2Stream {
state.flags |= STREAM_FLAGS_HAS_TRAILERS;
}

headers = processHeaders(headers);
headers = processHeaders(headers, options);
const headersList = mapToHeaders(headers, assertValidPseudoHeaderResponse);
this[kSentHeaders] = headers;

Expand Down Expand Up @@ -2690,7 +2693,7 @@ class ServerHttp2Stream extends Http2Stream {
this[kUpdateTimer]();
this.ownsFd = false;

headers = processHeaders(headers);
headers = processHeaders(headers, options);
const statusCode = headers[HTTP2_HEADER_STATUS] |= 0;
// Payload/DATA frames are not permitted in these cases
if (statusCode === HTTP_STATUS_NO_CONTENT ||
Expand Down Expand Up @@ -2751,7 +2754,7 @@ class ServerHttp2Stream extends Http2Stream {
this[kUpdateTimer]();
this.ownsFd = true;

headers = processHeaders(headers);
headers = processHeaders(headers, options);
const statusCode = headers[HTTP2_HEADER_STATUS] |= 0;
// Payload/DATA frames are not permitted in these cases
if (statusCode === HTTP_STATUS_NO_CONTENT ||
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';
const common = require('../common');
if (!common.hasCrypto) { common.skip('missing crypto'); }
const assert = require('assert');
const http2 = require('http2');

const server = http2.createServer(common.mustCall((request, response) => {
response.sendDate = false;
response.writeHead(200);
response.end();
}));

server.listen(0, common.mustCall(() => {
const session = http2.connect(`http://localhost:${server.address().port}`);
const req = session.request();

req.on('response', common.mustCall((headers, flags) => {
assert.strictEqual('Date' in headers, false);
assert.strictEqual('date' in headers, false);
}));

req.on('end', common.mustCall(() => {
session.close();
server.close();
}));
}));
5 changes: 5 additions & 0 deletions test/parallel/test-http2-compat-serverresponse-headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ server.listen(0, common.mustCall(function() {
response.sendDate = false;
assert.strictEqual(response.sendDate, false);

response.sendDate = true;
assert.strictEqual(response.sendDate, true);
response.removeHeader('Date');
assert.strictEqual(response.sendDate, false);

response.on('finish', common.mustCall(function() {
assert.strictEqual(response.headersSent, true);

Expand Down