Comparison

net/websocket/frames.lua @ 6395:e0164b0fcafd

net/websocket: Add new websocket client code
author daurnimator <quae@daurnimator.com>
date Wed, 03 Sep 2014 15:28:46 -0400
child 6398:ad434f47bfc0
comparison
equal deleted inserted replaced
6387:6d769afd0bc5 6395:e0164b0fcafd
1 -- Prosody IM
2 -- Copyright (C) 2012 Florian Zeitz
3 -- Copyright (C) 2014 Daurnimator
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 local softreq = require "util.dependencies".softreq;
10 local log = require "util.logger".init "websocket.frames";
11 local random_bytes = require "util.random".bytes;
12
13 local bit;
14 pcall(function() bit = require"bit"; end);
15 bit = bit or softreq"bit32"
16 if not bit then log("error", "No bit module found. Either LuaJIT 2, lua-bitop or Lua 5.2 is required"); end
17 local band = bit.band;
18 local bor = bit.bor;
19 local bxor = bit.bxor;
20 local lshift = bit.lshift;
21 local rshift = bit.rshift;
22
23 local t_concat = table.concat;
24 local s_byte = string.byte;
25 local s_char= string.char;
26 local s_sub = string.sub;
27
28 local function read_uint16be(str, pos)
29 local l1, l2 = s_byte(str, pos, pos+1);
30 return l1*256 + l2;
31 end
32 -- FIXME: this may lose precision
33 local function read_uint64be(str, pos)
34 local l1, l2, l3, l4, l5, l6, l7, l8 = s_byte(str, pos, pos+7);
35 return lshift(l1, 56) + lshift(l2, 48) + lshift(l3, 40) + lshift(l4, 32)
36 + lshift(l5, 24) + lshift(l6, 16) + lshift(l7, 8) + l8;
37 end
38 local function pack_uint16be(x)
39 return s_char(rshift(x, 8), band(x, 0xFF));
40 end
41 local function get_byte(x, n)
42 return band(rshift(x, n), 0xFF);
43 end
44 local function pack_uint64be(x)
45 return s_char(rshift(x, 56), get_byte(x, 48), get_byte(x, 40), get_byte(x, 32),
46 get_byte(x, 24), get_byte(x, 16), get_byte(x, 8), band(x, 0xFF));
47 end
48
49 local function parse_frame_header(frame)
50 if #frame < 2 then return; end
51
52 local byte1, byte2 = s_byte(frame, 1, 2);
53 local result = {
54 FIN = band(byte1, 0x80) > 0;
55 RSV1 = band(byte1, 0x40) > 0;
56 RSV2 = band(byte1, 0x20) > 0;
57 RSV3 = band(byte1, 0x10) > 0;
58 opcode = band(byte1, 0x0F);
59
60 MASK = band(byte2, 0x80) > 0;
61 length = band(byte2, 0x7F);
62 };
63
64 local length_bytes = 0;
65 if result.length == 126 then
66 length_bytes = 2;
67 elseif result.length == 127 then
68 length_bytes = 8;
69 end
70
71 local header_length = 2 + length_bytes + (result.MASK and 4 or 0);
72 if #frame < header_length then return; end
73
74 if length_bytes == 2 then
75 result.length = read_uint16be(frame, 3);
76 elseif length_bytes == 8 then
77 result.length = read_uint64be(frame, 3);
78 end
79
80 if result.MASK then
81 result.key = { s_byte(frame, length_bytes+3, length_bytes+6) };
82 end
83
84 return result, header_length;
85 end
86
87 -- XORs the string `str` with the array of bytes `key`
88 -- TODO: optimize
89 local function apply_mask(str, key, from, to)
90 from = from or 1
91 if from < 0 then from = #str + from + 1 end -- negative indicies
92 to = to or #str
93 if to < 0 then to = #str + to + 1 end -- negative indicies
94 local key_len = #key
95 local counter = 0;
96 local data = {};
97 for i = from, to do
98 local key_index = counter%key_len + 1;
99 counter = counter + 1;
100 data[counter] = s_char(bxor(key[key_index], s_byte(str, i)));
101 end
102 return t_concat(data);
103 end
104
105 local function parse_frame_body(frame, header, pos)
106 if header.MASK then
107 return apply_mask(frame, header.key, pos, pos + header.length - 1);
108 else
109 return frame:sub(pos, pos + header.length - 1);
110 end
111 end
112
113 local function parse_frame(frame)
114 local result, pos = parse_frame_header(frame);
115 if result == nil or #frame < (pos + result.length) then return; end
116 result.data = parse_frame_body(frame, result, pos+1);
117 return result, pos + result.length;
118 end
119
120 local function build_frame(desc)
121 local data = desc.data or "";
122
123 assert(desc.opcode and desc.opcode >= 0 and desc.opcode <= 0xF, "Invalid WebSocket opcode");
124 if desc.opcode >= 0x8 then
125 -- RFC 6455 5.5
126 assert(#data <= 125, "WebSocket control frames MUST have a payload length of 125 bytes or less.");
127 end
128
129 local b1 = bor(desc.opcode,
130 desc.FIN and 0x80 or 0,
131 desc.RSV1 and 0x40 or 0,
132 desc.RSV2 and 0x20 or 0,
133 desc.RSV3 and 0x10 or 0);
134
135 local b2 = #data;
136 local length_extra;
137 if b2 <= 125 then -- 7-bit length
138 length_extra = "";
139 elseif b2 <= 0xFFFF then -- 2-byte length
140 b2 = 126;
141 length_extra = pack_uint16be(#data);
142 else -- 8-byte length
143 b2 = 127;
144 length_extra = pack_uint64be(#data);
145 end
146
147 local key = ""
148 if desc.MASK then
149 local key_a = desc.key
150 if key_a then
151 key = s_char(unpack(key_a, 1, 4));
152 else
153 key = random_bytes(4);
154 key_a = {key:byte(1,4)};
155 end
156 b2 = bor(b2, 0x80);
157 data = apply_mask(data, key_a);
158 end
159
160 return s_char(b1, b2) .. length_extra .. key .. data
161 end
162
163 local function parse_close(data)
164 local code, message
165 if #data >= 2 then
166 code = read_uint16be(data, 1);
167 if #data > 2 then
168 message = s_sub(data, 3);
169 end
170 end
171 return code, message
172 end
173
174 local function build_close(code, message)
175 local data = pack_uint16be(code);
176 if message then
177 assert(#message<=123, "Close reason must be <=123 bytes");
178 data = data .. message;
179 end
180 return build_frame({
181 opcode = 0x8;
182 FIN = true;
183 MASK = true;
184 data = data;
185 });
186 end
187
188 return {
189 parse_header = parse_frame_header;
190 parse_body = parse_frame_body;
191 parse = parse_frame;
192 build = build_frame;
193 parse_close = parse_close;
194 build_close = build_close;
195 };