mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 20:23:06 +00:00
websocket upgrade (#1279)
This commit is contained in:
@@ -67,26 +67,32 @@ local function write_handshake(self, host, url, header)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function read_handshake(self)
|
local function read_handshake(self, upgrade_ops)
|
||||||
local tmpline = {}
|
local header, method, url
|
||||||
local header_body = internal.recvheader(self.read, tmpline, "")
|
if upgrade_ops then
|
||||||
if not header_body then
|
header, method, url = upgrade_ops.header, upgrade_ops.method, upgrade_ops.url
|
||||||
return 413
|
else
|
||||||
|
local tmpline = {}
|
||||||
|
local header_body = internal.recvheader(self.read, tmpline, "")
|
||||||
|
if not header_body then
|
||||||
|
return 413
|
||||||
|
end
|
||||||
|
|
||||||
|
local request = assert(tmpline[1])
|
||||||
|
local httpver
|
||||||
|
method, url, httpver = request:match "^(%a+)%s+(.-)%s+HTTP/([%d%.]+)$"
|
||||||
|
assert(method and url and httpver)
|
||||||
|
if method ~= "GET" then
|
||||||
|
return 400, "need GET method"
|
||||||
|
end
|
||||||
|
|
||||||
|
httpver = assert(tonumber(httpver))
|
||||||
|
if httpver < 1.1 then
|
||||||
|
return 505 -- HTTP Version not supported
|
||||||
|
end
|
||||||
|
header = internal.parseheader(tmpline, 2, {})
|
||||||
end
|
end
|
||||||
|
|
||||||
local request = assert(tmpline[1])
|
|
||||||
local method, url, httpver = request:match "^(%a+)%s+(.-)%s+HTTP/([%d%.]+)$"
|
|
||||||
assert(method and url and httpver)
|
|
||||||
if method ~= "GET" then
|
|
||||||
return 400, "need GET method"
|
|
||||||
end
|
|
||||||
|
|
||||||
httpver = assert(tonumber(httpver))
|
|
||||||
if httpver < 1.1 then
|
|
||||||
return 505 -- HTTP Version not supported
|
|
||||||
end
|
|
||||||
|
|
||||||
local header = internal.parseheader(tmpline, 2, {})
|
|
||||||
if not header then
|
if not header then
|
||||||
return 400 -- Bad request
|
return 400 -- Bad request
|
||||||
end
|
end
|
||||||
@@ -239,9 +245,9 @@ local function read_frame(self)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function resolve_accept(self)
|
local function resolve_accept(self, options)
|
||||||
try_handle(self, "connect")
|
try_handle(self, "connect")
|
||||||
local code, err, url = read_handshake(self)
|
local code, err, url = read_handshake(self, options and options.upgrade)
|
||||||
if code then
|
if code then
|
||||||
local ok, s = httpd.write_response(self.write, code, err)
|
local ok, s = httpd.write_response(self.write, code, err)
|
||||||
if not ok then
|
if not ok then
|
||||||
@@ -384,8 +390,10 @@ end
|
|||||||
|
|
||||||
-- handle interface
|
-- handle interface
|
||||||
-- connect / handshake / message / ping / pong / close / error
|
-- connect / handshake / message / ping / pong / close / error
|
||||||
function M.accept(socket_id, handle, protocol, addr)
|
function M.accept(socket_id, handle, protocol, addr, options)
|
||||||
socket.start(socket_id)
|
if not (options and options.upgrade) then
|
||||||
|
socket.start(socket_id)
|
||||||
|
end
|
||||||
protocol = protocol or "ws"
|
protocol = protocol or "ws"
|
||||||
local ws_obj = _new_server_ws(socket_id, handle, protocol)
|
local ws_obj = _new_server_ws(socket_id, handle, protocol)
|
||||||
ws_obj.addr = addr
|
ws_obj.addr = addr
|
||||||
@@ -396,7 +404,7 @@ function M.accept(socket_id, handle, protocol, addr)
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
local ok, err = xpcall(resolve_accept, debug.traceback, ws_obj)
|
local ok, err = xpcall(resolve_accept, debug.traceback, ws_obj, options)
|
||||||
local closed = _isws_closed(socket_id)
|
local closed = _isws_closed(socket_id)
|
||||||
if not closed then
|
if not closed then
|
||||||
_close_websocket(ws_obj)
|
_close_websocket(ws_obj)
|
||||||
|
|||||||
Reference in New Issue
Block a user