Skip to content

Commit

Permalink
http: fix http client socket path
Browse files Browse the repository at this point in the history
  • Loading branch information
theanarkh committed Feb 27, 2024
1 parent 1263bb6 commit f8cfaa7
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,16 @@ function ClientRequest(input, options, cb) {
// No agent, default to Connection:close.
this._last = true;
this.shouldKeepAlive = false;
if (typeof optsWithoutSignal.createConnection === 'function') {
let opts = optsWithoutSignal;
if (opts.path || opts.socketPath) {
opts = { ...optsWithoutSignal };
if (opts.socketPath) {
opts.path = opts.socketPath;
} else if (opts.path) {
delete opts.path;
}
}
if (typeof opts.createConnection === 'function') {
const oncreate = once((err, socket) => {
if (err) {
process.nextTick(() => this.emit('error', err));
Expand All @@ -346,17 +355,16 @@ function ClientRequest(input, options, cb) {
});

try {
const newSocket = optsWithoutSignal.createConnection(optsWithoutSignal,
oncreate);
const newSocket = opts.createConnection(opts, oncreate);
if (newSocket) {
oncreate(null, newSocket);
}
} catch (err) {
oncreate(err);
}
} else {
debug('CLIENT use net.createConnection', optsWithoutSignal);
this.onSocket(net.createConnection(optsWithoutSignal));
debug('CLIENT use net.createConnection', opts);
this.onSocket(net.createConnection(opts));
}
}
}
Expand Down
55 changes: 55 additions & 0 deletions test/parallel/test-http-client-with-create-connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';
const common = require('../common');
const http = require('http');
const net = require('net');
const tmpdir = require('../common/tmpdir');

tmpdir.refresh();

let count = 0;
let server1;
let server2;

function request(options) {
count++;
http.get({
...options,
createConnection: (...args) => {
return net.connect(...args);
}
}, (res) => {
res.resume();
res.on('end', () => {
if (--count === 0) {
server1.close();
server2.close();
}
});
});
}

server1 = http.createServer((req, res) => {
res.end('ok');
}).listen(common.PIPE, () => {
server2 = http.createServer((req, res) => {
res.end('ok');
}).listen(() => {
request({
path: '/',
socketPath: common.PIPE,
});

request({
socketPath: common.PIPE,
});

request({
path: '/',
port: server2.address().port,
});

request({
port: server2.address().port,
});
});
});

0 comments on commit f8cfaa7

Please sign in to comment.