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

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

‎lib/_http_client.js‎

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

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

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

@@ -200,6 +202,7 @@ function ClientRequest(input, options, cb){
200202
constsignal=options.signal;
201203
if(signal){
202204
addAbortSignal(signal,this);
205+
deleteoptsWithoutSignal.signal;
203206
}
204207
letmethod=options.method;
205208
constmethodIsString=(typeofmethod==='string');
@@ -326,12 +329,6 @@ function ClientRequest(input, options, cb){
326329

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

329-
letoptsWithoutSignal=options;
330-
if(optsWithoutSignal.signal){
331-
optsWithoutSignal=ObjectAssign({},options);
332-
deleteoptsWithoutSignal.signal;
333-
}
334-
335332
// initiate connection
336333
if(this.agent){
337334
this.agent.addRequest(this,optsWithoutSignal);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
constcommon=require('../common');
4+
consthttp=require('http');
5+
constassert=require('assert');
6+
7+
{
8+
constserver=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+
constreq=newhttp.ClientRequest(server.address(),common.mustCall((response)=>{
15+
letbody='';
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+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
require('../common');
4+
constassert=require('assert');
5+
constClientRequest=require('http').ClientRequest;
6+
7+
{
8+
assert.throws(()=>{
9+
newClientRequest({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)