2 -- Copyright (C) 2012 Florian Zeitz
3 -- Copyright (C) 2014 Daurnimator
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 local softreq = require "util.dependencies".softreq;
10 local log = require "util.logger".init "websocket.frames";
11 local random_bytes = require "util.random".bytes;
13 local bit = softreq"bit" or softreq"bit32";
14 if not bit then log("error", "No bit module found. Either LuaJIT 2, lua-bitop or Lua 5.2 is required"); end
15 local band = bit.band;
17 local bxor = bit.bxor;
18 local lshift = bit.lshift;
19 local rshift = bit.rshift;
21 local t_concat = table.concat;
22 local s_byte = string.byte;
23 local s_char= string.char;
24 local s_sub = string.sub;
26 local function read_uint16be(str, pos)
27 local l1, l2 = s_byte(str, pos, pos+1);
30 -- FIXME: this may lose precision
31 local function read_uint64be(str, pos)
32 local l1, l2, l3, l4, l5, l6, l7, l8 = s_byte(str, pos, pos+7);
33 return lshift(l1, 56) + lshift(l2, 48) + lshift(l3, 40) + lshift(l4, 32)
34 + lshift(l5, 24) + lshift(l6, 16) + lshift(l7, 8) + l8;
36 local function pack_uint16be(x)
37 return s_char(rshift(x, 8), band(x, 0xFF));
39 local function get_byte(x, n)
40 return band(rshift(x, n), 0xFF);
42 local function pack_uint64be(x)
43 return s_char(rshift(x, 56), get_byte(x, 48), get_byte(x, 40), get_byte(x, 32),
44 get_byte(x, 24), get_byte(x, 16), get_byte(x, 8), band(x, 0xFF));
47 local function parse_frame_header(frame)
48 if #frame < 2 then return; end
50 local byte1, byte2 = s_byte(frame, 1, 2);
52 FIN = band(byte1, 0x80) > 0;
53 RSV1 = band(byte1, 0x40) > 0;
54 RSV2 = band(byte1, 0x20) > 0;
55 RSV3 = band(byte1, 0x10) > 0;
56 opcode = band(byte1, 0x0F);
58 MASK = band(byte2, 0x80) > 0;
59 length = band(byte2, 0x7F);
62 local length_bytes = 0;
63 if result.length == 126 then
65 elseif result.length == 127 then
69 local header_length = 2 + length_bytes + (result.MASK and 4 or 0);
70 if #frame < header_length then return; end
72 if length_bytes == 2 then
73 result.length = read_uint16be(frame, 3);
74 elseif length_bytes == 8 then
75 result.length = read_uint64be(frame, 3);
79 result.key = { s_byte(frame, length_bytes+3, length_bytes+6) };
82 return result, header_length;
85 -- XORs the string `str` with the array of bytes `key`
87 local function apply_mask(str, key, from, to)
89 if from < 0 then from = #str + from + 1 end -- negative indicies
91 if to < 0 then to = #str + to + 1 end -- negative indicies
96 local key_index = counter%key_len + 1;
97 counter = counter + 1;
98 data[counter] = s_char(bxor(key[key_index], s_byte(str, i)));
100 return t_concat(data);
103 local function parse_frame_body(frame, header, pos)
105 return apply_mask(frame, header.key, pos, pos + header.length - 1);
107 return frame:sub(pos, pos + header.length - 1);
111 local function parse_frame(frame)
112 local result, pos = parse_frame_header(frame);
113 if result == nil or #frame < (pos + result.length) then return; end
114 result.data = parse_frame_body(frame, result, pos+1);
115 return result, pos + result.length;
118 local function build_frame(desc)
119 local data = desc.data or "";
121 assert(desc.opcode and desc.opcode >= 0 and desc.opcode <= 0xF, "Invalid WebSocket opcode");
122 if desc.opcode >= 0x8 then
124 assert(#data <= 125, "WebSocket control frames MUST have a payload length of 125 bytes or less.");
127 local b1 = bor(desc.opcode,
128 desc.FIN and 0x80 or 0,
129 desc.RSV1 and 0x40 or 0,
130 desc.RSV2 and 0x20 or 0,
131 desc.RSV3 and 0x10 or 0);
135 if b2 <= 125 then -- 7-bit length
137 elseif b2 <= 0xFFFF then -- 2-byte length
139 length_extra = pack_uint16be(#data);
140 else -- 8-byte length
142 length_extra = pack_uint64be(#data);
147 local key_a = desc.key
149 key = s_char(unpack(key_a, 1, 4));
151 key = random_bytes(4);
152 key_a = {key:byte(1,4)};
155 data = apply_mask(data, key_a);
158 return s_char(b1, b2) .. length_extra .. key .. data
161 local function parse_close(data)
164 code = read_uint16be(data, 1);
166 message = s_sub(data, 3);
172 local function build_close(code, message, mask)
173 local data = pack_uint16be(code);
175 assert(#message<=123, "Close reason must be <=123 bytes");
176 data = data .. message;
187 parse_header = parse_frame_header;
188 parse_body = parse_frame_body;
191 parse_close = parse_close;
192 build_close = build_close;