commit 66d5fcc367a72e2e2d6eda85a06c1aaf83a707f2
parent c3c892492b9c67482ce8ef69057a18e3d5a73e3a
Author: lash <dev@holbrook.no>
Date: Tue, 25 Jan 2022 19:05:43 +0000
Add port autocomplete, origin string generation
Diffstat:
4 files changed, 76 insertions(+), 1 deletion(-)
diff --git a/CHANGELOG b/CHANGELOG
@@ -1,2 +1,4 @@
+- 0.0.2
+ * Add method to enforce port on host, optionally as origin string
- 0.0.1
* Add urlmerge and urlhostmerge methods
diff --git a/setup.cfg b/setup.cfg
@@ -1,6 +1,6 @@
[metadata]
name = urlybird
-version = 0.0.1
+version = 0.0.2
description = Common url operations not covered by the standard library urllib
author = Louis Holbrook
author_email = dev@holbrook.no
diff --git a/tests/test_host.py b/tests/test_host.py
@@ -0,0 +1,37 @@
+# standard imports
+import unittest
+
+# local imports
+from urlybird.host import url_apply_port_string
+
+
+class TestHost(unittest.TestCase):
+
+ def test_host_strings(self):
+ s = 'http://localhost'
+ v = 'http://localhost:80'
+ r = url_apply_port_string(s)
+ self.assertEqual(r, v)
+
+ s = 'http://localhost:80'
+ v = 'http://localhost:80'
+ r = url_apply_port_string(s)
+ self.assertEqual(r, v)
+
+ s = 'http://localhost:8000'
+ v = 'http://localhost:8000'
+ r = url_apply_port_string(s)
+ self.assertEqual(r, v)
+
+ s = 'https://localhost/foo/bar?baz=xyzzy'
+ v = 'https://localhost:443/foo/bar?baz=xyzzy'
+ r = url_apply_port_string(s)
+ self.assertEqual(r, v)
+
+ s = 'https://localhost/foo/bar?baz=xyzzy'
+ v = 'https://localhost:443'
+ r = url_apply_port_string(s, as_origin=True)
+ self.assertEqual(r, v)
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/urlybird/host.py b/urlybird/host.py
@@ -0,0 +1,36 @@
+# standard imports
+from socket import getservbyname
+import urllib.parse
+
+
+def url_apply_port(url_parts, as_origin=False):
+ url_parts_origin_host = url_parts[1].split(":")
+ host = url_parts_origin_host[0]
+ port = None
+ try:
+ port = ':' + url_parts_origin_host[1]
+ except IndexError:
+ port = ':' + str(getservbyname(url_parts[0]))
+ #logg.info('changed origin with missing port number from {} to {}'.format(url_parts[1], host))
+ host += port
+ path = ''
+ query = ''
+ fragment = ''
+ if not as_origin:
+ path=url_parts[2]
+ query=url_parts[3]
+ fragment=url_parts[4]
+
+ return urllib.parse.SplitResult(
+ scheme=url_parts[0],
+ netloc=host,
+ path=path,
+ query=query,
+ fragment=fragment,
+ )
+
+
+def url_apply_port_string(url_string, as_origin=False):
+ url_parts = urllib.parse.urlsplit(url_string)
+ u = url_apply_port(url_parts, as_origin=as_origin)
+ return urllib.parse.urlunsplit(u)