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 6766044961)
This commit is contained in:
Hongbin Lu 2018-11-25 21:46:44 +00:00
parent 3d944ae67a
commit 3adcd73ac7
1 changed files with 9 additions and 0 deletions

View File

@ -23,6 +23,7 @@ from oslo_log import log as logging
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()