-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSimpleWebSocket.cpp
More file actions
719 lines (608 loc) · 14.5 KB
/
Copy pathSimpleWebSocket.cpp
File metadata and controls
719 lines (608 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
// Copyright © 2022 Michael Thornburgh
// SPDX-License-Identifier: MIT
#include <cassert>
#include <cctype>
#include <cstring>
#include <regex>
#include "rtmfp/Retainer.hpp"
#include "rtmfp/URIParse.hpp"
#include "SimpleWebSocket.hpp"
namespace {
const uint8_t WS_FLAG_FIN = 0x80;
const uint8_t WS_FLAG_MSK = 0x80;
const uint8_t WS_OPCODE_MASK = 0x0f;
const uint8_t WS_LENGTH_MASK = 0x7f;
const size_t WS_LENGTH_16 = 126;
const size_t WS_LENGTH_64 = 127;
const size_t WS_MAX_FRAME = 1 << 24; // 16MB, big enough for anything reasonable
enum {
WS_OP_CONTINUATION = 0x0,
WS_OP_TEXT = 0x1,
WS_OP_BINARY = 0x2,
WS_OP_CLOSE = 0x8,
WS_OP_PING = 0x9,
WS_OP_PONG = 0xa
};
std::string _replace(const std::string &s, const char *pattern, const char *fmt)
{
return std::regex_replace(s, std::regex(pattern), fmt);
}
std::string _trim(const std::string &s)
{
auto left = s.data();
auto right = left + s.size();
while((left < right) and std::isspace(*left))
left++;
while((right > left) and std::isspace(*(right - 1)))
right--;
return std::string(left, right);
}
bool _istchar(int c)
{
// RFC 7230 §3.2.6
switch(c)
{
case '!':
case '#':
case '$':
case '%':
case '&':
case '\'':
case '*':
case '+':
case '-':
case '.':
case '^':
case '_':
case '`':
case '|':
case '~':
return true;
default:
return ::isdigit(c) or ::isalpha(c);
}
}
std::string _base64enc(const void *bytes, size_t len, bool pad = true)
{
const char *alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const uint8_t *cursor = (const uint8_t *)bytes;
const uint8_t *limit = cursor + len;
std::string rv;
uint8_t b;
while(cursor < limit)
{
b = *cursor >> 2;
rv.push_back(alphabet[b]);
b = (*cursor & 0x03) << 4;
cursor++;
if(cursor < limit)
{
b += *cursor >> 4;
rv.push_back(alphabet[b]);
b = (*cursor & 0x0f) << 2;
cursor++;
if(cursor < limit)
{
b += *cursor >> 6;
rv.push_back(alphabet[b]);
rv.push_back(alphabet[*cursor & 0x3f]);
cursor++;
}
else
{
rv.push_back(alphabet[b]);
if(pad)
rv.append("=");
}
}
else
{
rv.push_back(alphabet[b]);
if(pad)
rv.append("==");
}
}
return rv;
}
}
namespace com { namespace zenomt { namespace websock {
// --- HeaderBodyStream
HeaderBodyStream::HeaderBodyStream(std::shared_ptr<IStreamPlatformAdapter> platform) :
m_platform(platform)
{
}
Time HeaderBodyStream::getCurrentTime()
{
return m_platform->getCurrentTime();
}
bool HeaderBodyStream::init()
{
auto myself = retain_ref(this);
m_platform->setOnReceiveBytesCallback([myself] (const void *bytes, size_t len) { return myself->onReceiveBytes(bytes, len); });
m_platform->setOnStreamDidCloseCallback([myself] { myself->setClosedState(); });
return true;
}
void HeaderBodyStream::close()
{
retain();
clearCallbacks();
setClosedState();
release();
}
void HeaderBodyStream::shutdown()
{
if(m_state < S_CLOSING)
{
m_state = S_CLOSING;
scheduleWrite();
}
}
void HeaderBodyStream::writeBytes(const void *bytes, size_t len)
{
const uint8_t *cursor = (const uint8_t *)bytes;
const uint8_t *limit = cursor + len;
m_rawOutputBuffer.insert(m_rawOutputBuffer.end(), cursor, limit);
scheduleWrite();
}
void HeaderBodyStream::writeBytes(const Bytes &bytes)
{
writeBytes(bytes.data(), bytes.size());
}
void HeaderBodyStream::writeBytes(const std::string &s)
{
writeBytes(s.data(), s.size());
}
bool HeaderBodyStream::onReceiveBytes(const void *bytes, size_t len)
{
auto myself = retain_ref(this);
const uint8_t *cursor = (const uint8_t *)bytes;
const uint8_t *limit = cursor + len;
while((m_state < S_CLOSING) and (cursor < limit))
{
if(m_headerComplete)
cursor = onBodyBytes(cursor, limit);
else
cursor = onHeaderBytes(cursor, limit);
}
return true;
}
void HeaderBodyStream::notifyWhenWritable(const onwritable_f &onwritable)
{
m_client_onwritable = onwritable;
scheduleWrite();
}
void HeaderBodyStream::clearCallbacks()
{
onError = nullptr;
m_client_onwritable = nullptr;
}
void HeaderBodyStream::setClosedState()
{
Task onError_f;
if(m_state < S_ERROR)
{
m_state = S_ERROR;
m_platform->onClientClosed();
swap(onError_f, onError);
}
clearCallbacks();
if(onError_f)
onError_f();
}
void HeaderBodyStream::scheduleWrite()
{
if((m_state < S_ERROR) and not m_writeScheduled)
{
auto myself = retain_ref(this);
m_platform->notifyWhenWritable([myself] { return myself->onWritable(); });
m_writeScheduled = true;
}
}
bool HeaderBodyStream::onWritable()
{
if(writeRawOutputBuffer())
return true;
if((m_state < S_CLOSING) and m_client_onwritable)
{
if(not m_client_onwritable())
m_client_onwritable = nullptr;
writeRawOutputBuffer();
return true;
}
// if we get here then we're flushed
if(S_CLOSING == m_state)
setClosedState();
m_writeScheduled = false;
return false;
}
bool HeaderBodyStream::writeRawOutputBuffer()
{
if((m_state < S_ERROR) and not m_rawOutputBuffer.empty())
{
m_platform->writeBytes(m_rawOutputBuffer.data(), m_rawOutputBuffer.size());
m_rawOutputBuffer.clear();
return true;
}
return false;
}
// --- SimpleHttpStream
std::string SimpleHttpStream::getStartLine() const
{
return m_startLine;
}
bool SimpleHttpStream::hasHeader(const std::string &name) const
{
return m_headers.count(URIParse::lowercase(name)) > 0;
}
std::string SimpleHttpStream::getHeader(const std::string &name) const
{
std::string rv;
auto vals = getHeaderValues(name);
bool first = true;
for(auto it = vals.begin(); it != vals.end(); it++)
{
if(not first)
rv.push_back(',');
rv.append(*it);
first = false;
}
return rv;
}
std::vector<std::string> SimpleHttpStream::getHeaderValues(const std::string &name) const
{
auto it = m_headers.find(URIParse::lowercase(name));
if(it != m_headers.end())
return it->second;
return std::vector<std::string>();
}
bool SimpleHttpStream::isToken(const std::string &s)
{
for(auto it = s.begin(); it != s.end(); it++)
if(not _istchar(*it))
return false;
return not s.empty();
}
const uint8_t * SimpleHttpStream::onHeaderBytes(const uint8_t *bytes, const uint8_t *limit)
{
const uint8_t *cursor = bytes;
while(cursor < limit)
{
uint8_t c = *cursor++;
m_headerBlock.push_back(c);
if('\n' == c)
{
if(m_gotNewline)
{
parseHeaderBlock();
break;
}
m_gotNewline = true;
}
else if('\r' != c)
m_gotNewline = false;
}
return cursor;
}
void SimpleHttpStream::onHeadersComplete()
{
if(onHttpHeadersReceived)
onHttpHeadersReceived();
}
void SimpleHttpStream::parseHeaderBlock()
{
m_headerComplete = true;
std::string tmp = _replace(m_headerBlock, "\r\n", "\n");
tmp = _replace(tmp, "\r", " ");
tmp = _replace(tmp, "\n[ \t]", " ");
auto lines = URIParse::split(tmp, "\n");
bool needStartLine = true;
for(auto it = lines.begin(); it != lines.end(); it++)
{
if(needStartLine)
{
m_startLine = *it;
needStartLine = false;
}
else if(it->size())
{
auto parts = URIParse::split(*it, ":", 2);
if((2 != parts.size()) or not isToken(parts[0]))
{
setClosedState();
return;
}
m_headers[URIParse::lowercase(parts[0])].push_back(_trim(parts[1]));
}
}
onHeadersComplete();
}
void SimpleHttpStream::clearCallbacks()
{
HeaderBodyStream::clearCallbacks();
onHttpHeadersReceived = nullptr;
}
void SimpleHttpStream::reset()
{
m_gotNewline = false;
m_headerBlock.clear();
m_headers.clear();
m_startLine.clear();
}
// --- SimpleWebSocket
void SimpleWebSocket::sendBinaryMessage(const void *bytes, size_t len)
{
writeFrame(WS_OP_BINARY, bytes, len);
}
void SimpleWebSocket::sendBinaryMessage(const Bytes &bytes)
{
sendBinaryMessage(bytes.data(), bytes.size());
}
void SimpleWebSocket::sendTextMessage(const std::string &message)
{
writeFrame(WS_OP_TEXT, message.data(), message.size());
}
void SimpleWebSocket::cleanClose()
{
writeFrame(WS_OP_CLOSE, nullptr, 0);
m_closing = true;
}
const uint8_t * SimpleWebSocket::onBodyBytes(const uint8_t *bytes, const uint8_t *limit)
{
auto myself = retain_ref(this);
m_inputBuffer.insert(m_inputBuffer.end(), bytes, limit);
const uint8_t *buffer = m_inputBuffer.data();
const uint8_t *cursor = buffer;
const uint8_t *inputLimit = cursor + m_inputBuffer.size();
while(cursor < inputLimit)
{
long consumed = onInput(cursor, inputLimit);
if(consumed < 0)
{
setClosedState();
return limit;
}
if(0 == consumed)
break;
cursor += consumed;
}
shiftInputBuffer(cursor - buffer);
(void) myself;
return limit;
}
void SimpleWebSocket::shiftInputBuffer(size_t amount)
{
if(amount)
{
assert(amount <= m_inputBuffer.size());
size_t newSize = m_inputBuffer.size() - amount;
uint8_t *buf = m_inputBuffer.data();
::memmove(buf, buf + amount, newSize);
m_inputBuffer.resize(newSize);
}
}
long SimpleWebSocket::onInput(const uint8_t *bytes, const uint8_t *limit)
{
size_t remaining = limit - bytes;
assert(remaining > 0);
const uint8_t *cursor = bytes;
size_t needed = 2;
if(remaining < needed)
return 0;
bool isFinal = *cursor & WS_FLAG_FIN;
int opcode = *cursor & WS_OPCODE_MASK;
cursor++;
bool hasMask = *cursor & WS_FLAG_MSK;
size_t payloadLength = *cursor & WS_LENGTH_MASK;
cursor++;
if(hasMask)
needed += 4;
if(WS_LENGTH_16 == payloadLength)
needed += 2;
else if(WS_LENGTH_64 == payloadLength)
needed += 8;
if(remaining < needed)
return 0;
if(WS_LENGTH_16 == payloadLength)
{
payloadLength = *cursor++; payloadLength <<= 8;
payloadLength += *cursor++;
}
else if(WS_LENGTH_64 == payloadLength)
{
payloadLength = *cursor++; payloadLength <<= 8;
payloadLength += *cursor++; payloadLength <<= 8;
payloadLength += *cursor++; payloadLength <<= 8;
payloadLength += *cursor++; payloadLength <<= 8;
payloadLength += *cursor++; payloadLength <<= 8;
payloadLength += *cursor++; payloadLength <<= 8;
payloadLength += *cursor++; payloadLength <<= 8;
payloadLength += *cursor++;
}
if(payloadLength > WS_MAX_FRAME)
return -1;
needed += payloadLength;
if(remaining < needed)
return 0;
// at this point there's enough remaining for the entire frame
uint32_t mask = 0;
if(hasMask)
{
mask += *cursor++; mask <<=8;
mask += *cursor++; mask <<=8;
mask += *cursor++; mask <<=8;
mask += *cursor++;
}
const uint8_t *payloadLimit = cursor + payloadLength;
assert(payloadLimit <= limit);
if(hasMask)
{
int maskShift = 24;
m_tmpFrame.clear();
while(cursor < payloadLimit)
{
m_tmpFrame.push_back(*cursor++ ^ ((mask >> maskShift) & 0xff));
maskShift -= 8;
if(maskShift < 0)
maskShift = 24;
}
onFrame(opcode, isFinal, m_tmpFrame.data(), payloadLength);
}
else
onFrame(opcode, isFinal, cursor, payloadLength);
return needed;
}
void SimpleWebSocket::onFrame(int opcode, bool isFinal, const uint8_t *bytes, size_t len)
{
switch(opcode)
{
case WS_OP_CONTINUATION:
onContinuationFrame(isFinal, bytes, len);
break;
case WS_OP_PING:
onPingFrame(bytes, len);
break;
case WS_OP_PONG:
onPongFrame(bytes, len);
break;
case WS_OP_CLOSE:
onCloseFrame();
break;
default:
onMessageFrame(opcode, isFinal, bytes, len);
break;
}
}
void SimpleWebSocket::onContinuationFrame(bool isFinal, const uint8_t *bytes, size_t len)
{
if(m_fragmentedMessageOpcode < 0)
{
setClosedState();
return;
}
m_fragmentedMessage.insert(m_fragmentedMessage.end(), bytes, bytes + len);
if(isFinal)
{
int opcode = m_fragmentedMessageOpcode;
m_fragmentedMessageOpcode = -1;
onMessageFrame(opcode, true, m_fragmentedMessage.data(), m_fragmentedMessage.size());
m_fragmentedMessage.clear();
}
}
void SimpleWebSocket::onPingFrame(const uint8_t *bytes, size_t len)
{
writeFrame(WS_OP_PONG, bytes, len);
}
void SimpleWebSocket::onPongFrame(const uint8_t *bytes, size_t len)
{
}
void SimpleWebSocket::onCloseFrame()
{
if(m_closing)
setClosedState();
else
cleanClose();
}
void SimpleWebSocket::onMessageFrame(int opcode, bool isFinal, const uint8_t *bytes, size_t len)
{
if(m_fragmentedMessageOpcode > 0)
{
setClosedState();
return;
}
if(not isFinal)
{
m_fragmentedMessageOpcode = opcode;
m_fragmentedMessage.insert(m_fragmentedMessage.end(), bytes, bytes + len);
}
else
{
switch(opcode)
{
case WS_OP_TEXT:
if(onTextMessage)
onTextMessage(std::string(bytes, bytes + len));
break;
case WS_OP_BINARY:
if(onBinaryMessage)
onBinaryMessage(bytes, len);
break;
default:
break; // we don't know what this is.
}
}
}
void SimpleWebSocket::clearCallbacks()
{
SimpleHttpStream::clearCallbacks();
onOpen = nullptr;
onBinaryMessage = nullptr;
onTextMessage = nullptr;
}
void SimpleWebSocket::onHeadersComplete()
{
SimpleHttpStream::onHeadersComplete();
auto startline = URIParse::split(m_startLine, " ");
if((startline.size() != 3) or (0 != startline[0].compare("GET")))
{
setClosedState();
return;
}
std::string websocketKey = getHeader("sec-websocket-key");
if( (0 != URIParse::lowercase(getHeader("upgrade")).compare("websocket"))
or (std::string::npos == URIParse::lowercase(getHeader("connection")).find("upgrade"))
or (0 != getHeader("sec-websocket-version").compare("13"))
or (websocketKey.empty())
)
{
setClosedState();
return;
}
websocketKey.append("258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
uint8_t md[160/8] = { 0 };
sha1(md, websocketKey.data(), websocketKey.size());
std::string websocketAccept = _base64enc(md, sizeof(md));
writeBytes(std::string(
"HTTP/1.1 101 Switching Protocols\r\n"
"Upgrade: websocket\r\n"
"Connection: Upgrade\r\n"
"Sec-WebSocket-Accept: ") + websocketAccept + std::string("\r\n"
"\r\n"
));
m_handshakeComplete = true;
if(onOpen)
onOpen();
}
void SimpleWebSocket::writeFrame(int opcode, const void *bytes_, size_t len)
{
const uint8_t *bytes = (const uint8_t *)bytes_;
uint8_t basicLengthField = 0;
if(len > 65535)
basicLengthField = WS_LENGTH_64;
else if(len > 125)
basicLengthField = WS_LENGTH_16;
else
basicLengthField = len;
Bytes frame;
frame.push_back(WS_FLAG_FIN | (opcode & WS_OPCODE_MASK));
frame.push_back(0 | basicLengthField); // servers don't mask data
if(WS_LENGTH_64 == basicLengthField)
{
frame.push_back((len >> 56) & 0xff);
frame.push_back((len >> 48) & 0xff);
frame.push_back((len >> 40) & 0xff);
frame.push_back((len >> 32) & 0xff);
frame.push_back((len >> 24) & 0xff);
frame.push_back((len >> 16) & 0xff);
frame.push_back((len >> 8) & 0xff);
frame.push_back((len ) & 0xff);
}
else if(WS_LENGTH_16 == basicLengthField)
{
frame.push_back((len >> 8) & 0xff);
frame.push_back((len ) & 0xff);
}
if(len and bytes)
frame.insert(frame.end(), bytes, bytes + len);
writeBytes(frame);
}
} } } // namespace com::zenomt::websock