Skip to content

Commit 4079cdd

Browse files
joaolucasladdaleax
authored andcommitted
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 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]>
1 parent 06c5120 commit 4079cdd

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

lib/internal/http2/compat.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,13 @@ class Http2ServerResponse extends Stream {
583583
throw new ERR_HTTP2_HEADERS_SENT();
584584

585585
name = name.trim().toLowerCase();
586+
587+
if (name === 'date') {
588+
this[kState].sendDate = false;
589+
590+
return;
591+
}
592+
586593
delete this[kHeaders][name];
587594
}
588595

@@ -775,6 +782,7 @@ class Http2ServerResponse extends Stream {
775782
const options = {
776783
endStream: state.ending,
777784
waitForTrailers: true,
785+
sendDate: state.sendDate
778786
};
779787
this[kStream].respond(headers, options);
780788
}

lib/internal/http2/core.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2183,7 +2183,7 @@ function callStreamClose(stream) {
21832183
stream.close();
21842184
}
21852185

2186-
function processHeaders(oldHeaders) {
2186+
function processHeaders(oldHeaders, options) {
21872187
assertIsObject(oldHeaders, 'headers');
21882188
const headers = ObjectCreate(null);
21892189

@@ -2200,9 +2200,12 @@ function processHeaders(oldHeaders) {
22002200
headers[HTTP2_HEADER_STATUS] =
22012201
headers[HTTP2_HEADER_STATUS] | 0 || HTTP_STATUS_OK;
22022202

2203-
if (headers[HTTP2_HEADER_DATE] === null ||
2204-
headers[HTTP2_HEADER_DATE] === undefined)
2205-
headers[HTTP2_HEADER_DATE] = utcDate();
2203+
if (options.sendDate == null || options.sendDate) {
2204+
if (headers[HTTP2_HEADER_DATE] === null ||
2205+
headers[HTTP2_HEADER_DATE] === undefined) {
2206+
headers[HTTP2_HEADER_DATE] = utcDate();
2207+
}
2208+
}
22062209

22072210
// This is intentionally stricter than the HTTP/1 implementation, which
22082211
// allows values between 100 and 999 (inclusive) in order to allow for
@@ -2528,7 +2531,7 @@ class ServerHttp2Stream extends Http2Stream {
25282531
state.flags |= STREAM_FLAGS_HAS_TRAILERS;
25292532
}
25302533

2531-
headers = processHeaders(headers);
2534+
headers = processHeaders(headers, options);
25322535
const headersList = mapToHeaders(headers, assertValidPseudoHeaderResponse);
25332536
this[kSentHeaders] = headers;
25342537

@@ -2594,7 +2597,7 @@ class ServerHttp2Stream extends Http2Stream {
25942597
this[kUpdateTimer]();
25952598
this.ownsFd = false;
25962599

2597-
headers = processHeaders(headers);
2600+
headers = processHeaders(headers, options);
25982601
const statusCode = headers[HTTP2_HEADER_STATUS] |= 0;
25992602
// Payload/DATA frames are not permitted in these cases
26002603
if (statusCode === HTTP_STATUS_NO_CONTENT ||
@@ -2655,7 +2658,7 @@ class ServerHttp2Stream extends Http2Stream {
26552658
this[kUpdateTimer]();
26562659
this.ownsFd = true;
26572660

2658-
headers = processHeaders(headers);
2661+
headers = processHeaders(headers, options);
26592662
const statusCode = headers[HTTP2_HEADER_STATUS] |= 0;
26602663
// Payload/DATA frames are not permitted in these cases
26612664
if (statusCode === HTTP_STATUS_NO_CONTENT ||
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
const common = require('../common');
3+
if (!common.hasCrypto) { common.skip('missing crypto'); }
4+
const assert = require('assert');
5+
const http2 = require('http2');
6+
7+
const server = http2.createServer(common.mustCall((request, response) => {
8+
response.sendDate = false;
9+
response.writeHead(200);
10+
response.end();
11+
}));
12+
13+
server.listen(0, common.mustCall(() => {
14+
const session = http2.connect(`http://localhost:${server.address().port}`);
15+
const req = session.request();
16+
17+
req.on('response', common.mustCall((headers, flags) => {
18+
assert.strictEqual('Date' in headers, false);
19+
assert.strictEqual('date' in headers, false);
20+
}));
21+
22+
req.on('end', common.mustCall(() => {
23+
session.close();
24+
server.close();
25+
}));
26+
}));

test/parallel/test-http2-compat-serverresponse-headers.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ server.listen(0, common.mustCall(function() {
114114
response.sendDate = false;
115115
assert.strictEqual(response.sendDate, false);
116116

117+
response.sendDate = true;
118+
assert.strictEqual(response.sendDate, true);
119+
response.removeHeader('Date');
120+
assert.strictEqual(response.sendDate, false);
121+
117122
response.on('finish', common.mustCall(function() {
118123
assert.strictEqual(response.headersSent, true);
119124

0 commit comments

Comments
 (0)