Skip to content

Commit 3beb8ce

Browse files
committed
Merge remote-tracking branch 'MrJoes/master' into work
Conflicts: tornado/websocket.py
2 parents 442b49f + 3537a66 commit 3beb8ce

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

tornado/websocket.py

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,35 @@ def _execute(self, transforms, *args, **kwargs):
8383
self.open_args = args
8484
self.open_kwargs = kwargs
8585

86+
# Websocket only supports GET method
87+
if self.request.method != 'GET':
88+
self.stream.write(tornado.escape.utf8(
89+
"HTTP/1.1 405 Method Not Allowed\r\n\r\n"
90+
))
91+
self.stream.close()
92+
return
93+
94+
# Upgrade header should be present and should be equal to WebSocket
95+
if self.request.headers.get("Upgrade", "").lower() != 'websocket':
96+
self.stream.write(tornado.escape.utf8(
97+
"HTTP/1.1 400 Bad Request\r\n\r\n"
98+
"Can \"Upgrade\" only to \"WebSocket\"."
99+
))
100+
self.stream.close()
101+
return
102+
103+
# Connection header should be upgrade. Some proxy servers/load balancers
104+
# might mess with it.
105+
headers = self.request.headers
106+
connection = map(lambda s: s.strip().lower(), headers.get("Connection", "").split(","))
107+
if 'upgrade' not in connection:
108+
self.stream.write(tornado.escape.utf8(
109+
"HTTP/1.1 400 Bad Request\r\n\r\n"
110+
"\"Connection\" must be \"Upgrade\"."
111+
))
112+
self.stream.close()
113+
return
114+
86115
# The difference between version 8 and 13 is that in 8 the
87116
# client sends a "Sec-Websocket-Origin" header and in 13 it's
88117
# simply "Origin".
@@ -253,6 +282,7 @@ def accept_connection(self):
253282
logging.debug("Malformed WebSocket request received")
254283
self._abort()
255284
return
285+
256286
scheme = self.handler.get_websocket_scheme()
257287

258288
# draft76 only allows a single subprotocol
@@ -320,12 +350,9 @@ def _handle_websocket_headers(self):
320350
If a header is missing or have an incorrect value ValueError will be
321351
raised
322352
"""
323-
headers = self.request.headers
324353
fields = ("Origin", "Host", "Sec-Websocket-Key1",
325354
"Sec-Websocket-Key2")
326-
if headers.get("Upgrade", '').lower() != "websocket" or \
327-
headers.get("Connection", '').lower() != "upgrade" or \
328-
not all(map(lambda f: self.request.headers.get(f), fields)):
355+
if not all(map(lambda f: self.request.headers.get(f), fields)):
329356
raise ValueError("Missing/Invalid WebSocket headers")
330357

331358
def _calculate_part(self, key):
@@ -430,13 +457,8 @@ def _handle_websocket_headers(self):
430457
If a header is missing or have an incorrect value ValueError will be
431458
raised
432459
"""
433-
headers = self.request.headers
434460
fields = ("Host", "Sec-Websocket-Key", "Sec-Websocket-Version")
435-
connection = map(lambda s: s.strip().lower(), headers.get("Connection", '').split(","))
436-
if (self.request.method != "GET" or
437-
headers.get("Upgrade", '').lower() != "websocket" or
438-
"upgrade" not in connection or
439-
not all(map(lambda f: self.request.headers.get(f), fields))):
461+
if not all(map(lambda f: self.request.headers.get(f), fields)):
440462
raise ValueError("Missing/Invalid WebSocket headers")
441463

442464
def _challenge_response(self):

0 commit comments

Comments
 (0)