Skip to content

Commit

Permalink
http: make socketPath work with no agent
Browse files Browse the repository at this point in the history
Currently `Agent.prototype.createConnection()` is called uncoditionally
if the `socketPath` option is used. This throws an error if no agent is
used, preventing, for example, the `socketPath` and `createConnection`
options to be used together.

This commit fixes the issue by falling back to the `createConnection`
option or `net.createConnection()`.

PR-URL: #19425
Reviewed-By: Rod Vagg <rod@vagg.org>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Matheus Marchini <matheus@sthima.com>
Reviewed-By: Chen Gang <gangc.cxy@foxmail.com>
  • Loading branch information
lpinca authored and MylesBorins committed Apr 13, 2018
1 parent 1edadeb commit 18acad3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/_http_client.js
Expand Up @@ -138,7 +138,11 @@ function ClientRequest(options, cb) {
timeout: self.timeout,
rejectUnauthorized: !!options.rejectUnauthorized
};
const newSocket = self.agent.createConnection(optionsPath, oncreate);
const newSocket = self.agent
? self.agent.createConnection(optionsPath, oncreate)
: typeof options.createConnection === 'function'
? options.createConnection(optionsPath, oncreate)
: net.createConnection(optionsPath);
if (newSocket && !called) {
called = true;
self.onSocket(newSocket);
Expand Down
28 changes: 28 additions & 0 deletions test/parallel/test-http-client-unix-socket-no-agent.js
@@ -0,0 +1,28 @@
'use strict';
const common = require('../common');
const Countdown = require('../common/countdown');

const http = require('http');
const { createConnection } = require('net');

const server = http.createServer((req, res) => {
res.end();
});

const countdown = new Countdown(2, () => {
server.close();
});

common.refreshTmpDir();

server.listen(common.PIPE, common.mustCall(() => {
http.get({ createConnection, socketPath: common.PIPE }, onResponse);
http.get({ agent: 0, socketPath: common.PIPE }, onResponse);
}));

function onResponse(res) {
res.on('end', () => {
countdown.dec();
});
res.resume();
}

0 comments on commit 18acad3

Please sign in to comment.