|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local basexx = require "basexx" |
|
|
local spack = string.pack or require "compat53.string".pack |
|
|
local sunpack = string.unpack or require "compat53.string".unpack |
|
|
local unpack = table.unpack or unpack |
|
|
local utf8 = utf8 or require "compat53.utf8" |
|
|
local cqueues = require "cqueues" |
|
|
local monotime = cqueues.monotime |
|
|
local ce = require "cqueues.errno" |
|
|
local lpeg = require "lpeg" |
|
|
local http_patts = require "lpeg_patterns.http" |
|
|
local rand = require "openssl.rand" |
|
|
local digest = require "openssl.digest" |
|
|
local bit = require "http.bit" |
|
|
local onerror = require "http.connection_common".onerror |
|
|
local new_headers = require "http.headers".new |
|
|
local http_request = require "http.request" |
|
|
|
|
|
local EOF = lpeg.P(-1) |
|
|
local Connection = lpeg.Ct(http_patts.Connection) * EOF |
|
|
local Sec_WebSocket_Protocol_Client = lpeg.Ct(http_patts.Sec_WebSocket_Protocol_Client) * EOF |
|
|
local Sec_WebSocket_Extensions = lpeg.Ct(http_patts.Sec_WebSocket_Extensions) * EOF |
|
|
|
|
|
local websocket_methods = { |
|
|
|
|
|
close_timeout = 3; |
|
|
} |
|
|
|
|
|
local websocket_mt = { |
|
|
__name = "http.websocket"; |
|
|
__index = websocket_methods; |
|
|
} |
|
|
|
|
|
function websocket_mt:__tostring() |
|
|
return string.format("http.websocket{type=%q;readyState=%d}", |
|
|
self.type, self.readyState) |
|
|
end |
|
|
|
|
|
local magic = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" |
|
|
|
|
|
|
|
|
local function new_key() |
|
|
return basexx.to_base64(rand.bytes(16)) |
|
|
end |
|
|
|
|
|
local function base64_sha1(str) |
|
|
return basexx.to_base64(digest.new("sha1"):final(str)) |
|
|
end |
|
|
|
|
|
|
|
|
local function trim(s) |
|
|
local from = s:match"^%s*()" |
|
|
return from > #s and "" or s:match(".*%S", from) |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
local function validate_protocol(p) |
|
|
return p:match("^[\33\35-\39\42\43\45\46\48-\57\65-\90\94-\122\124\126\127]+$") |
|
|
end |
|
|
|
|
|
local function validate_utf8(s) |
|
|
local ok, pos = utf8.len(s) |
|
|
if not ok then |
|
|
return nil, pos |
|
|
end |
|
|
|
|
|
for p, c in utf8.codes(s) do |
|
|
if c >= 0xD800 and c <= 0xDFFF then |
|
|
return nil, p |
|
|
end |
|
|
end |
|
|
return true |
|
|
end |
|
|
|
|
|
|
|
|
local function apply_mask(str, key) |
|
|
assert(#key == 4) |
|
|
local data = {} |
|
|
for i = 1, #str do |
|
|
local key_index = (i-1)%4 + 1 |
|
|
data[i] = string.char(bit.bxor(key[key_index], str:byte(i))) |
|
|
end |
|
|
return table.concat(data, "", 1, #str) |
|
|
end |
|
|
|
|
|
local function build_frame(desc) |
|
|
local data = desc.data or "" |
|
|
|
|
|
assert(desc.opcode and desc.opcode >= 0 and desc.opcode <= 0xF, "Invalid WebSocket opcode") |
|
|
if desc.opcode >= 0x8 then |
|
|
|
|
|
assert(#data <= 125, "WebSocket control frames MUST have a payload length of 125 bytes or less.") |
|
|
end |
|
|
|
|
|
local b1 = desc.opcode |
|
|
if desc.FIN then |
|
|
b1 = bit.bor(b1, 0x80) |
|
|
end |
|
|
if desc.RSV1 then |
|
|
b1 = bit.bor(b1, 0x40) |
|
|
end |
|
|
if desc.RSV2 then |
|
|
b1 = bit.bor(b1, 0x20) |
|
|
end |
|
|
if desc.RSV3 then |
|
|
b1 = bit.bor(b1, 0x10) |
|
|
end |
|
|
|
|
|
local b2 = #data |
|
|
local length_extra |
|
|
if b2 <= 125 then |
|
|
length_extra = "" |
|
|
elseif b2 <= 0xFFFF then |
|
|
b2 = 126 |
|
|
length_extra = spack(">I2", #data) |
|
|
else |
|
|
b2 = 127 |
|
|
length_extra = spack(">I8", #data) |
|
|
end |
|
|
|
|
|
local key = "" |
|
|
if desc.MASK then |
|
|
local key_a = desc.key |
|
|
if key_a then |
|
|
key = string.char(unpack(key_a, 1, 4)) |
|
|
else |
|
|
key = rand.bytes(4) |
|
|
key_a = {key:byte(1,4)} |
|
|
end |
|
|
b2 = bit.bor(b2, 0x80) |
|
|
data = apply_mask(data, key_a) |
|
|
end |
|
|
|
|
|
return string.char(b1, b2) .. length_extra .. key .. data |
|
|
end |
|
|
|
|
|
local function build_close(code, message, mask) |
|
|
local data |
|
|
if code then |
|
|
data = spack(">I2", code) |
|
|
if message then |
|
|
assert(#message<=123, "Close reason must be <=123 bytes") |
|
|
data = data .. message |
|
|
end |
|
|
else |
|
|
data = "" |
|
|
end |
|
|
return { |
|
|
opcode = 0x8; |
|
|
FIN = true; |
|
|
MASK = mask; |
|
|
data = data; |
|
|
} |
|
|
end |
|
|
|
|
|
local function read_frame(sock, deadline) |
|
|
local frame, first_2 do |
|
|
local err, errno |
|
|
first_2, err, errno = sock:xread(2, "b", deadline and (deadline-monotime())) |
|
|
if not first_2 then |
|
|
return nil, err, errno |
|
|
elseif #first_2 ~= 2 then |
|
|
sock:seterror("r", ce.EILSEQ) |
|
|
local ok, errno2 = sock:unget(first_2) |
|
|
if not ok then |
|
|
return nil, onerror(sock, "unget", errno2) |
|
|
end |
|
|
return nil, onerror(sock, "read_frame", ce.EILSEQ) |
|
|
end |
|
|
local byte1, byte2 = first_2:byte(1, 2) |
|
|
frame = { |
|
|
FIN = bit.band(byte1, 0x80) ~= 0; |
|
|
RSV1 = bit.band(byte1, 0x40) ~= 0; |
|
|
RSV2 = bit.band(byte1, 0x20) ~= 0; |
|
|
RSV3 = bit.band(byte1, 0x10) ~= 0; |
|
|
opcode = bit.band(byte1, 0x0F); |
|
|
|
|
|
MASK = bit.band(byte2, 0x80) ~= 0; |
|
|
length = bit.band(byte2, 0x7F); |
|
|
|
|
|
data = nil; |
|
|
} |
|
|
end |
|
|
|
|
|
local fill_length = frame.length |
|
|
if fill_length == 126 then |
|
|
fill_length = 2 |
|
|
elseif fill_length == 127 then |
|
|
fill_length = 8 |
|
|
end |
|
|
if frame.MASK then |
|
|
fill_length = fill_length + 4 |
|
|
end |
|
|
do |
|
|
local ok, err, errno = sock:fill(fill_length, 0) |
|
|
if not ok then |
|
|
local unget_ok1, unget_errno1 = sock:unget(first_2) |
|
|
if not unget_ok1 then |
|
|
return nil, onerror(sock, "unget", unget_errno1) |
|
|
end |
|
|
if errno == ce.ETIMEDOUT then |
|
|
local timeout = deadline and deadline-monotime() |
|
|
if cqueues.poll(sock, timeout) ~= timeout then |
|
|
|
|
|
return read_frame(sock, deadline) |
|
|
end |
|
|
elseif err == nil then |
|
|
sock:seterror("r", ce.EILSEQ) |
|
|
return nil, onerror(sock, "read_frame", ce.EILSEQ) |
|
|
end |
|
|
return nil, err, errno |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
local extra_fill_unget |
|
|
if frame.length == 126 then |
|
|
extra_fill_unget = assert(sock:xread(2, "b", 0)) |
|
|
frame.length = sunpack(">I2", extra_fill_unget) |
|
|
fill_length = fill_length - 2 |
|
|
elseif frame.length == 127 then |
|
|
extra_fill_unget = assert(sock:xread(8, "b", 0)) |
|
|
frame.length = sunpack(">I8", extra_fill_unget) |
|
|
fill_length = fill_length - 8 + frame.length |
|
|
end |
|
|
|
|
|
if extra_fill_unget then |
|
|
local ok, err, errno = sock:fill(fill_length, 0) |
|
|
if not ok then |
|
|
local unget_ok1, unget_errno1 = sock:unget(extra_fill_unget) |
|
|
if not unget_ok1 then |
|
|
return nil, onerror(sock, "unget", unget_errno1) |
|
|
end |
|
|
local unget_ok2, unget_errno2 = sock:unget(first_2) |
|
|
if not unget_ok2 then |
|
|
return nil, onerror(sock, "unget", unget_errno2) |
|
|
end |
|
|
if errno == ce.ETIMEDOUT then |
|
|
local timeout = deadline and deadline-monotime() |
|
|
if cqueues.poll(sock, timeout) ~= timeout then |
|
|
|
|
|
return read_frame(sock, deadline) |
|
|
end |
|
|
elseif err == nil then |
|
|
sock:seterror("r", ce.EILSEQ) |
|
|
return nil, onerror(sock, "read_frame", ce.EILSEQ) |
|
|
end |
|
|
return nil, err, errno |
|
|
end |
|
|
end |
|
|
|
|
|
if frame.MASK then |
|
|
local key = assert(sock:xread(4, "b", 0)) |
|
|
frame.key = { key:byte(1, 4) } |
|
|
end |
|
|
|
|
|
do |
|
|
local data = assert(sock:xread(frame.length, "b", 0)) |
|
|
if frame.MASK then |
|
|
frame.data = apply_mask(data, frame.key) |
|
|
else |
|
|
frame.data = data |
|
|
end |
|
|
end |
|
|
|
|
|
return frame |
|
|
end |
|
|
|
|
|
local function parse_close(data) |
|
|
local code, message |
|
|
if #data >= 2 then |
|
|
code = sunpack(">I2", data) |
|
|
if #data > 2 then |
|
|
message = data:sub(3) |
|
|
end |
|
|
end |
|
|
return code, message |
|
|
end |
|
|
|
|
|
function websocket_methods:send_frame(frame, timeout) |
|
|
if self.readyState < 1 then |
|
|
return nil, onerror(self.socket, "send_frame", ce.ENOTCONN) |
|
|
elseif self.readyState > 2 then |
|
|
return nil, onerror(self.socket, "send_frame", ce.EPIPE) |
|
|
end |
|
|
local ok, err, errno = self.socket:xwrite(build_frame(frame), "bn", timeout) |
|
|
if not ok then |
|
|
return nil, err, errno |
|
|
end |
|
|
if frame.opcode == 0x8 then |
|
|
self.readyState = 2 |
|
|
end |
|
|
return true |
|
|
end |
|
|
|
|
|
function websocket_methods:send(data, opcode, timeout) |
|
|
assert(type(data) == "string") |
|
|
if opcode == "text" or opcode == nil then |
|
|
opcode = 0x1 |
|
|
elseif opcode == "binary" then |
|
|
opcode = 0x2; |
|
|
end |
|
|
return self:send_frame({ |
|
|
FIN = true; |
|
|
|
|
|
|
|
|
|
|
|
MASK = self.type == "client"; |
|
|
opcode = opcode; |
|
|
data = data; |
|
|
}, timeout) |
|
|
end |
|
|
|
|
|
local function close_helper(self, code, reason, deadline) |
|
|
if self.readyState < 1 then |
|
|
self.request = nil |
|
|
self.stream = nil |
|
|
self.readyState = 3 |
|
|
|
|
|
return nil, ce.strerror(ce.ENOTCONN), ce.ENOTCONN |
|
|
elseif self.readyState == 3 then |
|
|
return nil, ce.strerror(ce.EPIPE), ce.EPIPE |
|
|
end |
|
|
|
|
|
if self.readyState < 2 then |
|
|
local close_frame = build_close(code, reason, self.type == "client") |
|
|
|
|
|
self:send_frame(close_frame, deadline and deadline-monotime()) |
|
|
end |
|
|
|
|
|
if code ~= 1002 and not self.got_close_code and self.readyState == 2 then |
|
|
|
|
|
local read_deadline = monotime() + self.close_timeout |
|
|
if deadline then |
|
|
read_deadline = math.min(read_deadline, deadline) |
|
|
end |
|
|
repeat |
|
|
if not self:receive(read_deadline-monotime()) then |
|
|
break |
|
|
end |
|
|
until self.got_close_code |
|
|
end |
|
|
|
|
|
if self.readyState < 3 then |
|
|
self.socket:shutdown() |
|
|
self.readyState = 3 |
|
|
cqueues.poll() |
|
|
cqueues.poll() |
|
|
self.socket:close() |
|
|
end |
|
|
|
|
|
return nil, reason, code |
|
|
end |
|
|
|
|
|
function websocket_methods:close(code, reason, timeout) |
|
|
local deadline = timeout and (monotime()+timeout) |
|
|
code = code or 1000 |
|
|
close_helper(self, code, reason, deadline) |
|
|
return true |
|
|
end |
|
|
|
|
|
function websocket_methods:receive(timeout) |
|
|
if self.readyState < 1 then |
|
|
return nil, onerror(self.socket, "receive", ce.ENOTCONN) |
|
|
elseif self.readyState > 2 then |
|
|
return nil, onerror(self.socket, "receive", ce.EPIPE) |
|
|
end |
|
|
local deadline = timeout and (monotime()+timeout) |
|
|
while true do |
|
|
local frame, err, errno = read_frame(self.socket, deadline) |
|
|
if frame == nil then |
|
|
return nil, err, errno |
|
|
end |
|
|
|
|
|
|
|
|
if frame.RSV1 or frame.RSV2 or frame.RSV3 then |
|
|
return close_helper(self, 1002, "Reserved bits not zero", deadline) |
|
|
end |
|
|
|
|
|
if frame.opcode < 0x8 then |
|
|
if frame.opcode == 0x0 then |
|
|
if not self.databuffer then |
|
|
return close_helper(self, 1002, "Unexpected continuation frame", deadline) |
|
|
end |
|
|
self.databuffer[#self.databuffer+1] = frame.data |
|
|
elseif frame.opcode == 0x1 or frame.opcode == 0x2 then |
|
|
if self.databuffer then |
|
|
return close_helper(self, 1002, "Continuation frame expected", deadline) |
|
|
end |
|
|
self.databuffer = { frame.data } |
|
|
self.databuffer_type = frame.opcode |
|
|
else |
|
|
return close_helper(self, 1002, "Reserved opcode", deadline) |
|
|
end |
|
|
if frame.FIN then |
|
|
local databuffer_type = self.databuffer_type |
|
|
local databuffer = table.concat(self.databuffer) |
|
|
if databuffer_type == 0x1 then |
|
|
databuffer_type = "text" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local valid_utf8, err_pos = validate_utf8(databuffer) |
|
|
if not valid_utf8 then |
|
|
return close_helper(self, 1007, string.format("invalid utf-8 at position %d", err_pos)) |
|
|
end |
|
|
elseif databuffer_type == 0x2 then |
|
|
databuffer_type = "binary" |
|
|
end |
|
|
self.databuffer_type, self.databuffer = nil, nil |
|
|
return databuffer, databuffer_type |
|
|
end |
|
|
else |
|
|
if frame.length > 125 then |
|
|
return close_helper(self, 1002, "Payload too large", deadline) |
|
|
elseif not frame.FIN then |
|
|
return close_helper(self, 1002, "Fragmented control frame", deadline) |
|
|
end |
|
|
if frame.opcode == 0x8 then |
|
|
if frame.length == 1 then |
|
|
return close_helper(self, 1002, "Close frame with payload, but too short for status code", deadline) |
|
|
end |
|
|
local status_code, message = parse_close(frame.data) |
|
|
if status_code == nil then |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.got_close_code = 1005 |
|
|
status_code = 1000 |
|
|
elseif status_code < 1000 then |
|
|
self.got_close_code = true |
|
|
return close_helper(self, 1002, "Closed with invalid status code", deadline) |
|
|
elseif ((status_code > 1003 and status_code < 1007) or status_code > 1011) and status_code < 3000 then |
|
|
self.got_close_code = true |
|
|
return close_helper(self, 1002, "Closed with reserved status code", deadline) |
|
|
else |
|
|
self.got_close_code = status_code |
|
|
if message then |
|
|
local valid_utf8, err_pos = validate_utf8(message) |
|
|
if not valid_utf8 then |
|
|
return close_helper(self, 1007, string.format("invalid utf-8 at position %d", err_pos)) |
|
|
end |
|
|
self.got_close_message = message |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
return close_helper(self, status_code, message, deadline) |
|
|
elseif frame.opcode == 0x9 then |
|
|
local ok, err2 = self:send_pong(frame.data, deadline and (deadline-monotime())) |
|
|
if not ok and err2 ~= ce.EPIPE then |
|
|
return close_helper(self, 1002, "Pong failed", deadline) |
|
|
end |
|
|
elseif frame.opcode == 0xA then |
|
|
|
|
|
else |
|
|
return close_helper(self, 1002, "Reserved opcode", deadline) |
|
|
end |
|
|
end |
|
|
end |
|
|
end |
|
|
|
|
|
function websocket_methods:each() |
|
|
return function(self) |
|
|
return self:receive() |
|
|
end, self |
|
|
end |
|
|
|
|
|
function websocket_methods:send_ping(data, timeout) |
|
|
return self:send_frame({ |
|
|
FIN = true; |
|
|
|
|
|
|
|
|
|
|
|
MASK = self.type == "client"; |
|
|
opcode = 0x9; |
|
|
data = data; |
|
|
}, timeout) |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function websocket_methods:send_pong(data, timeout) |
|
|
return self:send_frame({ |
|
|
FIN = true; |
|
|
|
|
|
|
|
|
|
|
|
MASK = self.type == "client"; |
|
|
opcode = 0xA; |
|
|
data = data; |
|
|
}, timeout) |
|
|
end |
|
|
|
|
|
local function new(type) |
|
|
assert(type == "client" or type == "server") |
|
|
local self = setmetatable({ |
|
|
socket = nil; |
|
|
type = type; |
|
|
readyState = 0; |
|
|
databuffer = nil; |
|
|
databuffer_type = nil; |
|
|
got_close_code = nil; |
|
|
got_close_reason = nil; |
|
|
key = nil; |
|
|
protocol = nil; |
|
|
protocols = nil; |
|
|
|
|
|
request = nil; |
|
|
headers = nil; |
|
|
|
|
|
stream = nil; |
|
|
}, websocket_mt) |
|
|
return self |
|
|
end |
|
|
|
|
|
local function new_from_uri(uri, protocols) |
|
|
local request = http_request.new_from_uri(uri) |
|
|
local self = new("client") |
|
|
self.request = request |
|
|
self.request.version = 1.1 |
|
|
self.request.headers:append("upgrade", "websocket") |
|
|
self.request.headers:append("connection", "upgrade") |
|
|
self.key = new_key() |
|
|
self.request.headers:append("sec-websocket-key", self.key, true) |
|
|
self.request.headers:append("sec-websocket-version", "13") |
|
|
if protocols then |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local n_protocols = #protocols |
|
|
|
|
|
local protocols_copy = {} |
|
|
for i=1, n_protocols do |
|
|
local v = protocols[i] |
|
|
if protocols_copy[v] then |
|
|
error("duplicate protocol") |
|
|
end |
|
|
assert(validate_protocol(v), "invalid protocol") |
|
|
protocols_copy[v] = true |
|
|
protocols_copy[i] = v |
|
|
end |
|
|
self.protocols = protocols_copy |
|
|
self.request.headers:append("sec-websocket-protocol", table.concat(protocols_copy, ",", 1, n_protocols)) |
|
|
end |
|
|
return self |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
local function handle_websocket_response(self, headers, stream) |
|
|
assert(self.type == "client" and self.readyState == 0) |
|
|
|
|
|
if stream.connection.version < 1 or stream.connection.version >= 2 then |
|
|
return nil, "websockets only supported with HTTP 1.x", ce.EINVAL |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if headers:get(":status") ~= "101" then |
|
|
return nil, "status code not 101", ce.EINVAL |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local upgrade = headers:get("upgrade") |
|
|
if not upgrade or upgrade:lower() ~= "websocket" then |
|
|
return nil, "upgrade header not websocket", ce.EINVAL |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
do |
|
|
local has_connection_upgrade = false |
|
|
local h = headers:get_comma_separated("connection") |
|
|
if not h then |
|
|
return nil, "invalid connection header", ce.EINVAL |
|
|
end |
|
|
local connection_header = Connection:match(h) |
|
|
for i=1, #connection_header do |
|
|
if connection_header[i] == "upgrade" then |
|
|
has_connection_upgrade = true |
|
|
break |
|
|
end |
|
|
end |
|
|
if not has_connection_upgrade then |
|
|
return nil, "connection header doesn't contain upgrade", ce.EINVAL |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local sec_websocket_accept = headers:get("sec-websocket-accept") |
|
|
if sec_websocket_accept == nil or |
|
|
trim(sec_websocket_accept) ~= base64_sha1(self.key .. magic) |
|
|
then |
|
|
return nil, "sec-websocket-accept header incorrect", ce.EINVAL |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
do |
|
|
local h = headers:get_comma_separated("sec-websocket-extensions") |
|
|
if h then |
|
|
local extensions = Sec_WebSocket_Extensions:match(h) |
|
|
if not extensions then |
|
|
return nil, "invalid sec-websocket-extensions header", ce.EINVAL |
|
|
end |
|
|
return nil, "extensions not supported", ce.EINVAL |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local protocol = headers:get("sec-websocket-protocol") |
|
|
if protocol then |
|
|
local has_matching_protocol = self.protocols and self.protocols[protocol] |
|
|
if not has_matching_protocol then |
|
|
return nil, "unexpected protocol", ce.EINVAL |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
assert(self.socket == nil, "websocket:connect called twice") |
|
|
self.socket = assert(stream.connection:take_socket()) |
|
|
self.socket:onerror(onerror) |
|
|
self.request = nil |
|
|
self.headers = headers |
|
|
self.readyState = 1 |
|
|
self.protocol = protocol |
|
|
|
|
|
return true |
|
|
end |
|
|
|
|
|
function websocket_methods:connect(timeout) |
|
|
assert(self.type == "client" and self.readyState == 0) |
|
|
local headers, stream, errno = self.request:go(timeout) |
|
|
if not headers then |
|
|
return nil, stream, errno |
|
|
end |
|
|
return handle_websocket_response(self, headers, stream) |
|
|
end |
|
|
|
|
|
|
|
|
local function new_from_stream(stream, headers) |
|
|
assert(stream.connection.type == "server") |
|
|
|
|
|
if stream.connection.version < 1 or stream.connection.version >= 2 then |
|
|
return nil, "websockets only supported with HTTP 1.x", ce.EINVAL |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
if stream.peer_version == 1.0 then |
|
|
return nil, "upgrade headers MUST be ignored in HTTP 1.0", ce.EINVAL |
|
|
end |
|
|
local upgrade = headers:get("upgrade") |
|
|
if not upgrade or upgrade:lower() ~= "websocket" then |
|
|
return nil, "upgrade header not websocket", ce.EINVAL |
|
|
end |
|
|
|
|
|
do |
|
|
local has_connection_upgrade = false |
|
|
local h = headers:get_comma_separated("connection") |
|
|
if not h then |
|
|
return nil, "invalid connection header", ce.EINVAL |
|
|
end |
|
|
local connection_header = Connection:match(h) |
|
|
for i=1, #connection_header do |
|
|
if connection_header[i] == "upgrade" then |
|
|
has_connection_upgrade = true |
|
|
break |
|
|
end |
|
|
end |
|
|
if not has_connection_upgrade then |
|
|
return nil, "connection header doesn't contain upgrade", ce.EINVAL |
|
|
end |
|
|
end |
|
|
|
|
|
local key = headers:get("sec-websocket-key") |
|
|
if not key then |
|
|
return nil, "missing sec-websocket-key", ce.EINVAL |
|
|
end |
|
|
key = trim(key) |
|
|
|
|
|
if headers:get("sec-websocket-version") ~= "13" then |
|
|
return nil, "unsupported sec-websocket-version", ce.EINVAL |
|
|
end |
|
|
|
|
|
local protocols_available |
|
|
if headers:has("sec-websocket-protocol") then |
|
|
local h = headers:get_comma_separated("sec-websocket-protocol") |
|
|
local client_protocols = Sec_WebSocket_Protocol_Client:match(h) |
|
|
if not client_protocols then |
|
|
return nil, "invalid sec-websocket-protocol header", ce.EINVAL |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protocols_available = {} |
|
|
for i, protocol in ipairs(client_protocols) do |
|
|
protocol = trim(protocol) |
|
|
if protocols_available[protocol] then |
|
|
return nil, "duplicate protocol", ce.EINVAL |
|
|
end |
|
|
if not validate_protocol(protocol) then |
|
|
return nil, "invalid protocol", ce.EINVAL |
|
|
end |
|
|
protocols_available[protocol] = true |
|
|
protocols_available[i] = protocol |
|
|
end |
|
|
end |
|
|
|
|
|
local self = new("server") |
|
|
self.key = key |
|
|
self.protocols = protocols_available |
|
|
self.stream = stream |
|
|
return self |
|
|
end |
|
|
|
|
|
function websocket_methods:accept(options, timeout) |
|
|
assert(self.type == "server" and self.readyState == 0) |
|
|
options = options or {} |
|
|
|
|
|
local response_headers |
|
|
if options.headers then |
|
|
response_headers = options.headers:clone() |
|
|
else |
|
|
response_headers = new_headers() |
|
|
end |
|
|
response_headers:upsert(":status", "101") |
|
|
response_headers:upsert("upgrade", "websocket") |
|
|
response_headers:upsert("connection", "upgrade") |
|
|
response_headers:upsert("sec-websocket-accept", base64_sha1(self.key .. magic)) |
|
|
|
|
|
local chosen_protocol |
|
|
if self.protocols and options.protocols then |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for _, protocol in ipairs(options.protocols) do |
|
|
if self.protocols[protocol] then |
|
|
response_headers:upsert("sec-websocket-protocol", protocol) |
|
|
chosen_protocol = protocol |
|
|
break |
|
|
end |
|
|
end |
|
|
end |
|
|
|
|
|
do |
|
|
local ok, err, errno = self.stream:write_headers(response_headers, false, timeout) |
|
|
if not ok then |
|
|
return ok, err, errno |
|
|
end |
|
|
end |
|
|
|
|
|
self.socket = assert(self.stream.connection:take_socket()) |
|
|
self.socket:onerror(onerror) |
|
|
self.stream = nil |
|
|
self.readyState = 1 |
|
|
self.protocol = chosen_protocol |
|
|
return true |
|
|
end |
|
|
|
|
|
return { |
|
|
new_from_uri = new_from_uri; |
|
|
new_from_stream = new_from_stream; |
|
|
methods = websocket_methods; |
|
|
mt = websocket_mt; |
|
|
|
|
|
new = new; |
|
|
build_frame = build_frame; |
|
|
read_frame = read_frame; |
|
|
build_close = build_close; |
|
|
parse_close = parse_close; |
|
|
} |
|
|
|