Skip to content

Commit d0115f1

Browse files
simovtargos
authored andcommitted
http: add http.ClientRequest.getRawHeaderNames()
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>
1 parent e6a7980 commit d0115f1

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
@@ -1340,6 +1340,7 @@ The `http` module `OutgoingMessage.prototype._headers` and
13401340
the public methods (e.g. `OutgoingMessage.prototype.getHeader()`,
13411341
`OutgoingMessage.prototype.getHeaders()`,
13421342
`OutgoingMessage.prototype.getHeaderNames()`,
1343+
`OutgoingMessage.prototype.getRawHeaderNames()`,
13431344
`OutgoingMessage.prototype.hasHeader()`,
13441345
`OutgoingMessage.prototype.removeHeader()`,
13451346
`OutgoingMessage.prototype.setHeader()`) for working with outgoing headers.

doc/api/http.md

+18
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,24 @@ const cookie = request.getHeader('Cookie');
756756
// 'cookie' is of type string[]
757757
```
758758

759+
### `request.getRawHeaderNames()`
760+
<!-- YAML
761+
added: REPLACEME
762+
-->
763+
764+
* Returns: {string[]}
765+
766+
Returns an array containing the unique names of the current outgoing raw
767+
headers. Header names are returned with their exact casing being set.
768+
769+
```js
770+
request.setHeader('Foo', 'bar');
771+
request.setHeader('Set-Cookie', ['foo=bar', 'bar=baz']);
772+
773+
const headerNames = request.getRawHeaderNames();
774+
// headerNames === ['Foo', 'Set-Cookie']
775+
```
776+
759777
### `request.maxHeadersCount`
760778

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

lib/_http_outgoing.js

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

2424
const {
25+
Array,
2526
ArrayIsArray,
2627
ObjectCreate,
2728
ObjectDefineProperty,
2829
ObjectKeys,
30+
ObjectValues,
2931
ObjectPrototypeHasOwnProperty,
3032
ObjectSetPrototypeOf,
3133
MathFloor,
@@ -588,6 +590,23 @@ OutgoingMessage.prototype.getHeaderNames = function getHeaderNames() {
588590
};
589591

590592

593+
// Returns an array of the names of the current outgoing raw headers.
594+
OutgoingMessage.prototype.getRawHeaderNames = function getRawHeaderNames() {
595+
const headersMap = this[kOutHeaders];
596+
if (headersMap === null) return [];
597+
598+
const values = ObjectValues(headersMap);
599+
const headers = Array(values.length);
600+
// Retain for(;;) loop for performance reasons
601+
// Refs: https://github.com/nodejs/node/pull/30958
602+
for (let i = 0, l = values.length; i < l; i++) {
603+
headers[i] = values[i][0];
604+
}
605+
606+
return headers;
607+
};
608+
609+
591610
// Returns a shallow copy of the current outgoing headers.
592611
OutgoingMessage.prototype.getHeaders = function getHeaders() {
593612
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)