changeset 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
parents ec4c6e8f277d
children 46e807dff445
files mod_websocket/mod_websocket.lua
diffstat 1 files changed, 4 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/mod_websocket/mod_websocket.lua	Wed Feb 13 21:03:45 2013 +0100
+++ b/mod_websocket/mod_websocket.lua	Fri Feb 15 01:32:03 2013 +0100
@@ -19,6 +19,7 @@
 if not bit then module:log("error", "No bit module found. Either LuaJIT 2, lua-bitop or Lua 5.2 is required"); end
 local band = bit.band;
 local bxor = bit.bxor;
+local rshift = bit.rshift;
 
 local cross_domain = module:get_option("cross_domain_websocket");
 if cross_domain then
@@ -104,11 +105,11 @@
 		result = result .. string.char(length);
 	elseif length <= 0xFFFF then -- 2-byte length
 		result = result .. string.char(126);
-		result = result .. string.char(length/0x100) .. string.char(length%0x100);
+		result = result .. string.char(rshift(length, 8)) .. string.char(length%0x100);
 	else -- 8-byte length
 		result = result .. string.char(127);
 		for i = 7, 0, -1 do
-			result = result .. string.char(( length / (2^(8*i)) ) % 0x100);
+			result = result .. string.char(rshift(length, 8*i) % 0x100);
 		end
 	end
 
@@ -139,7 +140,7 @@
 	end
 
 	local function websocket_close(code, message)
-		local data = string.char(code/0x100) .. string.char(code%0x100) .. message;
+		local data = string.char(rshift(code, 8)) .. string.char(code%0x100) .. message;
 		conn:write(build_frame({opcode = 0x8, FIN = true, data = data}));
 		conn:close();
 	end