diff --git a/matrix/server.py b/matrix/server.py index 72a4f71..0b683fc 100644 --- a/matrix/server.py +++ b/matrix/server.py @@ -67,6 +67,10 @@ from .utils import create_server_buffer, key_from_value, server_buffer_prnt from .colors import Formatted, FormattedString, DEFAULT_ATTRIBUTES +try: + from urllib.parse import urlparse +except ImportError: + from urlparse import urlparse try: FileNotFoundError # type: ignore @@ -319,8 +323,28 @@ class MatrixServer(object): with atomic_write(path, overwrite=True) as device_file: device_file.write(self.device_id) + @staticmethod + def _parse_url(address, port): + parsed_url = urlparse(address) + + if parsed_url.netloc: + netloc, path = (parsed_url.netloc, parsed_url.path) + else: + try: + netloc, path = address.split("/", 1) + except ValueError: + netloc, path = (address, "") + + path = path.strip("/") + netloc = "{}:{}".format(netloc, port) + + return "/".join([netloc, path]).rstrip("/") + def _change_client(self): - host = ":".join([self.config.address, str(self.config.port)]) + host = MatrixServer._parse_url( + self.config.address, + str(self.config.port) + ) self.client = HttpClient( host, self.config.username, diff --git a/tests/server_test.py b/tests/server_test.py new file mode 100644 index 0000000..05021e4 --- /dev/null +++ b/tests/server_test.py @@ -0,0 +1,19 @@ +from matrix.server import MatrixServer +from matrix._weechat import MockConfig +import matrix.globals as G + +G.CONFIG = MockConfig() + +class TestClass(object): + def test_address_parsing(self): + host = MatrixServer._parse_url("example.org", "443") + assert host == "example.org:443" + + host = MatrixServer._parse_url("example.org/_matrix", "443") + assert host == "example.org:443/_matrix" + + host = MatrixServer._parse_url("https://example.org/_matrix", "443") + assert host == "example.org:443/_matrix" + + host = MatrixServer._parse_url("https://example.org", "443") + assert host == "example.org:443"