Mercurial > prosody-modules
comparison mod_websocket/mod_websocket.lua @ 687:d141375ece4b
mod_websocket: Move frame building into a function
author | Florian Zeitz <florob@babelmonkeys.de> |
---|---|
date | Sun, 27 May 2012 17:09:19 +0200 |
parents | eeb41cd5e9f3 |
children | e87678a52720 |
comparison
equal
deleted
inserted
replaced
686:592cfa6cf5d9 | 687:d141375ece4b |
---|---|
84 counter = (counter + 1) % 4; | 84 counter = (counter + 1) % 4; |
85 end | 85 end |
86 else | 86 else |
87 result.data = frame:sub(pos + 1, pos + result.length); | 87 result.data = frame:sub(pos + 1, pos + result.length); |
88 end | 88 end |
89 | |
90 return result; | |
91 end | |
92 | |
93 local function build_frame(desc) | |
94 local length; | |
95 local result = ""; | |
96 | |
97 result = result .. string.char(0x80 * (desc.FIN and 1 or 0) + desc.opcode); | |
98 | |
99 length = #desc.data; | |
100 if length <= 125 then -- 7-bit length | |
101 result = result .. string.char(length); | |
102 elseif length <= 0xFFFF then -- 2-byte length | |
103 result = result .. string.char(126); | |
104 result = result .. string.char(length/0x100) .. string.char(length%0x100); | |
105 else -- 8-byte length | |
106 result = result .. string.char(127); | |
107 for i = 7, 0, -1 do | |
108 result = result .. string.char(( length / (2^(8*i)) ) % 0x100); | |
109 end | |
110 end | |
111 | |
112 result = result .. desc.data; | |
89 | 113 |
90 return result; | 114 return result; |
91 end | 115 end |
92 | 116 |
93 --- Stream events handlers | 117 --- Stream events handlers |
250 session:close("not-well-formed"); | 274 session:close("not-well-formed"); |
251 end | 275 end |
252 end | 276 end |
253 | 277 |
254 function session.send(s) | 278 function session.send(s) |
255 s = tostring(s); | 279 conn:write(build_frame({ FIN = true, opcode = 0x01, data = tostring(s)})); |
256 local len = #s; | |
257 if len < 126 then | |
258 conn:write("\x81" .. string.char(len) .. s); | |
259 elseif len <= 0xffff then | |
260 conn:write("\x81" .. string.char(126) .. string.char(len/0x100) .. string.char(len%0x100) .. s); | |
261 else | |
262 conn:write("\x81" .. string.char(127) .. string.char(len/0x100000000000000) | |
263 .. string.char((len%0x100000000000000)/0x1000000000000) .. string.char((len%0x1000000000000)/0x10000000000) | |
264 .. string.char((len%0x10000000000)/0x100000000) .. string.char((len%0x100000000)/0x1000000) | |
265 .. string.char((len%0x1000000)/0x10000) .. string.char((len%0x10000)/0x100) | |
266 .. string.char((len%0x100))) | |
267 end | |
268 end | 280 end |
269 | 281 |
270 if c2s_timeout then | 282 if c2s_timeout then |
271 add_task(c2s_timeout, function () | 283 add_task(c2s_timeout, function () |
272 if session.type == "c2s_unauthed" then | 284 if session.type == "c2s_unauthed" then |