Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: fix http client socket path #51900

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
opts.path = undefined;
}
}
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,
});
});
});