Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions tests/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ def select_subprotocol(self, protocol):
self.assertEqual(sock.data[:13], b'HTTP/1.1 101 ')
self.assertTrue(b'\r\nSec-WebSocket-Protocol: gazonk\r\n' in sock.data)

def test_protocol_with_space_after_comma(self):
class ProtoSocket(websocket.WebSocket):
def select_subprotocol(self, protocol):
return 'gazonk'

ws = ProtoSocket()
sock = FakeSocket()
ws.accept(sock, {'upgrade': 'websocket',
'Sec-WebSocket-Version': '13',
'Sec-WebSocket-Key': 'DKURYVK9cRFul1vOZVA56Q==',
'Sec-WebSocket-Protocol': 'foobar, gazonk'})
self.assertEqual(sock.data[:13], b'HTTP/1.1 101 ')
self.assertTrue(b'\r\nSec-WebSocket-Protocol: gazonk\r\n' in sock.data)

def test_no_protocol(self):
ws = websocket.WebSocket()
sock = FakeSocket()
Expand Down
4 changes: 3 additions & 1 deletion websockify/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,9 @@ def accept(self, socket, headers):
accept = b64encode(accept).decode("ascii")

self.protocol = ''
protocols = headers.get('Sec-WebSocket-Protocol', '').split(',')
# Tokens may be separated by ", " so strip whitespace per RFC 7230
protocols = [p.strip() for p in
headers.get('Sec-WebSocket-Protocol', '').split(',')]
if protocols:
self.protocol = self.select_subprotocol(protocols)
# We are required to choose one of the protocols
Expand Down