From 2dd12fcbb6c5b00bfb8290b53bb0e368428674ea Mon Sep 17 00:00:00 2001 From: Scott Fennell Date: Wed, 27 Jul 2022 08:40:40 -0500 Subject: [PATCH] #1315 parse websocket msg len for correct data (#1316) closes #1315 --- include/trick/MyCivetServer.hh | 2 +- include/trick/WebSocketSession.hh | 2 +- trick_sims/Cannon/models/httpMethods/TimeSession.cpp | 2 +- trick_sims/Cannon/models/httpMethods/TimeSession.hh | 2 +- .../web/CivetServer/include/VariableServerSession.hh | 2 +- trick_source/web/CivetServer/src/MyCivetServer.cpp | 2 +- trick_source/web/CivetServer/src/VariableServerSession.cpp | 2 +- trick_source/web/CivetServer/src/http_GET_handlers.cpp | 6 ++++-- 8 files changed, 11 insertions(+), 9 deletions(-) diff --git a/include/trick/MyCivetServer.hh b/include/trick/MyCivetServer.hh index 5e5a2855..e0763269 100644 --- a/include/trick/MyCivetServer.hh +++ b/include/trick/MyCivetServer.hh @@ -63,7 +63,7 @@ class MyCivetServer { void deleteWebSocketSession(struct mg_connection * nc); void installHTTPGEThandler(std::string handlerName, httpMethodHandler handler); void installWebSocketSessionMaker(std::string name, WebSocketSessionMaker maker); - void handleWebSocketClientMessage(struct mg_connection *conn, const char* data); + void handleWebSocketClientMessage(struct mg_connection *conn, const std::string& data); void handleHTTPGETrequest(struct mg_connection *conn, const struct mg_request_info* ri, std::string handlerName); diff --git a/include/trick/WebSocketSession.hh b/include/trick/WebSocketSession.hh index 0c643365..4dc2a416 100644 --- a/include/trick/WebSocketSession.hh +++ b/include/trick/WebSocketSession.hh @@ -21,7 +21,7 @@ class WebSocketSession { */ virtual void marshallData()=0; virtual void sendMessage()=0; - virtual int handleMessage(std::string)=0; + virtual int handleMessage(const std::string&)=0; struct mg_connection* connection; /* ** */ }; diff --git a/trick_sims/Cannon/models/httpMethods/TimeSession.cpp b/trick_sims/Cannon/models/httpMethods/TimeSession.cpp index 8dfe6c95..92a2cf24 100644 --- a/trick_sims/Cannon/models/httpMethods/TimeSession.cpp +++ b/trick_sims/Cannon/models/httpMethods/TimeSession.cpp @@ -36,7 +36,7 @@ void TimeSession::sendMessage() { mg_websocket_write(connection, MG_WEBSOCKET_OPCODE_TEXT, message, strlen(message)); } -int TimeSession::handleMessage(std::string client_msg) { +int TimeSession::handleMessage(const std::string& client_msg) { if (client_msg.compare("GMT") == 0) { zone = TimeSession::GMT; diff --git a/trick_sims/Cannon/models/httpMethods/TimeSession.hh b/trick_sims/Cannon/models/httpMethods/TimeSession.hh index dee11bc2..18f3629c 100644 --- a/trick_sims/Cannon/models/httpMethods/TimeSession.hh +++ b/trick_sims/Cannon/models/httpMethods/TimeSession.hh @@ -15,7 +15,7 @@ class TimeSession : public WebSocketSession { ~TimeSession(); void marshallData(); void sendMessage(); - int handleMessage(std::string); + int handleMessage(const std::string&); private: time_t now; Zone zone; diff --git a/trick_source/web/CivetServer/include/VariableServerSession.hh b/trick_source/web/CivetServer/include/VariableServerSession.hh index 36c81078..56408413 100644 --- a/trick_source/web/CivetServer/include/VariableServerSession.hh +++ b/trick_source/web/CivetServer/include/VariableServerSession.hh @@ -21,7 +21,7 @@ class VariableServerSession : public WebSocketSession { ~VariableServerSession(); void marshallData(); /* -- base */ void sendMessage(); /* -- base */ - int handleMessage(std::string); /* -- base */ + int handleMessage(const std::string&); /* -- base */ void setTimeInterval(unsigned int milliseconds); void addVariable(char* vname); diff --git a/trick_source/web/CivetServer/src/MyCivetServer.cpp b/trick_source/web/CivetServer/src/MyCivetServer.cpp index ce230082..daafbfaa 100644 --- a/trick_source/web/CivetServer/src/MyCivetServer.cpp +++ b/trick_source/web/CivetServer/src/MyCivetServer.cpp @@ -371,7 +371,7 @@ int MyCivetServer::join() { return 0; } -void MyCivetServer::handleWebSocketClientMessage(struct mg_connection *conn, const char* data) { +void MyCivetServer::handleWebSocketClientMessage(struct mg_connection *conn, const std::string& data) { std::map::iterator iter; iter = webSocketSessionMap.find(conn); if (iter != webSocketSessionMap.end()) { diff --git a/trick_source/web/CivetServer/src/VariableServerSession.cpp b/trick_source/web/CivetServer/src/VariableServerSession.cpp index c42d5694..e3c1a5ea 100644 --- a/trick_source/web/CivetServer/src/VariableServerSession.cpp +++ b/trick_source/web/CivetServer/src/VariableServerSession.cpp @@ -73,7 +73,7 @@ void VariableServerSession::sendMessage() { } // Base class virtual function. -int VariableServerSession::handleMessage(std::string client_msg) { +int VariableServerSession::handleMessage(const std::string& client_msg) { int status = 0; std::vector members = parseJSON(client_msg.c_str()); diff --git a/trick_source/web/CivetServer/src/http_GET_handlers.cpp b/trick_source/web/CivetServer/src/http_GET_handlers.cpp index ee914522..10b63768 100644 --- a/trick_source/web/CivetServer/src/http_GET_handlers.cpp +++ b/trick_source/web/CivetServer/src/http_GET_handlers.cpp @@ -177,10 +177,12 @@ int ws_data_handler(struct mg_connection *conn, int bits, { int rvalue = 1; MyCivetServer* server = (MyCivetServer*) my_server; - if (server->debug) { message_publish(MSG_INFO, "Trick Webserver: WEBSOCKET[%p] RECIEVED: \"%s\".\n", (void*)conn, data); } + std::string msg(data, data_len); + if (server->debug) { message_publish(MSG_INFO, "Trick Webserver: WEBSOCKET[%p] RECIEVED: \"%s\".\n", (void*)conn, msg.c_str()); } if (data_len > 0) { - server->handleWebSocketClientMessage(conn, data); + std::string msg(data, data_len); + server->handleWebSocketClientMessage(conn, msg); } return rvalue; }