Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: add setDefaultHeaders option to http.request #56112

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -3848,8 +3848,13 @@ changes:
* `port` {number} Port of remote server. **Default:** `defaultPort` if set,
else `80`.
* `protocol` {string} Protocol to use. **Default:** `'http:'`.
* `setDefaultHeaders` {boolean}: Specifies whether or not to automatically add
default headers such as `Connection`, `Content-Length`, `Transfer-Encoding`,
and `Host`. If set to `false` then all necessary headers must be added
manually. Defaults to `true`.
* `setHost` {boolean}: Specifies whether or not to automatically add the
`Host` header. Defaults to `true`.
`Host` header. If provided, this overrides `setDefaultHeaders`. Defaults to
`true`.
* `signal` {AbortSignal}: An AbortSignal that may be used to abort an ongoing
request.
* `socketPath` {string} Unix domain socket. Cannot be used if one of `host`
Expand Down
8 changes: 7 additions & 1 deletion lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,13 @@ function ClientRequest(input, options, cb) {
const host = optsWithoutSignal.host = validateHost(options.hostname, 'hostname') ||
validateHost(options.host, 'host') || 'localhost';

const setHost = (options.setHost === undefined || Boolean(options.setHost));
const setHost = options.setHost !== undefined ?
Boolean(options.setHost) :
options.setDefaultHeaders !== false;

this._removedConnection = options.setDefaultHeaders === false;
this._removedContLen = options.setDefaultHeaders === false;
this._removedTE = options.setDefaultHeaders === false;
jasnell marked this conversation as resolved.
Show resolved Hide resolved

this.socketPath = options.socketPath;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const http = require('http');

const server = http.createServer(common.mustCall(function(req, res) {
assert.deepStrictEqual(req.rawHeaders, [
'test', 'value',
'HOST', `127.0.0.1:${server.address().port}`,
'foo', 'bar',
'foo', 'baz',
'connection', 'close',
]);

res.end('ok');
server.close();
}));
server.listen(0, '127.0.0.1', function() {
const req = http.request({
method: 'POST',
host: '127.0.0.1',
port: this.address().port,
setDefaultHeaders: false,
});

req.setHeader('test', 'value');
req.setHeader('HOST', `127.0.0.1:${server.address().port}`);
req.setHeader('foo', ['bar', 'baz']);
req.setHeader('connection', 'close');

req.end();
});
pimterry marked this conversation as resolved.
Show resolved Hide resolved
23 changes: 23 additions & 0 deletions test/parallel/test-http-dont-set-default-headers-with-setHost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const http = require('http');

const server = http.createServer(common.mustCall(function(req, res) {
assert.deepStrictEqual(req.rawHeaders, [
'Host', `127.0.0.1:${server.address().port}`,
]);

res.end('ok');
server.close();
}));
server.listen(0, '127.0.0.1', function() {
http.request({
method: 'POST',
host: '127.0.0.1',
port: this.address().port,
setDefaultHeaders: false,
setHost: true
}).end();
});
pimterry marked this conversation as resolved.
Show resolved Hide resolved
31 changes: 31 additions & 0 deletions test/parallel/test-http-dont-set-default-headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const http = require('http');

const server = http.createServer(common.mustCall(function(req, res) {
assert.deepStrictEqual(req.rawHeaders, [
'host', `127.0.0.1:${server.address().port}`,
'foo', 'bar',
'test', 'value',
'foo', 'baz',
]);

res.end('ok');
server.close();
}));
server.listen(0, '127.0.0.1', function() {
http.request({
method: 'POST',
host: '127.0.0.1',
port: this.address().port,
setDefaultHeaders: false,
headers: [
'host', `127.0.0.1:${server.address().port}`,
'foo', 'bar',
'test', 'value',
'foo', 'baz',
]
}).end();
});
pimterry marked this conversation as resolved.
Show resolved Hide resolved
Loading