-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathngx_websocket_handler.c
393 lines (314 loc) · 9.88 KB
/
ngx_websocket_handler.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
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
389
390
391
392
393
/*
* @Author: im-pingo
* @Copyright: pngox
* @Github: https://github.com/im-pingo
* @EMail: [email protected]
*/
#include "ngx_websocket.h"
static void
ngx_websocket_send_close_frame(ngx_websocket_session_t *ws, ngx_uint_t close_status, ngx_str_t *close_reason)
{
// If close_status is set, then we are already in a CLOSING state
if (ws->close_status) {
return;
}
ws->close_status = close_status;
ngx_str_t frame_data;
frame_data.len = 2 + (close_reason? close_reason->len: 0);
frame_data.data = ngx_pcalloc(ws->r->pool, frame_data.len);
frame_data.data[0] = (close_status >> 8) & 0xff;
frame_data.data[1] = close_status & 0xff;
if (close_reason) {
ngx_memcpy(frame_data.data + 2, close_reason->data, close_reason->len);
}
ngx_websocket_send_message(ws, &frame_data, NGX_WEBSOCKET_OPCODE_CLOSE);
}
static void
ngx_websocket_close_request(ngx_http_request_t *r, ngx_int_t rc)
{
ngx_connection_t *c;
r = r->main;
c = r->connection;
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http request count:%d blk:%d", r->count, r->blocked);
if (r->count == 0) {
ngx_log_error(NGX_LOG_ALERT, c->log, 0, "http request count is zero");
}
r->count--;
if (r->count || r->blocked) {
return;
}
#if (NGX_HTTP_V2)
if (r->stream) {
ngx_http_v2_close_stream(r->stream, rc);
return;
}
#endif
ngx_http_free_request(r, rc);
ngx_http_close_connection(c);
}
void
ngx_websocket_finalize_session(ngx_websocket_session_t *ws)
{
if (!ws || !ws->r) {
return;
}
ngx_websocket_close_request(ws->r, NGX_HTTP_OK);
}
// https://www.rfc-editor.org/rfc/rfc6455#section-7.4.1
#define NGX_WS_CLOSE_STATUS_OK 1000
#define NGX_WS_CLOSE_STATUS_PROTOCOL_ERROR 1002
#define NGX_WS_CLOSE_STATUS_TOO_LARGE 1009
static ngx_int_t
ngx_websocket_recv(ngx_http_request_t *r, ngx_err_t *err)
{
ngx_int_t n;
ngx_connection_t *c;
ngx_websocket_session_t *ws;
ngx_websocket_ctx_t *ctx;
ngx_websocket_frame_t *f;
ngx_websocket_header_t h;
ngx_buf_t *b;
size_t old_size;
u_char *p, *old_pos;
uint64_t i;
ngx_event_t *rev;
ngx_str_t msg;
ngx_websocket_loc_conf_t *wlcf;
c = r->connection;
b = NULL;
old_pos = NULL;
old_size = 0;
rev = c->read;
wlcf = ngx_http_get_module_loc_conf(r, ngx_websocket_module);
ctx = ngx_http_get_module_ctx(r, ngx_websocket_module);
if (ctx == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"websocket: recv| ctx is null");
return NGX_ERROR;
}
ws = ctx->ws;
f = &ws->in_frame;
for (;;) {
if (f->buf == NULL) {
f->buf = ngx_create_temp_buf(ws->pool, wlcf->max_length);
}
b = f->buf;
if (old_size) {
b->pos = b->start;
b->last = ngx_movemem(b->pos, old_pos, old_size);
} else {
#if 1
n = recv(c->fd, b->last, b->end - b->last, 0);
if (n == 0) {
rev->eof = 1;
c->error = 1;
*err = 0;
return NGX_ERROR;
} else if (n == -1) {
*err = ngx_socket_errno;
if (*err != NGX_EAGAIN) {
rev->eof = 1;
c->error = 1;
return NGX_ERROR;
}
return NGX_AGAIN;
}
/* aio does not call this handler */
if ((ngx_event_flags & NGX_USE_LEVEL_EVENT) && rev->active) {
if (ngx_del_event(rev, NGX_READ_EVENT, 0) != NGX_OK) {
ngx_websocket_close_request(r, 0);
return NGX_OK;
}
}
#else
n = c->recv(c, b->last, b->end - b->last);
if (n == NGX_ERROR || n == 0) {
return NGX_ERROR;
}
if (n == NGX_AGAIN) {
if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
ngx_websocket_close_request(r, 0);
return NGX_OK;
}
return NGX_AGAIN;
}
#endif
b->last += n;
}
old_pos = NULL;
old_size = 0;
if (f->ph == NULL) {
f->ph = b->pos;
f->phl = NULL;
}
if (f->phl == NULL) {
p = f->ph;
h.fin = (*p) >> 7;
if (h.fin == 0) {
f->append = 1;
}
h.opcode = *p & 0x0f;
if (f->opcode == 0) {
f->opcode = h.opcode;
}
p++;
if (b->last - p < 1) {
continue;
}
h.mask = (*p) >> 7;
h.payload_length = (*p) & 0x7f;
p++;
if (h.payload_length < 126) {
f->len = h.payload_length;
} else if (h.payload_length == 126) {
if (b->last - p < 2) {
continue;
}
ngx_websocket_rmemcpy(&h.extended_playload_length16, p, 2);
p += 2;
f->len = h.extended_playload_length16;
} else if (h.payload_length == 127) {
if (b->last - p < 2) {
continue;
}
ngx_websocket_rmemcpy(&h.extended_playload_length64, p, 8);
p += 8;
f->len = h.extended_playload_length64;
}
if (h.mask) {
if (b->last - p < 4) {
continue;
}
ngx_memcpy(h.masking_key, p, 4);
//ngx_websocket_rmemcpy(h.masking_key, p, 4);
p += 4;
}
f->phl = p;
}
p = f->phl;
if (b->last == b->end && (uint64_t) (b->last - p) < f->len) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"websocket: recv| message is too large.");
*err = NGX_WS_CLOSE_STATUS_TOO_LARGE;
return NGX_HTTP_REQUEST_ENTITY_TOO_LARGE;
}
if ((uint64_t)(b->last - p) < f->len) {
return NGX_AGAIN;
}
for (i = 0; i < f->len; i++) {
*p = h.masking_key[i%4] ^ *p;
p++;
}
if (f->append) {
ngx_memmove(f->ph, f->phl, f->len);
f->mlen += f->len;
b->last -= f->phl - f->ph;
p -= f->phl - f->ph;
} else {
b->pos = f->phl;
}
if (h.fin == 1) {
msg.data = b->pos;
msg.len = p - b->pos;
if (f->opcode == NGX_WEBSOCKET_OPCODE_CLOSE) {
ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0,
"websocket: recv| get close message.");
*err = msg.len >= 2? (msg.data[ 0 ] << 8) + msg.data[ 1 ]: NGX_WS_CLOSE_STATUS_PROTOCOL_ERROR;
return NGX_HTTP_CLOSE;
} else if (f->opcode == NGX_WEBSOCKET_OPCODE_PING) {
ngx_websocket_send_message(ws, &msg, NGX_WEBSOCKET_OPCODE_PONG);
} else if (ws->recv_handler) {
ws->last_recv = ngx_time();
ws->recv_handler(ws, &msg, f->opcode);
}
if (b->last == p) {
b->last = b->pos = b->start;
} else {
old_pos = p;
old_size = b->last - p;
}
f->ph = NULL;
f->phl = NULL;
f->opcode = 0;
f->append = 0;
return NGX_OK;
}
// if fin == 0
if (b->last > p) {
f->ph = p;
f->phl = NULL;
}
}
return NGX_OK;
}
void
ngx_websocket_read_handler(ngx_http_request_t *r)
{
ngx_err_t err;
ngx_event_t *rev;
ngx_connection_t *c;
ngx_websocket_ctx_t *ctx;
int rc;
c = r->connection;
rev = c->read;
#if (NGX_HTTP_V2)
if (r->stream) {
if (c->error) {
err = 0;
goto closed;
}
return;
}
#endif
#if (NGX_HAVE_KQUEUE)
if (ngx_event_flags & NGX_USE_KQUEUE_EVENT && rev->pending_eof) {
rev->eof = 1;
c->error = 1;
err = rev->kq_errno;
goto closed;
}
#endif
#if (NGX_HAVE_EPOLLRDHUP)
if (((ngx_event_flags & NGX_USE_EPOLL_EVENT) && ngx_use_epoll_rdhup) &&
rev->pending_eof)
{
socklen_t len;
rev->eof = 1;
c->error = 1;
err = 0;
len = sizeof(ngx_err_t);
/*
* BSDs and Linux return 0 and set a pending error in err
* Solaris returns -1 and sets errno
*/
if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, (void *) &err, &len)
== -1)
{
err = ngx_socket_errno;
}
goto closed;
}
#endif
rc = ngx_websocket_recv(r, &err);
if (rc == NGX_ERROR) {
ngx_log_error(NGX_LOG_DEBUG, c->log, err,
"websocket-recv: read_handler| recv return error");
goto closed;
} else if (rc == NGX_HTTP_CLOSE || rc == NGX_HTTP_REQUEST_ENTITY_TOO_LARGE) {
// Echo the close frame back to the caller if the close operation originated there
if ((ctx = ngx_http_get_module_ctx(r, ngx_websocket_module)) != 0 &&
ctx->ws && !ctx->ws->close_status)
ngx_websocket_send_close_frame(ctx->ws, err, 0);
if (rc == NGX_HTTP_CLOSE)
ngx_websocket_close_request(r, NGX_HTTP_OK);
}
return;
closed:
if (err) {
rev->error = 1;
}
ngx_log_error(NGX_LOG_DEBUG, c->log, err,
"websocket-recv: read_handler| client prematurely closed connection");
ngx_http_finalize_request(r, NGX_HTTP_CLIENT_CLOSED_REQUEST);
}