Skip to content

Commit 810239e

Browse files
authored
Merge pull request #599 from minrk/fix-error-target
fix errorTarget with websockets
2 parents df026d7 + f103ed8 commit 810239e

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

lib/configproxy.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,14 @@ export class ConfigurableProxy extends EventEmitter {
486486
// socket-level error, no response to build
487487
return;
488488
}
489+
if (kind === "ws") {
490+
if (!res.writableEnded) {
491+
// send empty http response with status
492+
res.write(`HTTP/${req.httpVersion} ${code}\r\nContent-Length: 0\r\n\r\n`);
493+
}
494+
// http-proxy-3 calls res.destroySoon() after we return from here
495+
return;
496+
}
489497
if (this.errorTarget) {
490498
var urlSpec = new URL(this.errorTarget);
491499
// error request is $errorTarget/$code?url=$requestUrl
@@ -497,9 +505,14 @@ export class ConfigurableProxy extends EventEmitter {
497505
var options = this.proxyOptsForTarget(urlSpec, req.url);
498506
options.method = "GET";
499507

500-
var errorRequest = (options.secure ? https : http).request(url, options, function (upstream) {
501-
if (res.writableEnded) return; // response already done
502-
["content-type", "content-encoding"].map(function (key) {
508+
var errorRequest = (options.secure ? https : http).request(url, options, (upstream) => {
509+
if (res.writableEnded) {
510+
// response already done
511+
// make sure to consume upstream;
512+
upstream.resume();
513+
return;
514+
}
515+
["content-type", "content-encoding"].map((key) => {
503516
if (!upstream.headers[key]) return;
504517
if (res.setHeader) res.setHeader(key, upstream.headers[key]);
505518
});

0 commit comments

Comments
 (0)