From b0dda0483d28c9970e92c4497c2b3cf76b52f619 Mon Sep 17 00:00:00 2001 From: EfveZombie <34877029+EfveZombie@users.noreply.github.com> Date: Wed, 10 Mar 2021 12:35:56 +0800 Subject: [PATCH] fix websocket bugs (#1359) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix websocket bugs * revert write_frame Co-authored-by: 郑非凡 --- lualib/http/websocket.lua | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lualib/http/websocket.lua b/lualib/http/websocket.lua index ff6a21c0..92ac1730 100755 --- a/lualib/http/websocket.lua +++ b/lualib/http/websocket.lua @@ -43,7 +43,7 @@ local function write_handshake(self, host, url, header) local recvheader = {} local code, body = internal.request(self, "GET", host, url, recvheader, request_header) if code ~= 101 then - error(string.format("websocket handshake error: code[%s] info:%s", code, body)) + error(string.format("websocket handshake error: code[%s] info:%s", code, body)) end if not recvheader["upgrade"] or recvheader["upgrade"]:lower() ~= "websocket" then @@ -261,6 +261,7 @@ local function resolve_accept(self, options) try_handle(self, "handshake", header, url) local recv_count = 0 local recv_buf = {} + local first_op while true do if _isws_closed(self.id) then try_handle(self, "close") @@ -286,11 +287,13 @@ local function resolve_accept(self, options) if recv_count > MAX_FRAME_SIZE then error("payload_len is too large") end + first_op = first_op or op if fin then local s = table.concat(recv_buf) - try_handle(self, "message", s, op) + try_handle(self, "message", s, first_op) recv_buf = {} -- clear recv_buf recv_count = 0 + first_op = nil end end end @@ -323,7 +326,7 @@ local function _new_client_ws(socket_id, protocol) websocket = true, close = function () socket.close(socket_id) - tls.closefunc(tls_ctx)() + tls.closefunc(tls_ctx)() end, read = tls.readfunc(socket_id, tls_ctx), write = tls.writefunc(socket_id, tls_ctx), @@ -369,7 +372,7 @@ local function _new_server_ws(socket_id, handle, protocol) obj = { close = function () socket.close(socket_id) - tls.closefunc(tls_ctx)() + tls.closefunc(tls_ctx)() end, read = tls.readfunc(socket_id, tls_ctx), write = tls.writefunc(socket_id, tls_ctx), @@ -430,7 +433,7 @@ function M.connect(url, header, timeout) if protocol ~= "wss" and protocol ~= "ws" then error(string.format("invalid protocol: %s", protocol)) end - + assert(host) local host_name, host_port = string.match(host, "^([^:]+):?(%d*)$") assert(host_name and host_port) @@ -470,7 +473,6 @@ function M.read(id) end end end - assert(false) end