Skip to content

Commit a520581

Browse files
simovruyadorno
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 e85f311 commit a520581

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
@@ -763,6 +763,24 @@ const cookie = request.getHeader('Cookie');
763763
// 'cookie' is of type string[]
764764
```
765765

766+
### `request.getRawHeaderNames()`
767+
<!-- YAML
768+
added: REPLACEME
769+
-->
770+
771+
* Returns: {string[]}
772+
773+
Returns an array containing the unique names of the current outgoing raw
774+
headers. Header names are returned with their exact casing being set.
775+
776+
```js
777+
request.setHeader('Foo', 'bar');
778+
request.setHeader('Set-Cookie', ['foo=bar', 'bar=baz']);
779+
780+
const headerNames = request.getRawHeaderNames();
781+
// headerNames === ['Foo', 'Set-Cookie']
782+
```
783+
766784
### `request.maxHeadersCount`
767785

768786
* {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,
@@ -35,6 +36,7 @@ const {
3536
ObjectCreate,
3637
ObjectDefineProperty,
3738
ObjectKeys,
39+
ObjectValues,
3840
ObjectPrototypeHasOwnProperty,
3941
ObjectSetPrototypeOf,
4042
RegExpPrototypeTest,
@@ -602,6 +604,23 @@ OutgoingMessage.prototype.getHeaderNames = function getHeaderNames() {
602604
};
603605

604606

607+
// Returns an array of the names of the current outgoing raw headers.
608+
OutgoingMessage.prototype.getRawHeaderNames = function getRawHeaderNames() {
609+
const headersMap = this[kOutHeaders];
610+
if (headersMap === null) return [];
611+
612+
const values = ObjectValues(headersMap);
613+
const headers = Array(values.length);
614+
// Retain for(;;) loop for performance reasons
615+
// Refs: https://github.com/nodejs/node/pull/30958
616+
for (let i = 0, l = values.length; i < l; i++) {
617+
headers[i] = values[i][0];
618+
}
619+
620+
return headers;
621+
};
622+
623+
605624
// Returns a shallow copy of the current outgoing headers.
606625
OutgoingMessage.prototype.getHeaders = function getHeaders() {
607626
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)