Mercurial > prosody-modules
comparison mod_websocket/mod_websocket.lua @ 1028:81065638299d
mod_websocket: Access some values via locales instead of through tables
author | Florian Zeitz <florob@babelmonkeys.de> |
---|---|
date | Thu, 30 May 2013 23:49:14 +0200 |
parents | 6a2dfa8af421 |
children | 9d85aded2fb6 |
comparison
equal
deleted
inserted
replaced
1027:6a2dfa8af421 | 1028:81065638299d |
---|---|
80 if #frame < (2 + length_bytes + (result.MASK and 4 or 0) + result.length) then return; end | 80 if #frame < (2 + length_bytes + (result.MASK and 4 or 0) + result.length) then return; end |
81 | 81 |
82 if result.MASK then | 82 if result.MASK then |
83 local counter = 0; | 83 local counter = 0; |
84 local data = {}; | 84 local data = {}; |
85 result.key = {s_byte(frame, pos+1), s_byte(frame, pos+2), | 85 local key = {s_byte(frame, pos+1), s_byte(frame, pos+2), |
86 s_byte(frame, pos+3), s_byte(frame, pos+4)} | 86 s_byte(frame, pos+3), s_byte(frame, pos+4)} |
87 result.key = key; | |
87 | 88 |
88 pos = pos + 5; | 89 pos = pos + 5; |
89 for i = pos, pos + result.length - 1 do | 90 for i = pos, pos + result.length - 1 do |
90 data[#data+1] = s_char(bxor(result.key[counter+1], s_byte(frame, i))); | 91 data[#data+1] = s_char(bxor(key[counter+1], s_byte(frame, i))); |
91 counter = (counter + 1) % 4; | 92 counter = (counter + 1) % 4; |
92 end | 93 end |
93 result.data = t_concat(data, ""); | 94 result.data = t_concat(data, ""); |
94 else | 95 else |
95 result.data = frame:sub(pos + 1, pos + result.length); | 96 result.data = frame:sub(pos + 1, pos + result.length); |
153 conn:close(); | 154 conn:close(); |
154 end | 155 end |
155 | 156 |
156 local dataBuffer; | 157 local dataBuffer; |
157 local function handle_frame(frame) | 158 local function handle_frame(frame) |
159 local opcode = frame.opcode; | |
160 local length = frame.length; | |
158 module:log("debug", "Websocket received: %s (%i bytes)", frame.data, #frame.data); | 161 module:log("debug", "Websocket received: %s (%i bytes)", frame.data, #frame.data); |
159 | 162 |
160 -- Error cases | 163 -- Error cases |
161 if frame.RSV1 or frame.RSV2 or frame.RSV3 then -- Reserved bits non zero | 164 if frame.RSV1 or frame.RSV2 or frame.RSV3 then -- Reserved bits non zero |
162 websocket_close(1002, "Reserved bits not zero"); | 165 websocket_close(1002, "Reserved bits not zero"); |
163 return false; | 166 return false; |
164 end | 167 end |
165 | 168 |
166 if frame.opcode >= 0x8 and frame.length > 125 then -- Control frame with too much payload | 169 if opcode >= 0x8 and length > 125 then -- Control frame with too much payload |
167 websocket_close(1002, "Payload too large"); | 170 websocket_close(1002, "Payload too large"); |
168 return false; | 171 return false; |
169 end | 172 end |
170 | 173 |
171 if frame.opcode >= 0x8 and not frame.FIN then -- Fragmented control frame | 174 if opcode >= 0x8 and not frame.FIN then -- Fragmented control frame |
172 websocket_close(1002, "Fragmented control frame"); | 175 websocket_close(1002, "Fragmented control frame"); |
173 return false; | 176 return false; |
174 end | 177 end |
175 | 178 |
176 if (frame.opcode > 0x2 and frame.opcode < 0x8) or (frame.opcode > 0xA) then | 179 if (opcode > 0x2 and opcode < 0x8) or (opcode > 0xA) then |
177 websocket_close(1002, "Reserved opcode"); | 180 websocket_close(1002, "Reserved opcode"); |
178 return false; | 181 return false; |
179 end | 182 end |
180 | 183 |
181 if frame.opcode == 0x0 and not dataBuffer then | 184 if opcode == 0x0 and not dataBuffer then |
182 websocket_close(1002, "Unexpected continuation frame"); | 185 websocket_close(1002, "Unexpected continuation frame"); |
183 return false; | 186 return false; |
184 end | 187 end |
185 | 188 |
186 if (frame.opcode == 0x1 or frame.opcode == 0x2) and dataBuffer then | 189 if (opcode == 0x1 or opcode == 0x2) and dataBuffer then |
187 websocket_close(1002, "Continuation frame expected"); | 190 websocket_close(1002, "Continuation frame expected"); |
188 return false; | 191 return false; |
189 end | 192 end |
190 | 193 |
191 -- Valid cases | 194 -- Valid cases |
192 if frame.opcode == 0x0 then -- Continuation frame | 195 if opcode == 0x0 then -- Continuation frame |
193 dataBuffer[#dataBuffer+1] = frame.data; | 196 dataBuffer[#dataBuffer+1] = frame.data; |
194 elseif frame.opcode == 0x1 then -- Text frame | 197 elseif opcode == 0x1 then -- Text frame |
195 dataBuffer = {frame.data}; | 198 dataBuffer = {frame.data}; |
196 elseif frame.opcode == 0x2 then -- Binary frame | 199 elseif opcode == 0x2 then -- Binary frame |
197 websocket_close(1003, "Only text frames are supported"); | 200 websocket_close(1003, "Only text frames are supported"); |
198 return; | 201 return; |
199 elseif frame.opcode == 0x8 then -- Close request | 202 elseif opcode == 0x8 then -- Close request |
200 websocket_close(1000, "Goodbye"); | 203 websocket_close(1000, "Goodbye"); |
201 return; | 204 return; |
202 elseif frame.opcode == 0x9 then -- Ping frame | 205 elseif opcode == 0x9 then -- Ping frame |
203 frame.opcode = 0xA; | 206 frame.opcode = 0xA; |
204 conn:write(build_frame(frame)); | 207 conn:write(build_frame(frame)); |
205 return ""; | 208 return ""; |
206 else | 209 else |
207 log("warn", "Received frame with unsupported opcode %i", frame.opcode); | 210 log("warn", "Received frame with unsupported opcode %i", opcode); |
208 return ""; | 211 return ""; |
209 end | 212 end |
210 | 213 |
211 if frame.FIN then | 214 if frame.FIN then |
212 local data = t_concat(dataBuffer, ""); | 215 local data = t_concat(dataBuffer, ""); |