Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions src/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ export function setupOutgoing(

if (options.agent !== undefined) {
outgoing.agent = options.agent || false;
} else if (req.httpVersionMajor > 1) {
// HTTP/2 incoming requests: keep-alive agents can conflict with stream lifecycle
} else if (req.httpVersionMajor > 1 || upgradeHeader.test(req.headers.connection || "")) {
// WebSocket upgrades and HTTP/2 incoming requests: agents conflict with
// the socket lifecycle (upgrade handoff / stream multiplexing).
outgoing.agent = false;
} else {
// Use default keep-alive agents for connection reuse
Expand Down
15 changes: 15 additions & 0 deletions test/_utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,21 @@ describe("lib/http-proxy/common.js", () => {
expect(outgoing.agent).to.eql(false);
});

it("should not use keep-alive agent for websocket upgrade requests", () => {
const outgoing = createOutgoing();
common.setupOutgoing(
outgoing,
{ target: "http://localhost" },
stubIncomingMessage({
url: "/",
headers: { connection: "Upgrade", upgrade: "websocket" },
}),
);
// WebSocket upgrades take ownership of the socket after 101,
// so a keep-alive agent must not be used.
expect(outgoing.agent).to.eql(false);
});

it("set the port according to the protocol", () => {
const outgoing = createOutgoing();
common.setupOutgoing(
Expand Down
Loading