Skip to content

Commit b3730cc

Browse files
committed
Simplify handling of websocket PING/PONG frames
1 parent 4f96154 commit b3730cc

File tree

1 file changed

+4
-17
lines changed

1 file changed

+4
-17
lines changed

src/lib/lwan-websocket.c

+4-17
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,7 @@ static void
277277
ping_pong(struct lwan_request *request, uint16_t header, enum ws_opcode opcode)
278278
{
279279
const size_t len = header & 0x7f;
280-
char msg[128];
281-
char mask[4];
280+
char msg[4 + 128];
282281

283282
assert(header & WS_MASKED);
284283
assert(opcode == WS_OPCODE_PING || opcode == WS_OPCODE_PONG);
@@ -291,28 +290,16 @@ ping_pong(struct lwan_request *request, uint16_t header, enum ws_opcode opcode)
291290
__builtin_unreachable();
292291
}
293292

294-
struct iovec vec[] = {
295-
{.iov_base = mask, .iov_len = sizeof(mask)},
296-
{.iov_base = msg, .iov_len = len},
297-
};
298-
299293
if (opcode == WS_OPCODE_PING) {
300-
lwan_readv(request, vec, N_ELEMENTS(vec));
301-
unmask(msg, len, mask);
294+
lwan_recv(request, msg, len + 4, 0);
295+
unmask(msg + 4, len, msg);
302296
write_websocket_frame(request, WS_MASKED | WS_OPCODE_PONG, msg, len);
303297
} else {
304298
/* From MDN: "You might also get a pong without ever sending a ping;
305299
* ignore this if it happens." */
306300

307301
/* FIXME: should we care about the contents of PONG packets? */
308-
/* FIXME: should we have a lwan_recvmsg() too that takes an iovec? */
309-
const size_t total_len = vec[0].iov_len + vec[1].iov_len;
310-
if (LIKELY(total_len < sizeof(msg))) {
311-
lwan_recv(request, msg, total_len, MSG_TRUNC);
312-
} else {
313-
lwan_recv(request, vec[0].iov_base, vec[0].iov_len, MSG_TRUNC);
314-
lwan_recv(request, vec[1].iov_base, vec[1].iov_len, MSG_TRUNC);
315-
}
302+
lwan_recv(request, msg, len + 4, MSG_TRUNC);
316303
}
317304
}
318305

0 commit comments

Comments
 (0)