mirror of
https://github.com/nodejs/node.git
synced 2026-11-26 21:58:26 -05:00
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: https://github.com/nodejs/node/issues/34841 PR-URL: https://github.com/nodejs/node/pull/34850 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ricky Zhou <0x19951125@gmail.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
1 parent
4b3b0e3f98
commit
cc7258469c
4 files changed
+49
-7
No files matched your search
@@ -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];
|
||||
}
|
||||
|
||||
@@ -775,6 +782,7 @@ class Http2ServerResponse extends Stream {
|
||||
const options = {
|
||||
endStream: state.ending,
|
||||
waitForTrailers: true,
|
||||
sendDate: state.sendDate
|
||||
};
|
||||
this[kStream].respond(headers, options);
|
||||
}
|
||||
|
||||
@@ -2257,7 +2257,7 @@ function callStreamClose(stream) {
|
||||
stream.close();
|
||||
}
|
||||
|
||||
function processHeaders(oldHeaders) {
|
||||
function processHeaders(oldHeaders, options) {
|
||||
assertIsObject(oldHeaders, 'headers');
|
||||
const headers = ObjectCreate(null);
|
||||
|
||||
@@ -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
|
||||
@@ -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;
|
||||
|
||||
@@ -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 ||
|
||||
@@ -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 ||
|
||||
|
||||
@@ -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();
|
||||
}));
|
||||
}));
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in new issue
Block a user