From 45367a7355bbb320f3640fdc5175c004b1c5a02d Mon Sep 17 00:00:00 2001 From: tt2468 Date: Fri, 30 Apr 2021 10:03:38 -0700 Subject: [PATCH] WebSocketProtocol: Session parameter stuff --- src/WebSocketProtocol.cpp | 49 +++++++++++++++++++++++++++++++++++++++ src/WebSocketProtocol.h | 4 ++++ 2 files changed, 53 insertions(+) diff --git a/src/WebSocketProtocol.cpp b/src/WebSocketProtocol.cpp index ebd223b5..f2ca3df3 100644 --- a/src/WebSocketProtocol.cpp +++ b/src/WebSocketProtocol.cpp @@ -4,10 +4,46 @@ #include "plugin-macros.generated.h" +bool IsSupportedRpcVersion(uint8_t requestedVersion) +{ + for (auto version : WebSocketProtocol::SupportedRpcVersions) { + if (requestedVersion = version) + return true; + } + return false; +} + WebSocketProtocol::ProcessResult SetSessionParameters(SessionPtr session, json incomingMessage) { WebSocketProtocol::ProcessResult ret; + if (incomingMessage.contains("ignoreInvalidMessages")) { + if (!incomingMessage["ignoreInvalidMessages"].is_boolean()) { + ret.closeCode = WebSocketServer::WebSocketCloseCode::InvalidIdentifyParameter; + ret.closeReason = "You specified `ignoreInvalidMessages` but the value is not boolean."; + return ret; + } + session->SetIgnoreInvalidMessages(incomingMessage["ignoreInvalidMessages"]); + } + + if (incomingMessage.contains("ignoreNonFatalRequestChecks")) { + if (!incomingMessage["ignoreNonFatalRequestChecks"].is_boolean()) { + ret.closeCode = WebSocketServer::WebSocketCloseCode::InvalidIdentifyParameter; + ret.closeReason = "You specified `ignoreNonFatalRequestChecks` but the value is not boolean."; + return ret; + } + session->SetIgnoreNonFatalRequestChecks(incomingMessage["ignoreNonFatalRequestChecks"]); + } + + if (incomingMessage.contains("eventSubscriptions")) { + if (!incomingMessage["eventSubscriptions"].is_number_unsigned()) { + ret.closeCode = WebSocketServer::WebSocketCloseCode::InvalidIdentifyParameter; + ret.closeReason = "You specified `eventSubscriptions` but the value is not an unsigned integer."; + return ret; + } + session->SetEventSubscriptions(incomingMessage["eventSubscriptions"]); + } + return ret; } @@ -77,6 +113,19 @@ WebSocketProtocol::ProcessResult WebSocketProtocol::ProcessMessage(SessionPtr se } } + if (!incomingMessage.contains("rpcVersion") || !incomingMessage["rpcVersion"].is_number_unsigned()) { + ret.closeCode = WebSocketServer::WebSocketCloseCode::InvalidIdentifyParameter; + ret.closeReason = "Your Identify is missing `rpcVersion` or is not an integer."; + return ret; + } + uint8_t requestedRpcVersion = incomingMessage["rpcVersion"]; + if (!IsSupportedRpcVersion(requestedRpcVersion)) { + ret.closeCode = WebSocketServer::WebSocketCloseCode::UnsupportedProtocolVersion; + ret.closeReason = "Your requested RPC version is not supported by this server."; + return ret; + } + session->SetRpcVersion(requestedRpcVersion); + WebSocketProtocol::ProcessResult parameterResult = SetSessionParameters(session, incomingMessage); if (ret.closeCode != WebSocketServer::WebSocketCloseCode::DontClose) { return parameterResult; diff --git a/src/WebSocketProtocol.h b/src/WebSocketProtocol.h index b773740b..15dc50be 100644 --- a/src/WebSocketProtocol.h +++ b/src/WebSocketProtocol.h @@ -8,6 +8,10 @@ #include "requesthandler/RequestHandler.h" namespace WebSocketProtocol { + const std::vector SupportedRpcVersions( + 1 + ); + struct ProcessResult { WebSocketServer::WebSocketCloseCode closeCode = WebSocketServer::WebSocketCloseCode::DontClose; std::string closeReason;