summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulien Danjou <julien@danjou.info>2013-06-19 13:46:08 +0200
committerJulien Danjou <julien@danjou.info>2013-06-24 17:02:20 +0200
commit0bf03b753c45dd3663a0839bb0ad15187a517bf3 (patch)
tree6a42c058c555e2670db3fe80b6ed61d4a95a7b83
parentba32f8ecb33403885ea521e3c2dcc4b49a81bd71 (diff)
downloadoslo-0bf03b753c45dd3663a0839bb0ad15187a517bf3.zip
oslo-0bf03b753c45dd3663a0839bb0ad15187a517bf3.tar.gz
oslo-0bf03b753c45dd3663a0839bb0ad15187a517bf3.tar.xz
Add network_utils.urlsplit
This unfortunately needed if we want to parse URL with params and unknown schemes. Change-Id: Ia7d8212370201254e537e6f1dd9b15d669a3252f
-rw-r--r--openstack/common/network_utils.py17
-rw-r--r--tests/unit/test_network_utils.py17
2 files changed, 34 insertions, 0 deletions
diff --git a/openstack/common/network_utils.py b/openstack/common/network_utils.py
index 0fbf171..dbed1ce 100644
--- a/openstack/common/network_utils.py
+++ b/openstack/common/network_utils.py
@@ -19,6 +19,8 @@
Network-related utilities and helper functions.
"""
+import urlparse
+
def parse_host_port(address, default_port=None):
"""Interpret a string as a host:port pair.
@@ -62,3 +64,18 @@ def parse_host_port(address, default_port=None):
port = default_port
return (host, None if port is None else int(port))
+
+
+def urlsplit(url, scheme='', allow_fragments=True):
+ """Parse a URL using urlparse.urlsplit(), splitting query and fragments.
+ This function papers over Python issue9374 when needed.
+
+ The parameters are the same as urlparse.urlsplit.
+ """
+ scheme, netloc, path, query, fragment = urlparse.urlsplit(
+ url, scheme, allow_fragments)
+ if allow_fragments and '#' in path:
+ path, fragment = path.split('#', 1)
+ if '?' in path:
+ path, query = path.split('?', 1)
+ return urlparse.SplitResult(scheme, netloc, path, query, fragment)
diff --git a/tests/unit/test_network_utils.py b/tests/unit/test_network_utils.py
index 2783e70..4ac0222 100644
--- a/tests/unit/test_network_utils.py
+++ b/tests/unit/test_network_utils.py
@@ -40,3 +40,20 @@ class NetworkUtilsTest(utils.BaseTestCase):
network_utils.parse_host_port(
'2001:db8:85a3::8a2e:370:7334',
default_port=1234))
+
+ def test_urlsplit(self):
+ result = network_utils.urlsplit('rpc://myhost?someparam#somefragment')
+ self.assertEqual(result.scheme, 'rpc')
+ self.assertEqual(result.netloc, 'myhost')
+ self.assertEqual(result.path, '')
+ self.assertEqual(result.query, 'someparam')
+ self.assertEqual(result.fragment, 'somefragment')
+
+ result = network_utils.urlsplit(
+ 'rpc://myhost/mypath?someparam#somefragment',
+ allow_fragments=False)
+ self.assertEqual(result.scheme, 'rpc')
+ self.assertEqual(result.netloc, 'myhost')
+ self.assertEqual(result.path, '/mypath')
+ self.assertEqual(result.query, 'someparam#somefragment')
+ self.assertEqual(result.fragment, '')