#1315 parse websocket msg len for correct data (#1316)

closes #1315
This commit is contained in:
Scott Fennell 2022-07-27 08:40:40 -05:00 committed by GitHub
parent 61a378553d
commit 2dd12fcbb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 11 additions and 9 deletions

View File

@ -63,7 +63,7 @@ class MyCivetServer {
void deleteWebSocketSession(struct mg_connection * nc); void deleteWebSocketSession(struct mg_connection * nc);
void installHTTPGEThandler(std::string handlerName, httpMethodHandler handler); void installHTTPGEThandler(std::string handlerName, httpMethodHandler handler);
void installWebSocketSessionMaker(std::string name, WebSocketSessionMaker maker); 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); void handleHTTPGETrequest(struct mg_connection *conn, const struct mg_request_info* ri, std::string handlerName);

View File

@ -21,7 +21,7 @@ class WebSocketSession {
*/ */
virtual void marshallData()=0; virtual void marshallData()=0;
virtual void sendMessage()=0; virtual void sendMessage()=0;
virtual int handleMessage(std::string)=0; virtual int handleMessage(const std::string&)=0;
struct mg_connection* connection; /* ** */ struct mg_connection* connection; /* ** */
}; };

View File

@ -36,7 +36,7 @@ void TimeSession::sendMessage() {
mg_websocket_write(connection, MG_WEBSOCKET_OPCODE_TEXT, message, strlen(message)); 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) { if (client_msg.compare("GMT") == 0) {
zone = TimeSession::GMT; zone = TimeSession::GMT;

View File

@ -15,7 +15,7 @@ class TimeSession : public WebSocketSession {
~TimeSession(); ~TimeSession();
void marshallData(); void marshallData();
void sendMessage(); void sendMessage();
int handleMessage(std::string); int handleMessage(const std::string&);
private: private:
time_t now; time_t now;
Zone zone; Zone zone;

View File

@ -21,7 +21,7 @@ class VariableServerSession : public WebSocketSession {
~VariableServerSession(); ~VariableServerSession();
void marshallData(); /* -- base */ void marshallData(); /* -- base */
void sendMessage(); /* -- base */ void sendMessage(); /* -- base */
int handleMessage(std::string); /* -- base */ int handleMessage(const std::string&); /* -- base */
void setTimeInterval(unsigned int milliseconds); void setTimeInterval(unsigned int milliseconds);
void addVariable(char* vname); void addVariable(char* vname);

View File

@ -371,7 +371,7 @@ int MyCivetServer::join() {
return 0; 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<mg_connection*, WebSocketSession*>::iterator iter; std::map<mg_connection*, WebSocketSession*>::iterator iter;
iter = webSocketSessionMap.find(conn); iter = webSocketSessionMap.find(conn);
if (iter != webSocketSessionMap.end()) { if (iter != webSocketSessionMap.end()) {

View File

@ -73,7 +73,7 @@ void VariableServerSession::sendMessage() {
} }
// Base class virtual function. // Base class virtual function.
int VariableServerSession::handleMessage(std::string client_msg) { int VariableServerSession::handleMessage(const std::string& client_msg) {
int status = 0; int status = 0;
std::vector<Member*> members = parseJSON(client_msg.c_str()); std::vector<Member*> members = parseJSON(client_msg.c_str());

View File

@ -177,10 +177,12 @@ int ws_data_handler(struct mg_connection *conn, int bits,
{ {
int rvalue = 1; int rvalue = 1;
MyCivetServer* server = (MyCivetServer*) my_server; 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) { if (data_len > 0) {
server->handleWebSocketClientMessage(conn, data); std::string msg(data, data_len);
server->handleWebSocketClientMessage(conn, msg);
} }
return rvalue; return rvalue;
} }