Skip to content

Commit

Permalink
fix(https): pass ALL https options given by the user to .createServer…
Browse files Browse the repository at this point in the history
…() - fixes #956
  • Loading branch information
shakyShane committed Nov 20, 2016
1 parent f2f9722 commit 0c555bd
Showing 1 changed file with 53 additions and 24 deletions.
77 changes: 53 additions & 24 deletions lib/server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var fs = require("fs");
var path = require("path");
var filePath = require("path");
var join = require("path").join;
var connect = require("connect");
var Immutable = require("immutable");
var http = require("http");
Expand All @@ -18,29 +18,61 @@ var snippetUtils = require("../snippet").utils;
var lrSnippet = require("resp-modifier");
var utils = require("../utils");

function getCa (options) {
var caOption = options.getIn(["https", "ca"]);
// if not provided, use Browsersync self-signed
if (typeof caOption === "undefined") {
return fs.readFileSync(join(__dirname, "certs", "server.csr"));
}
// if a string was given, read that file from disk
if (typeof caOption === "string") {
return fs.readFileSync(caOption);
}
// if an array was given, read all
if (List.isList(caOption)) {
return caOption.toArray().map(function (x) {
return fs.readFileSync(x);
});
}
}

function getKey(options) {
return fs.readFileSync(options.getIn(["https", "key"]) || join(__dirname, "certs", "server.key"));
}

function getCert(options) {
return fs.readFileSync(options.getIn(["https", "cert"]) || join(__dirname, "certs", "server.crt"));
}

function getHttpsServerDefaults (options) {
return fromJS({
key: getKey(options),
cert: getCert(options),
ca: getCa(options),
passphrase: ""
});
}

function getPFXDefaults (options) {
return fromJS({
pfx: fs.readFileSync(options.getIn(["https", "pfx"]))
});
}

var serverUtils = {
/**
* @param options
* @returns {{key, cert}}
*/
getKeyAndCert: function (options) {
return {
key: fs.readFileSync(options.getIn(["https", "key"]) || filePath.join(__dirname, "certs/server.key")),
cert: fs.readFileSync(options.getIn(["https", "cert"]) || filePath.join(__dirname, "certs/server.crt")),
ca: fs.readFileSync(options.getIn(["https", "ca"]) || filePath.join(__dirname, "certs/server.csr")),
passphrase: options.getIn(["https", "passphrase"]) || ""
};
},
/**
* @param filePath
* @param passphrase
* @returns {{pfx}}
*/
getPFX: function (filePath, passphrase) {
return {
pfx: fs.readFileSync(filePath),
passphrase: passphrase,
};
getHttpsOptions: function (options) {
var userOption = options.get("https");
if (Map.isMap(userOption)) {
if (userOption.has("pfx")) {
return userOption.mergeDeep(getPFXDefaults(options));
}
return userOption.mergeDeep(getHttpsServerDefaults(options));
}
return getHttpsServerDefaults(options);
},
/**
* Get either http or https server
Expand All @@ -53,11 +85,8 @@ var serverUtils = {
var httpModule = serverUtils.getHttpModule(options);

if (options.get("scheme") === "https") {
var pfxPath = options.getIn(["https", "pfx"]);
var pfxPassphrase = options.getIn(["https", "pfxPassphrase"]);
return pfxPath ?
httpModule.createServer(serverUtils.getPFX(pfxPath, pfxPassphrase), app) :
httpModule.createServer(serverUtils.getKeyAndCert(options), app);
var opts = serverUtils.getHttpsOptions(options);
return httpModule.createServer(opts.toJS(), app);
}

return httpModule.createServer(app);
Expand Down

0 comments on commit 0c555bd

Please sign in to comment.