Skip to content

Commit 4686cb5

Browse files
simovTrott
authored andcommitted
http: add http.ClientRequest.getRawHeaderNames()
Fixes: #37641 PR-URL: #37660 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent eaadc4b commit 4686cb5

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

doc/api/deprecations.md

+1
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,7 @@ The `http` module `OutgoingMessage.prototype._headers` and
13471347
the public methods (e.g. `OutgoingMessage.prototype.getHeader()`,
13481348
`OutgoingMessage.prototype.getHeaders()`,
13491349
`OutgoingMessage.prototype.getHeaderNames()`,
1350+
`OutgoingMessage.prototype.getRawHeaderNames()`,
13501351
`OutgoingMessage.prototype.hasHeader()`,
13511352
`OutgoingMessage.prototype.removeHeader()`,
13521353
`OutgoingMessage.prototype.setHeader()`) for working with outgoing headers.

doc/api/http.md

+18
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,24 @@ const cookie = request.getHeader('Cookie');
777777
// 'cookie' is of type string[]
778778
```
779779

780+
### `request.getRawHeaderNames()`
781+
<!-- YAML
782+
added: REPLACEME
783+
-->
784+
785+
* Returns: {string[]}
786+
787+
Returns an array containing the unique names of the current outgoing raw
788+
headers. Header names are returned with their exact casing being set.
789+
790+
```js
791+
request.setHeader('Foo', 'bar');
792+
request.setHeader('Set-Cookie', ['foo=bar', 'bar=baz']);
793+
794+
const headerNames = request.getRawHeaderNames();
795+
// headerNames === ['Foo', 'Set-Cookie']
796+
```
797+
780798
### `request.maxHeadersCount`
781799

782800
* {number} **Default:** `2000`

lib/_http_outgoing.js

+19
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
'use strict';
2323

2424
const {
25+
Array,
2526
ArrayIsArray,
2627
ArrayPrototypeForEach,
2728
ArrayPrototypeJoin,
@@ -36,6 +37,7 @@ const {
3637
ObjectCreate,
3738
ObjectDefineProperty,
3839
ObjectKeys,
40+
ObjectValues,
3941
ObjectPrototypeHasOwnProperty,
4042
ObjectSetPrototypeOf,
4143
RegExpPrototypeTest,
@@ -656,6 +658,23 @@ OutgoingMessage.prototype.getHeaderNames = function getHeaderNames() {
656658
};
657659

658660

661+
// Returns an array of the names of the current outgoing raw headers.
662+
OutgoingMessage.prototype.getRawHeaderNames = function getRawHeaderNames() {
663+
const headersMap = this[kOutHeaders];
664+
if (headersMap === null) return [];
665+
666+
const values = ObjectValues(headersMap);
667+
const headers = Array(values.length);
668+
// Retain for(;;) loop for performance reasons
669+
// Refs: https://github.com/nodejs/node/pull/30958
670+
for (let i = 0, l = values.length; i < l; i++) {
671+
headers[i] = values[i][0];
672+
}
673+
674+
return headers;
675+
};
676+
677+
659678
// Returns a shallow copy of the current outgoing headers.
660679
OutgoingMessage.prototype.getHeaders = function getHeaders() {
661680
const headers = this[kOutHeaders];

test/parallel/test-http-mutable-headers.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ const s = http.createServer(common.mustCall((req, res) => {
108108
['x-test-header', 'x-test-header2',
109109
'set-cookie', 'x-test-array-header']);
110110

111+
assert.deepStrictEqual(res.getRawHeaderNames(),
112+
['x-test-header', 'X-TEST-HEADER2',
113+
'set-cookie', 'x-test-array-header']);
114+
111115
assert.strictEqual(res.hasHeader('x-test-header2'), true);
112116
assert.strictEqual(res.hasHeader('X-TEST-HEADER2'), true);
113117
assert.strictEqual(res.hasHeader('X-Test-Header2'), true);
@@ -171,7 +175,10 @@ function nextTest() {
171175

172176
let bufferedResponse = '';
173177

174-
http.get({ port: s.address().port }, common.mustCall((response) => {
178+
const req = http.get({
179+
port: s.address().port,
180+
headers: { 'X-foo': 'bar' }
181+
}, common.mustCall((response) => {
175182
switch (test) {
176183
case 'headers':
177184
assert.strictEqual(response.statusCode, 201);
@@ -214,4 +221,10 @@ function nextTest() {
214221
common.mustCall(nextTest)();
215222
}));
216223
}));
224+
225+
assert.deepStrictEqual(req.getHeaderNames(),
226+
['x-foo', 'host']);
227+
228+
assert.deepStrictEqual(req.getRawHeaderNames(),
229+
['X-foo', 'Host']);
217230
}

0 commit comments

Comments
 (0)