Skip to content

Commit

Permalink
http: add http.ClientRequest.getRawHeaderNames()
Browse files Browse the repository at this point in the history
Fixes: #37641

PR-URL: #37660
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
simov authored and Trott committed Mar 20, 2021
1 parent eaadc4b commit 4686cb5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
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
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']);
}

0 comments on commit 4686cb5

Please sign in to comment.