forked from nicolasff/webdis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket.c
254 lines (202 loc) · 5.36 KB
/
websocket.c
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
#include "md5/md5.h"
#include "websocket.h"
#include "client.h"
#include "cmd.h"
#include "worker.h"
#include "pool.h"
/* message parsers */
#include "formats/json.h"
#include "formats/raw.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
/**
* This code uses the WebSocket specification from May 23, 2010.
* The latest copy is available at http://www.whatwg.org/specs/web-socket-protocol/
*/
static uint32_t
ws_read_key(const char *s) {
uint32_t ret = 0, spaces = 0;
const char *p;
size_t sz;
if(!s) {
return 0;
}
sz = strlen(s);
for(p = s; p < s+sz; ++p) {
if(*p >= '0' && *p <= '9') {
ret *= 10;
ret += (*p) - '0';
} else if (*p == ' ') {
spaces++;
}
}
return htonl(ret / spaces);
}
static int
ws_compute_handshake(struct http_client *c, unsigned char *out) {
char buffer[16];
md5_state_t ctx;
// websocket handshake
uint32_t number_1 = ws_read_key(client_get_header(c, "Sec-WebSocket-Key1"));
uint32_t number_2 = ws_read_key(client_get_header(c, "Sec-WebSocket-Key2"));
if(c->body_sz < 8) { /* we need at least 8 bytes */
return -1;
}
/* copy number_1, number_2, and last 8 bytes of the body. */
memcpy(buffer, &number_1, 4);
memcpy(buffer + 4, &number_2, 4);
memcpy(buffer + 8, c->body + c->body_sz - 8, 8);
/* hash that buffer, that creates the handshake signature. */
md5_init(&ctx);
md5_append(&ctx, (const md5_byte_t *)buffer, sizeof(buffer));
md5_finish(&ctx, out);
return 0;
}
int
ws_handshake_reply(struct http_client *c) {
int ret;
unsigned char md5_handshake[16];
char *buffer = NULL, *p;
const char *origin = NULL, *host = NULL;
size_t origin_sz = 0, host_sz = 0, sz;
char template0[] = "HTTP/1.1 101 Websocket Protocol Handshake\r\n"
"Upgrade: WebSocket\r\n"
"Connection: Upgrade\r\n"
"Sec-WebSocket-Origin: "; /* %s */
char template1[] = "\r\n"
"Sec-WebSocket-Location: ws://"; /* %s%s */
char template2[] = "\r\n"
"Origin: http://"; /* %s */
char template3[] = "\r\n\r\n";
if((origin = client_get_header(c, "Origin"))) {
origin_sz = strlen(origin);
}
if((host = client_get_header(c, "Host"))) {
host_sz = strlen(host);
}
/* need those headers */
if(!origin || !origin_sz || !host || !host_sz || !c->path || !c->path_sz) {
return -1;
}
if(ws_compute_handshake(c, &md5_handshake[0]) != 0) {
/* failed to compute handshake. */
return -1;
}
sz = sizeof(template0)-1 + origin_sz
+ sizeof(template1)-1 + host_sz + c->path_sz
+ sizeof(template2)-1 + host_sz
+ sizeof(template3)-1 + sizeof(md5_handshake);
p = buffer = malloc(sz);
/* Concat all */
/* template0 */
memcpy(p, template0, sizeof(template0)-1);
p += sizeof(template0)-1;
memcpy(p, origin, origin_sz);
p += origin_sz;
/* template1 */
memcpy(p, template1, sizeof(template1)-1);
p += sizeof(template1)-1;
memcpy(p, host, host_sz);
p += host_sz;
memcpy(p, c->path, c->path_sz);
p += c->path_sz;
/* template2 */
memcpy(p, template2, sizeof(template2)-1);
p += sizeof(template2)-1;
memcpy(p, host, host_sz);
p += host_sz;
/* template3 */
memcpy(p, template3, sizeof(template3)-1);
p += sizeof(template3)-1;
memcpy(p, &md5_handshake[0], sizeof(md5_handshake));
ret = write(c->fd, buffer, sz);
(void)ret;
free(buffer);
return 0;
}
static int
ws_execute(struct http_client *c, const char *frame, size_t frame_len) {
struct cmd*(*fun_extract)(struct http_client *, const char *, size_t) = NULL;
formatting_fun fun_reply = NULL;
if((c->path_sz == 1 && strncmp(c->path, "/", 1) == 0) ||
strncmp(c->path, "/.json", 6) == 0) {
fun_extract = json_ws_extract;
fun_reply = json_reply;
} else if(strncmp(c->path, "/.raw", 5) == 0) {
fun_extract = raw_ws_extract;
fun_reply = raw_reply;
}
if(fun_extract) {
/* Parse websocket frame into a cmd object. */
struct cmd *cmd = fun_extract(c, frame, frame_len);
if(cmd) {
/* copy client info into cmd. */
cmd_setup(cmd, c);
cmd->is_websocket = 1;
/* get Redis connection from pool */
cmd->ac = (redisAsyncContext*)pool_get_context(c->w->pool);
/* send it off */
cmd_send(cmd, fun_reply);
return 0;
}
}
return -1;
}
/**
* Process some data just received on the socket.
*/
enum ws_read_action
ws_add_data(struct http_client *c) {
const char *frame_start, *frame_end;
char *tmp;
while(1) {
/* look for frame start */
if(!c->sz || c->buffer[0] != '\x00') {
/* can't find frame start */
return WS_READ_FAIL;
}
/* look for frame end */
int ret;
size_t frame_len;
frame_start = c->buffer;
frame_end = memchr(frame_start, '\xff', c->sz);
if(frame_end == NULL) {
/* continue reading */
return WS_READ_MORE;
}
/* parse and execute frame. */
frame_len = frame_end - frame_start - 1;
ret = ws_execute(c, frame_start + 1, frame_len);
if(ret != 0) {
/* can't process frame. */
return WS_READ_FAIL;
}
/* remove frame from buffer */
c->sz -= (2 + frame_len);
tmp = malloc(c->sz);
memcpy(tmp, c->buffer + 2 + frame_len, c->sz);
free(c->buffer);
c->buffer = tmp;
}
}
int
ws_reply(struct cmd *cmd, const char *p, size_t sz) {
int ret;
char *buffer = malloc(sz + 2);
/* create frame by prepending 0x00 and appending 0xff */
buffer[0] = '\x00';
memcpy(buffer + 1, p, sz);
buffer[sz + 1] = '\xff';
/* send WS frame */
ret = write(cmd->fd, buffer, sz+2);
free(buffer);
if(ret == (int)sz + 2) {
/* success */
return 0;
}
/* write fail */
return -1;
}