From 216ba98b4e285c3d23ab68a876afb38b3cc2482e Mon Sep 17 00:00:00 2001 From: Hongbin Lu Date: Sun, 25 Nov 2018 21:46:44 +0000 Subject: [PATCH] Set HTTP header "Origin" when using websocket This is because if this header is missing, the websocket client will populate this header as "http://...", which is incorrect if we are using secure connection (in which this header should be "https://..."). Change-Id: I441946dc168db645744da093e215f65fa1ca3637 Related-Bug: #1762511 (cherry picked from commit 67660449617b2a884735f000f922faafe69659b0) --- zunclient/common/websocketclient/websocketclient.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/zunclient/common/websocketclient/websocketclient.py b/zunclient/common/websocketclient/websocketclient.py index 92271585..7a1d24f0 100644 --- a/zunclient/common/websocketclient/websocketclient.py +++ b/zunclient/common/websocketclient/websocketclient.py @@ -23,6 +23,7 @@ import os import select import signal import six +import six.moves.urllib.parse as urlparse import socket import struct import sys @@ -238,6 +239,7 @@ class WebSocketClient(BaseClient): try: self.ws = websocket.create_connection( url, skip_utf8_validation=True, + origin=self._compute_origin_header(url), subprotocols=["binary", "base64"]) print('connected to %s, press Enter to continue' % self.id) print('type %s. to disconnect' % self.escape) @@ -248,6 +250,13 @@ class WebSocketClient(BaseClient): except websocket.WebSocketBadStatusException as e: raise exceptions.ConnectionFailed(e) + def _compute_origin_header(self, url): + origin = urlparse.urlparse(url) + if origin.scheme == 'wss': + return "https://%s:%s" % (origin.hostname, origin.port) + else: + return "http://%s:%s" % (origin.hostname, origin.port) + def fileno(self): return self.ws.fileno()