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: add http.ClientRequest.getRawHeaderNames() #37660

Merged
merged 1 commit into from Mar 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/api/deprecations.md
Expand Up @@ -1347,6 +1347,7 @@ The `http` module `OutgoingMessage.prototype._headers` and
the public methods (e.g. `OutgoingMessage.prototype.getHeader()`,
`OutgoingMessage.prototype.getHeaders()`,
`OutgoingMessage.prototype.getHeaderNames()`,
`OutgoingMessage.prototype.getRawHeaderNames()`,
`OutgoingMessage.prototype.hasHeader()`,
`OutgoingMessage.prototype.removeHeader()`,
`OutgoingMessage.prototype.setHeader()`) for working with outgoing headers.
Expand Down
18 changes: 18 additions & 0 deletions doc/api/http.md
Expand Up @@ -777,6 +777,24 @@ const cookie = request.getHeader('Cookie');
// 'cookie' is of type string[]
```

### `request.getRawHeaderNames()`
<!-- YAML
added: REPLACEME
-->

* Returns: {string[]}

Returns an array containing the unique names of the current outgoing raw
mcollina marked this conversation as resolved.
Show resolved Hide resolved
headers. Header names are returned with their exact casing being set.

```js
request.setHeader('Foo', 'bar');
request.setHeader('Set-Cookie', ['foo=bar', 'bar=baz']);

const headerNames = request.getRawHeaderNames();
// headerNames === ['Foo', 'Set-Cookie']
```

### `request.maxHeadersCount`

* {number} **Default:** `2000`
Expand Down
19 changes: 19 additions & 0 deletions lib/_http_outgoing.js
Expand Up @@ -22,6 +22,7 @@
'use strict';

const {
Array,
ArrayIsArray,
ArrayPrototypeForEach,
ArrayPrototypeJoin,
Expand All @@ -36,6 +37,7 @@ const {
ObjectCreate,
ObjectDefineProperty,
ObjectKeys,
ObjectValues,
ObjectPrototypeHasOwnProperty,
ObjectSetPrototypeOf,
RegExpPrototypeTest,
Expand Down Expand Up @@ -656,6 +658,23 @@ OutgoingMessage.prototype.getHeaderNames = function getHeaderNames() {
};


// Returns an array of the names of the current outgoing raw headers.
OutgoingMessage.prototype.getRawHeaderNames = function getRawHeaderNames() {
const headersMap = this[kOutHeaders];
if (headersMap === null) return [];

const values = ObjectValues(headersMap);
const headers = Array(values.length);
// Retain for(;;) loop for performance reasons
// Refs: https://github.com/nodejs/node/pull/30958
for (let i = 0, l = values.length; i < l; i++) {
headers[i] = values[i][0];
}

return headers;
};


// Returns a shallow copy of the current outgoing headers.
OutgoingMessage.prototype.getHeaders = function getHeaders() {
const headers = this[kOutHeaders];
Expand Down
15 changes: 14 additions & 1 deletion test/parallel/test-http-mutable-headers.js
Expand Up @@ -108,6 +108,10 @@ const s = http.createServer(common.mustCall((req, res) => {
['x-test-header', 'x-test-header2',
'set-cookie', 'x-test-array-header']);

assert.deepStrictEqual(res.getRawHeaderNames(),
['x-test-header', 'X-TEST-HEADER2',
'set-cookie', 'x-test-array-header']);

assert.strictEqual(res.hasHeader('x-test-header2'), true);
assert.strictEqual(res.hasHeader('X-TEST-HEADER2'), true);
assert.strictEqual(res.hasHeader('X-Test-Header2'), true);
Expand Down Expand Up @@ -171,7 +175,10 @@ function nextTest() {

let bufferedResponse = '';

http.get({ port: s.address().port }, common.mustCall((response) => {
const req = http.get({
port: s.address().port,
headers: { 'X-foo': 'bar' }
}, common.mustCall((response) => {
switch (test) {
case 'headers':
assert.strictEqual(response.statusCode, 201);
Expand Down Expand Up @@ -214,4 +221,10 @@ function nextTest() {
common.mustCall(nextTest)();
}));
}));

assert.deepStrictEqual(req.getHeaderNames(),
['x-foo', 'host']);

assert.deepStrictEqual(req.getRawHeaderNames(),
['X-foo', 'Host']);
}