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
This commit is contained in:
Hongbin Lu
2018-11-25 21:46:44 +00:00
parent ff3b380680
commit 6766044961

View File

@@ -23,6 +23,7 @@ from oslo_log import log as logging
import select import select
import signal import signal
import six import six
import six.moves.urllib.parse as urlparse
import socket import socket
import struct import struct
import sys import sys
@@ -238,6 +239,7 @@ class WebSocketClient(BaseClient):
try: try:
self.ws = websocket.create_connection( self.ws = websocket.create_connection(
url, skip_utf8_validation=True, url, skip_utf8_validation=True,
origin=self._compute_origin_header(url),
subprotocols=["binary", "base64"]) subprotocols=["binary", "base64"])
print('connected to %s, press Enter to continue' % self.id) print('connected to %s, press Enter to continue' % self.id)
print('type %s. to disconnect' % self.escape) print('type %s. to disconnect' % self.escape)
@@ -248,6 +250,13 @@ class WebSocketClient(BaseClient):
except websocket.WebSocketBadStatusException as e: except websocket.WebSocketBadStatusException as e:
raise exceptions.ConnectionFailed(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): def fileno(self):
return self.ws.fileno() return self.ws.fileno()