# HG changeset patch # User Florian Zeitz # Date 1369950554 -7200 # Node ID 81065638299df62880261220dfe2264354847043 # Parent 6a2dfa8af4212bdd283a821118413c2b5fabbc46 mod_websocket: Access some values via locales instead of through tables diff -r 6a2dfa8af421 -r 81065638299d mod_websocket/mod_websocket.lua --- a/mod_websocket/mod_websocket.lua Thu May 30 23:36:58 2013 +0200 +++ b/mod_websocket/mod_websocket.lua Thu May 30 23:49:14 2013 +0200 @@ -82,12 +82,13 @@ if result.MASK then local counter = 0; local data = {}; - result.key = {s_byte(frame, pos+1), s_byte(frame, pos+2), + local key = {s_byte(frame, pos+1), s_byte(frame, pos+2), s_byte(frame, pos+3), s_byte(frame, pos+4)} + result.key = key; pos = pos + 5; for i = pos, pos + result.length - 1 do - data[#data+1] = s_char(bxor(result.key[counter+1], s_byte(frame, i))); + data[#data+1] = s_char(bxor(key[counter+1], s_byte(frame, i))); counter = (counter + 1) % 4; end result.data = t_concat(data, ""); @@ -155,6 +156,8 @@ local dataBuffer; local function handle_frame(frame) + local opcode = frame.opcode; + local length = frame.length; module:log("debug", "Websocket received: %s (%i bytes)", frame.data, #frame.data); -- Error cases @@ -163,48 +166,48 @@ return false; end - if frame.opcode >= 0x8 and frame.length > 125 then -- Control frame with too much payload + if opcode >= 0x8 and length > 125 then -- Control frame with too much payload websocket_close(1002, "Payload too large"); return false; end - if frame.opcode >= 0x8 and not frame.FIN then -- Fragmented control frame + if opcode >= 0x8 and not frame.FIN then -- Fragmented control frame websocket_close(1002, "Fragmented control frame"); return false; end - if (frame.opcode > 0x2 and frame.opcode < 0x8) or (frame.opcode > 0xA) then + if (opcode > 0x2 and opcode < 0x8) or (opcode > 0xA) then websocket_close(1002, "Reserved opcode"); return false; end - if frame.opcode == 0x0 and not dataBuffer then + if opcode == 0x0 and not dataBuffer then websocket_close(1002, "Unexpected continuation frame"); return false; end - if (frame.opcode == 0x1 or frame.opcode == 0x2) and dataBuffer then + if (opcode == 0x1 or opcode == 0x2) and dataBuffer then websocket_close(1002, "Continuation frame expected"); return false; end -- Valid cases - if frame.opcode == 0x0 then -- Continuation frame + if opcode == 0x0 then -- Continuation frame dataBuffer[#dataBuffer+1] = frame.data; - elseif frame.opcode == 0x1 then -- Text frame + elseif opcode == 0x1 then -- Text frame dataBuffer = {frame.data}; - elseif frame.opcode == 0x2 then -- Binary frame + elseif opcode == 0x2 then -- Binary frame websocket_close(1003, "Only text frames are supported"); return; - elseif frame.opcode == 0x8 then -- Close request + elseif opcode == 0x8 then -- Close request websocket_close(1000, "Goodbye"); return; - elseif frame.opcode == 0x9 then -- Ping frame + elseif opcode == 0x9 then -- Ping frame frame.opcode = 0xA; conn:write(build_frame(frame)); return ""; else - log("warn", "Received frame with unsupported opcode %i", frame.opcode); + log("warn", "Received frame with unsupported opcode %i", opcode); return ""; end