Skip to content

First attempt at adding the ability to dynamically addHost(host,target) and removeHost(host) to the ProxyTable #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion lib/node-http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,28 @@ exports.createServer = function () {
});
}

server.changeHost = function (host, target){
this.addHost(host, target);
}

server.addHost = function (host, target){
if(!proxy instanceof RoutingProxy){
throw new Error('Cannot update ProxyTable routes on HttpProxy.');
}
else{
proxy.addHost(host, target);
}
}

server.removeHost = function (host){
if(!proxy instanceof RoutingProxy){
throw new Error('Cannot update ProxyTable routes on HttpProxy.');
}
else{
proxy.removeHost(host);
}
}

//
// Set the proxy on the server so it is available
// to the consumer of the server
Expand Down Expand Up @@ -392,4 +414,4 @@ exports._getBase = function _getBase (options) {
}

return result;
};
};
31 changes: 31 additions & 0 deletions lib/node-http-proxy/proxy-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,37 @@ var ProxyTable = exports.ProxyTable = function (options) {
//
util.inherits(ProxyTable, events.EventEmitter);

//
// ### function addRoute (route, target)
// #### @route {String} String containing route coming in
// #### @target {String} String containing the target
// Adds a host-based route to this instance.
//
ProxyTable.prototype.addRoute = function (route, target) {
if (!this.router) {
throw new Error('Cannot update ProxyTable routes without router.');
}

this.router[route] = target;

this.setRoutes(this.router);
};

//
// ### function removeRoute (route)
// #### @route {String} String containing route to remove
// Removes a host-based route from this instance.
//
ProxyTable.prototype.removeRoute = function (route) {
if (!this.router) {
throw new Error('Cannot update ProxyTable routes without router.');
}

delete this.router[route];

this.setRoutes(this.router);
};

//
// ### function setRoutes (router)
// #### @router {Object} Object containing the host based routes
Expand Down
23 changes: 23 additions & 0 deletions lib/node-http-proxy/routing-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,29 @@ RoutingProxy.prototype.remove = function (options) {
var key = this._getKey(options);
};

//
// ### function addHost (host, target)
// #### @host {String} Host to add to proxyTable
// #### @target {String} Target to add to proxyTable
// Adds a host to proxyTable
//
RoutingProxy.prototype.addHost = function (host, target) {
if (this.proxyTable) {
this.proxyTable.addRoute(host, target);
}
};

//
// ### function removeHost (host)
// #### @host {String} Host to remove from proxyTable
// Removes a host to proxyTable
//
RoutingProxy.prototype.removeHost = function (host) {
if (this.proxyTable) {
this.proxyTable.removeRoute(host);
}
};

//
// ### function close()
// Cleans up any state left behind (sockets, timeouts, etc)
Expand Down
24 changes: 24 additions & 0 deletions test/http/routing-proxy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ var hostnameOptions = {
}
};

var staticOptions = {
hostnameOnly: true,
router: {
"static.com": "127.0.0.1:8121",
"removed.com": "127.0.0.1:8122",
"change.com": "127.0.0.1:8123"
}
};

var dynamicHost = "dynamic1.com";
var dynamicTarget = "127.0.0.1:8125";

vows.describe('node-http-proxy/routing-proxy/' + testName).addBatch({
"When using server created by httpProxy.createServer()": {
"when passed a routing table": {
Expand All @@ -63,6 +75,18 @@ vows.describe('node-http-proxy/routing-proxy/' + testName).addBatch({
"an incoming request to foo.com": runner.assertProxied('foo.com', 8093, 8094),
"an incoming request to bar.com": runner.assertProxied('bar.com', 8093, 8095),
"an incoming request to unknown.com": runner.assertResponseCode(8093, 404)
},
"and adding a route by Hostname": {
topic: function () {
this.server = runner.startProxyServerWithTable(8120, staticOptions, this.callback);
this.server.removeHost('removed.com');
this.server.addHost(dynamicHost, dynamicTarget);
this.server.changeHost('change.com', '127.0.0.1:8124');
},
"an incoming request to static.com": runner.assertProxied('static.com', 8120, 8121),
"an incoming request to removed.com": !runner.assertProxied('removed.com', 8120, 8122),
"an incoming request to change.com": runner.assertProxied('change.com', 8120, 8124),
"an incoming request to dynamic1.com": runner.assertProxied('dynamic1.com', 8120, 8125)
}
},
"when passed a routing file": {
Expand Down