Skip to content

Latest commit

 

History

History
525 lines (455 loc) · 18.3 KB

File metadata and controls

525 lines (455 loc) · 18.3 KB
 
Oct 28, 2018
Oct 28, 2018
1
/*
Jun 8, 2020
Jun 8, 2020
2
* Authored by Alex Hultman, 2018-2020.
Jan 7, 2019
Jan 7, 2019
3
* Intellectual property of third-party.
Oct 28, 2018
Oct 28, 2018
4
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
* you may not use this file except in compliance with the License.
7
* You may obtain a copy of the License at
8
9
* http://www.apache.org/licenses/LICENSE-2.0
10
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
16
*/
17
Jun 9, 2019
Jun 9, 2019
18
#ifndef UWS_WEBSOCKETPROTOCOL_H
19
#define UWS_WEBSOCKETPROTOCOL_H
Sep 2, 2018
Sep 2, 2018
20
Apr 27, 2025
Apr 27, 2025
21
#include <libusockets.h>
22
Dec 28, 2018
Dec 28, 2018
23
#include <cstdint>
Sep 2, 2018
Sep 2, 2018
24
#include <cstring>
25
#include <cstdlib>
Jun 26, 2020
Jun 26, 2020
26
#include <string_view>
Sep 2, 2018
Sep 2, 2018
27
Jun 27, 2025
Jun 27, 2025
28
#ifdef UWS_USE_SIMDUTF
29
#include <simdutf.h>
30
#endif
31
Sep 2, 2018
Sep 2, 2018
32
namespace uWS {
33
Jun 26, 2020
Jun 26, 2020
34
/* We should not overcomplicate these */
Feb 3, 2025
Feb 3, 2025
35
constexpr std::string_view ERR_TOO_BIG_MESSAGE("Received too big message");
36
constexpr std::string_view ERR_WEBSOCKET_TIMEOUT("WebSocket timed out from inactivity");
37
constexpr std::string_view ERR_INVALID_TEXT("Received invalid UTF-8");
38
constexpr std::string_view ERR_TOO_BIG_MESSAGE_INFLATION("Received too big message, or other inflation error");
39
constexpr std::string_view ERR_INVALID_CLOSE_PAYLOAD("Received invalid close payload");
40
constexpr std::string_view ERR_PROTOCOL("Received invalid WebSocket frame");
41
constexpr std::string_view ERR_TCP_FIN("Received TCP FIN before WebSocket close frame");
Jun 26, 2020
Jun 26, 2020
42
Sep 2, 2018
Sep 2, 2018
43
enum OpCode : unsigned char {
Oct 30, 2021
Oct 30, 2021
44
CONTINUATION = 0,
Sep 2, 2018
Sep 2, 2018
45
TEXT = 1,
46
BINARY = 2,
47
CLOSE = 8,
48
PING = 9,
49
PONG = 10
50
};
51
52
enum {
53
CLIENT,
54
SERVER
55
};
56
57
// 24 bytes perfectly
58
template <bool isServer>
59
struct WebSocketState {
60
public:
61
static const unsigned int SHORT_MESSAGE_HEADER = isServer ? 6 : 2;
62
static const unsigned int MEDIUM_MESSAGE_HEADER = isServer ? 8 : 4;
63
static const unsigned int LONG_MESSAGE_HEADER = isServer ? 14 : 10;
64
65
// 16 bytes
66
struct State {
67
unsigned int wantsHead : 1;
68
unsigned int spillLength : 4;
May 8, 2020
May 8, 2020
69
signed int opStack : 2; // -1, 0, 1
Sep 2, 2018
Sep 2, 2018
70
unsigned int lastFin : 1;
71
72
// 15 bytes
73
unsigned char spill[LONG_MESSAGE_HEADER - 1];
74
OpCode opCode[2];
75
76
State() {
77
wantsHead = true;
78
spillLength = 0;
79
opStack = -1;
80
lastFin = true;
81
}
82
83
} state;
84
85
// 8 bytes
86
unsigned int remainingBytes = 0;
87
char mask[isServer ? 4 : 1];
88
};
89
Dec 28, 2018
Dec 28, 2018
90
namespace protocol {
91
Apr 15, 2019
Apr 15, 2019
92
template <typename T>
93
T bit_cast(char *c) {
94
T val;
95
memcpy(&val, c, sizeof(T));
96
return val;
97
}
98
Dec 28, 2018
Dec 28, 2018
99
/* Byte swap for little-endian systems */
100
template <typename T>
101
T cond_byte_swap(T value) {
Jun 10, 2025
Jun 10, 2025
102
static_assert(std::is_trivially_copyable<T>::value, "T must be trivially copyable");
Dec 28, 2018
Dec 28, 2018
103
uint32_t endian_test = 1;
Jun 10, 2025
Jun 10, 2025
104
if (*reinterpret_cast<char*>(&endian_test)) {
105
uint8_t src[sizeof(T)];
106
uint8_t dst[sizeof(T)];
107
108
std::memcpy(src, &value, sizeof(T));
109
for (size_t i = 0; i < sizeof(T); ++i) {
110
dst[i] = src[sizeof(T) - 1 - i];
Dec 28, 2018
Dec 28, 2018
111
}
112
Jun 10, 2025
Jun 10, 2025
113
T result;
114
std::memcpy(&result, dst, sizeof(T));
115
return result;
Dec 28, 2018
Dec 28, 2018
116
}
117
return value;
118
}
119
Jun 27, 2025
Jun 27, 2025
120
#ifdef UWS_USE_SIMDUTF
121
122
static bool isValidUtf8(unsigned char *s, size_t length)
123
{
124
return simdutf::validate_utf8((const char *)s, length);
125
}
126
127
#else
Dec 28, 2018
Dec 28, 2018
128
// Based on utf8_check.c by Markus Kuhn, 2005
129
// https://www.cl.cam.ac.uk/~mgk25/ucs/utf8_check.c
130
// Optimized for predominantly 7-bit content by Alex Hultman, 2016
131
// Licensed as Zlib, like the rest of this project
Jul 3, 2024
Jul 3, 2024
132
// This runs about 40% faster than simdutf with g++ -mavx
Dec 28, 2018
Dec 28, 2018
133
static bool isValidUtf8(unsigned char *s, size_t length)
134
{
135
for (unsigned char *e = s + length; s != e; ) {
Jul 3, 2024
Jul 3, 2024
136
if (s + 16 <= e) {
137
uint64_t tmp[2];
Jul 3, 2024
Jul 3, 2024
138
memcpy(tmp, s, 16);
Jul 3, 2024
Jul 3, 2024
139
if (((tmp[0] & 0x8080808080808080) | (tmp[1] & 0x8080808080808080)) == 0) {
140
s += 16;
Jan 1, 2020
Jan 1, 2020
141
continue;
Dec 28, 2018
Dec 28, 2018
142
}
Jan 1, 2020
Jan 1, 2020
143
}
Dec 28, 2018
Dec 28, 2018
144
Jan 1, 2020
Jan 1, 2020
145
while (!(*s & 0x80)) {
146
if (++s == e) {
147
return true;
148
}
149
}
150
151
if ((s[0] & 0x60) == 0x40) {
152
if (s + 1 >= e || (s[1] & 0xc0) != 0x80 || (s[0] & 0xfe) == 0xc0) {
153
return false;
154
}
155
s += 2;
156
} else if ((s[0] & 0xf0) == 0xe0) {
157
if (s + 2 >= e || (s[1] & 0xc0) != 0x80 || (s[2] & 0xc0) != 0x80 ||
158
(s[0] == 0xe0 && (s[1] & 0xe0) == 0x80) || (s[0] == 0xed && (s[1] & 0xe0) == 0xa0)) {
Dec 28, 2018
Dec 28, 2018
159
return false;
160
}
Jan 1, 2020
Jan 1, 2020
161
s += 3;
162
} else if ((s[0] & 0xf8) == 0xf0) {
163
if (s + 3 >= e || (s[1] & 0xc0) != 0x80 || (s[2] & 0xc0) != 0x80 || (s[3] & 0xc0) != 0x80 ||
164
(s[0] == 0xf0 && (s[1] & 0xf0) == 0x80) || (s[0] == 0xf4 && s[1] > 0x8f) || s[0] > 0xf4) {
165
return false;
166
}
167
s += 4;
168
} else {
169
return false;
Dec 28, 2018
Dec 28, 2018
170
}
171
}
172
return true;
173
}
174
Jun 27, 2025
Jun 27, 2025
175
#endif
176
Dec 28, 2018
Dec 28, 2018
177
struct CloseFrame {
178
uint16_t code;
179
char *message;
180
size_t length;
181
};
182
183
static inline CloseFrame parseClosePayload(char *src, size_t length) {
Jun 26, 2020
Jun 26, 2020
184
/* If we get no code or message, default to reporting 1005 no status code present */
Nov 30, 2020
Nov 30, 2020
185
CloseFrame cf = {1005, nullptr, 0};
Dec 28, 2018
Dec 28, 2018
186
if (length >= 2) {
187
memcpy(&cf.code, src, 2);
Dec 28, 2018
Dec 28, 2018
188
cf = {cond_byte_swap<uint16_t>(cf.code), src + 2, length - 2};
Dec 28, 2018
Dec 28, 2018
189
if (cf.code < 1000 || cf.code > 4999 || (cf.code > 1011 && cf.code < 4000) ||
190
(cf.code >= 1004 && cf.code <= 1006) || !isValidUtf8((unsigned char *) cf.message, cf.length)) {
Jun 26, 2020
Jun 26, 2020
191
/* Even though we got a WebSocket close frame, it in itself is abnormal */
Jun 19, 2024
Jun 19, 2024
192
return {1006, (char *) ERR_INVALID_CLOSE_PAYLOAD.data(), ERR_INVALID_CLOSE_PAYLOAD.length()};
Dec 28, 2018
Dec 28, 2018
193
}
194
}
195
return cf;
196
}
197
198
static inline size_t formatClosePayload(char *dst, uint16_t code, const char *message, size_t length) {
Jun 26, 2020
Jun 26, 2020
199
/* We could have more strict checks here, but never append code 0 or 1005 or 1006 */
200
if (code && code != 1005 && code != 1006) {
Dec 28, 2018
Dec 28, 2018
201
code = cond_byte_swap<uint16_t>(code);
Dec 28, 2018
Dec 28, 2018
202
memcpy(dst, &code, 2);
Jan 1, 2020
Jan 1, 2020
203
/* It is invalid to pass nullptr to memcpy, even though length is 0 */
204
if (message) {
205
memcpy(dst + 2, message, length);
206
}
Dec 28, 2018
Dec 28, 2018
207
return length + 2;
208
}
209
return 0;
210
}
211
212
static inline size_t messageFrameSize(size_t messageSize) {
213
if (messageSize < 126) {
214
return 2 + messageSize;
215
} else if (messageSize <= UINT16_MAX) {
216
return 4 + messageSize;
217
}
218
return 10 + messageSize;
219
}
220
221
enum {
222
SND_CONTINUATION = 1,
223
SND_NO_FIN = 2,
224
SND_COMPRESSED = 64
225
};
226
227
template <bool isServer>
Oct 30, 2021
Oct 30, 2021
228
static inline size_t formatMessage(char *dst, const char *src, size_t length, OpCode opCode, size_t reportedLength, bool compressed, bool fin) {
Dec 28, 2018
Dec 28, 2018
229
size_t messageLength;
230
size_t headerLength;
231
if (reportedLength < 126) {
232
headerLength = 2;
Dec 28, 2019
Dec 28, 2019
233
dst[1] = (char) reportedLength;
Dec 28, 2018
Dec 28, 2018
234
} else if (reportedLength <= UINT16_MAX) {
235
headerLength = 4;
236
dst[1] = 126;
Dec 28, 2019
Dec 28, 2019
237
uint16_t tmp = cond_byte_swap<uint16_t>((uint16_t) reportedLength);
Jul 8, 2019
Jul 8, 2019
238
memcpy(&dst[2], &tmp, sizeof(uint16_t));
Dec 28, 2018
Dec 28, 2018
239
} else {
240
headerLength = 10;
241
dst[1] = 127;
Dec 28, 2019
Dec 28, 2019
242
uint64_t tmp = cond_byte_swap<uint64_t>((uint64_t) reportedLength);
Jul 8, 2019
Jul 8, 2019
243
memcpy(&dst[2], &tmp, sizeof(uint64_t));
Dec 28, 2018
Dec 28, 2018
244
}
245
Oct 30, 2021
Oct 30, 2021
246
dst[0] = (char) ((fin ? 128 : 0) | ((compressed && opCode) ? SND_COMPRESSED : 0) | (char) opCode);
247
248
//printf("%d\n", (int)dst[0]);
Dec 28, 2018
Dec 28, 2018
249
250
char mask[4];
251
if (!isServer) {
252
dst[1] |= 0x80;
Nov 30, 2020
Nov 30, 2020
253
uint32_t random = (uint32_t) rand();
Dec 28, 2018
Dec 28, 2018
254
memcpy(mask, &random, 4);
255
memcpy(dst + headerLength, &random, 4);
256
headerLength += 4;
257
}
258
259
messageLength = headerLength + length;
260
memcpy(dst + headerLength, src, length);
261
262
if (!isServer) {
263
264
// overwrites up to 3 bytes outside of the given buffer!
265
//WebSocketProtocol<isServer>::unmaskInplace(dst + headerLength, dst + headerLength + length, mask);
266
267
// this is not optimal
268
char *start = dst + headerLength;
269
char *stop = start + length;
270
int i = 0;
271
while (start != stop) {
272
(*start++) ^= mask[i++ % 4];
273
}
274
}
275
return messageLength;
276
}
277
278
}
279
280
// essentially this is only a parser
Jun 9, 2019
Jun 9, 2019
281
template <const bool isServer, typename Impl>
Nov 2, 2022
Nov 2, 2022
282
struct WebSocketProtocol {
Sep 2, 2018
Sep 2, 2018
283
public:
284
static const unsigned int SHORT_MESSAGE_HEADER = isServer ? 6 : 2;
285
static const unsigned int MEDIUM_MESSAGE_HEADER = isServer ? 8 : 4;
286
static const unsigned int LONG_MESSAGE_HEADER = isServer ? 14 : 10;
287
288
protected:
289
static inline bool isFin(char *frame) {return *((unsigned char *) frame) & 128;}
290
static inline unsigned char getOpCode(char *frame) {return *((unsigned char *) frame) & 15;}
291
static inline unsigned char payloadLength(char *frame) {return ((unsigned char *) frame)[1] & 127;}
292
static inline bool rsv23(char *frame) {return *((unsigned char *) frame) & 48;}
293
static inline bool rsv1(char *frame) {return *((unsigned char *) frame) & 64;}
294
Dec 19, 2021
Dec 19, 2021
295
template <int N>
296
static inline void UnrolledXor(char * __restrict data, char * __restrict mask) {
297
if constexpr (N != 1) {
298
UnrolledXor<N - 1>(data, mask);
299
}
300
data[N - 1] ^= mask[(N - 1) % 4];
301
}
302
May 6, 2023
May 6, 2023
303
template <int DESTINATION>
304
static inline void unmaskImprecise8(char *src, uint64_t mask, unsigned int length) {
305
for (unsigned int n = (length >> 3) + 1; n; n--) {
306
uint64_t loaded;
307
memcpy(&loaded, src, 8);
308
loaded ^= mask;
309
memcpy(src - DESTINATION, &loaded, 8);
310
src += 8;
311
}
312
}
313
314
/* DESTINATION = 6 makes this not SIMD, DESTINATION = 4 is with SIMD but we don't want that for short messages */
315
template <int DESTINATION>
316
static inline void unmaskImprecise4(char *src, uint32_t mask, unsigned int length) {
Sep 2, 2018
Sep 2, 2018
317
for (unsigned int n = (length >> 2) + 1; n; n--) {
May 6, 2023
May 6, 2023
318
uint32_t loaded;
319
memcpy(&loaded, src, 4);
320
loaded ^= mask;
321
memcpy(src - DESTINATION, &loaded, 4);
322
src += 4;
Sep 2, 2018
Sep 2, 2018
323
}
324
}
325
May 6, 2023
May 6, 2023
326
template <int HEADER_SIZE>
327
static inline void unmaskImpreciseCopyMask(char *src, unsigned int length) {
328
if constexpr (HEADER_SIZE != 6) {
329
char mask[8] = {src[-4], src[-3], src[-2], src[-1], src[-4], src[-3], src[-2], src[-1]};
330
uint64_t maskInt;
331
memcpy(&maskInt, mask, 8);
332
unmaskImprecise8<HEADER_SIZE>(src, maskInt, length);
333
} else {
334
char mask[4] = {src[-4], src[-3], src[-2], src[-1]};
335
uint32_t maskInt;
336
memcpy(&maskInt, mask, 4);
337
unmaskImprecise4<HEADER_SIZE>(src, maskInt, length);
338
}
Sep 2, 2018
Sep 2, 2018
339
}
340
341
static inline void rotateMask(unsigned int offset, char *mask) {
342
char originalMask[4] = {mask[0], mask[1], mask[2], mask[3]};
343
mask[(0 + offset) % 4] = originalMask[0];
344
mask[(1 + offset) % 4] = originalMask[1];
345
mask[(2 + offset) % 4] = originalMask[2];
346
mask[(3 + offset) % 4] = originalMask[3];
347
}
348
349
static inline void unmaskInplace(char *data, char *stop, char *mask) {
350
while (data < stop) {
351
*(data++) ^= mask[0];
352
*(data++) ^= mask[1];
353
*(data++) ^= mask[2];
354
*(data++) ^= mask[3];
355
}
356
}
357
358
template <unsigned int MESSAGE_HEADER, typename T>
Oct 30, 2018
Oct 30, 2018
359
static inline bool consumeMessage(T payLength, char *&src, unsigned int &length, WebSocketState<isServer> *wState, void *user) {
Sep 2, 2018
Sep 2, 2018
360
if (getOpCode(src)) {
361
if (wState->state.opStack == 1 || (!wState->state.lastFin && getOpCode(src) < 2)) {
Aug 2, 2024
Aug 2, 2024
362
Impl::forceClose(wState, user, ERR_PROTOCOL);
Sep 2, 2018
Sep 2, 2018
363
return true;
364
}
365
wState->state.opCode[++wState->state.opStack] = (OpCode) getOpCode(src);
366
} else if (wState->state.opStack == -1) {
Aug 2, 2024
Aug 2, 2024
367
Impl::forceClose(wState, user, ERR_PROTOCOL);
Sep 2, 2018
Sep 2, 2018
368
return true;
369
}
370
wState->state.lastFin = isFin(src);
371
Dec 29, 2018
Dec 29, 2018
372
if (Impl::refusePayloadLength(payLength, wState, user)) {
Jun 26, 2020
Jun 26, 2020
373
Impl::forceClose(wState, user, ERR_TOO_BIG_MESSAGE);
Sep 2, 2018
Sep 2, 2018
374
return true;
375
}
376
377
if (payLength + MESSAGE_HEADER <= length) {
May 6, 2023
May 6, 2023
378
bool fin = isFin(src);
Sep 2, 2018
Sep 2, 2018
379
if (isServer) {
May 6, 2023
May 6, 2023
380
/* This guy can never be assumed to be perfectly aligned since we can get multiple messages in one read */
381
unmaskImpreciseCopyMask<MESSAGE_HEADER>(src + MESSAGE_HEADER, (unsigned int) payLength);
382
if (Impl::handleFragment(src, payLength, 0, wState->state.opCode[wState->state.opStack], fin, wState, user)) {
Sep 2, 2018
Sep 2, 2018
383
return true;
384
}
385
} else {
Oct 30, 2018
Oct 30, 2018
386
if (Impl::handleFragment(src + MESSAGE_HEADER, payLength, 0, wState->state.opCode[wState->state.opStack], isFin(src), wState, user)) {
Sep 2, 2018
Sep 2, 2018
387
return true;
388
}
389
}
390
May 6, 2023
May 6, 2023
391
if (fin) {
Sep 2, 2018
Sep 2, 2018
392
wState->state.opStack--;
393
}
394
395
src += payLength + MESSAGE_HEADER;
Dec 28, 2019
Dec 28, 2019
396
length -= (unsigned int) (payLength + MESSAGE_HEADER);
Sep 2, 2018
Sep 2, 2018
397
wState->state.spillLength = 0;
398
return false;
399
} else {
400
wState->state.spillLength = 0;
401
wState->state.wantsHead = false;
402
wState->remainingBytes = (unsigned int) (payLength - length + MESSAGE_HEADER);
403
bool fin = isFin(src);
May 6, 2023
May 6, 2023
404
if constexpr (isServer) {
Sep 2, 2018
Sep 2, 2018
405
memcpy(wState->mask, src + MESSAGE_HEADER - 4, 4);
May 6, 2023
May 6, 2023
406
uint64_t mask;
407
memcpy(&mask, src + MESSAGE_HEADER - 4, 4);
408
memcpy(((char *)&mask) + 4, src + MESSAGE_HEADER - 4, 4);
409
unmaskImprecise8<0>(src + MESSAGE_HEADER, mask, length);
Sep 2, 2018
Sep 2, 2018
410
rotateMask(4 - (length - MESSAGE_HEADER) % 4, wState->mask);
411
}
May 6, 2023
May 6, 2023
412
Impl::handleFragment(src + MESSAGE_HEADER, length - MESSAGE_HEADER, wState->remainingBytes, wState->state.opCode[wState->state.opStack], fin, wState, user);
Sep 2, 2018
Sep 2, 2018
413
return true;
414
}
415
}
416
Dec 19, 2021
Dec 19, 2021
417
/* This one is nicely vectorized on both ARM64 and X64 - especially with -mavx */
418
static inline void unmaskAll(char * __restrict data, char * __restrict mask) {
419
for (int i = 0; i < LIBUS_RECV_BUFFER_LENGTH; i += 16) {
420
UnrolledXor<16>(data + i, mask);
421
}
422
}
423
Oct 30, 2018
Oct 30, 2018
424
static inline bool consumeContinuation(char *&src, unsigned int &length, WebSocketState<isServer> *wState, void *user) {
Sep 2, 2018
Sep 2, 2018
425
if (wState->remainingBytes <= length) {
426
if (isServer) {
Dec 1, 2020
Dec 1, 2020
427
unsigned int n = wState->remainingBytes >> 2;
Sep 2, 2018
Sep 2, 2018
428
unmaskInplace(src, src + n * 4, wState->mask);
Dec 1, 2020
Dec 1, 2020
429
for (unsigned int i = 0, s = wState->remainingBytes % 4; i < s; i++) {
Sep 2, 2018
Sep 2, 2018
430
src[n * 4 + i] ^= wState->mask[i];
431
}
432
}
433
Oct 30, 2018
Oct 30, 2018
434
if (Impl::handleFragment(src, wState->remainingBytes, 0, wState->state.opCode[wState->state.opStack], wState->state.lastFin, wState, user)) {
Sep 2, 2018
Sep 2, 2018
435
return false;
436
}
437
438
if (wState->state.lastFin) {
439
wState->state.opStack--;
440
}
441
442
src += wState->remainingBytes;
443
length -= wState->remainingBytes;
444
wState->state.wantsHead = true;
445
return true;
446
} else {
447
if (isServer) {
Dec 19, 2021
Dec 19, 2021
448
/* No need to unmask if mask is 0 */
449
uint32_t nullmask = 0;
Jan 25, 2022
Jan 25, 2022
450
if (memcmp(wState->mask, &nullmask, sizeof(uint32_t))) {
Jan 3, 2022
Jan 3, 2022
451
if /*constexpr*/ (LIBUS_RECV_BUFFER_LENGTH == length) {
452
unmaskAll(src, wState->mask);
453
} else {
454
// Slow path
455
unmaskInplace(src, src + ((length >> 2) + 1) * 4, wState->mask);
456
}
Dec 19, 2021
Dec 19, 2021
457
}
Sep 2, 2018
Sep 2, 2018
458
}
459
460
wState->remainingBytes -= length;
Oct 30, 2018
Oct 30, 2018
461
if (Impl::handleFragment(src, length, wState->remainingBytes, wState->state.opCode[wState->state.opStack], wState->state.lastFin, wState, user)) {
Sep 2, 2018
Sep 2, 2018
462
return false;
463
}
464
465
if (isServer && length % 4) {
466
rotateMask(4 - (length % 4), wState->mask);
467
}
468
return false;
469
}
470
}
471
472
public:
473
WebSocketProtocol() {
474
475
}
476
Oct 30, 2018
Oct 30, 2018
477
static inline void consume(char *src, unsigned int length, WebSocketState<isServer> *wState, void *user) {
Sep 2, 2018
Sep 2, 2018
478
if (wState->state.spillLength) {
479
src -= wState->state.spillLength;
480
length += wState->state.spillLength;
481
memcpy(src, wState->state.spill, wState->state.spillLength);
482
}
483
if (wState->state.wantsHead) {
484
parseNext:
485
while (length >= SHORT_MESSAGE_HEADER) {
486
487
// invalid reserved bits / invalid opcodes / invalid control frames / set compressed frame
Nov 29, 2018
Nov 29, 2018
488
if ((rsv1(src) && !Impl::setCompressed(wState, user)) || rsv23(src) || (getOpCode(src) > 2 && getOpCode(src) < 8) ||
Sep 2, 2018
Sep 2, 2018
489
getOpCode(src) > 10 || (getOpCode(src) > 2 && (!isFin(src) || payloadLength(src) > 125))) {
Aug 2, 2024
Aug 2, 2024
490
Impl::forceClose(wState, user, ERR_PROTOCOL);
Sep 2, 2018
Sep 2, 2018
491
return;
492
}
493
494
if (payloadLength(src) < 126) {
Oct 30, 2018
Oct 30, 2018
495
if (consumeMessage<SHORT_MESSAGE_HEADER, uint8_t>(payloadLength(src), src, length, wState, user)) {
Sep 2, 2018
Sep 2, 2018
496
return;
497
}
498
} else if (payloadLength(src) == 126) {
499
if (length < MEDIUM_MESSAGE_HEADER) {
500
break;
Apr 15, 2019
Apr 15, 2019
501
} else if(consumeMessage<MEDIUM_MESSAGE_HEADER, uint16_t>(protocol::cond_byte_swap<uint16_t>(protocol::bit_cast<uint16_t>(src + 2)), src, length, wState, user)) {
Sep 2, 2018
Sep 2, 2018
502
return;
503
}
504
} else if (length < LONG_MESSAGE_HEADER) {
505
break;
Apr 15, 2019
Apr 15, 2019
506
} else if (consumeMessage<LONG_MESSAGE_HEADER, uint64_t>(protocol::cond_byte_swap<uint64_t>(protocol::bit_cast<uint64_t>(src + 2)), src, length, wState, user)) {
Sep 2, 2018
Sep 2, 2018
507
return;
508
}
509
}
510
if (length) {
511
memcpy(wState->state.spill, src, length);
Dec 28, 2019
Dec 28, 2019
512
wState->state.spillLength = length & 0xf;
Sep 2, 2018
Sep 2, 2018
513
}
Oct 30, 2018
Oct 30, 2018
514
} else if (consumeContinuation(src, length, wState, user)) {
Sep 2, 2018
Sep 2, 2018
515
goto parseNext;
516
}
517
}
518
519
static const int CONSUME_POST_PADDING = 4;
520
static const int CONSUME_PRE_PADDING = LONG_MESSAGE_HEADER - 1;
521
};
522
523
}
524
Jun 9, 2019
Jun 9, 2019
525
#endif // UWS_WEBSOCKETPROTOCOL_H