Skip to content

Commit

Permalink
http2: fix Http2Response.sendDate
Browse files Browse the repository at this point in the history
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

PR-URL: #34850
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Ricky Zhou <[email protected]>
Reviewed-By: Denys Otrishko <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
joaolucasl authored and BethGriggs committed Aug 24, 2020
1 parent 4b3b0e3 commit cc72584
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
8 changes: 8 additions & 0 deletions lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,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 @@ -775,6 +782,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 @@ -2257,7 +2257,7 @@ function callStreamClose(stream) {
stream.close();
}

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

Expand All @@ -2274,9 +2274,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 @@ -2602,7 +2605,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 @@ -2668,7 +2671,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 @@ -2729,7 +2732,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

0 comments on commit cc72584

Please sign in to comment.