From 18acad349c0f0bacdd7e318a5c7a509cad9574c9 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Sun, 18 Mar 2018 10:59:44 +0100 Subject: [PATCH] http: make socketPath work with no agent 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: https://github.com/nodejs/node/pull/19425 Reviewed-By: Rod Vagg Reviewed-By: Matteo Collina Reviewed-By: Matheus Marchini Reviewed-By: Chen Gang --- lib/_http_client.js | 6 +++- .../test-http-client-unix-socket-no-agent.js | 28 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-http-client-unix-socket-no-agent.js diff --git a/lib/_http_client.js b/lib/_http_client.js index 9d2057814133b7..ea181cff31a482 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -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); diff --git a/test/parallel/test-http-client-unix-socket-no-agent.js b/test/parallel/test-http-client-unix-socket-no-agent.js new file mode 100644 index 00000000000000..9dd11403c6bf79 --- /dev/null +++ b/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(); +}