Skip to content

Commit

Permalink
Emit more telling 1006 close events
Browse files Browse the repository at this point in the history
  • Loading branch information
uNetworkingAB committed Aug 2, 2024
1 parent 8d8fc0a commit 57f2662
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/WebSocketContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,11 @@ struct WebSocketContext {
return s;
});

/* Handle FIN, HTTP does not support half-closed sockets, so simply close */
/* Handle FIN, WebSocket does not support half-closed sockets, so simply close */
us_socket_context_on_end(SSL, getSocketContext(), [](auto *s) {

/* If we get a fin, we just close I guess */
us_socket_close(SSL, (us_socket_t *) s, 0, nullptr);
us_socket_close(SSL, (us_socket_t *) s, (int) ERR_TCP_FIN.length(), (void *) ERR_TCP_FIN.data());

return s;
});
Expand Down
8 changes: 5 additions & 3 deletions src/WebSocketProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const std::string_view ERR_WEBSOCKET_TIMEOUT("WebSocket timed out from inactivit
const std::string_view ERR_INVALID_TEXT("Received invalid UTF-8");
const std::string_view ERR_TOO_BIG_MESSAGE_INFLATION("Received too big message, or other inflation error");
const std::string_view ERR_INVALID_CLOSE_PAYLOAD("Received invalid close payload");
const std::string_view ERR_PROTOCOL("Received invalid WebSocket frame");
const std::string_view ERR_TCP_FIN("Received TCP FIN before WebSocket close frame");

enum OpCode : unsigned char {
CONTINUATION = 0,
Expand Down Expand Up @@ -341,12 +343,12 @@ struct WebSocketProtocol {
static inline bool consumeMessage(T payLength, char *&src, unsigned int &length, WebSocketState<isServer> *wState, void *user) {
if (getOpCode(src)) {
if (wState->state.opStack == 1 || (!wState->state.lastFin && getOpCode(src) < 2)) {
Impl::forceClose(wState, user);
Impl::forceClose(wState, user, ERR_PROTOCOL);
return true;
}
wState->state.opCode[++wState->state.opStack] = (OpCode) getOpCode(src);
} else if (wState->state.opStack == -1) {
Impl::forceClose(wState, user);
Impl::forceClose(wState, user, ERR_PROTOCOL);
return true;
}
wState->state.lastFin = isFin(src);
Expand Down Expand Up @@ -469,7 +471,7 @@ struct WebSocketProtocol {
// invalid reserved bits / invalid opcodes / invalid control frames / set compressed frame
if ((rsv1(src) && !Impl::setCompressed(wState, user)) || rsv23(src) || (getOpCode(src) > 2 && getOpCode(src) < 8) ||
getOpCode(src) > 10 || (getOpCode(src) > 2 && (!isFin(src) || payloadLength(src) > 125))) {
Impl::forceClose(wState, user);
Impl::forceClose(wState, user, ERR_PROTOCOL);
return;
}

Expand Down

0 comments on commit 57f2662

Please sign in to comment.