Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion lib/http-proxy/passes/ws-incoming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The names of passes are exported as WS_PASSES from this module.
import * as http from "node:http";
import * as https from "node:https";
import * as common from "../common";
import type { Request } from "./web-incoming";
import type { Request, ProxyResponse } from "./web-incoming";
import type { Socket } from "node:net";
import debug from "debug";
import type { NormalizedServerOptions, ProxyServer } from "..";
Expand Down Expand Up @@ -219,6 +219,28 @@ export function stream(
// which may be another leak type situation and definitely doesn't work for unit testing.
socket.destroySoon();
}

// if we get a response, backend is not a websocket endpoint,
// relay HTTP response and close the socket
proxyReq.on("response", (proxyRes: ProxyResponse) => {
log("got non-ws HTTP response",
{
statusCode: proxyRes.statusCode,
statusMessage: proxyRes.statusMessage,
}
);
const proxyHead = createHttpHeader(
`HTTP/1.1 ${proxyRes.statusCode} ${proxyRes.statusMessage}`,
{"Connection": "close"},
);
if (!socket.destroyed) {
socket.write(proxyHead);
proxyRes.pipe(socket);
} else {
// make sure response is consumed
proxyRes.resume();
}
});

proxyReq.end();
}
Expand Down
85 changes: 85 additions & 0 deletions lib/test/websocket/websocket-proxy-http.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
pnpm test websocket-proxy-http.test.ts
Test clients connecting a websocket to a non-websocket backend.
The connection should fail promptly and preserve the original error code.
See https://nodejs.org/api/http.html#event-response
DEVELOPMENT:
pnpm test websocket-proxy-http.test.ts
*/

import * as http from "node:http";
import * as httpProxy from "../..";
import log from "../log";
import getPort from "../get-port";
import { once } from "node:events";
import {describe, it, expect, beforeAll, afterAll} from "vitest";

describe("Example of client requesting websocket when backend is plain http", () => {
let ports: Record<'httpOnly' | 'proxy', number>;
beforeAll(async () => {
// assigns ports
ports = { httpOnly: await getPort(), proxy: await getPort() };
});

let servers: any = {};

it("Create an http server that doesn't support websockets", async () => {
const server = http.createServer((_req, res) => {
res.writeHead(418, { "Content-Type": "text/plain" });
res.end("not a websocket!");
});

servers.httpOnly = server;
server.listen(ports.httpOnly);
});

it("Try a websocket client connecting to a regular HTTP backend", async () => {
const options = {
port: ports.httpOnly,
host: "localhost",
headers: {
Connection: "Upgrade",
Upgrade: "websocket",
},
};
const req = http.request(options);
req.end();
const [res] = await once(req, "response");
expect(res.statusCode).toEqual(418);
const body = await res.read().toString();
expect(body.trim()).toEqual("not a websocket!");
log("we got an http response.");
});

it("Create a proxy server pointed at the non-websocket server, expecting websockets", async () => {
servers.proxy = httpProxy
.createServer({ target: `ws://localhost:${ports.httpOnly}`, ws: true })
.listen(ports.proxy);
});

it("Create a websocket client and test the proxy server", async () => {
const options = {
port: ports.proxy,
host: "localhost",
headers: {
Connection: "Upgrade",
Upgrade: "websocket",
},
};
const req = http.request(options);
req.end();
const [res] = await once(req, "response");
expect(res.statusCode).toEqual(418);
const body = await res.read().toString();
expect(body.trim()).toEqual("not a websocket!");
});

afterAll(async () => {
// cleans up
Object.values(servers).map((x: any) => x?.close());
});
});