Skip to content

Commit c25b7eb

Browse files
authored
Merge pull request #206 from ivan-gomes/feature/206-custom-headers
Command line option for custom headers
2 parents 6cd6dd7 + 2665a9d commit c25b7eb

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ Options:
143143
--auto-rewrite Rewrite the Location header host/port in redirect responses
144144
--change-origin Changes the origin of the host header to the target URL
145145
--protocol-rewrite <proto> Rewrite the Location header protocol in redirect responses to the specified protocol
146+
--custom-headers <headers> Comma separated list of custom headers to add to proxied requests (k1:v1,k2:v2,...)
146147
--insecure Disable SSL cert verification
147148
--host-routing Use host routing (host as first level of path)
148149

bin/configurable-http-proxy

+27
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ args
8181
"--protocol-rewrite <proto>",
8282
"Rewrite the Location header protocol in redirect responses to the specified protocol"
8383
)
84+
.option(
85+
"--custom-header <header>",
86+
"Custom header to add to proxied requests. Use same option for multiple headers (--custom-header k1:v1 --custom-header k2:v2)",
87+
collect, []
88+
)
8489
.option("--insecure", "Disable SSL cert verification")
8590
.option("--host-routing", "Use host routing (host as first level of path)")
8691
.option("--statsd-host <host>", "Host to send statsd statistics to")
@@ -102,6 +107,10 @@ args
102107
"Define an external storage class. Defaults to in-MemoryStore."
103108
);
104109

110+
function collect(value, previous) {
111+
return previous.concat([value]);
112+
}
113+
105114
args.parse(process.argv);
106115

107116
var ConfigurableProxy = require("../lib/configproxy.js").ConfigurableProxy;
@@ -300,6 +309,24 @@ if (!options.authToken) {
300309
log.warn("REST API is not authenticated.");
301310
}
302311

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+
303330
// external backend class
304331
options.storageBackend = args.storageBackend;
305332

lib/configproxy.js

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

229230
this.proxy.on("proxyRes", function(proxyRes, req, res) {
230231
that.statsd.increment("requests." + proxyRes.statusCode, 1);
232+
if (that.customHeaders) {
233+
that.customHeaders.forEach(header => res.setHeader(header.key, header.value));
234+
}
231235
});
232236
}
233237

0 commit comments

Comments
 (0)