Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP/2 server compatibility API ignores sendDate #34841

Closed
pimterry opened this issue Aug 19, 2020 · 2 comments
Closed

HTTP/2 server compatibility API ignores sendDate #34841

pimterry opened this issue Aug 19, 2020 · 2 comments

Comments

@pimterry
Copy link
Member

pimterry commented Aug 19, 2020

  • Version: 10.21.0, 12.18.3, 14.8.0
  • Platform: Linux
  • Subsystem: HTTP/2

What steps will reproduce the bug?

Run this:

const http = require("http");
const http2 = require("http2");

const h2Server = http2.createServer((req, res) => {
    res.sendDate = false;
    res.writeHead(200);
});

h2Server.listen(9001, () => {
    const client = http2.connect('http://localhost:9001');
    const req = client.request();
    req.on('response', (headers) => {
        console.log('Received H2 headers', headers);
    });
});

const h1Server = http.createServer((req, res) => {
    res.sendDate = false;
    res.writeHead(200).end();
});

h1Server.listen(9002, () => {
    http.request('http://localhost:9002', (res) => {
        console.log("Received H1 headers", res.headers);
    }).end();
});

It prints:

Received H2 headers [Object: null prototype] {
  ':status': 200,
  date: 'Wed, 19 Aug 2020 16:33:29 GMT'
}
Received H1 headers { connection: 'close', 'transfer-encoding': 'chunked' }

How often does it reproduce? Is there a required condition?

Always reproduces.

What is the expected behavior?

When response.sendDate is set to false, no date header should be included in the response.

What do you see instead?

This works fine for HTTP/1 servers, but as visible in the output above, HTTP/2 servers send the date header in the response regardless.

This is inconsistent with HTTP/1, and this behaviour is explicitly documented as working for the HTTP/2 API too.

For context: I'm using these HTTP/2 APIs to mock services and test HTTP/2 client behaviour, so I'd like to be able to fully control the HTTP headers that are returned in the response. With HTTP/1 that's easy to do, by using sendDate or by manually removing headers e.g. with res.removeHeader('content-length'). That's explicitly supported by the code, which watches for removed default headers and ensures they're not reinserted:

node/lib/_http_outgoing.js

Lines 604 to 631 in 849d9e7

OutgoingMessage.prototype.removeHeader = function removeHeader(name) {
validateString(name, 'name');
if (this._header) {
throw new ERR_HTTP_HEADERS_SENT('remove');
}
const key = name.toLowerCase();
switch (key) {
case 'connection':
this._removedConnection = true;
break;
case 'content-length':
this._removedContLen = true;
break;
case 'transfer-encoding':
this._removedTE = true;
break;
case 'date':
this.sendDate = false;
break;
}
if (this[kOutHeaders] !== null) {
delete this[kOutHeaders][key];
}
};

In HTTP/2 the Date header is the only remaining default header (ignoring pseudo headers), but neither sendDate = false nor removeHeader('date') do anything, so there doesn't seem to be any way to disable it.

@joaolucasl
Copy link
Contributor

If you're not already working on it, I would like to try my hand at this @pimterry :)

@pimterry
Copy link
Member Author

No @joaolucasl I'm not! If you want to take a look at this that sounds great, feel free 👍

joaolucasl added a commit to joaolucasl/node that referenced this issue Aug 20, 2020
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: nodejs#34841
@Trott Trott closed this as completed in f5102fb Aug 22, 2020
BethGriggs pushed a commit that referenced this issue Aug 24, 2020
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 <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>
addaleax pushed a commit that referenced this issue Sep 22, 2020
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 <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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants