diff options
| author | Andrew Godwin <andrew@aeracode.org> | 2010-06-10 15:45:14 +0100 |
|---|---|---|
| committer | Andrew Godwin <andrew@aeracode.org> | 2010-06-10 15:45:14 +0100 |
| commit | 9ed0ea9fc5bf816ef369468e88a7c65d2a40f208 (patch) | |
| tree | 62df1e103169f874d93724a1b2309c381ebe6376 /eventlet/websocket.py | |
| parent | 4c7a9f74e66fa9d6f8316b0ac882a46fc2ab4b92 (diff) | |
| download | eventlet-9ed0ea9fc5bf816ef369468e88a7c65d2a40f208.tar.gz | |
Tests for WebSocket-76, and renaming the old ones to *_75
Diffstat (limited to 'eventlet/websocket.py')
| -rw-r--r-- | eventlet/websocket.py | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/eventlet/websocket.py b/eventlet/websocket.py index 95ef237..d6b6450 100644 --- a/eventlet/websocket.py +++ b/eventlet/websocket.py @@ -2,6 +2,7 @@ import collections import errno import string import struct +from socket import error as SocketError try: from hashlib import md5 @@ -47,6 +48,10 @@ class WebSocketWSGI(object): # See if they sent the new-format headers if 'HTTP_SEC_WEBSOCKET_KEY1' in environ: self.protocol_version = 76 + if 'HTTP_SEC_WEBSOCKET_KEY2' not in environ: + # That's bad. + start_response('400 Bad Request', [('Connection','close')]) + return [] else: self.protocol_version = 75 @@ -84,7 +89,7 @@ class WebSocketWSGI(object): "Sec-WebSocket-Location: ws://%s%s\r\n" "\r\n%s"% ( environ.get('HTTP_ORIGIN'), - environ.get('HTTP_SEC_WEBSOCKET_PROTOCOL'), + environ.get('HTTP_SEC_WEBSOCKET_PROTOCOL', 'default'), environ.get('HTTP_HOST'), environ.get('PATH_INFO'), response)) @@ -98,7 +103,7 @@ class WebSocketWSGI(object): if get_errno(e) not in ACCEPTABLE_CLIENT_ERRORS: raise # Make sure we send the closing frame - ws._send_closing_frame() + ws._send_closing_frame(True) # use this undocumented feature of eventlet.wsgi to ensure that it # doesn't barf on the fact that we didn't call start_response return wsgi.ALREADY_HANDLED @@ -222,10 +227,16 @@ class WebSocket(object): self._msgs.extend(msgs) return self._msgs.popleft() - def _send_closing_frame(self): + def _send_closing_frame(self, ignore_send_errors=False): """Sends the closing frame to the client, if required.""" if self.version == 76 and not self.websocket_closed: - self.socket.sendall("\xff\x00") + try: + self.socket.sendall("\xff\x00") + except SocketError: + # Sometimes, like when the remote side cuts off the connection, + # we don't care about this. + if not ignore_send_errors: + raise self.websocket_closed = True def close(self): |
