Skip to content

Commit 3dafef7

Browse files
committed
Use default TLS port if scheme is HTTPS
1 parent 6db939b commit 3dafef7

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
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-3
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,
@@ -167,7 +167,6 @@ def __init__(
167167
):
168168
# Automatically assign http_schema, port based on hostname
169169
parsed_host = urlparse(host, allow_fragments=False)
170-
171170
if encoding is _USE_DEFAULT_ENCODING:
172171
encoding = [
173172
"json+zstd",
@@ -176,7 +175,6 @@ def __init__(
176175
]
177176

178177
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
180178
self.user = user
181179
self.source = source
182180
self.catalog = catalog
@@ -204,6 +202,16 @@ def __init__(
204202
self._http_session = http_session
205203
self.http_headers = http_headers
206204
self.http_scheme = http_scheme if not parsed_host.scheme else parsed_host.scheme
205+
206+
# Infer connection port: `hostname` takes precedence over explicit `port` argument
207+
# If none is given, use default based on HTTP protocol
208+
default_port = constants.DEFAULT_TLS_PORT if self.http_scheme == constants.HTTPS else constants.DEFAULT_PORT
209+
self.port = (
210+
parsed_host.port if parsed_host.port is not None
211+
else port if port is not None
212+
else default_port
213+
)
214+
207215
self.auth = auth
208216
self.extra_credential = extra_credential
209217
self.max_attempts = max_attempts

0 commit comments

Comments
 (0)