Skip to content

Commit 459335d

Browse files
committed
close proxied websockets when upstream sends a regular response
1 parent 1a6094a commit 459335d

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

lib/configproxy.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,24 @@ export class ConfigurableProxy extends EventEmitter {
176176
}
177177
options.ws = true;
178178
this.proxy = httpProxy.createProxyServer(options);
179+
this.proxy.on("proxyReqWs", (proxyReq, req, socket, options, head) => {
180+
proxyReq.on("response", (res) => {
181+
that.log.info(
182+
"Client requested websocket at %s, proxy target %s responded with HTTP %s %s",
183+
req.url,
184+
proxyReq.host,
185+
res.statusCode,
186+
res.statusMessage
187+
);
188+
if (!socket.destroyed) {
189+
socket.write(
190+
`HTTP/1.1 ${res.statusCode} Upstream not a websocket\r\nConnection: close\r\n\r\n`
191+
);
192+
socket.end();
193+
}
194+
res.resume();
195+
});
196+
});
179197

180198
// tornado-style regex routing,
181199
// because cross-language cargo-culting is always a good idea

0 commit comments

Comments
 (0)