Skip to content

Use default TLS port if scheme is HTTPS #549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions tests/unit/test_dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def test_hostname_parsing():

https_server_without_port = Connection("https://mytrinoserver.domain")
assert https_server_without_port.host == "mytrinoserver.domain"
assert https_server_without_port.port == 8080
assert https_server_without_port.port == constants.DEFAULT_TLS_PORT
assert https_server_without_port.http_scheme == constants.HTTPS

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

http_server_without_port = Connection("http://mytrinoserver.domain")
assert http_server_without_port.host == "mytrinoserver.domain"
assert http_server_without_port.port == 8080
assert http_server_without_port.port == constants.DEFAULT_PORT
assert http_server_without_port.http_scheme == constants.HTTP

http_server_with_path = Connection("http://mytrinoserver.domain/some_path")
assert http_server_with_path.host == "mytrinoserver.domain/some_path"
assert http_server_with_path.port == 8080
assert http_server_with_path.port == constants.DEFAULT_PORT
assert http_server_with_path.http_scheme == constants.HTTP

only_hostname = Connection("mytrinoserver.domain")
assert only_hostname.host == "mytrinoserver.domain"
assert only_hostname.port == 8080
assert only_hostname.port == constants.DEFAULT_PORT
assert only_hostname.http_scheme == constants.HTTP

only_hostname_with_path = Connection("mytrinoserver.domain/some_path")
assert only_hostname_with_path.host == "mytrinoserver.domain/some_path"
assert only_hostname_with_path.port == 8080
assert only_hostname_with_path.port == constants.DEFAULT_PORT
assert only_hostname_with_path.http_scheme == constants.HTTP


Expand Down
13 changes: 11 additions & 2 deletions trino/dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class Connection:
def __init__(
self,
host: str,
port=constants.DEFAULT_PORT,
port=None,
user=None,
source=constants.DEFAULT_SOURCE,
catalog=constants.DEFAULT_CATALOG,
Expand Down Expand Up @@ -176,7 +176,6 @@ def __init__(
]

self.host = host if parsed_host.hostname is None else parsed_host.hostname + parsed_host.path
self.port = port if parsed_host.port is None else parsed_host.port
self.user = user
self.source = source
self.catalog = catalog
Expand Down Expand Up @@ -204,6 +203,16 @@ def __init__(
self._http_session = http_session
self.http_headers = http_headers
self.http_scheme = http_scheme if not parsed_host.scheme else parsed_host.scheme

# Infer connection port: `hostname` takes precedence over explicit `port` argument
# If none is given, use default based on HTTP protocol
default_port = constants.DEFAULT_TLS_PORT if self.http_scheme == constants.HTTPS else constants.DEFAULT_PORT
self.port = (
parsed_host.port if parsed_host.port is not None
else port if port is not None
else default_port
)

self.auth = auth
self.extra_credential = extra_credential
self.max_attempts = max_attempts
Expand Down