From b884536e7e329a9ea91b0908dddcbaa67c01a8da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Sun, 3 Feb 2019 22:56:10 +0100 Subject: [PATCH] server: Don't use the address in the config for connection establishment. The address in the config can contain subpaths and it doesn't make sense for a socket to connect to this address. The net location without the subpath is now contained in server.address. --- main.py | 2 +- matrix/commands.py | 2 +- matrix/server.py | 15 +++++++++------ matrix/utils.py | 2 +- tests/server_test.py | 15 +++++++++++---- 5 files changed, 23 insertions(+), 13 deletions(-) diff --git a/main.py b/main.py index 1790b75..b8c405f 100644 --- a/main.py +++ b/main.py @@ -180,7 +180,7 @@ def wrap_socket(server, file_descriptor): ssl_socket = server.ssl_context.wrap_socket( sock, do_handshake_on_connect=False, - server_hostname=server.config.address) # type: ssl.SSLSocket + server_hostname=server.address) # type: ssl.SSLSocket server.socket = ssl_socket diff --git a/matrix/commands.py b/matrix/commands.py index 615a58e..b03e427 100644 --- a/matrix/commands.py +++ b/matrix/commands.py @@ -1078,7 +1078,7 @@ def matrix_upload_command_cb(data, buffer, args): upload = Upload( server.name, - server.config.address, + server.client.host, server.client.access_token, room_buffer.room.room_id, parsed_args.file, diff --git a/matrix/server.py b/matrix/server.py index 0b683fc..b26672c 100644 --- a/matrix/server.py +++ b/matrix/server.py @@ -251,6 +251,7 @@ class MatrixServer(object): except NotImplementedError: pass + self.address = None self.client = None self.access_token = None # type: Optional[str] self.next_batch = None # type: Optional[str] @@ -336,15 +337,18 @@ class MatrixServer(object): netloc, path = (address, "") path = path.strip("/") - netloc = "{}:{}".format(netloc, port) - return "/".join([netloc, path]).rstrip("/") + return netloc, "/".join([ + "{}:{}".format(netloc, port), + path + ]).rstrip("/") def _change_client(self): - host = MatrixServer._parse_url( + netloc, host = MatrixServer._parse_url( self.config.address, str(self.config.port) ) + self.address = netloc self.client = HttpClient( host, self.config.username, @@ -558,7 +562,6 @@ class MatrixServer(object): def connect(self): # type: (MatrixServer) -> int if not self.config.address or not self.config.port: - W.prnt("", self.config.address) message = "{prefix}Server address or port not set".format( prefix=W.prefix("error") ) @@ -589,7 +592,7 @@ class MatrixServer(object): "{prefix}matrix: Connecting to " "{server}:{port}{ssl}..." ).format( prefix=W.prefix("network"), - server=self.config.address, + server=self.address, port=self.config.port, ssl=ssl_message, ) @@ -598,7 +601,7 @@ class MatrixServer(object): W.hook_connect( self.config.proxy, - self.config.address, + self.address, self.config.port, 1, 0, diff --git a/matrix/utils.py b/matrix/utils.py index 5b27a49..6c29003 100644 --- a/matrix/utils.py +++ b/matrix/utils.py @@ -82,7 +82,7 @@ def server_buffer_set_title(server): ip_string = "" title = ("Matrix: {address}:{port}{ip}").format( - address=server.config.address, port=server.config.port, ip=ip_string + address=server.address, port=server.config.port, ip=ip_string ) W.buffer_set(server.server_buffer, "title", title) diff --git a/tests/server_test.py b/tests/server_test.py index 05021e4..7b0513c 100644 --- a/tests/server_test.py +++ b/tests/server_test.py @@ -6,14 +6,21 @@ G.CONFIG = MockConfig() class TestClass(object): def test_address_parsing(self): - host = MatrixServer._parse_url("example.org", "443") + netloc, host = MatrixServer._parse_url("example.org", "443") assert host == "example.org:443" + assert netloc == "example.org" - host = MatrixServer._parse_url("example.org/_matrix", "443") + netloc, host = MatrixServer._parse_url("example.org/_matrix", "443") assert host == "example.org:443/_matrix" + assert netloc == "example.org" - host = MatrixServer._parse_url("https://example.org/_matrix", "443") + netloc, host = MatrixServer._parse_url( + "https://example.org/_matrix", + "443" + ) assert host == "example.org:443/_matrix" + assert netloc == "example.org" - host = MatrixServer._parse_url("https://example.org", "443") + netloc, host = MatrixServer._parse_url("https://example.org", "443") assert host == "example.org:443" + assert netloc == "example.org"