add SSL configuration options

This commit is contained in:
Spencer Alger
2015-01-05 12:56:48 -07:00
parent 38828673ab
commit 4fc1c91634
5 changed files with 39 additions and 8 deletions

View File

@ -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) {

View File

@ -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('/');

View File

@ -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 () {

View File

@ -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;
}