Skip to content

Commit b341b7d

Browse files
authored
bugfix: should abort when status code is invalid in send_close(server)(#64)
1 parent 86fe843 commit b341b7d

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

lib/resty/websocket/server.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ function _M.send_close(self, code, msg)
189189
local payload
190190
if code then
191191
if type(code) ~= "number" or code > 0x7fff then
192+
return nil, "bad status code"
192193
end
193194
payload = char(band(rshift(code, 8), 0xff), band(code, 0xff))
194195
.. (msg or "")

t/cs.t

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use Protocol::WebSocket::Frame;
66

77
repeat_each(2);
88

9-
plan tests => repeat_each() * (blocks() * 4 + 14);
9+
plan tests => repeat_each() * (blocks() * 4 + 13);
1010

1111
my $pwd = cwd();
1212

@@ -2007,3 +2007,52 @@ GET /c
20072007
a-header = a header value
20082008
another-header = another header value
20092009
yet-another-header = yet another header value
2010+
2011+
2012+
2013+
=== TEST 28: send invalid close status code
2014+
--- http_config eval: $::HttpConfig
2015+
--- config
2016+
location = /c {
2017+
content_by_lua_block {
2018+
local client = require "resty.websocket.client"
2019+
local wb, err = client:new()
2020+
local uri = "ws://127.0.0.1:" .. ngx.var.server_port .. "/s"
2021+
local ok, err = wb:connect(uri)
2022+
if not ok then
2023+
ngx.say("failed to connect: " .. err)
2024+
return
2025+
end
2026+
2027+
local data, typ, err = wb:recv_frame()
2028+
if not data then
2029+
ngx.say("failed to receive: ", err)
2030+
return
2031+
end
2032+
2033+
ngx.say("received ", typ, ": ", data, ": ", err)
2034+
}
2035+
}
2036+
2037+
location = /s {
2038+
content_by_lua_block {
2039+
local server = require "resty.websocket.server"
2040+
local wb, err = server:new()
2041+
if not wb then
2042+
ngx.log(ngx.ERR, "failed to new websocket: ", err)
2043+
return ngx.exit(444)
2044+
end
2045+
2046+
local bytes, err = wb:send_close(0xf000, "client, let\'s close!")
2047+
if not bytes then
2048+
ngx.log(ngx.ERR, "failed to send close: ", err)
2049+
return ngx.exit(444)
2050+
end
2051+
}
2052+
}
2053+
--- request
2054+
GET /c
2055+
--- response_body
2056+
failed to receive: failed to receive the first 2 bytes: closed
2057+
--- error_log
2058+
failed to send close: bad status code

0 commit comments

Comments
 (0)