diff options
author | Anton Hvornum <anton.feeds@gmail.com> | 2018-11-25 14:13:17 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-25 14:13:17 +0100 |
commit | 2480ad60cf35ff3336c5d4db0bdef33b3ba60778 (patch) | |
tree | 79bf4b93ee5542cd6b0a8720867978fece426115 /websocket | |
parent | 102033e44bcc9badf85c78769516d3f9127ebdc7 (diff) | |
download | websocket-client-2480ad60cf35ff3336c5d4db0bdef33b3ba60778.tar.gz |
Manually assigning WebSocket-Version
When manually assigning `Sec-WebSocket-Key` & `Sec-WebSocket-Version`, websocket-client would raise:
WebSocketException("Invalid WebSocket Header")
Because the key for instance, would be inserted twice.
The best way to get around this in the long run, would be to refrain from using lists as the header builder, since keys can't occur twice anyway. Thus, just creating the automated headers first, and then iterating over whatever the user inputs - would effectively replace any default headers automatically, without needing to parse "if host in options['headers']" etc. Just iterate over the headers given, replace the default ones and bam, profit.
Diffstat (limited to 'websocket')
-rw-r--r-- | websocket/_handshake.py | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/websocket/_handshake.py b/websocket/_handshake.py index 3b8ddc0..cf3c559 100644 --- a/websocket/_handshake.py +++ b/websocket/_handshake.py @@ -115,8 +115,16 @@ def _get_handshake_headers(resource, host, port, options): headers.append("Origin: http://%s" % hostport) key = _create_sec_websocket_key() - headers.append("Sec-WebSocket-Key: %s" % key) - headers.append("Sec-WebSocket-Version: %s" % VERSION) + + # Append Sec-WebSocket-Key & Sec-WebSocket-Version if not manually specified + if not 'header' in options or 'Sec-WebSocket-Key' not in options['header']: + key = _create_sec_websocket_key() + headers.append("Sec-WebSocket-Key: %s" % key) + else: + key = options['header']['Sec-WebSocket-Key'] + + if not 'header' in options or 'Sec-WebSocket-Version' not in options['header']: + headers.append("Sec-WebSocket-Version: %s" % VERSION) subprotocols = options.get("subprotocols") if subprotocols: |