http: add setDefaultHeaders option to http.request · nodejs/node@79152b5 · GitHub
Skip to content

Commit 79152b5

Browse 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

5 files changed

Lines changed: 100 additions & 2 deletions

doc/api/http.md

Lines changed: 6 additions & 1 deletion

lib/_http_client.js

Lines 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

Lines 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+
});
Lines 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+
});
Lines changed: 31 additions & 0 deletions

0 commit comments

Comments
 (0)