websockets: handle HTTP_CONNECTION more flexibly
This fixes GitHub issue #73
This commit is contained in:
@@ -97,7 +97,10 @@ class WebSocketWSGI(object):
|
|||||||
return decorator(handler)
|
return decorator(handler)
|
||||||
|
|
||||||
def __call__(self, environ, start_response):
|
def __call__(self, environ, start_response):
|
||||||
if not (environ.get('HTTP_CONNECTION') == 'Upgrade' and
|
http_connection_parts = [
|
||||||
|
part.strip()
|
||||||
|
for part in environ.get('HTTP_CONNECTION', '').lower().split(',')]
|
||||||
|
if not ('upgrade' in http_connection_parts and
|
||||||
environ.get('HTTP_UPGRADE').lower() == 'websocket'):
|
environ.get('HTTP_UPGRADE').lower() == 'websocket'):
|
||||||
# need to check a few more things here for true compliance
|
# need to check a few more things here for true compliance
|
||||||
start_response('400 Bad Request', [('Connection', 'close')])
|
start_response('400 Bad Request', [('Connection', 'close')])
|
||||||
|
@@ -68,25 +68,28 @@ class TestWebSocket(_TestBase):
|
|||||||
self.assertEqual(resp.read(), '')
|
self.assertEqual(resp.read(), '')
|
||||||
|
|
||||||
def test_correct_upgrade_request_13(self):
|
def test_correct_upgrade_request_13(self):
|
||||||
|
for http_connection in ['Upgrade', 'UpGrAdE', 'keep-alive, Upgrade']:
|
||||||
connect = [
|
connect = [
|
||||||
"GET /echo HTTP/1.1",
|
"GET /echo HTTP/1.1",
|
||||||
"Upgrade: websocket",
|
"Upgrade: websocket",
|
||||||
"Connection: Upgrade",
|
"Connection: %s" % http_connection,
|
||||||
"Host: localhost:%s" % self.port,
|
"Host: localhost:%s" % self.port,
|
||||||
"Origin: http://localhost:%s" % self.port,
|
"Origin: http://localhost:%s" % self.port,
|
||||||
"Sec-WebSocket-Version: 13",
|
"Sec-WebSocket-Version: 13",
|
||||||
"Sec-WebSocket-Key: d9MXuOzlVQ0h+qRllvSCIg==", ]
|
"Sec-WebSocket-Key: d9MXuOzlVQ0h+qRllvSCIg==",
|
||||||
sock = eventlet.connect(
|
]
|
||||||
('localhost', self.port))
|
sock = eventlet.connect(('localhost', self.port))
|
||||||
|
|
||||||
sock.sendall('\r\n'.join(connect) + '\r\n\r\n')
|
sock.sendall('\r\n'.join(connect) + '\r\n\r\n')
|
||||||
result = sock.recv(1024)
|
result = sock.recv(1024)
|
||||||
## The server responds the correct Websocket handshake
|
## The server responds the correct Websocket handshake
|
||||||
self.assertEqual(result,
|
print('Connection string: %r' % http_connection)
|
||||||
'\r\n'.join(['HTTP/1.1 101 Switching Protocols',
|
self.assertEqual(result, '\r\n'.join([
|
||||||
|
'HTTP/1.1 101 Switching Protocols',
|
||||||
'Upgrade: websocket',
|
'Upgrade: websocket',
|
||||||
'Connection: Upgrade',
|
'Connection: Upgrade',
|
||||||
'Sec-WebSocket-Accept: ywSyWXCPNsDxLrQdQrn5RFNRfBU=\r\n\r\n', ]))
|
'Sec-WebSocket-Accept: ywSyWXCPNsDxLrQdQrn5RFNRfBU=\r\n\r\n',
|
||||||
|
]))
|
||||||
|
|
||||||
def test_send_recv_13(self):
|
def test_send_recv_13(self):
|
||||||
connect = [
|
connect = [
|
||||||
|
Reference in New Issue
Block a user