Skip to content

Commit da8fa48

Browse files
theanarkhrichardlau
authored andcommitted
lib: fix http client socket path
PR-URL: #51900 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 208dd88 commit da8fa48

File tree

2 files changed

+68
-5
lines changed

2 files changed

+68
-5
lines changed

lib/_http_client.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,16 @@ function ClientRequest(input, options, cb) {
336336
// No agent, default to Connection:close.
337337
this._last = true;
338338
this.shouldKeepAlive = false;
339-
if (typeof optsWithoutSignal.createConnection === 'function') {
339+
let opts = optsWithoutSignal;
340+
if (opts.path || opts.socketPath) {
341+
opts = { ...optsWithoutSignal };
342+
if (opts.socketPath) {
343+
opts.path = opts.socketPath;
344+
} else if (opts.path) {
345+
opts.path = undefined;
346+
}
347+
}
348+
if (typeof opts.createConnection === 'function') {
340349
const oncreate = once((err, socket) => {
341350
if (err) {
342351
process.nextTick(() => this.emit('error', err));
@@ -346,17 +355,16 @@ function ClientRequest(input, options, cb) {
346355
});
347356

348357
try {
349-
const newSocket = optsWithoutSignal.createConnection(optsWithoutSignal,
350-
oncreate);
358+
const newSocket = opts.createConnection(opts, oncreate);
351359
if (newSocket) {
352360
oncreate(null, newSocket);
353361
}
354362
} catch (err) {
355363
oncreate(err);
356364
}
357365
} else {
358-
debug('CLIENT use net.createConnection', optsWithoutSignal);
359-
this.onSocket(net.createConnection(optsWithoutSignal));
366+
debug('CLIENT use net.createConnection', opts);
367+
this.onSocket(net.createConnection(opts));
360368
}
361369
}
362370
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
const common = require('../common');
3+
const http = require('http');
4+
const net = require('net');
5+
const tmpdir = require('../common/tmpdir');
6+
7+
tmpdir.refresh();
8+
9+
let count = 0;
10+
let server1;
11+
let server2;
12+
13+
function request(options) {
14+
count++;
15+
http.get({
16+
...options,
17+
createConnection: (...args) => {
18+
return net.connect(...args);
19+
}
20+
}, (res) => {
21+
res.resume();
22+
res.on('end', () => {
23+
if (--count === 0) {
24+
server1.close();
25+
server2.close();
26+
}
27+
});
28+
});
29+
}
30+
31+
server1 = http.createServer((req, res) => {
32+
res.end('ok');
33+
}).listen(common.PIPE, () => {
34+
server2 = http.createServer((req, res) => {
35+
res.end('ok');
36+
}).listen(() => {
37+
request({
38+
path: '/',
39+
socketPath: common.PIPE,
40+
});
41+
42+
request({
43+
socketPath: common.PIPE,
44+
});
45+
46+
request({
47+
path: '/',
48+
port: server2.address().port,
49+
});
50+
51+
request({
52+
port: server2.address().port,
53+
});
54+
});
55+
});

0 commit comments

Comments
 (0)