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

Missing Http2ServerResponse.setHeaders() fixed #51576

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions doc/api/http2.md
Original file line number Diff line number Diff line change
Expand Up @@ -4511,6 +4511,15 @@ const server = http2.createServer((req, res) => {
});
```

#### `response.setHeaders()`

* `headers` {Headers|Map}

Sets multiple header values for implicit headers.
`headers` must be an instance of \[`Headers`]\[] or `Map`.
If a header already exists in the to-be-sent headers,
its value will be replaced.

#### `response.setTimeout(msecs[, callback])`

<!-- YAML
Expand Down
19 changes: 19 additions & 0 deletions lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const {
ERR_HTTP2_NO_SOCKET_MANIPULATION,
ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED,
ERR_HTTP2_STATUS_INVALID,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_INVALID_HTTP_TOKEN,
ERR_STREAM_WRITE_AFTER_END,
Expand Down Expand Up @@ -630,6 +631,24 @@ class Http2ServerResponse extends Stream {
this[kSetHeader](name, value);
}

setHeaders(headers) {
if (this[kStream].headersSent)
throw new ERR_HTTP2_HEADERS_SENT();

if (
!headers ||
ArrayIsArray(headers) ||
typeof headers.keys !== 'function' ||
typeof headers.get !== 'function'
) {
throw new ERR_INVALID_ARG_TYPE('headers', ['Headers', 'Map'], headers);
}

for (const key of headers.keys()) {
this.setHeader(key, headers.get(key));
}
}

[kSetHeader](name, value) {
name = name.trim().toLowerCase();
assertValidHeader(name, value);
Expand Down
46 changes: 46 additions & 0 deletions test/parallel/test-http2-server-set-headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const http2 = require('http2');
const body =
'<html><head></head><body><h1>this is some data</h2></body></html>';

const server = http2.createServer((req, res) => {
res.setHeaders(new Map([
['foobar', 'baz'],
['X-POWERED-BY', 'node-test'],
['connection', 'connection-test'],
]));
res.end(body);
});

server.listen(0, common.mustCall(() => {
const client = http2.connect(`http://localhost:${server.address().port}`);
const headers = { ':path': '/' };
const req = client.request(headers);
req.setEncoding('utf8');
req.on('response', common.mustCall(function(headers) {
assert.strictEqual(headers.foobar, 'baz');
assert.strictEqual(headers['x-powered-by'], 'node-test');
}));

let data = '';
req.on('data', (d) => data += d);
req.on('end', () => {
assert.strictEqual(body, data);
server.close();
client.close();
});
req.end();
}));

const compatMsg = 'The provided connection header is not valid, ' +
'the value will be dropped from the header and ' +
'will never be in use.';

common.expectWarning('UnsupportedWarning', compatMsg);

server.on('error', common.mustNotCall());
Loading