|
@@ -0,0 +1,101 @@
|
|
1
|
+--- mod_websocket.lua
|
|
2
|
++++ mod_websocket.lua
|
|
3
|
+@@ -163,34 +163,34 @@ function handle_request(event)
|
|
4
|
+ return 403;
|
|
5
|
+ end
|
|
6
|
+
|
|
7
|
+- local function websocket_close(code, message)
|
|
8
|
++ local function websocket_close(conn, code, message)
|
|
9
|
+ conn:write(build_close(code, message));
|
|
10
|
+ conn:close();
|
|
11
|
+ end
|
|
12
|
+
|
|
13
|
+ local dataBuffer;
|
|
14
|
+- local function handle_frame(frame)
|
|
15
|
++ local function handle_frame(conn, frame)
|
|
16
|
+ local opcode = frame.opcode;
|
|
17
|
+ local length = frame.length;
|
|
18
|
+ module:log("debug", "Websocket received frame: opcode=%0x, %i bytes", frame.opcode, #frame.data);
|
|
19
|
+
|
|
20
|
+ -- Error cases
|
|
21
|
+ if frame.RSV1 or frame.RSV2 or frame.RSV3 then -- Reserved bits non zero
|
|
22
|
+- websocket_close(1002, "Reserved bits not zero");
|
|
23
|
++ websocket_close(conn, 1002, "Reserved bits not zero");
|
|
24
|
+ return false;
|
|
25
|
+ end
|
|
26
|
+
|
|
27
|
+ if opcode == 0x8 then -- close frame
|
|
28
|
+ if length == 1 then
|
|
29
|
+- websocket_close(1002, "Close frame with payload, but too short for status code");
|
|
30
|
++ websocket_close(conn, 1002, "Close frame with payload, but too short for status code");
|
|
31
|
+ return false;
|
|
32
|
+ elseif length >= 2 then
|
|
33
|
+ local status_code = parse_close(frame.data)
|
|
34
|
+ if status_code < 1000 then
|
|
35
|
+- websocket_close(1002, "Closed with invalid status code");
|
|
36
|
++ websocket_close(conn, 1002, "Closed with invalid status code");
|
|
37
|
+ return false;
|
|
38
|
+ elseif ((status_code > 1003 and status_code < 1007) or status_code > 1011) and status_code < 3000 then
|
|
39
|
+- websocket_close(1002, "Closed with reserved status code");
|
|
40
|
++ websocket_close(conn, 1002, "Closed with reserved status code");
|
|
41
|
+ return false;
|
|
42
|
+ end
|
|
43
|
+ end
|
|
44
|
+@@ -198,28 +198,28 @@ function handle_request(event)
|
|
45
|
+
|
|
46
|
+ if opcode >= 0x8 then
|
|
47
|
+ if length > 125 then -- Control frame with too much payload
|
|
48
|
+- websocket_close(1002, "Payload too large");
|
|
49
|
++ websocket_close(conn, 1002, "Payload too large");
|
|
50
|
+ return false;
|
|
51
|
+ end
|
|
52
|
+
|
|
53
|
+ if not frame.FIN then -- Fragmented control frame
|
|
54
|
+- websocket_close(1002, "Fragmented control frame");
|
|
55
|
++ websocket_close(conn, 1002, "Fragmented control frame");
|
|
56
|
+ return false;
|
|
57
|
+ end
|
|
58
|
+ end
|
|
59
|
+
|
|
60
|
+ if (opcode > 0x2 and opcode < 0x8) or (opcode > 0xA) then
|
|
61
|
+- websocket_close(1002, "Reserved opcode");
|
|
62
|
++ websocket_close(conn, 1002, "Reserved opcode");
|
|
63
|
+ return false;
|
|
64
|
+ end
|
|
65
|
+
|
|
66
|
+ if opcode == 0x0 and not dataBuffer then
|
|
67
|
+- websocket_close(1002, "Unexpected continuation frame");
|
|
68
|
++ websocket_close(conn, 1002, "Unexpected continuation frame");
|
|
69
|
+ return false;
|
|
70
|
+ end
|
|
71
|
+
|
|
72
|
+ if (opcode == 0x1 or opcode == 0x2) and dataBuffer then
|
|
73
|
+- websocket_close(1002, "Continuation frame expected");
|
|
74
|
++ websocket_close(conn, 1002, "Continuation frame expected");
|
|
75
|
+ return false;
|
|
76
|
+ end
|
|
77
|
+
|
|
78
|
+@@ -229,11 +229,11 @@ function handle_request(event)
|
|
79
|
+ elseif opcode == 0x1 then -- Text frame
|
|
80
|
+ dataBuffer = {frame.data};
|
|
81
|
+ elseif opcode == 0x2 then -- Binary frame
|
|
82
|
+- websocket_close(1003, "Only text frames are supported");
|
|
83
|
++ websocket_close(conn, 1003, "Only text frames are supported");
|
|
84
|
+ return;
|
|
85
|
+ elseif opcode == 0x8 then -- Close request
|
|
86
|
+- websocket_close(1000, "Goodbye");
|
|
87
|
+- return;
|
|
88
|
++ websocket_close(conn, 1000, "Goodbye");
|
|
89
|
++ return "";
|
|
90
|
+ elseif opcode == 0x9 then -- Ping frame
|
|
91
|
+ frame.opcode = 0xA;
|
|
92
|
+ conn:write(build_frame(frame));
|
|
93
|
+@@ -276,7 +276,7 @@ function handle_request(event)
|
|
94
|
+
|
|
95
|
+ while frame do
|
|
96
|
+ frameBuffer = frameBuffer:sub(length + 1);
|
|
97
|
+- local result = handle_frame(frame);
|
|
98
|
++ local result = handle_frame(session.conn, frame);
|
|
99
|
+ if not result then return; end
|
|
100
|
+ cache[#cache+1] = filter_open_close(result);
|
|
101
|
+ frame, length = parse_frame(frameBuffer);
|