Skip to content

fix: --custom-header flag implementation #242

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 2 commits into from
Jul 4, 2020
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
32 changes: 11 additions & 21 deletions bin/configurable-http-proxy
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ args
.option(
"--custom-header <header>",
"Custom header to add to proxied requests. Use same option for multiple headers (--custom-header k1:v1 --custom-header k2:v2)",
collect, []
collectHeadersIntoObject, {}
)
.option("--insecure", "Disable SSL cert verification")
.option("--host-routing", "Use host routing (host as first level of path)")
Expand All @@ -107,8 +107,15 @@ args
"Define an external storage class. Defaults to in-MemoryStore."
);

function collect(value, previous) {
return previous.concat([value]);
// collects multiple flags to an object
// --custom-header "k1:v1" --custom-header " k2 : v2 " --> {"k1":"v1","k2":"v2"}
function collectHeadersIntoObject(value, previous) {
var headerParts = value.split(":").map(p => p.trim())
if (headerParts.length != 2) {
log.error("A single colon was expected in custom header: " + value);
process.exit(1);
}
previous[headerParts[0]] = headerParts[1]
}

args.parse(process.argv);
Expand Down Expand Up @@ -263,6 +270,7 @@ options.hostRouting = args.hostRouting;
options.authToken = process.env.CONFIGPROXY_AUTH_TOKEN;
options.redirectPort = args.redirectPort;
options.redirectTo = args.redirectTo;
options.headers = args.customHeader;
options.timeout = args.timeout;
options.proxyTimeout = args.proxyTimeout;

Expand Down Expand Up @@ -309,24 +317,6 @@ if (!options.authToken) {
log.warn("REST API is not authenticated.");
}

// custom headers option
options.customHeader = [];
if (args.customHeader) {
options.customHeader = args.customHeader.map(s => s.trim()).map(header => {
var i = header.indexOf(':');
var key, value;
if (i < 0) {
log.error("Custom header is invalid: " + header);
process.exit(1);
}
else {
var key = header.substr(0, i).trim();
var value = header.substr(i + 1).trim();
}
return { 'key': key, 'value': value };
}).filter(header => header.key);
}

// external backend class
options.storageBackend = args.storageBackend;

Expand Down
5 changes: 1 addition & 4 deletions lib/configproxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class ConfigurableProxy extends EventEmitter {
} else {
this.includePrefix = true;
}
this.customHeaders = this.options.customHeaders;
this.headers = this.options.headers;
this.hostRouting = this.options.hostRouting;
this.errorTarget = options.errorTarget;
if (this.errorTarget && this.errorTarget.slice(-1) !== "/") {
Expand Down Expand Up @@ -229,9 +229,6 @@ class ConfigurableProxy extends EventEmitter {

this.proxy.on("proxyRes", function(proxyRes, req, res) {
that.statsd.increment("requests." + proxyRes.statusCode, 1);
if (that.customHeaders) {
that.customHeaders.forEach(header => res.setHeader(header.key, header.value));
}
});
}

Expand Down
25 changes: 25 additions & 0 deletions test/cli_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,29 @@ describe("CLI Tests", function() {
});
});
});

it("custom-header", function(done) {
var args = [
'--ip', '127.0.0.1',
'--ssl-cert', 'test/server.crt',
'--ssl-key', 'test/server.key',
'--port', port,
'--default-target', testUrl,
'--custom-header', 'k1: v1',
'--custom-header', ' k2 : v2 v2 ',
];
executeCLI(execCmd, args).then((cliProcess) => {
childProcess = cliProcess;
r(SSLproxyUrl).then(body => {
body = JSON.parse(body);
expect(body.headers).toEqual(
jasmine.objectContaining({
k1: "v1",
k2: "v2 v2",
})
);
done();
});
});
});
});