Skip to content
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

[Backport 8.11] Always set default HTTPS port to 443 #128

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 6 additions & 10 deletions elastic_transport/client_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,27 +186,23 @@ def url_to_node_config(
) -> NodeConfig:
"""Constructs a :class:`elastic_transport.NodeConfig` instance from a URL.
If a username/password are specified in the URL they are converted to an
'Authorization' header.
'Authorization' header. Always fills in a default port for HTTPS.

:param url: URL to transform into a NodeConfig.
:param use_default_ports_for_scheme: If 'True' will resolve default ports for the given scheme.
:param use_default_ports_for_scheme: If 'True' will resolve default ports for HTTP.
"""
try:
parsed_url = parse_url(url)
except LocationParseError:
raise ValueError(f"Could not parse URL {url!r}") from None

# Only fill in a default port for HTTP and HTTPS
# when we know the scheme is one of those two.
parsed_port: Optional[int] = parsed_url.port
if (
parsed_url.port is None
and parsed_url.scheme is not None
and use_default_ports_for_scheme is True
):
if parsed_url.port is None and parsed_url.scheme is not None:
# Always fill in a default port for HTTPS
if parsed_url.scheme == "https":
parsed_port = 443
elif parsed_url.scheme == "http":
# Only fill HTTP default port when asked to explicitly
elif parsed_url.scheme == "http" and use_default_ports_for_scheme:
parsed_port = 80

if any(
Expand Down
6 changes: 6 additions & 0 deletions tests/test_client_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ def test_invalid_cloud_id(cloud_id):
@pytest.mark.parametrize(
["url", "node_base_url", "path_prefix"],
[
("https://localhost", "https://localhost:443", ""),
("http://localhost:3002", "http://localhost:3002", ""),
("http://127.0.0.1:3002", "http://127.0.0.1:3002", ""),
("http://127.0.0.1:3002/", "http://127.0.0.1:3002", ""),
Expand All @@ -158,6 +159,11 @@ def test_invalid_cloud_id(cloud_id):
"http://localhost:3002/url-prefix",
"/url-prefix",
),
(
"https://localhost/url-prefix",
"https://localhost:443/url-prefix",
"/url-prefix",
),
("http://[::1]:3002/url-prefix", "http://[::1]:3002/url-prefix", "/url-prefix"),
("https://[::1]:0/", "https://[::1]:0", ""),
],
Expand Down