Skip to content

Preserve explicit port 0 in connection_from_url and HTTP/2 authority - #5101

Merged
illia-v merged 3 commits into
urllib3:mainfrom
nishat-06:preserve-explicit-port-zero
Aug 2, 2026
Merged

illia-v merged 3 commits into
urllib3:mainfrom
nishat-06:preserve-explicit-port-zero

Conversation

@nishat-06

Copy link
Copy Markdown
Contributor

connection_from_url() and the HTTP/2 request authority build the port with a truthiness check, so an explicit :0 in the URL gets dropped and replaced by the default 80 or 443. This is the same handling #5071 corrected for URL parsing, pool selection, and proxy configuration, but these two call sites were left using port or <default>. Switching them to an explicit None check keeps an explicit port 0.

@illia-v illia-v left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

This is a good follow-up to a recent #5071.

Comment on lines +130 to +134
port = self.port if self.port is not None else 443
if ":" in self.host:
authority = f"[{self.host}]:{self.port or 443}"
authority = f"[{self.host}]:{port}"
else:
authority = f"{self.host}:{self.port or 443}"
authority = f"{self.host}:{port}"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test covering these lines

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Added test_request_authority_port_zero in test/test_http2_connection.py, it builds the request with port 0 and asserts the :authority header comes out as example.com:0. It fails against the old self.port or 443 (gives :443) and passes with the None check.

@illia-v
illia-v merged commit ba8b97b into urllib3:main Aug 2, 2026
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants