Skip to content

Commit

Permalink
feat(https): Add HTTPS proxying - re: #399
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane Osbourne committed Feb 16, 2015
1 parent 6055652 commit 09dbca6
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 110 deletions.
1 change: 1 addition & 0 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ module.exports = {
* @param {Function} done
*/
setOptions: function (bs, done) {

done(null, {
options: {
urls: utils.getUrlOptions(bs.options),
Expand Down
28 changes: 15 additions & 13 deletions lib/cli/cli-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,24 @@ opts.callbacks = {
* @param argv
* @returns {*}
*/
proxy: function (value, argv) {
proxy: function (value) {

var target = value;
var mw;
var target;

if (!value || value === true) {
return false;
}

if (typeof value !== "string") {
target = value.get("target");
mw = value.get("middleware");
} else {
target = value;
}

if (!target.match(/^(https?):\/\//)) {
target = "http://" + value;
target = "http://" + target;
}

var parsedUrl = url.parse(target);
Expand All @@ -137,18 +141,16 @@ opts.callbacks = {
parsedUrl.port = 80;
}

return Immutable.Map({}).withMutations(function (map) {

map.set("target", parsedUrl.protocol + "//" + parsedUrl.host);
var out = {
target: parsedUrl.protocol + "//" + parsedUrl.host,
url: Immutable.Map(parsedUrl)
};

map.set("url", Immutable.Map(parsedUrl));
if (mw) {
out.middleware = mw;
}

if (argv && argv.startPath && parsedUrl.path === "/") {
map.setIn(["url", "path"],
argv.startPath.charAt(0) === "/" ? argv.startPath : "/" + argv.startPath
);
}
});
return Immutable.fromJS(out);
},
/**
* @param value
Expand Down
27 changes: 12 additions & 15 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ module.exports.update = function (options) {
* @param item
*/
function setScheme (item) {

var scheme = "http";

if (item.getIn(["server", "https"])) {
Expand All @@ -47,19 +48,27 @@ function setScheme (item) {
scheme = "https";
}

if (item.getIn(["proxy", "url", "protocol"])) {
if (item.getIn(["proxy", "url", "protocol"]) === "https:") {
scheme = "https";
}
}

item.set("scheme", scheme);
}

/**
* @param item
*/
function setStartPath (item) {

if (item.get("proxy")) {
var path = item.getIn(["proxy", "path"]);
var path = item.getIn(["proxy", "url", "path"]);
if (path !== "/") {
item.set("startPath", path);
}
}

}

/**
Expand Down Expand Up @@ -137,7 +146,7 @@ function getMiddlwares (item) {

var mw = item.get("middleware");
var serverMw = item.getIn(["server", "middleware"]);
var proxyMw = item.getIn(["proxy", "middleware"]);
var proxyMw = item.getIn(["proxy", "middleware"]);

var list = Immutable.List([]);

Expand All @@ -156,18 +165,6 @@ function getMiddlwares (item) {
return list;
}

/**
*
* @param item
* @returns {*}
*/
function toArray (item) {
if (!Array.isArray(item)) {
return [item];
}
return item;
}

/**
*
* @param item
Expand All @@ -191,4 +188,4 @@ function listMerge(list, item) {
}

return list;
}
}
12 changes: 7 additions & 5 deletions lib/server/proxy-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = function createProxyServer (bs, scripts) {

var options = bs.options;

checkProxyTarget(options.getIn(["proxy", "target"]), function (err) {
checkProxyTarget(options, function (err) {
if (err) {
bs.events.emit("config:warn", {msg: err});
}
Expand All @@ -29,7 +29,7 @@ module.exports = function createProxyServer (bs, scripts) {
rules: snippetUtils.getRegex(options.get("snippet"), options.get("snippetOptions")),
whitelist: snippetOptions.whitelist,
blacklist: snippetOptions.blacklist,
middleware: pluginMw,
middleware: bs.options.get("middleware").push(pluginMw),
errHandler: function (err) {
bs.logger.debug("{red:[proxy error]} %s", err.message);
}
Expand All @@ -47,13 +47,15 @@ module.exports = function createProxyServer (bs, scripts) {
};

/**
* @param {Object} target
* @param {Object} opts
* @param {Function} cb
*/
function checkProxyTarget (target, cb) {
function checkProxyTarget (opts, cb) {

var chunks = [];
var errored = false;
var target = opts.getIn(["proxy", "target"]);
var scheme = opts.get("scheme");

function logError() {
if (!errored) {
Expand All @@ -62,7 +64,7 @@ function checkProxyTarget (target, cb) {
}
}

require("http").get(target, function (res) {
require(scheme).get(target, function (res) {
res.on("data", function (data) {
chunks.push(data);
});
Expand Down
2 changes: 1 addition & 1 deletion lib/snippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ var utils = {
* @returns {Function}
*/
getProxyMiddleware: function (scripts, scriptPath) {
return function (req, res, next) {
return function serveBsJavascriptfile (req, res, next) {
if (req.url.indexOf(scriptPath) > -1) {
res.writeHead(200, {"Content-Type": "text/javascript"});
return res.end(scripts);
Expand Down
3 changes: 2 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ var utils = {
getUrlOptions: function (options) {

var scheme = options.get("scheme");

var port = options.get("port");
var urls = {};

Expand Down Expand Up @@ -103,7 +104,7 @@ var utils = {
var startPath = options.get("startPath");

if (options.get("proxy") && options.getIn(["proxy", "startPath"])) {
startPath = options.getIn(["proxy", "path"]);
startPath = options.getIn(["proxy", "url", "path"]);
}

if (startPath) {
Expand Down
22 changes: 3 additions & 19 deletions test/specs/cli/cli.options.proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,13 @@ describe("CLI: Options: Merging Proxy Options", function () {
proxy: "192.168.0.4:9001"
});
assert.equal(imm.getIn(["proxy", "target"]), "http://192.168.0.4:9001");
assert.equal(imm.getIn(["proxy", "port"]), 9001);
assert.equal(imm.getIn(["proxy", "url", "port"]), 9001);
});
it.only("should merge a url with no protocol & no port", function () {

it("should merge a url with no protocol & no port", function () {
var imm = merge({
proxy: "localhost"
});

//console.log(imm.toJS());
//
//assert.equal(imm.getIn(["proxy", "target"]), "http://localhost");

assert.equal(imm.getIn(["proxy", "url", "port"]), 80);

});
it("should merge a url with no protocol & no port", function () {
var imm = merge({
Expand All @@ -78,16 +71,7 @@ describe("CLI: Options: Merging Proxy Options", function () {
proxy: "http://local.dev/subdir/another/path?rel=123"
});
assert.equal(imm.getIn(["proxy", "target"]), "http://local.dev");
assert.equal(imm.getIn(["proxy", "port"]), 80);
});
it("should set a start path when given 'startPath'", function () {
var imm = merge({
proxy: "http://local.dev"
}, {
startPath: "index.php"
});
assert.equal(imm.getIn(["proxy", "target"]), "http://local.dev");
assert.equal(imm.getIn(["proxy", "path"]), "/index.php");
assert.equal(imm.getIn(["proxy", "url", "port"]), 80);
});
});
});
1 change: 1 addition & 0 deletions test/specs/e2e/cli/e2e.cli.proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ describe("E2E CLI proxy test", function () {
assert.equal(instance.options.get("mode"), "snippet");
});
});

describe("E2E CLI proxy test", function () {

var instance;
Expand Down
3 changes: 3 additions & 0 deletions test/specs/e2e/cli/e2e.cli.ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ describe("E2E CLI UI test", function () {
}
},
cb: function (err, bs) {
if (err) {
return done(err);
}
instance = bs;
done();
}
Expand Down
6 changes: 1 addition & 5 deletions test/specs/e2e/middleware/middleware.option.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
"use strict";

var browserSync = require("../../../../index");

var connect = require("connect");

var request = require("supertest");
var assert = require("chai").assert;

describe("Accepting middleware as an option", function () {
Expand Down Expand Up @@ -74,4 +70,4 @@ describe("Accepting middleware as an option", function () {
it("should accept middlewares when given as top-level", function () {
assert.equal(bs.options.get("middleware").size, 2);
});
});
});
11 changes: 4 additions & 7 deletions test/specs/e2e/middleware/middleware.proxy.option.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
"use strict";

var browserSync = require("../../../../index");

var connect = require("connect");

var request = require("supertest");
var assert = require("chai").assert;

describe("Accepting middleware as a server option", function () {
Expand All @@ -15,7 +11,7 @@ describe("Accepting middleware as a server option", function () {

browserSync.reset();

var fn = function (req, res, next) {
var fn = function (req) {
console.log(req.url);
};

Expand All @@ -39,6 +35,7 @@ describe("Accepting middleware as a server option", function () {
assert.equal(bs.options.get("middleware").size, 1);
});
});

describe("Ignoring middleware as a server option when given at top level", function () {

var bs;
Expand All @@ -47,10 +44,10 @@ describe("Ignoring middleware as a server option when given at top level", funct

browserSync.reset();

var fn = function fn(req, res, next) {
var fn = function fn (req) {
console.log(req.url);
};
var fn2 = function fn2(req, res, next) {
var fn2 = function fn2 (req) {
console.log(req.url);
};

Expand Down
48 changes: 4 additions & 44 deletions test/specs/e2e/middleware/middleware.server.option.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,23 @@

var browserSync = require("../../../../index");

var connect = require("connect");

var request = require("supertest");
var assert = require("chai").assert;

describe("Accepting middleware as a proxy option", function () {
describe("Accepting middleware as a server option", function () {

var bs;

before(function (done) {

browserSync.reset();

var fn = function (req, res, next) {
var fn = function (req) {
console.log(req.url);
};

var config = {
proxy: {
target: "http://www.bbc.co.uk",
server: {
baseDir: "./app",
middleware: fn // Back compat
},
logLevel: "silent",
Expand All @@ -39,40 +36,3 @@ describe("Accepting middleware as a proxy option", function () {
assert.equal(bs.options.get("middleware").size, 1);
});
});
//describe("Ignoring middleware as a server option when given at top level", function () {
//
// var bs;
//
// before(function (done) {
//
// browserSync.reset();
//
// var fn = function fn(req, res, next) {
// console.log(req.url);
// };
// var fn2 = function fn2(req, res, next) {
// console.log(req.url);
// };
//
// var config = {
// server: {
// baseDir: "test/fixtures",
// middleware: fn // Back compat
// },
// middleware: fn2,
// logLevel: "silent",
// open: false
// };
//
// bs = browserSync.init(config, done).instance;
// });
//
// after(function () {
// bs.cleanup();
// });
//
// it("serves files from the middleware with snippet added", function () {
// assert.equal(bs.options.get("middleware").get(0).name, "fn2");
// assert.equal(bs.options.get("middleware").size, 1);
// });
//});
Loading

0 comments on commit 09dbca6

Please sign in to comment.