Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 79152b5

Browse filesBrowse files
pimterryruyadorno
authored andcommitted
http: add setDefaultHeaders option to http.request
This makes it possible to disable the various default headers directly from the constructor. While this is possible for many use cases by manually calling removeHeader on the request object instead, when passing a raw header array to the request constructor the headers are serialized and prepared to send immediately, and removeHeader cannot subsequently be used. With this change, it's now possible to 100% control sent request headers by passing 'setDefaultHeaders: false' and a raw headers array to http.request. PR-URL: #56112 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 8494512 commit 79152b5
Copy full SHA for 79152b5

File tree

Expand file treeCollapse file tree

5 files changed

+100
-2
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+100
-2
lines changed
Open diff view settings
Collapse file

‎doc/api/http.md‎

Copy file name to clipboardExpand all lines: doc/api/http.md
+6-1Lines changed: 6 additions & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -3848,8 +3848,13 @@ changes:
38483848
* `port` {number} Port of remote server. **Default:** `defaultPort` if set,
38493849
else `80`.
38503850
* `protocol` {string} Protocol to use. **Default:** `'http:'`.
3851+
* `setDefaultHeaders` {boolean}: Specifies whether or not to automatically add
3852+
default headers such as `Connection`, `Content-Length`, `Transfer-Encoding`,
3853+
and `Host`. If set to `false` then all necessary headers must be added
3854+
manually. Defaults to `true`.
38513855
* `setHost` {boolean}: Specifies whether or not to automatically add the
3852-
`Host` header. Defaults to `true`.
3856+
`Host` header. If provided, this overrides `setDefaultHeaders`. Defaults to
3857+
`true`.
38533858
* `signal` {AbortSignal}: An AbortSignal that may be used to abort an ongoing
38543859
request.
38553860
* `socketPath` {string} Unix domain socket. Cannot be used if one of `host`
Collapse file

‎lib/_http_client.js‎

Copy file name to clipboardExpand all lines: lib/_http_client.js
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,13 @@ function ClientRequest(input, options, cb) {
199199
const host = optsWithoutSignal.host = validateHost(options.hostname, 'hostname') ||
200200
validateHost(options.host, 'host') || 'localhost';
201201

202-
const setHost = (options.setHost === undefined || Boolean(options.setHost));
202+
const setHost = options.setHost !== undefined ?
203+
Boolean(options.setHost) :
204+
options.setDefaultHeaders !== false;
205+
206+
this._removedConnection = options.setDefaultHeaders === false;
207+
this._removedContLen = options.setDefaultHeaders === false;
208+
this._removedTE = options.setDefaultHeaders === false;
203209

204210
this.socketPath = options.socketPath;
205211

Collapse file
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const http = require('http');
6+
7+
const server = http.createServer(common.mustCall(function(req, res) {
8+
assert.deepStrictEqual(req.rawHeaders, [
9+
'test', 'value',
10+
'HOST', `127.0.0.1:${server.address().port}`,
11+
'foo', 'bar',
12+
'foo', 'baz',
13+
'connection', 'close',
14+
]);
15+
16+
res.end('ok');
17+
server.close();
18+
}));
19+
server.listen(0, common.localhostIPv4, function() {
20+
const req = http.request({
21+
method: 'POST',
22+
host: common.localhostIPv4,
23+
port: this.address().port,
24+
setDefaultHeaders: false,
25+
});
26+
27+
req.setHeader('test', 'value');
28+
req.setHeader('HOST', `${common.localhostIPv4}:${server.address().port}`);
29+
req.setHeader('foo', ['bar', 'baz']);
30+
req.setHeader('connection', 'close');
31+
32+
req.end();
33+
});
Collapse file
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const http = require('http');
6+
7+
const server = http.createServer(common.mustCall(function(req, res) {
8+
assert.deepStrictEqual(req.rawHeaders, [
9+
'Host', `${common.localhostIPv4}:${server.address().port}`,
10+
]);
11+
12+
res.end('ok');
13+
server.close();
14+
}));
15+
server.listen(0, common.localhostIPv4, function() {
16+
http.request({
17+
method: 'POST',
18+
host: common.localhostIPv4,
19+
port: this.address().port,
20+
setDefaultHeaders: false,
21+
setHost: true
22+
}).end();
23+
});
Collapse file
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const http = require('http');
6+
7+
const server = http.createServer(common.mustCall(function(req, res) {
8+
assert.deepStrictEqual(req.rawHeaders, [
9+
'host', `${common.localhostIPv4}:${server.address().port}`,
10+
'foo', 'bar',
11+
'test', 'value',
12+
'foo', 'baz',
13+
]);
14+
15+
res.end('ok');
16+
server.close();
17+
}));
18+
server.listen(0, common.localhostIPv4, function() {
19+
http.request({
20+
method: 'POST',
21+
host: common.localhostIPv4,
22+
port: this.address().port,
23+
setDefaultHeaders: false,
24+
headers: [
25+
'host', `${common.localhostIPv4}:${server.address().port}`,
26+
'foo', 'bar',
27+
'test', 'value',
28+
'foo', 'baz',
29+
]
30+
}).end();
31+
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.