Software /
code /
prosody
Comparison
net/websocket.lua @ 6407:4bbd198cf3e6
net.websocket: Fix handling of 'protocol' argument
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 11 Sep 2014 00:55:51 +0200 |
parent | 6398:ad434f47bfc0 |
child | 6455:b6514e691a70 |
comparison
equal
deleted
inserted
replaced
6406:f04a6c700b2f | 6407:4bbd198cf3e6 |
---|---|
188 local key = base64.encode(random_bytes(16)); | 188 local key = base64.encode(random_bytes(16)); |
189 | 189 |
190 -- Either a single protocol string or an array of protocol strings. | 190 -- Either a single protocol string or an array of protocol strings. |
191 local protocol = ex.protocol; | 191 local protocol = ex.protocol; |
192 if type(protocol) == "string" then | 192 if type(protocol) == "string" then |
193 protocol = { protocol }; | 193 protocol = { protocol, [protocol] = true }; |
194 end | 194 elseif type(protocol) == "table" and protocol[1] then |
195 for _, v in ipairs(protocol) do | 195 for _, v in ipairs(protocol) do |
196 protocol[v] = true; | 196 protocol[v] = true; |
197 end | |
198 else | |
199 protocol = nil; | |
197 end | 200 end |
198 | 201 |
199 local headers = { | 202 local headers = { |
200 ["Upgrade"] = "websocket"; | 203 ["Upgrade"] = "websocket"; |
201 ["Connection"] = "Upgrade"; | 204 ["Connection"] = "Upgrade"; |
202 ["Sec-WebSocket-Key"] = key; | 205 ["Sec-WebSocket-Key"] = key; |
203 ["Sec-WebSocket-Protocol"] = t_concat(protocol, ", "); | 206 ["Sec-WebSocket-Protocol"] = protocol and t_concat(protocol, ", "); |
204 ["Sec-WebSocket-Version"] = "13"; | 207 ["Sec-WebSocket-Version"] = "13"; |
205 ["Sec-WebSocket-Extensions"] = ex.extensions; | 208 ["Sec-WebSocket-Extensions"] = ex.extensions; |
206 } | 209 } |
207 if ex.headers then | 210 if ex.headers then |
208 for k,v in pairs(ex.headers) do | 211 for k,v in pairs(ex.headers) do |
236 }, function(b, c, r, http_req) | 239 }, function(b, c, r, http_req) |
237 if c ~= 101 | 240 if c ~= 101 |
238 or r.headers["connection"]:lower() ~= "upgrade" | 241 or r.headers["connection"]:lower() ~= "upgrade" |
239 or r.headers["upgrade"] ~= "websocket" | 242 or r.headers["upgrade"] ~= "websocket" |
240 or r.headers["sec-websocket-accept"] ~= base64.encode(sha1(key .. "258EAFA5-E914-47DA-95CA-C5AB0DC85B11")) | 243 or r.headers["sec-websocket-accept"] ~= base64.encode(sha1(key .. "258EAFA5-E914-47DA-95CA-C5AB0DC85B11")) |
241 or not protocol[r.headers["sec-websocket-protocol"]] | 244 or (protocol and not protocol[r.headers["sec-websocket-protocol"]]) |
242 then | 245 then |
243 s.readyState = 3; | 246 s.readyState = 3; |
244 log("warn", "WebSocket connection to %s failed: %s", url, tostring(b)); | 247 log("warn", "WebSocket connection to %s failed: %s", url, tostring(b)); |
245 if s.onerror then s:onerror("connecting-failed"); end | 248 if s.onerror then s:onerror("connecting-failed"); end |
246 return; | 249 return; |