forked from fkie/async_web_server_cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket_message.cpp
249 lines (235 loc) · 7.65 KB
/
websocket_message.cpp
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
#include "async_web_server_cpp/websocket_message.hpp"
namespace async_web_server_cpp
{
bool WebsocketFrame::fromMessage(const WebsocketMessage& message)
{
switch (message.type)
{
case WebsocketMessage::type_text:
header.opcode = WebsocketFrame::Header::opcode_text;
break;
case WebsocketMessage::type_binary:
header.opcode = WebsocketFrame::Header::opcode_binary;
break;
case WebsocketMessage::type_close:
header.opcode = WebsocketFrame::Header::opcode_close;
break;
case WebsocketMessage::type_ping:
header.opcode = WebsocketFrame::Header::opcode_ping;
break;
case WebsocketMessage::type_pong:
header.opcode = WebsocketFrame::Header::opcode_pong;
break;
default:
return false;
}
header.fin = true;
header.rsv1 = false;
header.rsv2 = false;
header.rsv3 = false;
content = message.content;
length = message.content.size();
return true;
}
bool WebsocketFrame::serialize(std::vector<unsigned char>& buffer)
{
int header_size = 2; // first two bytes of header
if (length < 126)
{
header.len = length;
}
else if (length <= std::numeric_limits<uint16_t>::max())
{
header.len = 126;
header_size += 2;
}
else if (length <= std::numeric_limits<uint64_t>::max())
{
header.len = 127;
header_size += 8;
}
else
return false; // Message too big
header.mask = false; // sending from server
buffer.resize(header_size + content.size());
buffer[0] = header_bytes[0];
buffer[1] = header_bytes[1];
if (length < 126)
{
// already copied in header bytes
}
else if (length <= std::numeric_limits<uint16_t>::max())
{
buffer[2] = (length >> 8) & 0xFF;
buffer[3] = (length >> 0) & 0xFF;
}
else if (length <= std::numeric_limits<uint64_t>::max())
{
buffer[2] = (length >> 56) & 0xFF;
buffer[3] = (length >> 48) & 0xFF;
buffer[4] = (length >> 40) & 0xFF;
buffer[5] = (length >> 32) & 0xFF;
buffer[6] = (length >> 24) & 0xFF;
buffer[7] = (length >> 16) & 0xFF;
buffer[8] = (length >> 8) & 0xFF;
buffer[9] = (length >> 0) & 0xFF;
}
content.copy((char*)&buffer[header_size], content.size());
return true;
}
WebsocketFrameParser::WebsocketFrameParser()
{
reset();
}
void WebsocketFrameParser::reset()
{
state_ = header_byte1;
}
boost::tribool WebsocketFrameParser::consume(WebsocketFrame& frame, char input)
{
switch (state_)
{
case header_byte1:
frame.header_bytes[0] = input;
state_ = header_byte2;
return boost::indeterminate;
case header_byte2:
frame.header_bytes[1] = input;
if (frame.header.len < 126)
{
frame.length = frame.header.len;
frame.content.reserve(frame.length);
frame.content.resize(0);
if (frame.header.mask)
state_ = mask_byte1;
else if (frame.length > 0)
state_ = body;
else
return true;
}
else if (frame.header.len == 126)
{
frame.length = 0;
state_ = length_2bytes_left;
}
else
{
frame.length = 0;
state_ = length_8bytes_left;
}
return boost::indeterminate;
case length_8bytes_left:
frame.length |= ((uint64_t)(input & 0xFF) << 56);
state_ = length_7bytes_left;
return boost::indeterminate;
case length_7bytes_left:
frame.length |= ((uint64_t)(input & 0xFF) << 48);
state_ = length_6bytes_left;
return boost::indeterminate;
case length_6bytes_left:
frame.length |= ((uint64_t)(input & 0xFF) << 40);
state_ = length_5bytes_left;
return boost::indeterminate;
case length_5bytes_left:
frame.length |= ((uint64_t)(input & 0xFF) << 32);
state_ = length_4bytes_left;
return boost::indeterminate;
case length_4bytes_left:
frame.length |= ((uint64_t)(input & 0xFF) << 24);
state_ = length_3bytes_left;
return boost::indeterminate;
case length_3bytes_left:
frame.length |= ((uint64_t)(input & 0xFF) << 16);
state_ = length_2bytes_left;
return boost::indeterminate;
case length_2bytes_left:
frame.length |= ((uint64_t)(input & 0xFF) << 8);
state_ = length_1bytes_left;
return boost::indeterminate;
case length_1bytes_left:
frame.length |= ((uint64_t)(input & 0xFF) << 0);
frame.content.reserve(frame.length);
frame.content.resize(0);
if (frame.header.mask)
state_ = mask_byte1;
else
state_ = body;
return boost::indeterminate;
case mask_byte1:
frame.mask[0] = input;
state_ = mask_byte2;
return boost::indeterminate;
case mask_byte2:
frame.mask[1] = input;
state_ = mask_byte3;
return boost::indeterminate;
case mask_byte3:
frame.mask[2] = input;
state_ = mask_byte4;
return boost::indeterminate;
case mask_byte4:
frame.mask[3] = input;
if (frame.length > 0)
state_ = body;
else
return true;
return boost::indeterminate;
case body:
frame.content += input;
if (frame.content.size() < frame.length)
return boost::indeterminate;
// unmask the frame
if (frame.header.mask)
{
for (uint64_t i = 0; i < frame.length; ++i)
{
frame.content[i] = frame.content[i] ^ frame.mask[i % 4];
}
}
return true;
default:
return false;
}
}
WebsocketMessage::WebsocketMessage() : type(type_unknown) {}
boost::tribool WebsocketFrameBuffer::consume(WebsocketMessage& message,
WebsocketFrame& frame)
{
if (frame.header.opcode == WebsocketFrame::Header::opcode_continuation)
{
if (message.type == WebsocketMessage::type_unknown)
return false;
else
message.content.append(frame.content);
}
else
{
switch (frame.header.opcode)
{
case WebsocketFrame::Header::opcode_text:
message.type = WebsocketMessage::type_text;
break;
case WebsocketFrame::Header::opcode_binary:
message.type = WebsocketMessage::type_binary;
break;
case WebsocketFrame::Header::opcode_close:
message.type = WebsocketMessage::type_close;
break;
case WebsocketFrame::Header::opcode_ping:
message.type = WebsocketMessage::type_ping;
break;
case WebsocketFrame::Header::opcode_pong:
message.type = WebsocketMessage::type_pong;
break;
default:
message.type = WebsocketMessage::type_unknown;
return false;
}
message.content = frame.content;
}
if (frame.header.fin)
return true;
else
return boost::indeterminate;
}
} // namespace async_web_server_cpp