Skip to content

Commit cc72584

Browse files
joaolucaslBethGriggs
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 4b3b0e3 commit cc72584

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
@@ -2257,7 +2257,7 @@ function callStreamClose(stream) {
22572257
stream.close();
22582258
}
22592259

2260-
function processHeaders(oldHeaders) {
2260+
function processHeaders(oldHeaders, options) {
22612261
assertIsObject(oldHeaders, 'headers');
22622262
const headers = ObjectCreate(null);
22632263

@@ -2274,9 +2274,12 @@ function processHeaders(oldHeaders) {
22742274
headers[HTTP2_HEADER_STATUS] =
22752275
headers[HTTP2_HEADER_STATUS] | 0 || HTTP_STATUS_OK;
22762276

2277-
if (headers[HTTP2_HEADER_DATE] === null ||
2278-
headers[HTTP2_HEADER_DATE] === undefined)
2279-
headers[HTTP2_HEADER_DATE] = utcDate();
2277+
if (options.sendDate == null || options.sendDate) {
2278+
if (headers[HTTP2_HEADER_DATE] === null ||
2279+
headers[HTTP2_HEADER_DATE] === undefined) {
2280+
headers[HTTP2_HEADER_DATE] = utcDate();
2281+
}
2282+
}
22802283

22812284
// This is intentionally stricter than the HTTP/1 implementation, which
22822285
// allows values between 100 and 999 (inclusive) in order to allow for
@@ -2602,7 +2605,7 @@ class ServerHttp2Stream extends Http2Stream {
26022605
state.flags |= STREAM_FLAGS_HAS_TRAILERS;
26032606
}
26042607

2605-
headers = processHeaders(headers);
2608+
headers = processHeaders(headers, options);
26062609
const headersList = mapToHeaders(headers, assertValidPseudoHeaderResponse);
26072610
this[kSentHeaders] = headers;
26082611

@@ -2668,7 +2671,7 @@ class ServerHttp2Stream extends Http2Stream {
26682671
this[kUpdateTimer]();
26692672
this.ownsFd = false;
26702673

2671-
headers = processHeaders(headers);
2674+
headers = processHeaders(headers, options);
26722675
const statusCode = headers[HTTP2_HEADER_STATUS] |= 0;
26732676
// Payload/DATA frames are not permitted in these cases
26742677
if (statusCode === HTTP_STATUS_NO_CONTENT ||
@@ -2729,7 +2732,7 @@ class ServerHttp2Stream extends Http2Stream {
27292732
this[kUpdateTimer]();
27302733
this.ownsFd = true;
27312734

2732-
headers = processHeaders(headers);
2735+
headers = processHeaders(headers, options);
27332736
const statusCode = headers[HTTP2_HEADER_STATUS] |= 0;
27342737
// Payload/DATA frames are not permitted in these cases
27352738
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)