Skip to content

Commit 98caf88

Browse files
chancezminrk
authored andcommitted
lib: Use proxy to access default error target
Fixes usage of self-signed certs with JupyterHub with CHP. Original issue here: jupyterhub/zero-to-jupyterhub-k8s#1742 (comment)
1 parent 2ea06dd commit 98caf88

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

lib/configproxy.js

+14-21
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,34 +438,28 @@ 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) {
446-
var urlSpec = URL.parse(this.errorTarget);
447-
urlSpec.search = "?" + querystring.encode({ url: req.url });
448-
urlSpec.pathname = urlSpec.pathname + code.toString();
449-
var secure = /https/gi.test(urlSpec.protocol) ? true : false;
450-
var url = URL.format(urlSpec);
451-
var errorRequest = (secure ? https : http).request(url, function (upstream) {
452-
["content-type", "content-encoding"].map(function (key) {
453-
if (!upstream.headers[key]) return;
454-
if (res.setHeader) res.setHeader(key, upstream.headers[key]);
455-
});
456-
if (res.writeHead) res.writeHead(code);
457-
upstream.on("data", (data) => {
458-
if (res.write) res.write(data);
459-
});
460-
upstream.on("end", () => {
461-
if (res.end) res.end();
462-
});
463-
});
464-
errorRequest.on("error", (e) => {
446+
var target = URL.parse(this.errorTarget);
447+
var secure = target.protocol.toLowerCase() === "https:";
448+
target.search = "?" + querystring.encode({ url: req.url });
449+
target.pathname = target.pathname + code.toString();
450+
451+
if (secure && this.options.clientSsl) {
452+
target.key = this.options.clientSsl.key;
453+
target.cert = this.options.clientSsl.cert;
454+
target.ca = this.options.clientSsl.ca;
455+
}
456+
457+
this.log.debug("Requesting custom error page: %s", target.format());
458+
this.proxy.web(req, res, { target: target }, (e) => {
465459
// custom error failed, fallback on default
466460
this.log.error("Failed to get custom error page: %s", e);
467461
this._handleProxyErrorDefault(code, kind, req, res);
468462
});
469-
errorRequest.end();
470463
} else if (this.errorPath) {
471464
var filename = path.join(this.errorPath, code.toString() + ".html");
472465
if (!fs.existsSync(filename)) {

0 commit comments

Comments
 (0)