Skip to content

Commit 9cd0b3d

Browse files
author
Sergi Almacellas Abellana
committed
Use urlparse to generate websocket url
Fixes #246
1 parent e245dbb commit 9cd0b3d

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

kubernetes/client/ws_client.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
from websocket import WebSocket, ABNF, enableTrace
2020
import six
2121
import ssl
22-
from six.moves.urllib.parse import urlencode
23-
from six.moves.urllib.parse import quote_plus
22+
from six.moves.urllib.parse import urlencode, quote_plus, urlparse, urlunparse
2423

2524
STDIN_CHANNEL = 0
2625
STDOUT_CHANNEL = 1
@@ -203,18 +202,21 @@ def close(self, **kwargs):
203202
WSResponse = collections.namedtuple('WSResponse', ['data'])
204203

205204

205+
def get_websocket_url(url):
206+
parsed_url = urlparse(url)
207+
parts = list(parsed_url)
208+
if parsed_url.scheme == 'http':
209+
parts[0] = 'ws'
210+
elif parsed_url.scheme == 'https':
211+
parts[0] = 'wss'
212+
return urlunparse(parts)
213+
214+
206215
def websocket_call(configuration, url, query_params, _request_timeout,
207216
_preload_content, headers):
208217
"""An internal function to be called in api-client when a websocket
209218
connection is required."""
210219

211-
# switch protocols from http to websocket
212-
url = url.replace('http://', 'ws://')
213-
url = url.replace('https://', 'wss://')
214-
215-
# patch extra /
216-
url = url.replace('//api', '/api')
217-
218220
# Extract the command from the list of tuples
219221
commands = None
220222
for key, value in query_params:
@@ -238,7 +240,7 @@ def websocket_call(configuration, url, query_params, _request_timeout,
238240
url += '&command=' + quote_plus(commands)
239241

240242
try:
241-
client = WSClient(configuration, url, headers)
243+
client = WSClient(configuration, get_websocket_url(url), headers)
242244
if not _preload_content:
243245
return client
244246
client.run_forever(timeout=_request_timeout)

kubernetes/client/ws_client_test.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2017 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import unittest
16+
17+
from .ws_client import get_websocket_url
18+
19+
20+
class WSClientTest(unittest.TestCase):
21+
22+
def test_websocket_client(self):
23+
for url, ws_url in [
24+
('http://localhost/api', 'ws://localhost/api'),
25+
('https://localhost/api', 'wss://localhost/api'),
26+
('https://domain.com/api', 'wss://domain.com/api'),
27+
('https://api.domain.com/api', 'wss://api.domain.com/api'),
28+
('http://api.domain.com', 'ws://api.domain.com'),
29+
('https://api.domain.com', 'wss://api.domain.com'),
30+
('http://api.domain.com/', 'ws://api.domain.com/'),
31+
('https://api.domain.com/', 'wss://api.domain.com/'),
32+
]:
33+
self.assertEqual(get_websocket_url(url), ws_url)
34+
35+
36+
if __name__ == '__main__':
37+
unittest.main()

0 commit comments

Comments
 (0)