-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathWebSocket.cpp
executable file
·275 lines (225 loc) · 7.46 KB
/
WebSocket.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
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
// WebSocket, v1.00 2012-09-13
//
// Description: WebSocket FRC6544 codec, written in C++.
// Homepage: http://katzarsky.github.com/WebSocket
// Author: [email protected]
#include "WebSocket.h"
//#include "md5/md5.h"
#include "base64/base64.h"
#include "sha1/sha1.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
WebSocket::WebSocket() {
}
WebSocketFrameType WebSocket::parseHandshake(unsigned char* input_frame, int input_len)
{
// 1. copy char*/len into string
// 2. try to parse headers until \r\n occurs
string headers((char*)input_frame, input_len);
int header_end = headers.find("\r\n\r\n");
if(header_end == string::npos) { // end-of-headers not found - do not parse
return INCOMPLETE_FRAME;
}
headers.resize(header_end); // trim off any data we don't need after the headers
int key_count = 0;
vector<string> headers_rows = explode(headers, string("\r\n"));
for(int i=0; i<headers_rows.size(); i++) {
string& header = headers_rows[i];
if(header.find("GET") == 0) {
vector<string> get_tokens = explode(header, string(" "));
if(get_tokens.size() >= 2) {
this->resource = get_tokens[1];
}
key_count ++;
}
else {
int pos = header.find(":");
if(pos != string::npos) {
string header_key(header, 0, pos);
string header_value(header, pos+1);
header_value = trim(header_value);
if(header_key == "Host") this->host = header_value;
else if(header_key == "Origin") this->origin = header_value;
else if(header_key == "Sec-WebSocket-Key") this->key = header_value;
else if(header_key == "Sec-WebSocket-Protocol") this->protocol = header_value;
key_count ++;
}
}
}
if(key_count < 2) {
return INCOMPLETE_FRAME;
}
//printf("HANDSHAKE-PARSED\n");
return OPENING_FRAME;
}
string WebSocket::trim(string str)
{
//printf("TRIM\n");
char* whitespace = " \t\r\n";
string::size_type pos = str.find_last_not_of(whitespace);
if(pos != string::npos) {
str.erase(pos + 1);
pos = str.find_first_not_of(whitespace);
if(pos != string::npos) str.erase(0, pos);
}
else {
return string();
}
return str;
}
vector<string> WebSocket::explode(
string theString,
string theDelimiter,
bool theIncludeEmptyStrings)
{
//printf("EXPLODE\n");
//UASSERT( theDelimiter.size(), >, 0 );
vector<string> theStringVector;
int start = 0, end = 0, length = 0;
while ( end != string::npos )
{
end = theString.find( theDelimiter, start );
// If at end, use length=maxLength. Else use length=end-start.
length = (end == string::npos) ? string::npos : end - start;
if (theIncludeEmptyStrings
|| ( ( length > 0 ) /* At end, end == length == string::npos */
&& ( start < theString.size() ) ) )
theStringVector.push_back( theString.substr( start, length ) );
// If at end, use start=maxSize. Else use start=end+delimiter.
start = ( ( end > (string::npos - theDelimiter.size()) )
? string::npos : end + theDelimiter.size() );
}
return theStringVector;
}
string WebSocket::answerHandshake()
{
unsigned char digest[20]; // 160 bit sha1 digest
string answer;
answer += "HTTP/1.1 101 Switching Protocols\r\n";
answer += "Upgrade: WebSocket\r\n";
answer += "Connection: Upgrade\r\n";
if(this->key.length() > 0) {
string accept_key;
accept_key += this->key;
accept_key += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; //RFC6544_MAGIC_KEY
//printf("INTERMEDIATE_KEY:(%s)\n", accept_key.data());
SHA1 sha;
sha.Input(accept_key.data(), accept_key.size());
sha.Result((unsigned*)digest);
//printf("DIGEST:"); for(int i=0; i<20; i++) printf("%02x ",digest[i]); printf("\n");
//little endian to big endian
for(int i=0; i<20; i+=4) {
unsigned char c;
c = digest[i];
digest[i] = digest[i+3];
digest[i+3] = c;
c = digest[i+1];
digest[i+1] = digest[i+2];
digest[i+2] = c;
}
//printf("DIGEST:"); for(int i=0; i<20; i++) printf("%02x ",digest[i]); printf("\n");
accept_key = base64_encode2((const unsigned char *)digest, 20); //160bit = 20 bytes/chars
answer += "Sec-WebSocket-Accept: "+(accept_key)+"\r\n";
}
if(this->protocol.length() > 0) {
answer += "Sec-WebSocket-Protocol: "+(this->protocol)+"\r\n";
}
answer += "\r\n";
return answer;
//return WS_OPENING_FRAME;
}
int WebSocket::makeFrame(WebSocketFrameType frame_type, unsigned char* msg, int msg_length, unsigned char* buffer, int buffer_size)
{
int pos = 0;
int size = msg_length;
buffer[pos++] = (unsigned char)frame_type; // text frame
if(size <= 125) {
buffer[pos++] = size;
}
else if(size <= 65535) {
buffer[pos++] = 126; //16 bit length follows
buffer[pos++] = (size >> 8) & 0xFF; // leftmost first
buffer[pos++] = size & 0xFF;
}
else { // >2^16-1 (65535)
buffer[pos++] = 127; //64 bit length follows
// write 8 bytes length (significant first)
// since msg_length is int it can be no longer than 4 bytes = 2^32-1
// padd zeroes for the first 4 bytes
for(int i=3; i>=0; i--) {
buffer[pos++] = 0;
}
// write the actual 32bit msg_length in the next 4 bytes
for(int i=3; i>=0; i--) {
buffer[pos++] = ((size >> 8*i) & 0xFF);
}
}
memcpy((void*)(buffer+pos), msg, size);
return (size+pos);
}
WebSocketFrameType WebSocket::getFrame(unsigned char* in_buffer, int in_length, unsigned char* out_buffer, int out_size, int &out_length, int &use_count)
{
//printf("getTextFrame()\n");
if(in_length < 3) return INCOMPLETE_FRAME;
unsigned char msg_opcode = in_buffer[0] & 0x0F;
unsigned char msg_fin = (in_buffer[0] >> 7) & 0x01;
unsigned char msg_masked = (in_buffer[1] >> 7) & 0x01;
// *** message decoding
int payload_length = 0;
int pos = 2;
int length_field = in_buffer[1] & (~0x80);
unsigned int mask = 0;
//printf("IN:"); for(int i=0; i<20; i++) printf("%02x ",buffer[i]); printf("\n");
if(length_field <= 125) {
payload_length = length_field;
}
else if(length_field == 126) { //msglen is 16bit!
//payload_length = in_buffer[2] + (in_buffer[3]<<8);
//payload_length = in_buffer[3] + (in_buffer[2]<<8);
payload_length = in_buffer[2];
for(int i = 0 ; i < 1 ; i++) {
payload_length = (payload_length<<8) + in_buffer[3+i];
}
pos += 2;
}
else if(length_field == 127) { //msglen is 64bit!
payload_length = in_buffer[2];
for(int i = 0 ; i < 4 ; i++) {
payload_length = (payload_length<<8) + in_buffer[3+i];
}
//payload_length = in_buffer[2] + (in_buffer[3]<<8);
pos += 8;
}
//printf("PAYLOAD_LEN: %08x\n", payload_length);
if(in_length < payload_length+pos) {
printf("%s\n", "return INCOMPLETE_FRAME");
return INCOMPLETE_FRAME;
}
if(msg_masked) {
mask = *((unsigned int*)(in_buffer+pos));
//printf("MASK: %08x\n", mask);
pos += 4;
// unmask data:
unsigned char* c = in_buffer+pos;
for(int i=0; i<payload_length; i++) {
c[i] = c[i] ^ ((unsigned char*)(&mask))[i%4];
}
}
use_count = payload_length + pos;
out_length = payload_length;
if(payload_length > out_size || use_count > in_length) {
//TODO: if output buffer is too small -- ERROR or resize(free and allocate bigger one) the buffer ?
return ERROR_FRAME;
}
memcpy((void*)out_buffer, (void*)(in_buffer+pos), payload_length);
//out_buffer[payload_length] = 0;
//printf("TEXT: %s\n", out_buffer);
if(msg_opcode == 0x0) return (msg_fin)?TEXT_FRAME:INCOMPLETE_TEXT_FRAME; // continuation frame ?
if(msg_opcode == 0x1) return (msg_fin)?TEXT_FRAME:INCOMPLETE_TEXT_FRAME;
if(msg_opcode == 0x2) return (msg_fin)?BINARY_FRAME:INCOMPLETE_BINARY_FRAME;
if(msg_opcode == 0x9) return PING_FRAME;
if(msg_opcode == 0xA) return PONG_FRAME;
return ERROR_FRAME;
}