Skip to content

Commit 52f2806

Browse files
authored
Merge pull request #254 from chancez/fix_default_error_target_https_github
lib: Use client ssl config to access error target
2 parents 2ea06dd + 6a84567 commit 52f2806

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

lib/configproxy.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,6 @@ class ConfigurableProxy extends EventEmitter {
421421
// custom error server gets `/CODE?url=/escapedUrl/`, e.g.
422422
// /404?url=%2Fuser%2Ffoo
423423

424-
var proxy = this;
425424
var errMsg = "";
426425
this.statsd.increment("requests." + code, 1);
427426
if (e) {
@@ -439,16 +438,31 @@ class ConfigurableProxy extends EventEmitter {
439438
}
440439
this.log.error("%s %s %s %s", code, req.method, req.url, errMsg);
441440
if (!res) {
441+
this.log.debug("Socket error, no response to send");
442442
// socket-level error, no response to build
443443
return;
444444
}
445445
if (this.errorTarget) {
446446
var urlSpec = URL.parse(this.errorTarget);
447+
// error request is $errorTarget/$code?url=$requestUrl
447448
urlSpec.search = "?" + querystring.encode({ url: req.url });
448449
urlSpec.pathname = urlSpec.pathname + code.toString();
449450
var secure = /https/gi.test(urlSpec.protocol) ? true : false;
450451
var url = URL.format(urlSpec);
451-
var errorRequest = (secure ? https : http).request(url, function (upstream) {
452+
this.log.debug("Requesting custom error page: %s", urlSpec.format());
453+
454+
// construct request target from urlSpec
455+
var target = URL.parse(url);
456+
target.method = "GET";
457+
458+
// add client SSL config if error target is using https
459+
if (secure && this.options.clientSsl) {
460+
target.key = this.options.clientSsl.key;
461+
target.cert = this.options.clientSsl.cert;
462+
target.ca = this.options.clientSsl.ca;
463+
}
464+
465+
var errorRequest = (secure ? https : http).request(target, function (upstream) {
452466
["content-type", "content-encoding"].map(function (key) {
453467
if (!upstream.headers[key]) return;
454468
if (res.setHeader) res.setHeader(key, upstream.headers[key]);

test/proxy_spec.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ var WebSocket = require("ws");
88

99
var ConfigurableProxy = require("../lib/configproxy").ConfigurableProxy;
1010

11+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
12+
1113
describe("Proxy Tests", function () {
1214
var port = 8902;
1315
var testPort = port + 10;
@@ -268,10 +270,10 @@ describe("Proxy Tests", function () {
268270
});
269271

270272
it("custom error target", function (done) {
271-
var port = 55555;
273+
var proxyPort = 55550;
272274
util
273-
.setupProxy(port, { errorTarget: "http://127.0.0.1:55565" }, [])
274-
.then(() => r("http://127.0.0.1:" + port + "/foo/bar"))
275+
.setupProxy(proxyPort, { errorTarget: "http://127.0.0.1:55565" }, [])
276+
.then(() => r("http://127.0.0.1:" + proxyPort + "/foo/bar"))
275277
.then((body) => done.fail("Expected 404"))
276278
.catch((err) => {
277279
expect(err.statusCode).toEqual(404);
@@ -338,7 +340,7 @@ describe("Proxy Tests", function () {
338340
});
339341

340342
it("Redirect location with rewriting", function (done) {
341-
var proxyPort = 55555;
343+
var proxyPort = 55556;
342344
var options = {
343345
protocolRewrite: "https",
344346
autoRewrite: true,

0 commit comments

Comments
 (0)