forked from caosiyang/websocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.cpp
388 lines (343 loc) · 9.57 KB
/
connection.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
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
#include "connection.h"
//create a websocket connection
ws_conn_t *ws_conn_new() {
ws_conn_t *conn = new (nothrow) ws_conn_t;
if (conn) {
conn->bev = NULL;
conn->ws_req_str = "";
conn->ws_resp_str = "";
conn->step = ZERO;
conn->ntoread = 0;
conn->frame = frame_new();
conn->handshake_cb_unit.cb = NULL;
conn->handshake_cb_unit.cbarg = NULL;
conn->frame_recv_cb_unit.cb = NULL;
conn->frame_recv_cb_unit.cbarg = NULL;
conn->write_cb_unit.cb = NULL;
conn->write_cb_unit.cbarg = NULL;
conn->close_cb_unit.cb = NULL;
conn->close_cb_unit.cbarg = NULL;
conn->ping_cb_unit.cb = NULL;
conn->ping_cb_unit.cbarg = NULL;
}
return conn;
}
//destroy a websocket connection
void ws_conn_free(ws_conn_t *conn) {
if (conn) {
if (conn->frame) {
frame_free(conn->frame);
conn->frame = NULL;
}
delete conn;
}
}
//websocket serve start
void ws_serve_start(ws_conn_t *conn) {
if (conn && conn->bev) {
accept_websocket_request(conn);
} else {
ws_serve_exit(conn);
}
}
//websocket serve exit
void ws_serve_exit(ws_conn_t *conn) {
if (conn) {
if (conn->close_cb_unit.cb) {
websocket_cb cb = conn->close_cb_unit.cb;
void *cbarg = conn->close_cb_unit.cbarg;
cb(cbarg);
}
}
}
//accept the websocket request
void accept_websocket_request(ws_conn_t *conn) {
if (conn && conn->bev) {
//read websocket request
bufferevent_setcb(conn->bev, request_read_cb, response_write_cb, close_cb, conn);
bufferevent_setwatermark(conn->bev, EV_READ, 1, 1);
bufferevent_setwatermark(conn->bev, EV_WRITE, 0, 0);
bufferevent_enable(conn->bev, EV_READ);
} else {
ws_serve_exit(conn);
}
}
//respond the websocket request
void respond_websocket_request(ws_conn_t *conn) {
if (conn && conn->bev) {
ws_req_t ws_req;
parse_websocket_request(conn->ws_req_str.c_str(), &ws_req); //parse request
//TODO
//check if it is a websocket request
//if (!valid) {
// ws_serve_exit(conn);
// return;
//}
conn->ws_resp_str = generate_websocket_response(&ws_req); //generate response
if (!conn->ws_resp_str.empty()) {
bufferevent_write(conn->bev, conn->ws_resp_str.c_str(), conn->ws_resp_str.length());
} else {
ws_serve_exit(conn);
}
} else {
ws_serve_exit(conn);
}
}
//request read callback
void request_read_cb(struct bufferevent *bev, void *ctx) {
ws_conn_t *conn = (ws_conn_t*)ctx;
if (conn && conn->bev) {
char c;
bufferevent_read(bev, &c, 1);
conn->ws_req_str += c;
size_t n = conn->ws_req_str.size();
//TODO
//for security
//if (n > MAX_WS_REQ_LEN) {
// ws_serve_exit();
//}
//receive request completely
if (n >= 4 && conn->ws_req_str.substr(n - 4) == "\r\n\r\n") {
bufferevent_disable(conn->bev, EV_READ); //stop reading before a valid handshake
respond_websocket_request(conn); //send websocket response
}
} else {
ws_serve_exit(conn);
}
}
//response write callback
void response_write_cb(struct bufferevent *bev, void *ctx) {
ws_conn_t *conn = (ws_conn_t*)ctx;
if (conn && conn->bev) {
if (conn->handshake_cb_unit.cb) {
websocket_cb cb = conn->handshake_cb_unit.cb;
void *cbarg = conn->handshake_cb_unit.cbarg;
cb(cbarg);
}
LOG("%s", conn->ws_req_str.c_str());
LOG("%s", conn->ws_resp_str.c_str());
frame_recv_loop(conn); //frame receive loop
} else {
ws_serve_exit(conn);
}
}
//send a frame
inline int32_t send_a_frame(ws_conn_t *conn, const frame_buffer_t *fb) {
return bufferevent_write(conn->bev, fb->data, fb->len);
}
void frame_recv_loop(ws_conn_t *conn) {
if (conn && conn->bev) {
conn->step = ONE;
conn->ntoread = 2;
bufferevent_setcb(conn->bev, frame_read_cb, write_cb, close_cb, conn);
bufferevent_setwatermark(conn->bev, EV_READ, conn->ntoread, conn->ntoread);
bufferevent_setwatermark(conn->bev, EV_WRITE, 0, 0);
bufferevent_enable(conn->bev, EV_READ);
} else {
ws_serve_exit(conn);
}
}
void frame_read_cb(struct bufferevent *bev, void *ctx) {
ws_conn_t *conn = (ws_conn_t*)ctx;
if (!conn || !conn->bev) {
ws_serve_exit(conn);
return;
}
switch (conn->step) {
case ONE:
{
LOG("---- STEP 1 ----");
char tmp[conn->ntoread];
bufferevent_read(bev, tmp, conn->ntoread);
//parse header
if (parse_frame_header(tmp, conn->frame) == 0) {
LOG("FIN = %lu", conn->frame->fin);
LOG("OPCODE = %lu", conn->frame->opcode);
LOG("MASK = %lu", conn->frame->mask);
LOG("PAYLOAD_LEN = %lu", conn->frame->payload_len);
//payload_len is [0, 127]
if (conn->frame->payload_len <= 125) {
conn->step = THREE;
conn->ntoread = 4;
bufferevent_setwatermark(bev, EV_READ, conn->ntoread, conn->ntoread);
} else if (conn->frame->payload_len == 126) {
conn->step = TWO;
conn->ntoread = 2;
bufferevent_setwatermark(bev, EV_READ, conn->ntoread, conn->ntoread);
} else if (conn->frame->payload_len == 127) {
conn->step = TWO;
conn->ntoread = 8;
bufferevent_setwatermark(bev, EV_READ, conn->ntoread, conn->ntoread);
}
}
//TODO
//validate frame header
if (!is_frame_valid(conn->frame)) {
return;
}
break;
}
case TWO:
{
LOG("---- STEP 2 ----");
char tmp[conn->ntoread];
bufferevent_read(bev, tmp, conn->ntoread);
if (conn->frame->payload_len == 126) {
conn->frame->payload_len = ntohs(*(uint16_t*)tmp);
LOG("PAYLOAD_LEN = %lu", conn->frame->payload_len);
} else if (conn->frame->payload_len == 127) {
conn->frame->payload_len = myntohll(*(uint64_t*)tmp);
LOG("PAYLOAD_LEN = %llu", conn->frame->payload_len);
}
conn->step = THREE;
conn->ntoread = 4;
bufferevent_setwatermark(bev, EV_READ, conn->ntoread, conn->ntoread);
break;
}
case THREE:
{
LOG("---- STEP 3 ----");
char tmp[conn->ntoread];
bufferevent_read(bev, tmp, conn->ntoread);
memcpy(conn->frame->masking_key, tmp, conn->ntoread);
if (conn->frame->payload_len > 0) {
conn->step = FOUR;
conn->ntoread = conn->frame->payload_len;
bufferevent_setwatermark(bev, EV_READ, conn->ntoread, conn->ntoread);
} else if (conn->frame->payload_len == 0) {
/*recv a whole frame*/
if (conn->frame->mask == 0) {
//recv an unmasked frame
}
if (conn->frame->fin == 1 && conn->frame->opcode == 0x8) {
//0x8 denotes a connection close
frame_buffer_t *fb = frame_buffer_new(1, 8, 0, NULL);
send_a_frame(conn, fb);
LOG("send a close frame");
frame_buffer_free(fb);
#if 0
if (conn->conn_close_cb_unit.cb) {
websocket_cb cb = conn->conn_close_cb_unit.cb;
void *cbarg = conn->conn_close_cb_unit.cbarg;
cb(cbarg);
} else {
//bufferevent_disable(conn->bev, EV_READ | EV_WRITE);
}
#endif
break;
} else if (conn->frame->fin == 1 && conn->frame->opcode == 0x9) {
//0x9 denotes a ping
//TODO
//make a pong
} else {
//execute custom operation
if (conn->frame_recv_cb_unit.cb) {
websocket_cb cb = conn->frame_recv_cb_unit.cb;
void *cbarg = conn->frame_recv_cb_unit.cbarg;
cb(cbarg);
}
}
conn->step = ONE;
conn->ntoread = 2;
bufferevent_setwatermark(bev, EV_READ, conn->ntoread, conn->ntoread);
}
break;
}
case FOUR:
{
LOG("---- STEP 4 ----");
if (conn->frame->payload_len > 0) {
if (conn->frame->payload_data) {
delete[] conn->frame->payload_data;
conn->frame->payload_data = NULL;
}
conn->frame->payload_data = new char[conn->frame->payload_len];
bufferevent_read(bev, conn->frame->payload_data, conn->frame->payload_len);
unmask_payload_data(conn->frame);
}
/*recv a whole frame*/
if (conn->frame->fin == 1 && conn->frame->opcode == 0x8) {
//0x8 denotes a connection close
frame_buffer_t *fb = frame_buffer_new(1, 8, 0, NULL);
send_a_frame(conn, fb);
LOG("send a close frame");
frame_buffer_free(fb);
#if 0
if (conn->conn_close_cb_unit.cb) {
websocket_cb cb = conn->conn_close_cb_unit.cb;
void *cbarg = conn->conn_close_cb_unit.cbarg;
cb(cbarg);
} else {
//bufferevent_disable(conn->bev, EV_READ | EV_WRITE);
}
#endif
break;
} else if (conn->frame->fin == 1 && conn->frame->opcode == 0x9) {
//0x9 denotes a ping
//TODO
//make a pong
} else {
//execute custom operation
if (conn->frame_recv_cb_unit.cb) {
websocket_cb cb = conn->frame_recv_cb_unit.cb;
void *cbarg = conn->frame_recv_cb_unit.cbarg;
cb(cbarg);
}
}
if (conn->frame->opcode == 0x1) { //0x1 denotes a text frame
}
if (conn->frame->opcode == 0x2) { //0x1 denotes a binary frame
}
conn->step = ONE;
conn->ntoread = 2;
bufferevent_setwatermark(bev, EV_READ, conn->ntoread, conn->ntoread);
break;
}
default:
LOG("---- STEP UNKNOWN ----");
LOG("exit");
exit(-1);
break;
}
}
void ws_conn_setcb(ws_conn_t *conn, enum CBTYPE cbtype, websocket_cb cb, void *cbarg) {
if (conn) {
switch (cbtype) {
case HANDSHAKE:
conn->handshake_cb_unit.cb = cb;
conn->handshake_cb_unit.cbarg = cbarg;
break;
case FRAME_RECV:
conn->frame_recv_cb_unit.cb = cb;
conn->frame_recv_cb_unit.cbarg = cbarg;
break;
case WRITE:
conn->write_cb_unit.cb = cb;
conn->write_cb_unit.cbarg = cbarg;
break;
case CLOSE:
conn->close_cb_unit.cb = cb;
conn->close_cb_unit.cbarg = cbarg;
break;
case PING:
conn->ping_cb_unit.cb = cb;
conn->ping_cb_unit.cbarg = cbarg;
break;
default:
break;
}
}
}
void write_cb(struct bufferevent *bev, void *ctx) {
ws_conn_t *conn = (ws_conn_t*)ctx;
if (conn) {
if (conn->write_cb_unit.cb) {
websocket_cb cb = conn->write_cb_unit.cb;
void *cbarg = conn->write_cb_unit.cbarg;
cb(cbarg);
}
}
}
void close_cb(struct bufferevent *bev, short what, void *ctx) {
ws_serve_exit((ws_conn_t*)ctx);
}