Skip to content

Commit 1eca397

Browse files
nfantonewendigo
authored andcommitted
Use default TLS port if scheme is HTTPS
1 parent 19c3ab5 commit 1eca397

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

tests/unit/test_dbapi.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def test_hostname_parsing():
281281

282282
https_server_without_port = Connection("https://mytrinoserver.domain")
283283
assert https_server_without_port.host == "mytrinoserver.domain"
284-
assert https_server_without_port.port == 8080
284+
assert https_server_without_port.port == constants.DEFAULT_TLS_PORT
285285
assert https_server_without_port.http_scheme == constants.HTTPS
286286

287287
http_server_with_port = Connection("http://mytrinoserver.domain:9999")
@@ -291,22 +291,22 @@ def test_hostname_parsing():
291291

292292
http_server_without_port = Connection("http://mytrinoserver.domain")
293293
assert http_server_without_port.host == "mytrinoserver.domain"
294-
assert http_server_without_port.port == 8080
294+
assert http_server_without_port.port == constants.DEFAULT_PORT
295295
assert http_server_without_port.http_scheme == constants.HTTP
296296

297297
http_server_with_path = Connection("http://mytrinoserver.domain/some_path")
298298
assert http_server_with_path.host == "mytrinoserver.domain/some_path"
299-
assert http_server_with_path.port == 8080
299+
assert http_server_with_path.port == constants.DEFAULT_PORT
300300
assert http_server_with_path.http_scheme == constants.HTTP
301301

302302
only_hostname = Connection("mytrinoserver.domain")
303303
assert only_hostname.host == "mytrinoserver.domain"
304-
assert only_hostname.port == 8080
304+
assert only_hostname.port == constants.DEFAULT_PORT
305305
assert only_hostname.http_scheme == constants.HTTP
306306

307307
only_hostname_with_path = Connection("mytrinoserver.domain/some_path")
308308
assert only_hostname_with_path.host == "mytrinoserver.domain/some_path"
309-
assert only_hostname_with_path.port == 8080
309+
assert only_hostname_with_path.port == constants.DEFAULT_PORT
310310
assert only_hostname_with_path.http_scheme == constants.HTTP
311311

312312

trino/dbapi.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class Connection:
143143
def __init__(
144144
self,
145145
host: str,
146-
port=constants.DEFAULT_PORT,
146+
port=None,
147147
user=None,
148148
source=constants.DEFAULT_SOURCE,
149149
catalog=constants.DEFAULT_CATALOG,
@@ -176,7 +176,6 @@ def __init__(
176176
]
177177

178178
self.host = host if parsed_host.hostname is None else parsed_host.hostname + parsed_host.path
179-
self.port = port if parsed_host.port is None else parsed_host.port
180179
self.user = user
181180
self.source = source
182181
self.catalog = catalog
@@ -204,6 +203,16 @@ def __init__(
204203
self._http_session = http_session
205204
self.http_headers = http_headers
206205
self.http_scheme = http_scheme if not parsed_host.scheme else parsed_host.scheme
206+
207+
# Infer connection port: `hostname` takes precedence over explicit `port` argument
208+
# If none is given, use default based on HTTP protocol
209+
default_port = constants.DEFAULT_TLS_PORT if self.http_scheme == constants.HTTPS else constants.DEFAULT_PORT
210+
self.port = (
211+
parsed_host.port if parsed_host.port is not None
212+
else port if port is not None
213+
else default_port
214+
)
215+
207216
self.auth = auth
208217
self.extra_credential = extra_credential
209218
self.max_attempts = max_attempts

0 commit comments

Comments
 (0)