aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2015-10-06 18:05:27 +0200
committerKim Alvefur <zash@zash.se>2015-10-06 18:05:27 +0200
commit7bff4f91287f17c1e6e6c4ba7275e325aae45f2d (patch)
tree5432aece87866ba29420c9886bd3082f1e3b0758
parent98b2e87b9d013b64a9cc095d737b4431e9d4dea7 (diff)
downloadprosody-7bff4f91287f17c1e6e6c4ba7275e325aae45f2d.tar.gz
prosody-7bff4f91287f17c1e6e6c4ba7275e325aae45f2d.zip
net.websocket.frames: Use struct packing in Lua 5.3 or struct lib if available
-rw-r--r--net/websocket/frames.lua25
1 files changed, 25 insertions, 0 deletions
diff --git a/net/websocket/frames.lua b/net/websocket/frames.lua
index 8528ab99..95e41479 100644
--- a/net/websocket/frames.lua
+++ b/net/websocket/frames.lua
@@ -22,6 +22,13 @@ local t_concat = table.concat;
local s_byte = string.byte;
local s_char= string.char;
local s_sub = string.sub;
+local s_pack = string.pack;
+local s_unpack = string.unpack;
+
+if not s_pack and softreq"struct" then
+ s_pack = softreq"struct".pack;
+ s_unpack = softreq"struct".unpack;
+end
local function read_uint16be(str, pos)
local l1, l2 = s_byte(str, pos, pos+1);
@@ -46,6 +53,24 @@ local function pack_uint64be(x)
get_byte(x, 24), get_byte(x, 16), get_byte(x, 8), band(x, 0xFF);
end
+if s_pack then
+ function pack_uint16be(x)
+ return s_pack(">I2", x);
+ end
+ function pack_uint64be(x)
+ return s_pack(">I8", x);
+ end
+end
+
+if s_unpack then
+ function read_uint16be(str, pos)
+ return s_unpack(">I2", str, pos);
+ end
+ function read_uint64be(str, pos)
+ return s_unpack(">I8", str, pos);
+ end
+end
+
local function parse_frame_header(frame)
if #frame < 2 then return; end