Comparison

mod_websocket/mod_websocket.lua @ 910:c469a2b2d77d

mod_websocket: Avoid floating point division The problem here was that Lua's integer conversion (rounding?) routines behave differently on x86 vs. x86_64 (and even on those there can be minor differenes). Usually the former does proper rounding, while the later floors.
author Florian Zeitz <florob@babelmonkeys.de>
date Fri, 15 Feb 2013 01:32:03 +0100
parent 909:ec4c6e8f277d
child 969:8eba9d4809d2
comparison
equal deleted inserted replaced
909:ec4c6e8f277d 910:c469a2b2d77d
17 pcall(function() bit = require"bit"; end); 17 pcall(function() bit = require"bit"; end);
18 bit = bit or softreq"bit32" 18 bit = bit or softreq"bit32"
19 if not bit then module:log("error", "No bit module found. Either LuaJIT 2, lua-bitop or Lua 5.2 is required"); end 19 if not bit then module:log("error", "No bit module found. Either LuaJIT 2, lua-bitop or Lua 5.2 is required"); end
20 local band = bit.band; 20 local band = bit.band;
21 local bxor = bit.bxor; 21 local bxor = bit.bxor;
22 local rshift = bit.rshift;
22 23
23 local cross_domain = module:get_option("cross_domain_websocket"); 24 local cross_domain = module:get_option("cross_domain_websocket");
24 if cross_domain then 25 if cross_domain then
25 if cross_domain == true then 26 if cross_domain == true then
26 cross_domain = "*"; 27 cross_domain = "*";
102 length = #data; 103 length = #data;
103 if length <= 125 then -- 7-bit length 104 if length <= 125 then -- 7-bit length
104 result = result .. string.char(length); 105 result = result .. string.char(length);
105 elseif length <= 0xFFFF then -- 2-byte length 106 elseif length <= 0xFFFF then -- 2-byte length
106 result = result .. string.char(126); 107 result = result .. string.char(126);
107 result = result .. string.char(length/0x100) .. string.char(length%0x100); 108 result = result .. string.char(rshift(length, 8)) .. string.char(length%0x100);
108 else -- 8-byte length 109 else -- 8-byte length
109 result = result .. string.char(127); 110 result = result .. string.char(127);
110 for i = 7, 0, -1 do 111 for i = 7, 0, -1 do
111 result = result .. string.char(( length / (2^(8*i)) ) % 0x100); 112 result = result .. string.char(rshift(length, 8*i) % 0x100);
112 end 113 end
113 end 114 end
114 115
115 result = result .. data; 116 result = result .. data;
116 117
137 if not wants_xmpp then 138 if not wants_xmpp then
138 return 501; 139 return 501;
139 end 140 end
140 141
141 local function websocket_close(code, message) 142 local function websocket_close(code, message)
142 local data = string.char(code/0x100) .. string.char(code%0x100) .. message; 143 local data = string.char(rshift(code, 8)) .. string.char(code%0x100) .. message;
143 conn:write(build_frame({opcode = 0x8, FIN = true, data = data})); 144 conn:write(build_frame({opcode = 0x8, FIN = true, data = data}));
144 conn:close(); 145 conn:close();
145 end 146 end
146 147
147 local dataBuffer; 148 local dataBuffer;