Skip to content

Commit

Permalink
fix: properly detect requests without body
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Aug 12, 2023
1 parent e7f0375 commit ac7bb8e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 9 deletions.
12 changes: 5 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,11 @@ const setupMaster = (httpServer, opts) => {
return;
}
workerId = computeWorkerId(data);
const mayHaveMultipleChunks = !(
data.startsWith("GET") ||
data
.substring(0, data.indexOf("\r\n\r\n"))
.includes("pgrade: websocket")
);
socket.pause();

const head = data.substring(0, data.indexOf("\r\n\r\n")).toLowerCase();
const mayHaveMultipleChunks =
head.includes("content-length:") || head.includes("transfer-encoding:");

if (mayHaveMultipleChunks) {
connectionId = randomId();
}
Expand Down
71 changes: 71 additions & 0 deletions test/fixtures/cors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const cluster = require("cluster");
const { createServer, request } = require("http");
const { Server } = require("socket.io");
const { setupMaster, setupWorker } = require("../..");
const assert = require("assert").strict;

if (cluster.isWorker) {
const httpServer = createServer();
const io = new Server(httpServer, {
cors: {
origin: true,
},
});
setupWorker(io);

io.on("connection", (socket) => {
socket.on("foo", (val) => {
socket.emit("bar", val);
});
});

return;
}

const WORKER_COUNT = 3;

for (let i = 0; i < WORKER_COUNT; i++) {
cluster.fork();
}

const httpServer = createServer();

setupMaster(httpServer, {
loadBalancingMethod: process.env.LB_METHOD || "least-connection",
});

const waitFor = (emitter, event) => {
return new Promise((resolve) => {
emitter.once(event, resolve);
});
};

httpServer.listen(async () => {
const port = httpServer.address().port;

const res = await sendRequest({
port,
method: "options",
path: "/socket.io/",
headers: {
origin: "https://example.com",
},
});

assert.equal(res.statusCode, 204);

assert.equal(
res.headers["access-control-allow-origin"],
"https://example.com"
);

// cleanup
for (const id in cluster.workers) {
cluster.workers[id].kill();
}
httpServer.close();
});

function sendRequest(options) {
return new Promise((resolve) => request(options, resolve).end());
}
4 changes: 2 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ describe("@socket.io/sticky", () => {
exec(fixture("connection.js"), { env: { TRANSPORT: "polling" } }, done);
});

it("should work with HTTP long-polling only", (done) => {
exec(fixture("connection.js"), { env: { TRANSPORT: "polling" } }, done);
it("should work with CORS", (done) => {
exec(fixture("cors.js"), done);
});

it("should return a 503 error when no worker is available (polling)", (done) => {
Expand Down

0 comments on commit ac7bb8e

Please sign in to comment.