Skip to content

Commit eaef4f0

Browse files
authored
Merge pull request #369 from sanketplus/master
make masking RFC complaint and fix #208
2 parents 50e2e36 + 4db22fe commit eaef4f0

File tree

4 files changed

+16
-15
lines changed

4 files changed

+16
-15
lines changed

src/WebSockets.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,11 @@ void WebSockets::clientDisconnect(WSclient_t * client, uint16_t code, char * rea
7777
* @param opcode WSopcode_t
7878
* @param payload uint8_t * ptr to the payload
7979
* @param length size_t length of the payload
80-
* @param mask bool add dummy mask to the frame (needed for web browser)
8180
* @param fin bool can be used to send data in more then one frame (set fin on the last frame)
8281
* @param headerToPayload bool set true if the payload has reserved 14 Byte at the beginning to dynamically add the Header (payload neet to be in RAM!)
8382
* @return true if ok
8483
*/
85-
bool WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length, bool mask, bool fin, bool headerToPayload) {
84+
bool WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length, bool fin, bool headerToPayload) {
8685

8786
if(client->tcp && !client->tcp->connected()) {
8887
DEBUG_WEBSOCKETS("[WS][%d][sendFrame] not Connected!?\n", client->num);
@@ -95,7 +94,7 @@ bool WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * pay
9594
}
9695

9796
DEBUG_WEBSOCKETS("[WS][%d][sendFrame] ------- send message frame -------\n", client->num);
98-
DEBUG_WEBSOCKETS("[WS][%d][sendFrame] fin: %u opCode: %u mask: %u length: %u headerToPayload: %u\n", client->num, fin, opcode, mask, length, headerToPayload);
97+
DEBUG_WEBSOCKETS("[WS][%d][sendFrame] fin: %u opCode: %u mask: %u length: %u headerToPayload: %u\n", client->num, fin, opcode, client->cIsClient, length, headerToPayload);
9998

10099
if(opcode == WSop_text) {
101100
DEBUG_WEBSOCKETS("[WS][%d][sendFrame] text: %s\n", client->num, (payload + (headerToPayload ? 14 : 0)));
@@ -119,7 +118,7 @@ bool WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * pay
119118
headerSize = 10;
120119
}
121120

122-
if(mask) {
121+
if(client->cIsClient) {
123122
headerSize += 4;
124123
}
125124

@@ -158,7 +157,7 @@ bool WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * pay
158157

159158
// byte 1
160159
*headerPtr = 0x00;
161-
if(mask) {
160+
if(client->cIsClient) {
162161
*headerPtr |= bit(7); ///< set mask
163162
}
164163

@@ -194,7 +193,7 @@ bool WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * pay
194193
headerPtr++;
195194
}
196195

197-
if(mask) {
196+
if(client->cIsClient) {
198197
if(useInternBuffer) {
199198
// if we use a Intern Buffer we can modify the data
200199
// by this fact its possible the do the masking
@@ -434,7 +433,7 @@ void WebSockets::handleWebsocketPayloadCb(WSclient_t * client, bool ok, uint8_t
434433
break;
435434
case WSop_ping:
436435
// send pong back
437-
sendFrame(client, WSop_pong, payload, header->payloadLen, true);
436+
sendFrame(client, WSop_pong, payload, header->payloadLen);
438437
break;
439438
case WSop_pong:
440439
DEBUG_WEBSOCKETS("[WS][%d][handleWebsocket] get pong (%s)\n", client->num, payload ? (const char*)payload : "");

src/WebSockets.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ typedef struct {
241241
String cUrl; ///< http url
242242
uint16_t cCode; ///< http code
243243

244+
bool cIsClient = false; ///< will be used for masking
244245
bool cIsUpgrade; ///< Connection == Upgrade
245246
bool cIsWebsocket; ///< Upgrade == websocket
246247

@@ -285,7 +286,7 @@ class WebSockets {
285286
virtual void messageReceived(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length, bool fin) = 0;
286287

287288
void clientDisconnect(WSclient_t * client, uint16_t code, char * reason = NULL, size_t reasonLen = 0);
288-
bool sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * payload = NULL, size_t length = 0, bool mask = false, bool fin = true, bool headerToPayload = false);
289+
bool sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * payload = NULL, size_t length = 0, bool fin = true, bool headerToPayload = false);
289290

290291
void headerDone(WSclient_t * client);
291292

src/WebSocketsClient.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
WebSocketsClient::WebSocketsClient() {
2929
_cbEvent = NULL;
3030
_client.num = 0;
31+
_client.cIsClient = true;
3132
_client.extraHeaders = WEBSOCKETS_STRING("Origin: file://");
3233
}
3334

@@ -194,7 +195,7 @@ bool WebSocketsClient::sendTXT(uint8_t * payload, size_t length, bool headerToPa
194195
length = strlen((const char *) payload);
195196
}
196197
if(clientIsConnected(&_client)) {
197-
return sendFrame(&_client, WSop_text, payload, length, true, true, headerToPayload);
198+
return sendFrame(&_client, WSop_text, payload, length, true, headerToPayload);
198199
}
199200
return false;
200201
}
@@ -225,7 +226,7 @@ bool WebSocketsClient::sendTXT(String & payload) {
225226
*/
226227
bool WebSocketsClient::sendBIN(uint8_t * payload, size_t length, bool headerToPayload) {
227228
if(clientIsConnected(&_client)) {
228-
return sendFrame(&_client, WSop_binary, payload, length, true, true, headerToPayload);
229+
return sendFrame(&_client, WSop_binary, payload, length, true, headerToPayload);
229230
}
230231
return false;
231232
}
@@ -242,7 +243,7 @@ bool WebSocketsClient::sendBIN(const uint8_t * payload, size_t length) {
242243
*/
243244
bool WebSocketsClient::sendPing(uint8_t * payload, size_t length) {
244245
if(clientIsConnected(&_client)) {
245-
return sendFrame(&_client, WSop_ping, payload, length, true);
246+
return sendFrame(&_client, WSop_ping, payload, length);
246247
}
247248
return false;
248249
}

src/WebSocketsServer.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ bool WebSocketsServer::sendTXT(uint8_t num, uint8_t * payload, size_t length, bo
184184
}
185185
WSclient_t * client = &_clients[num];
186186
if(clientIsConnected(client)) {
187-
return sendFrame(client, WSop_text, payload, length, false, true, headerToPayload);
187+
return sendFrame(client, WSop_text, payload, length, true, headerToPayload);
188188
}
189189
return false;
190190
}
@@ -222,7 +222,7 @@ bool WebSocketsServer::broadcastTXT(uint8_t * payload, size_t length, bool heade
222222
for(uint8_t i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) {
223223
client = &_clients[i];
224224
if(clientIsConnected(client)) {
225-
if(!sendFrame(client, WSop_text, payload, length, false, true, headerToPayload)) {
225+
if(!sendFrame(client, WSop_text, payload, length, true, headerToPayload)) {
226226
ret = false;
227227
}
228228
}
@@ -263,7 +263,7 @@ bool WebSocketsServer::sendBIN(uint8_t num, uint8_t * payload, size_t length, bo
263263
}
264264
WSclient_t * client = &_clients[num];
265265
if(clientIsConnected(client)) {
266-
return sendFrame(client, WSop_binary, payload, length, false, true, headerToPayload);
266+
return sendFrame(client, WSop_binary, payload, length, true, headerToPayload);
267267
}
268268
return false;
269269
}
@@ -285,7 +285,7 @@ bool WebSocketsServer::broadcastBIN(uint8_t * payload, size_t length, bool heade
285285
for(uint8_t i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) {
286286
client = &_clients[i];
287287
if(clientIsConnected(client)) {
288-
if(!sendFrame(client, WSop_binary, payload, length, false, true, headerToPayload)) {
288+
if(!sendFrame(client, WSop_binary, payload, length, true, headerToPayload)) {
289289
ret = false;
290290
}
291291
}

0 commit comments

Comments
 (0)