This commit is contained in:
Sai Asish Y 2026-05-24 21:30:32 -07:00 committed by GitHub
commit 56963373ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 1 deletions

View File

@ -87,6 +87,20 @@ class AcceptTestCase(unittest.TestCase):
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()

View File

@ -281,7 +281,9 @@ class WebSocket:
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