Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
http2: fix Http2Response.sendDate
The `sendDate` flag was not being respected
by the current implementation and the `Date`
header was being sent regardless of the config.
This commit fixes that and adds tests for this case.

Fixes: #34841
  • Loading branch information
joaolucasl committed Aug 20, 2020
commit 7d1bfc8f75ba82255b05f7dc584e978702b43e71
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
16 changes: 10 additions & 6 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,8 +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)
let sendDate = true;
if (options.sendDate !== null && options.sendDate !== undefined)
sendDate = options.sendDate;

if (sendDate && (headers[HTTP2_HEADER_DATE] === null ||
headers[HTTP2_HEADER_DATE] === undefined))
Comment thread
joaolucasl marked this conversation as resolved.
Outdated
headers[HTTP2_HEADER_DATE] = utcDate();

// This is intentionally stricter than the HTTP/1 implementation, which
Expand Down Expand Up @@ -2624,7 +2628,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 +2694,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 +2755,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', (headers, flags) => {
Comment thread
joaolucasl marked this conversation as resolved.
Outdated
assert.strictEqual('Date' in headers, false);
assert.strictEqual('date' in headers, false);
});

req.on('end', () => {
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