Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add on_open option in WebSocketWSGI #253

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 18 additions & 4 deletions eventlet/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ def my_handler(ws):
the time of closure.
"""

def __init__(self, handler):
def __init__(self, handler, on_open=None):
self.handler = handler
self.protocol_version = None
self.support_legacy_versions = True
self.supported_protocols = []
self.origin_checker = None
self.on_open = on_open

@classmethod
def configured(cls,
Expand Down Expand Up @@ -154,6 +155,8 @@ def _handle_legacy_request(self, environ):
key = struct.pack(">II", key1, key2) + key3
response = md5(key).digest()

custom_headers = [] if self.on_open is None else self.on_open(environ)

# Start building the response
scheme = 'ws'
if environ.get('wsgi.url_scheme') == 'https':
Expand All @@ -173,7 +176,9 @@ def _handle_legacy_request(self, environ):
b"Upgrade: WebSocket\r\n"
b"Connection: Upgrade\r\n"
b"WebSocket-Origin: " + six.b(environ.get('HTTP_ORIGIN')) + b"\r\n"
b"WebSocket-Location: " + six.b(location) + b"\r\n\r\n"
b"WebSocket-Location: " + six.b(location) + b"\r\n" +
self._format_headers(custom_headers) +
b"\r\n"
)
elif self.protocol_version == 76:
handshake_reply = (
Expand All @@ -183,7 +188,8 @@ def _handle_legacy_request(self, environ):
b"Sec-WebSocket-Origin: " + six.b(environ.get('HTTP_ORIGIN')) + b"\r\n"
b"Sec-WebSocket-Protocol: " +
six.b(environ.get('HTTP_SEC_WEBSOCKET_PROTOCOL', 'default')) + b"\r\n"
b"Sec-WebSocket-Location: " + six.b(location) + b"\r\n"
b"Sec-WebSocket-Location: " + six.b(location) + b"\r\n" +
self._format_headers(custom_headers) +
b"\r\n" + response
)
else: # pragma NO COVER
Expand Down Expand Up @@ -219,6 +225,8 @@ def _handle_hybi_request(self, environ):
# if extensions:
# extensions = [i.strip() for i in extensions.split(',')]

custom_headers = [] if self.on_open is None else self.on_open(environ)

key = environ['HTTP_SEC_WEBSOCKET_KEY']
response = base64.b64encode(sha1(six.b(key) + PROTOCOL_GUID).digest())
handshake_reply = [b"HTTP/1.1 101 Switching Protocols",
Expand All @@ -227,7 +235,8 @@ def _handle_hybi_request(self, environ):
b"Sec-WebSocket-Accept: " + response]
if negotiated_protocol:
handshake_reply.append(b"Sec-WebSocket-Protocol: " + six.b(negotiated_protocol))
sock.sendall(b'\r\n'.join(handshake_reply) + b'\r\n\r\n')
sock.sendall(b'\r\n'.join(handshake_reply) + b'\r\n' +
self._format_headers(custom_headers) + b'\r\n')
return RFC6455WebSocket(sock, environ, self.protocol_version,
protocol=negotiated_protocol)

Expand All @@ -245,6 +254,11 @@ def _extract_number(self, value):
spaces += 1
return int(out) // spaces

def _format_headers(self, headers):
if len(headers) == 0:
return b''
return b''.join(six.b(k) + b': ' + six.b(v) + b'\r\n' for (k, v) in headers)


class WebSocket(object):
"""A websocket object that handles the details of
Expand Down