Skip to content

Commit 8eaede3

Browse files
authored
Merge pull request #242 from consideRatio/fix-custom-header-flag
fix: --custom-header flag implementation
2 parents 6eb8409 + 0bfca2d commit 8eaede3

File tree

3 files changed

+37
-25
lines changed

3 files changed

+37
-25
lines changed

bin/configurable-http-proxy

+11-21
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ args
8484
.option(
8585
"--custom-header <header>",
8686
"Custom header to add to proxied requests. Use same option for multiple headers (--custom-header k1:v1 --custom-header k2:v2)",
87-
collect, []
87+
collectHeadersIntoObject, {}
8888
)
8989
.option("--insecure", "Disable SSL cert verification")
9090
.option("--host-routing", "Use host routing (host as first level of path)")
@@ -107,8 +107,15 @@ args
107107
"Define an external storage class. Defaults to in-MemoryStore."
108108
);
109109

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

114121
args.parse(process.argv);
@@ -263,6 +270,7 @@ options.hostRouting = args.hostRouting;
263270
options.authToken = process.env.CONFIGPROXY_AUTH_TOKEN;
264271
options.redirectPort = args.redirectPort;
265272
options.redirectTo = args.redirectTo;
273+
options.headers = args.customHeader;
266274
options.timeout = args.timeout;
267275
options.proxyTimeout = args.proxyTimeout;
268276

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

312-
// custom headers option
313-
options.customHeader = [];
314-
if (args.customHeader) {
315-
options.customHeader = args.customHeader.map(s => s.trim()).map(header => {
316-
var i = header.indexOf(':');
317-
var key, value;
318-
if (i < 0) {
319-
log.error("Custom header is invalid: " + header);
320-
process.exit(1);
321-
}
322-
else {
323-
var key = header.substr(0, i).trim();
324-
var value = header.substr(i + 1).trim();
325-
}
326-
return { 'key': key, 'value': value };
327-
}).filter(header => header.key);
328-
}
329-
330320
// external backend class
331321
options.storageBackend = args.storageBackend;
332322

lib/configproxy.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class ConfigurableProxy extends EventEmitter {
145145
} else {
146146
this.includePrefix = true;
147147
}
148-
this.customHeaders = this.options.customHeaders;
148+
this.headers = this.options.headers;
149149
this.hostRouting = this.options.hostRouting;
150150
this.errorTarget = options.errorTarget;
151151
if (this.errorTarget && this.errorTarget.slice(-1) !== "/") {
@@ -229,9 +229,6 @@ class ConfigurableProxy extends EventEmitter {
229229

230230
this.proxy.on("proxyRes", function(proxyRes, req, res) {
231231
that.statsd.increment("requests." + proxyRes.statusCode, 1);
232-
if (that.customHeaders) {
233-
that.customHeaders.forEach(header => res.setHeader(header.key, header.value));
234-
}
235232
});
236233
}
237234

test/cli_spec.js

+25
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,29 @@ describe("CLI Tests", function() {
193193
});
194194
});
195195
});
196+
197+
it("custom-header", function(done) {
198+
var args = [
199+
'--ip', '127.0.0.1',
200+
'--ssl-cert', 'test/server.crt',
201+
'--ssl-key', 'test/server.key',
202+
'--port', port,
203+
'--default-target', testUrl,
204+
'--custom-header', 'k1: v1',
205+
'--custom-header', ' k2 : v2 v2 ',
206+
];
207+
executeCLI(execCmd, args).then((cliProcess) => {
208+
childProcess = cliProcess;
209+
r(SSLproxyUrl).then(body => {
210+
body = JSON.parse(body);
211+
expect(body.headers).toEqual(
212+
jasmine.objectContaining({
213+
k1: "v1",
214+
k2: "v2 v2",
215+
})
216+
);
217+
done();
218+
});
219+
});
220+
});
196221
});

0 commit comments

Comments
 (0)