websockets: Test and handle lack of Upgrade header

This commit is contained in:
Jakub Stasiak
2014-01-24 00:17:26 +00:00
parent fe5bebbb09
commit f441c1eb53
2 changed files with 16 additions and 1 deletions

View File

@@ -101,7 +101,7 @@ class WebSocketWSGI(object):
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
start_response('400 Bad Request', [('Connection', 'close')])
return []

View File

@@ -67,6 +67,21 @@ class TestWebSocket(_TestBase):
self.assertEqual(resp.getheader('connection'), 'close')
self.assertEqual(resp.read(), '')
# No Upgrade now
headers = dict(kv.split(': ') for kv in [
"Connection: Upgrade",
"Host: localhost:%s" % self.port,
"Origin: http://localhost:%s" % self.port,
"Sec-WebSocket-Version: 13", ])
http = httplib.HTTPConnection('localhost', self.port)
http.request("GET", "/echo", headers=headers)
resp = http.getresponse()
self.assertEqual(resp.status, 400)
self.assertEqual(resp.getheader('connection'), 'close')
self.assertEqual(resp.read(), '')
def test_correct_upgrade_request_13(self):
for http_connection in ['Upgrade', 'UpGrAdE', 'keep-alive, Upgrade']:
connect = [