Skip to content

Commit 2af71ab

Browse files
committed
fix memory leek
send reason code on clientDisconnect if no reason buffer is set code style
1 parent f1ecfa9 commit 2af71ab

File tree

3 files changed

+63
-35
lines changed

3 files changed

+63
-35
lines changed

examples/WebSocketsServer/WebSocketsServer.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <ESP8266WiFi.h>
1111
#include <ESP8266WiFiMulti.h>
1212
#include <WebSocketsServer.h>
13-
#include <hash.h>
13+
#include <Hash.h>
1414

1515
ESP8266WiFiMulti WiFiMulti;
1616

src/WebSockets.cpp

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@
3333
*/
3434
void WebSockets::clientDisconnect(WSclient_t * client, uint16_t code, char * reason, size_t reasonLen) {
3535
if(client->status == WSC_CONNECTED && code) {
36-
sendFrame(client, WSop_close, (uint8_t *) reason, reasonLen);
36+
if(reason) {
37+
sendFrame(client, WSop_close, (uint8_t *) reason, reasonLen);
38+
} else {
39+
uint8_t buffer[2];
40+
buffer[0] = ((code >> 8) & 0xFF);
41+
buffer[1] = (code & 0xFF);
42+
sendFrame(client, WSop_close, &buffer[0], 2);
43+
}
3744
}
3845
clientDisconnect(client);
3946
}
@@ -171,6 +178,7 @@ void WebSockets::handleWebsocket(WSclient_t * client) {
171178

172179
if(!readWait(client, payload, payloadLen)) {
173180
DEBUG_WEBSOCKETS("[WS-Server][%d][handleWebsocket] missing data!\n", client->num);
181+
free(payload);
174182
clientDisconnect(client, 1002);
175183
return;
176184
}
@@ -183,36 +191,51 @@ void WebSockets::handleWebsocket(WSclient_t * client) {
183191
payload[i] = (payload[i] ^ maskKey[i % 4]);
184192
}
185193
}
194+
}
186195

187-
switch(opCode) {
188-
case WSop_text:
189-
DEBUG_WEBSOCKETS("[WS-Server][%d][handleWebsocket] text: %s\n", client->num, payload);
190-
// no break here!
191-
case WSop_binary:
192-
messageRecived(client, opCode, payload, payloadLen);
193-
break;
194-
case WSop_ping:
195-
// send pong back
196-
sendFrame(client, WSop_pong, payload, payloadLen);
197-
break;
198-
case WSop_pong:
199-
DEBUG_WEBSOCKETS("[WS-Server][%d][handleWebsocket] get pong from Client (%s)\n", client->num, payload);
200-
break;
201-
case WSop_close:
202-
{
203-
uint16_t reasonCode = buffer[0] << 8 | buffer[1];
204-
DEBUG_WEBSOCKETS("[WS-Server][%d][handleWebsocket] client ask for close. Code: %d (%s)\n", client->num, reasonCode, (payload + 2));
205-
clientDisconnect(client, 1000);
196+
switch(opCode) {
197+
case WSop_text:
198+
DEBUG_WEBSOCKETS("[WS-Server][%d][handleWebsocket] text: %s\n", client->num, payload)
199+
;
200+
// no break here!
201+
case WSop_binary:
202+
messageRecived(client, opCode, payload, payloadLen);
203+
break;
204+
case WSop_ping:
205+
// send pong back
206+
sendFrame(client, WSop_pong, payload, payloadLen);
207+
break;
208+
case WSop_pong:
209+
DEBUG_WEBSOCKETS("[WS-Server][%d][handleWebsocket] get pong from Client (%s)\n", client->num, payload)
210+
;
211+
break;
212+
case WSop_close:
213+
{
214+
uint16_t reasonCode = 1000;
215+
if(payloadLen >= 2) {
216+
reasonCode = payload[0] << 8 | payload[1];
206217
}
207-
break;
208-
case WSop_continuation:
209-
// continuation is not supported
210-
clientDisconnect(client, 1003);
211-
break;
212-
default:
213-
clientDisconnect(client, 1002);
214-
break;
215-
}
218+
219+
DEBUG_WEBSOCKETS("[WS-Server][%d][handleWebsocket] client ask for close. Code: %d", client->num, reasonCode);
220+
if(payloadLen > 2) {
221+
DEBUG_WEBSOCKETS("(%s)\n", (payload+2));
222+
} else {
223+
DEBUG_WEBSOCKETS("\n");
224+
}
225+
clientDisconnect(client, 1000);
226+
}
227+
break;
228+
case WSop_continuation:
229+
// continuation is not supported
230+
clientDisconnect(client, 1003);
231+
break;
232+
default:
233+
clientDisconnect(client, 1002);
234+
break;
235+
}
236+
237+
if(payload) {
238+
free(payload);
216239
}
217240

218241
}

src/WebSocketsServer.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void WebSocketsServer::sendTXT(uint8_t num, uint8_t * payload, size_t length) {
102102
}
103103

104104
void WebSocketsServer::sendTXT(uint8_t num, String payload) {
105-
sendTXT(num, (uint8_t *)payload.c_str(), payload.length());
105+
sendTXT(num, (uint8_t *) payload.c_str(), payload.length());
106106
}
107107

108108
/**
@@ -120,9 +120,8 @@ void WebSocketsServer::broadcastTXT(uint8_t * payload, size_t length) {
120120
}
121121
}
122122

123-
124123
void WebSocketsServer::broadcastTXT(String payload) {
125-
broadcastTXT((uint8_t *)payload.c_str(), payload.length());
124+
broadcastTXT((uint8_t *) payload.c_str(), payload.length());
126125
}
127126

128127
/**
@@ -311,7 +310,7 @@ void WebSocketsServer::handleHeader(WSclient_t * client) {
311310
if(headerLine.length() > 0) {
312311
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] RX: %s\n", client->num, headerLine.c_str());
313312

314-
// websocket request starts allway with GET see rfc6455
313+
// websocket request starts allways with GET see rfc6455
315314
if(headerLine.startsWith("GET ")) {
316315
// cut URL out
317316
client->cUrl = headerLine.substring(4, headerLine.indexOf(' ', 4));
@@ -337,7 +336,13 @@ void WebSocketsServer::handleHeader(WSclient_t * client) {
337336
} else {
338337
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] Header read fin.\n", client->num);
339338

340-
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);
339+
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] - cURL: %s\n", client->num, client->cUrl.c_str());
340+
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] - cIsUpgrade: %d\n", client->num, client->cIsUpgrade);
341+
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] - cIsWebsocket: %d\n", client->num, client->cIsWebsocket);
342+
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] - cKey: %s\n", client->num, client->cKey.c_str());
343+
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] - cProtocol: %s\n", client->num, client->cProtocol.c_str());
344+
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] - cExtensions: %s\n", client->num, client->cExtensions.c_str());
345+
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] - cVersion: %d\n", client->num, client->cVersion);
341346

342347
bool ok = (client->cIsUpgrade && client->cIsWebsocket);
343348

0 commit comments

Comments
 (0)