diff --git a/src/lib/connectors/_custom_agent.js b/src/lib/connectors/_keep_alive_agent.js similarity index 100% rename from src/lib/connectors/_custom_agent.js rename to src/lib/connectors/_keep_alive_agent.js diff --git a/src/lib/connectors/http.js b/src/lib/connectors/http.js index fb054ddd0..d2e6b409a 100644 --- a/src/lib/connectors/http.js +++ b/src/lib/connectors/http.js @@ -14,7 +14,7 @@ var handles = { }; var _ = require('../utils'); var qs = require('querystring'); -var ForeverAgent = require('./_custom_agent'); +var KeepAliveAgent = require('./_keep_alive_agent'); var ConnectionAbstract = require('../connection'); var zlib = require('zlib'); @@ -34,6 +34,8 @@ function HttpConnector(host, config) { '", expected one of ' + _.keys(handles).join(', ')); } + this.useSsl = this.host.protocol === 'https'; + config = _.defaults(config || {}, { keepAlive: true, minSockets: 10, @@ -77,7 +79,7 @@ HttpConnector.prototype.createAgent = function (config) { } if (config.keepAlive) { - Agent = this.host.protocol === 'https' ? ForeverAgent.SSL : ForeverAgent; + Agent = this.useSsl ? KeepAliveAgent.SSL : KeepAliveAgent; this.on('status set', this.bound.onStatusSet); } @@ -85,10 +87,16 @@ HttpConnector.prototype.createAgent = function (config) { }; HttpConnector.prototype.makeAgentConfig = function (config) { - return { + var agentConfig = { maxSockets: config.maxSockets, minSockets: config.minSockets }; + + if (this.useSsl) { + _.merge(agentConfig, this.host.ssl); + } + + return agentConfig; }; HttpConnector.prototype.makeReqParams = function (params) { diff --git a/src/lib/host.js b/src/lib/host.js index 8d95779c0..05e30091b 100644 --- a/src/lib/host.js +++ b/src/lib/host.js @@ -23,6 +23,17 @@ var urlParseFields = [ var simplify = ['host', 'path']; +var sslDefaults = { + pfx: null, + key: null, + passphrase: null, + cert: null, + ca: null, + ciphers: null, + rejectUnauthorized: false, + secureProtocol: null +}; + // simple reference used when formatting as a url // and defines when parsing from a string Host.defaultPorts = { @@ -44,6 +55,8 @@ function Host(config, globalConfig) { this.headers = null; this.suggestCompression = !!globalConfig.suggestCompression; + this.ssl = _.defaults({}, config.ssl || {}, globalConfig.ssl || {}, sslDefaults); + if (typeof config === 'string') { var firstColon = config.indexOf(':'); var firstSlash = config.indexOf('/'); diff --git a/test/unit/specs/host.js b/test/unit/specs/host.js index 395123234..d77282eb3 100644 --- a/test/unit/specs/host.js +++ b/test/unit/specs/host.js @@ -12,7 +12,17 @@ var hostDefaults = { auth: null, query: {}, headers: null, - suggestCompression: false + suggestCompression: false, + ssl: { + pfx: null, + key: null, + passphrase: null, + cert: null, + ca: null, + ciphers: null, + rejectUnauthorized: false, + secureProtocol: null + } }; describe('Host class', function () { diff --git a/test/unit/specs/http_connector.js b/test/unit/specs/http_connector.js index def0b1569..84c78f177 100644 --- a/test/unit/specs/http_connector.js +++ b/test/unit/specs/http_connector.js @@ -7,7 +7,7 @@ describe('Http Connector', function () { var util = require('util'); var http = require('http'); var https = require('https'); - var CustomAgent = require('../../../src/lib/connectors/_custom_agent'); + var KeepAliveAgent = require('../../../src/lib/connectors/_keep_alive_agent'); var Host = require('../../../src/lib/host'); var errors = require('../../../src/lib/errors'); @@ -164,7 +164,7 @@ describe('Http Connector', function () { con.request({}, function () { expect(http.request.callCount).to.be(1); expect(https.request.callCount).to.be(0); - expect(http.request.lastCall.args[0].agent).to.be.a(CustomAgent); + expect(http.request.lastCall.args[0].agent).to.be.a(KeepAliveAgent); done(); }); }); @@ -174,7 +174,7 @@ describe('Http Connector', function () { con.request({}, function () { expect(http.request.callCount).to.be(0); expect(https.request.callCount).to.be(1); - expect(https.request.lastCall.args[0].agent).to.be.a(CustomAgent.SSL); + expect(https.request.lastCall.args[0].agent).to.be.a(KeepAliveAgent.SSL); done(); }); }); @@ -465,7 +465,7 @@ describe('Http Connector', function () { describe('Connection cleanup', function () { // skip these tests if native keep alive requests are supported - if (CustomAgent.supportsNativeKeepAlive) { + if (KeepAliveAgent.supportsNativeKeepAlive) { return; }