From e72e8fedea5346f581656f160c72cd4794a5a85b Mon Sep 17 00:00:00 2001 From: daurnimator Date: Fri, 26 Jan 2018 13:46:41 +1100 Subject: [PATCH] http/websocket.lua: Server can pick 0 or 1 sub protocols --- http/websocket.lua | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/http/websocket.lua b/http/websocket.lua index e1abb1a1..5a6addd1 100644 --- a/http/websocket.lua +++ b/http/websocket.lua @@ -752,19 +752,20 @@ function websocket_methods:accept(options, timeout) response_headers:upsert("sec-websocket-accept", base64_sha1(self.key .. magic)) local chosen_protocol - if self.protocols then - if options.protocols then - for _, protocol in ipairs(options.protocols) do - if self.protocols[protocol] then - chosen_protocol = protocol - break - end + if self.protocols and options.protocols then + --[[ The |Sec-WebSocket-Protocol| request-header field can be + used to indicate what subprotocols (application-level protocols + layered over the WebSocket Protocol) are acceptable to the client. + The server selects one or none of the acceptable protocols and echoes + that value in its handshake to indicate that it has selected that + protocol.]] + 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 - if not chosen_protocol then - return nil, "no matching protocol", ce.EILSEQNOSUPPORT - end - response_headers:upsert("sec-websocket-protocol", chosen_protocol) end do