Skip to content

Commit 5f6960b

Browse files
pquentingithub-actions[bot]
authored andcommitted
Always set default HTTPS port to 443 (#127)
This is the correct port most of the time, including in our cloud offering. (cherry picked from commit 1c0993b)
1 parent 65424ca commit 5f6960b

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

elastic_transport/client_utils.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -186,27 +186,23 @@ def url_to_node_config(
186186
) -> NodeConfig:
187187
"""Constructs a :class:`elastic_transport.NodeConfig` instance from a URL.
188188
If a username/password are specified in the URL they are converted to an
189-
'Authorization' header.
189+
'Authorization' header. Always fills in a default port for HTTPS.
190190
191191
:param url: URL to transform into a NodeConfig.
192-
:param use_default_ports_for_scheme: If 'True' will resolve default ports for the given scheme.
192+
:param use_default_ports_for_scheme: If 'True' will resolve default ports for HTTP.
193193
"""
194194
try:
195195
parsed_url = parse_url(url)
196196
except LocationParseError:
197197
raise ValueError(f"Could not parse URL {url!r}") from None
198198

199-
# Only fill in a default port for HTTP and HTTPS
200-
# when we know the scheme is one of those two.
201199
parsed_port: Optional[int] = parsed_url.port
202-
if (
203-
parsed_url.port is None
204-
and parsed_url.scheme is not None
205-
and use_default_ports_for_scheme is True
206-
):
200+
if parsed_url.port is None and parsed_url.scheme is not None:
201+
# Always fill in a default port for HTTPS
207202
if parsed_url.scheme == "https":
208203
parsed_port = 443
209-
elif parsed_url.scheme == "http":
204+
# Only fill HTTP default port when asked to explicitly
205+
elif parsed_url.scheme == "http" and use_default_ports_for_scheme:
210206
parsed_port = 80
211207

212208
if any(

tests/test_client_utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ def test_invalid_cloud_id(cloud_id):
145145
@pytest.mark.parametrize(
146146
["url", "node_base_url", "path_prefix"],
147147
[
148+
("https://localhost", "https://localhost:443", ""),
148149
("http://localhost:3002", "http://localhost:3002", ""),
149150
("http://127.0.0.1:3002", "http://127.0.0.1:3002", ""),
150151
("http://127.0.0.1:3002/", "http://127.0.0.1:3002", ""),
@@ -158,6 +159,11 @@ def test_invalid_cloud_id(cloud_id):
158159
"http://localhost:3002/url-prefix",
159160
"/url-prefix",
160161
),
162+
(
163+
"https://localhost/url-prefix",
164+
"https://localhost:443/url-prefix",
165+
"/url-prefix",
166+
),
161167
("http://[::1]:3002/url-prefix", "http://[::1]:3002/url-prefix", "/url-prefix"),
162168
("https://[::1]:0/", "https://[::1]:0", ""),
163169
],

0 commit comments

Comments
 (0)