Skip to content

Protocol/host rewrite #73

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

Merged
merged 3 commits into from
Sep 7, 2016
Merged
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
11 changes: 11 additions & 0 deletions bin/configurable-http-proxy
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ args
.option('--no-x-forward', "Don't add 'X-forward-' headers to proxied requests")
.option('--no-prepend-path', "Avoid prepending target paths to proxied requests")
.option('--no-include-prefix', "Don't include the routing prefix in proxied requests")
.option('--auto-rewrite', "Rewrite the Location header host/port in redirect responses")
.option('--protocol-rewrite <proto>', "Rewrite the Location header protocol in redirect responses to the specified protocol")
.option('--insecure', "Disable SSL cert verification")
.option('--host-routing', "Use host routing (host as first level of path)")
.option('--statsd-host <host>', 'Host to send statsd statistics to')
Expand Down Expand Up @@ -181,6 +183,15 @@ if (args.insecure) options.secure = false;
options.xfwd = args.xForward ? false : true;
options.prependPath = args.prependPath;
options.includePrefix = args.includePrefix;
if (args.autoRewrite) {
options.autoRewrite = true;
log.info("AutoRewrite of Location headers enabled.");
}

if (args.protocolRewrite) {
options.protocolRewrite = args.protocolRewrite;
log.info("ProtocolRewrite enabled. Rewriting to "+options.protocolRewrite);
}

if (!options.auth_token) {
log.warn("REST API is not authenticated.");
Expand Down
19 changes: 19 additions & 0 deletions lib/testutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ var add_target = exports.add_target = function (proxy, path, port, websocket, ta
proxy.add_route(path, {target: target});
};

var add_target_redirecting = exports.add_target_redirecting = function (proxy, path, port, target_path, redirect_to) {
// Like the above, but the server returns a redirect response with a Location header.
// Cannot use default arguments as they are apparently not supported.
var target = 'http://127.0.0.1:' + port;
if (target_path) {
target = target + target_path;
}
var server;
server = http.createServer(function (req, res) {
res.setHeader("Location", redirect_to);
res.statusCode = 301;
res.write('');
res.end();
});
server.listen(port);
servers.push(server);
proxy.add_route(path, {target: target});
};

exports.setup_proxy = function (port, callback, options, paths) {
options = options || {};
options.auth_token = 'secret';
Expand Down
39 changes: 39 additions & 0 deletions test/proxy_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe("Proxy Tests", function () {
var r = request.defaults({
method: 'GET',
url: proxy_url,
followRedirect: false,
});

beforeEach(function (callback) {
Expand Down Expand Up @@ -258,4 +259,42 @@ describe("Proxy Tests", function () {
});
});
});

it("Redirect location untouched without rewrite options", function (done) {
var redirect_to = 'http://foo.com:12345/whatever';
util.add_target_redirecting(proxy, '/external/urlpath/', test_port, '/internal/urlpath/', redirect_to);
r(proxy_url + '/external/urlpath/rest/of/it', function (error, res, body) {
expect(error).toBe(null);
expect(res.statusCode).toEqual(301);
expect(res.headers.location).toEqual(redirect_to);
done();
});
});

it("Redirect location with rewriting", function (done) {
var proxy_port = 55555;
var options = {
protocolRewrite: "https",
autoRewrite: true,
};

// where the backend server redirects us.
// Note that http-proxy requires (logically) the redirection to be to the same (internal) host.
var redirect_to = "http://127.0.0.1:"+test_port+"/whatever";

var validation_callback = function (proxy) {
util.add_target_redirecting(proxy, '/external/urlpath/', test_port, '/internal/urlpath/', redirect_to);
var url = 'http://127.0.0.1:' + proxy_port;

r(url + '/external/urlpath/', function (error, res, body) {
expect(error).toBe(null);
expect(res.statusCode).toEqual(301);
expect(res.headers.location).toEqual("https://127.0.0.1:"+proxy_port+"/whatever");
done();
});
};

var proxy = util.setup_proxy(proxy_port, validation_callback, options, []);
});

});