Skip to content

Commit a325746

Browse files
KuthorXRafaelGSS
authored andcommitted
http: do not override user-provided options object
PR-URL: #33633 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Paolo Insogna <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent b2135ae commit a325746

3 files changed

+49
-9
lines changed

lib/_http_client.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,11 @@ function ClientRequest(input, options, cb) {
186186
const defaultPort = options.defaultPort ||
187187
(this.agent && this.agent.defaultPort);
188188

189-
const port = options.port = options.port || defaultPort || 80;
190-
const host = options.host = validateHost(options.hostname, 'hostname') ||
191-
validateHost(options.host, 'host') || 'localhost';
189+
const optsWithoutSignal = { __proto__: null, ...options };
190+
191+
const port = optsWithoutSignal.port = options.port || defaultPort || 80;
192+
const host = optsWithoutSignal.host = validateHost(options.hostname, 'hostname') ||
193+
validateHost(options.host, 'host') || 'localhost';
192194

193195
const setHost = (options.setHost === undefined || Boolean(options.setHost));
194196

@@ -200,6 +202,7 @@ function ClientRequest(input, options, cb) {
200202
const signal = options.signal;
201203
if (signal) {
202204
addAbortSignal(signal, this);
205+
delete optsWithoutSignal.signal;
203206
}
204207
let method = options.method;
205208
const methodIsString = (typeof method === 'string');
@@ -326,12 +329,6 @@ function ClientRequest(input, options, cb) {
326329

327330
this[kUniqueHeaders] = parseUniqueHeadersOption(options.uniqueHeaders);
328331

329-
let optsWithoutSignal = options;
330-
if (optsWithoutSignal.signal) {
331-
optsWithoutSignal = ObjectAssign({}, options);
332-
delete optsWithoutSignal.signal;
333-
}
334-
335332
// initiate connection
336333
if (this.agent) {
337334
this.agent.addRequest(this, optsWithoutSignal);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const http = require('http');
5+
const assert = require('assert');
6+
7+
{
8+
const server = http.createServer(common.mustCall((req, res) => {
9+
res.writeHead(200);
10+
res.end('hello world');
11+
})).listen(0, '127.0.0.1');
12+
13+
server.on('listening', common.mustCall(() => {
14+
const req = new http.ClientRequest(server.address(), common.mustCall((response) => {
15+
let body = '';
16+
response.setEncoding('utf8');
17+
response.on('data', (chunk) => {
18+
body += chunk;
19+
});
20+
21+
response.on('end', common.mustCall(() => {
22+
assert.strictEqual(body, 'hello world');
23+
server.close();
24+
}));
25+
}));
26+
27+
req.end();
28+
}));
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const ClientRequest = require('http').ClientRequest;
6+
7+
{
8+
assert.throws(() => {
9+
new ClientRequest({ insecureHTTPParser: 'wrongValue' });
10+
}, {
11+
code: 'ERR_INVALID_ARG_TYPE',
12+
message: /insecureHTTPParser/
13+
}, 'http request should throw when passing invalid insecureHTTPParser');
14+
}

0 commit comments

Comments
 (0)