Skip to content

Commit

Permalink
http: do not override user-provided options object
Browse files Browse the repository at this point in the history
PR-URL: #33633
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Paolo Insogna <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
KuthorX authored and richardlau committed Mar 25, 2024
1 parent 0ac070c commit 9062d30
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
15 changes: 6 additions & 9 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,11 @@ function ClientRequest(input, options, cb) {
const defaultPort = options.defaultPort ||
(this.agent && this.agent.defaultPort);

const port = options.port = options.port || defaultPort || 80;
const host = options.host = validateHost(options.hostname, 'hostname') ||
validateHost(options.host, 'host') || 'localhost';
const optsWithoutSignal = { __proto__: null, ...options };

const port = optsWithoutSignal.port = options.port || defaultPort || 80;
const host = optsWithoutSignal.host = validateHost(options.hostname, 'hostname') ||
validateHost(options.host, 'host') || 'localhost';

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

Expand All @@ -200,6 +202,7 @@ function ClientRequest(input, options, cb) {
const signal = options.signal;
if (signal) {
addAbortSignal(signal, this);
delete optsWithoutSignal.signal;
}
let method = options.method;
const methodIsString = (typeof method === 'string');
Expand Down Expand Up @@ -326,12 +329,6 @@ function ClientRequest(input, options, cb) {

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

let optsWithoutSignal = options;
if (optsWithoutSignal.signal) {
optsWithoutSignal = ObjectAssign({}, options);
delete optsWithoutSignal.signal;
}

// initiate connection
if (this.agent) {
this.agent.addRequest(this, optsWithoutSignal);
Expand Down
29 changes: 29 additions & 0 deletions test/parallel/test-http-client-input-function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

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

{
const server = http.createServer(common.mustCall((req, res) => {
res.writeHead(200);
res.end('hello world');
})).listen(0, '127.0.0.1');

server.on('listening', common.mustCall(() => {
const req = new http.ClientRequest(server.address(), common.mustCall((response) => {
let body = '';
response.setEncoding('utf8');
response.on('data', (chunk) => {
body += chunk;
});

response.on('end', common.mustCall(() => {
assert.strictEqual(body, 'hello world');
server.close();
}));
}));

req.end();
}));
}
14 changes: 14 additions & 0 deletions test/parallel/test-http-client-insecure-http-parser-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

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

{
assert.throws(() => {
new ClientRequest({ insecureHTTPParser: 'wrongValue' });
}, {
code: 'ERR_INVALID_ARG_TYPE',
message: /insecureHTTPParser/
}, 'http request should throw when passing invalid insecureHTTPParser');
}

0 comments on commit 9062d30

Please sign in to comment.