Skip to content

Commit 7a99ec1

Browse files
committed
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 e54e49e commit 7a99ec1

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

lib/configproxy.js

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

424-
var proxy = this;
424+
var that = this;
425425
var errMsg = "";
426426
this.statsd.increment("requests." + code, 1);
427427
if (e) {
@@ -439,34 +439,27 @@ class ConfigurableProxy extends EventEmitter {
439439
}
440440
this.log.error("%s %s %s %s", code, req.method, req.url, errMsg);
441441
if (!res) {
442+
this.log.debug("socket error, no response to send");
442443
// socket-level error, no response to build
443444
return;
444445
}
445446
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 => {
447+
var target = URL.parse(this.errorTarget);
448+
target.search = "?" + querystring.encode({ url: req.url });
449+
target.pathname = target.pathname + code.toString();
450+
451+
if (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("sending to custom error page: %s", target.format());
458+
this.proxy.web(req, res, { target: target}, err => {
465459
// custom error failed, fallback on default
466-
this.log.error("Failed to get custom error page: %s", e);
467-
this._handleProxyErrorDefault(code, kind, req, res);
460+
that.log.error("Failed to get custom error page: %s", err);
461+
that._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)