-
Notifications
You must be signed in to change notification settings - Fork 63
/
websocket.cpp
153 lines (136 loc) · 3.75 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
#include "websocket.h"
#ifdef _MSC_VER
#define strcasecmp _stricmp
#endif
int32_t parse_websocket_request(const char *s_req, ws_req_t *ws_req) {
if (!s_req || !ws_req) {
return -1;
}
int len = strlen(s_req);
char* tmp = (char*)malloc(len + 1);
tmp[len] = 0;
memcpy(tmp, s_req, len);
char *delim = "\r\n";
char *p = NULL, *q = NULL;
p = strtok(tmp, delim);
if (p) {
//printf("%s\n", p);
ws_req->req = p;
while (p = strtok(NULL, delim)) {
//printf("%s\n", p);
if ((q = strstr(p, ":")) != NULL) {
*q = '\0';
if (strcasecmp(p, "Connection") == 0) {
while (*++q == ' ');
ws_req->connection = q;
}
if (strcasecmp(p, "Upgrade") == 0) {
while (*++q == ' ');
ws_req->upgrade = q;
}
if (strcasecmp(p, "Host") == 0) {
while (*++q == ' ');
ws_req->host = q + 1;
}
if (strcasecmp(p, "Origin") == 0) {
while (*++q == ' ');
ws_req->origin = q;
}
if (strcasecmp(p, "Cookie") == 0) {
while (*++q == ' ');
ws_req->cookie = q;
}
if (strcasecmp(p, "Sec-WebSocket-Key") == 0) {
while (*++q == ' ');
ws_req->sec_websocket_key = q;
}
if (strcasecmp(p, "Sec-WebSocket-Version") == 0) {
while (*++q == ' ');
ws_req->sec_websocket_version = q;
}
}
}
}
free(tmp);
return 0;
}
void print_websocket_request(const ws_req_t *ws_req) {
if (ws_req) {
if (!ws_req->req.empty()) {
fprintf(stdout, "%s\r\n", ws_req->req.c_str());
}
if (!ws_req->connection.empty()) {
fprintf(stdout, "Connection: %s\r\n", ws_req->connection.c_str());
}
if (!ws_req->upgrade.empty()) {
fprintf(stdout, "Upgrade: %s\r\n", ws_req->upgrade.c_str());
}
if (!ws_req->host.empty()) {
fprintf(stdout, "Host: %s\r\n", ws_req->host.c_str());
}
if (!ws_req->origin.empty()) {
fprintf(stdout, "Origin: %s\r\n", ws_req->origin.c_str());
}
if (!ws_req->cookie.empty()) {
fprintf(stdout, "Cookie: %s\r\n", ws_req->cookie.c_str());
}
if (!ws_req->sec_websocket_key.empty()) {
fprintf(stdout, "Sec-WebSocket-Key: %s\r\n", ws_req->sec_websocket_key.c_str());
}
if (!ws_req->sec_websocket_version.empty()) {
fprintf(stdout, "Sec-WebSocket-Version: %s\r\n", ws_req->sec_websocket_version.c_str());
}
fprintf(stdout, "\r\n");
}
}
string generate_websocket_response(const ws_req_t *ws_req) {
string resp;
if (ws_req) {
resp += "HTTP/1.1 101 WebSocket Protocol HandShake\r\n";
resp += "Connection: Upgrade\r\n";
resp += "Upgrade: WebSocket\r\n";
resp += "Server: WebChat Demo Server\r\n";
resp += "Sec-WebSocket-Accept: " + generate_key(ws_req->sec_websocket_key) + "\r\n";
resp += "\r\n";
}
return resp;
}
string generate_key(const string &key) {
//sha-1
string tmp = key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
unsigned char md[20] = {0};
SHA1((const unsigned char*)tmp.c_str(), tmp.length(), md);
//base64 encode
string res = base64_encode(md, 20);
return res;
}
int32_t parse_frame_header(const char *buf, frame_t *frame) {
if (!buf || !frame) {
return -1;
}
unsigned char c1 = *buf;
unsigned char c2 = *(buf + 1);
frame->fin = (c1 >> 7) & 0xff;
frame->opcode = c1 & 0x0f;
frame->mask = (c2 >> 7) & 0xff;
frame->payload_len = c2 & 0x7f;
return 0;
}
int32_t unmask_payload_data(frame_t *frame) {
if (frame && frame->payload_data && frame->payload_len > 0) {
for (int32_t i = 0; i < frame->payload_len; ++i) {
*(frame->payload_data + i) = *(frame->payload_data + i) ^ *(frame->masking_key + i % 4);
}
return 0;
}
return -1;
}
int32_t unmask_payload_data(const char *masking_key, char *payload_data, uint32_t payload_len) {
if (!masking_key || !payload_data || payload_len == 0) {
return -1;
}
for (int32_t i = 0; i < payload_len; ++i) {
*(payload_data + i) = *(payload_data + i) ^ *(masking_key + i % 4);
}
return 0;
}