mirror of
https://github.com/Links2004/arduinoWebSockets.git
synced 2025-07-13 07:16:31 +02:00
fix memory leek
send reason code on clientDisconnect if no reason buffer is set code style
This commit is contained in:
@ -10,7 +10,7 @@
|
|||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <ESP8266WiFiMulti.h>
|
#include <ESP8266WiFiMulti.h>
|
||||||
#include <WebSocketsServer.h>
|
#include <WebSocketsServer.h>
|
||||||
#include <hash.h>
|
#include <Hash.h>
|
||||||
|
|
||||||
ESP8266WiFiMulti WiFiMulti;
|
ESP8266WiFiMulti WiFiMulti;
|
||||||
|
|
||||||
|
@ -33,7 +33,14 @@
|
|||||||
*/
|
*/
|
||||||
void WebSockets::clientDisconnect(WSclient_t * client, uint16_t code, char * reason, size_t reasonLen) {
|
void WebSockets::clientDisconnect(WSclient_t * client, uint16_t code, char * reason, size_t reasonLen) {
|
||||||
if(client->status == WSC_CONNECTED && code) {
|
if(client->status == WSC_CONNECTED && code) {
|
||||||
sendFrame(client, WSop_close, (uint8_t *) reason, reasonLen);
|
if(reason) {
|
||||||
|
sendFrame(client, WSop_close, (uint8_t *) reason, reasonLen);
|
||||||
|
} else {
|
||||||
|
uint8_t buffer[2];
|
||||||
|
buffer[0] = ((code >> 8) & 0xFF);
|
||||||
|
buffer[1] = (code & 0xFF);
|
||||||
|
sendFrame(client, WSop_close, &buffer[0], 2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
clientDisconnect(client);
|
clientDisconnect(client);
|
||||||
}
|
}
|
||||||
@ -171,6 +178,7 @@ void WebSockets::handleWebsocket(WSclient_t * client) {
|
|||||||
|
|
||||||
if(!readWait(client, payload, payloadLen)) {
|
if(!readWait(client, payload, payloadLen)) {
|
||||||
DEBUG_WEBSOCKETS("[WS-Server][%d][handleWebsocket] missing data!\n", client->num);
|
DEBUG_WEBSOCKETS("[WS-Server][%d][handleWebsocket] missing data!\n", client->num);
|
||||||
|
free(payload);
|
||||||
clientDisconnect(client, 1002);
|
clientDisconnect(client, 1002);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -183,36 +191,51 @@ void WebSockets::handleWebsocket(WSclient_t * client) {
|
|||||||
payload[i] = (payload[i] ^ maskKey[i % 4]);
|
payload[i] = (payload[i] ^ maskKey[i % 4]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
switch(opCode) {
|
switch(opCode) {
|
||||||
case WSop_text:
|
case WSop_text:
|
||||||
DEBUG_WEBSOCKETS("[WS-Server][%d][handleWebsocket] text: %s\n", client->num, payload);
|
DEBUG_WEBSOCKETS("[WS-Server][%d][handleWebsocket] text: %s\n", client->num, payload)
|
||||||
// no break here!
|
;
|
||||||
case WSop_binary:
|
// no break here!
|
||||||
messageRecived(client, opCode, payload, payloadLen);
|
case WSop_binary:
|
||||||
break;
|
messageRecived(client, opCode, payload, payloadLen);
|
||||||
case WSop_ping:
|
break;
|
||||||
// send pong back
|
case WSop_ping:
|
||||||
sendFrame(client, WSop_pong, payload, payloadLen);
|
// send pong back
|
||||||
break;
|
sendFrame(client, WSop_pong, payload, payloadLen);
|
||||||
case WSop_pong:
|
break;
|
||||||
DEBUG_WEBSOCKETS("[WS-Server][%d][handleWebsocket] get pong from Client (%s)\n", client->num, payload);
|
case WSop_pong:
|
||||||
break;
|
DEBUG_WEBSOCKETS("[WS-Server][%d][handleWebsocket] get pong from Client (%s)\n", client->num, payload)
|
||||||
case WSop_close:
|
;
|
||||||
{
|
break;
|
||||||
uint16_t reasonCode = buffer[0] << 8 | buffer[1];
|
case WSop_close:
|
||||||
DEBUG_WEBSOCKETS("[WS-Server][%d][handleWebsocket] client ask for close. Code: %d (%s)\n", client->num, reasonCode, (payload + 2));
|
{
|
||||||
clientDisconnect(client, 1000);
|
uint16_t reasonCode = 1000;
|
||||||
|
if(payloadLen >= 2) {
|
||||||
|
reasonCode = payload[0] << 8 | payload[1];
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
case WSop_continuation:
|
DEBUG_WEBSOCKETS("[WS-Server][%d][handleWebsocket] client ask for close. Code: %d", client->num, reasonCode);
|
||||||
// continuation is not supported
|
if(payloadLen > 2) {
|
||||||
clientDisconnect(client, 1003);
|
DEBUG_WEBSOCKETS("(%s)\n", (payload+2));
|
||||||
break;
|
} else {
|
||||||
default:
|
DEBUG_WEBSOCKETS("\n");
|
||||||
clientDisconnect(client, 1002);
|
}
|
||||||
break;
|
clientDisconnect(client, 1000);
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
case WSop_continuation:
|
||||||
|
// continuation is not supported
|
||||||
|
clientDisconnect(client, 1003);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
clientDisconnect(client, 1002);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(payload) {
|
||||||
|
free(payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -102,7 +102,7 @@ void WebSocketsServer::sendTXT(uint8_t num, uint8_t * payload, size_t length) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void WebSocketsServer::sendTXT(uint8_t num, String payload) {
|
void WebSocketsServer::sendTXT(uint8_t num, String payload) {
|
||||||
sendTXT(num, (uint8_t *)payload.c_str(), payload.length());
|
sendTXT(num, (uint8_t *) payload.c_str(), payload.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -120,9 +120,8 @@ void WebSocketsServer::broadcastTXT(uint8_t * payload, size_t length) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void WebSocketsServer::broadcastTXT(String payload) {
|
void WebSocketsServer::broadcastTXT(String payload) {
|
||||||
broadcastTXT((uint8_t *)payload.c_str(), payload.length());
|
broadcastTXT((uint8_t *) payload.c_str(), payload.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -311,7 +310,7 @@ void WebSocketsServer::handleHeader(WSclient_t * client) {
|
|||||||
if(headerLine.length() > 0) {
|
if(headerLine.length() > 0) {
|
||||||
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] RX: %s\n", client->num, headerLine.c_str());
|
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] RX: %s\n", client->num, headerLine.c_str());
|
||||||
|
|
||||||
// websocket request starts allway with GET see rfc6455
|
// websocket request starts allways with GET see rfc6455
|
||||||
if(headerLine.startsWith("GET ")) {
|
if(headerLine.startsWith("GET ")) {
|
||||||
// cut URL out
|
// cut URL out
|
||||||
client->cUrl = headerLine.substring(4, headerLine.indexOf(' ', 4));
|
client->cUrl = headerLine.substring(4, headerLine.indexOf(' ', 4));
|
||||||
@ -337,7 +336,13 @@ void WebSocketsServer::handleHeader(WSclient_t * client) {
|
|||||||
} else {
|
} else {
|
||||||
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] Header read fin.\n", client->num);
|
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] Header read fin.\n", client->num);
|
||||||
|
|
||||||
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] - cURL: %s\n", client->num, client->cUrl.c_str());DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] - cIsUpgrade: %d\n", client->num, client->cIsUpgrade);DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] - cIsWebsocket: %d\n", client->num, client->cIsWebsocket);DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] - cKey: %s\n", client->num, client->cKey.c_str());DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] - cProtocol: %s\n", client->num, client->cProtocol.c_str());DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] - cExtensions: %s\n", client->num, client->cExtensions.c_str());DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] - cVersion: %d\n", client->num, client->cVersion);
|
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] - cURL: %s\n", client->num, client->cUrl.c_str());
|
||||||
|
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] - cIsUpgrade: %d\n", client->num, client->cIsUpgrade);
|
||||||
|
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] - cIsWebsocket: %d\n", client->num, client->cIsWebsocket);
|
||||||
|
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] - cKey: %s\n", client->num, client->cKey.c_str());
|
||||||
|
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] - cProtocol: %s\n", client->num, client->cProtocol.c_str());
|
||||||
|
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] - cExtensions: %s\n", client->num, client->cExtensions.c_str());
|
||||||
|
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] - cVersion: %d\n", client->num, client->cVersion);
|
||||||
|
|
||||||
bool ok = (client->cIsUpgrade && client->cIsWebsocket);
|
bool ok = (client->cIsUpgrade && client->cIsWebsocket);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user