forked from Refinitiv/ngx_http_websocket_stat_module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngx_http_websocket_stat_frame_counter.c
129 lines (126 loc) · 4.14 KB
/
ngx_http_websocket_stat_frame_counter.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
#include "ngx_http_websocket_stat_frame_counter.h"
#include <assert.h>
const char *
frame_type_to_str(frame_type frame)
{
switch (frame) {
case CONTINUATION:
return "cont";
case TEXT:
return "text";
case BINARY:
return "bin";
case CLOSE:
return "cls";
case PING:
return "ping";
case PONG:
return "pong";
default:
return "uknw";
}
}
void
move_buffer(u_char **buffer, ssize_t *size, int step)
{
*buffer += step;
*size -= step;
}
char
frame_counter_process_message(u_char **buffer, ssize_t *size,
ngx_frame_counter_t *frame_counter)
{
while (*size > 0) {
switch (frame_counter->stage) {
case HEADER:
frame_counter->current_frame_type = **buffer & 0x0f;
move_buffer(buffer, size, 1);
frame_counter->stage = PAYLOAD_LEN;
frame_counter->bytes_consumed =
frame_counter->current_payload_size = 0;
break;
case PAYLOAD_LEN:
frame_counter->payload_masked = **buffer >> 7;
u_char len = **buffer & 0x7f;
move_buffer(buffer, size, 1);
if (len < 126) {
if (len == 0 && !frame_counter->payload_masked) {
frame_counter->stage = HEADER;
return 1;
}
frame_counter->current_payload_size = len;
frame_counter->stage =
frame_counter->payload_masked ? MASK : PAYLOAD;
} else if (len == 126) {
frame_counter->stage = PAYLOAD_LEN_LARGE;
} else if (len == 127) {
frame_counter->stage = PAYLOAD_LEN_HUGE;
} else {
// WTF?
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0,
"Wrong payload length");
exit(-1);
}
break;
case PAYLOAD_LEN_LARGE:
case PAYLOAD_LEN_HUGE: {
int i;
if (frame_counter->stage == PAYLOAD_LEN_LARGE) {
assert(*size >= 2);
i = 2;
} else {
assert(*size >= 8);
i = 8;
}
while (1) {
frame_counter->current_payload_size |= **buffer;
move_buffer(buffer, size, 1);
if (--i)
frame_counter->current_payload_size <<= 8;
else
break;
}
frame_counter->stage =
frame_counter->payload_masked ? MASK : PAYLOAD;
frame_counter->bytes_consumed = 0;
break;
}
case MASK:
assert(frame_counter->payload_masked);
move_buffer(buffer, size, 1);
frame_counter->bytes_consumed++;
if (frame_counter->bytes_consumed == MASK_SIZE) {
if (frame_counter->current_payload_size == 0) {
frame_counter->stage = HEADER;
return 1;
}
frame_counter->bytes_consumed = 0;
frame_counter->stage = PAYLOAD;
}
break;
case PAYLOAD:
if (*size >= (u_int)(frame_counter->current_payload_size -
frame_counter->bytes_consumed)) {
move_buffer(buffer, size,
frame_counter->current_payload_size -
frame_counter->bytes_consumed);
frame_counter->stage = HEADER;
return 1;
} else {
frame_counter->bytes_consumed += *size;
if (frame_counter->bytes_consumed >
frame_counter->current_payload_size) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "Unknown error");
frame_counter->stage = HEADER;
}
*buffer += *size;
*size = 0;
}
break;
default:
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "Unknown stage");
move_buffer(buffer, size, 1);
}
}
return 0;
}