Skip to content

Preserve hostname when normalizing URIs with an empty port - #171

Open
xgbtxy wants to merge 1 commit into
python-hyper:mainfrom
xgbtxy:fix/empty-port-normalization-preserves-host
Open

xgbtxy wants to merge 1 commit into
python-hyper:mainfrom
xgbtxy:fix/empty-port-normalization-preserves-host

Conversation

@xgbtxy

@xgbtxy xgbtxy commented Nov 27, 2026

Copy link
Copy Markdown

Problem

PORT_RE in src/rfc3986/abnf_regexp.py is defined as "[0-9]{1,5}", whereas RFC 3986 §3.2.3 defines port = *DIGIT (zero or more digits) and §6.2.3 states that URI producers and normalizers should omit the ":" delimiter when the port is empty.

Because an empty port such as HTTP://example.com:/a fails SUBAUTHORITY_MATCHER, authority_info() raises InvalidAuthority. URIReference.normalize() catches InvalidAuthority via normalize_authority() and drops the authority components, silently rewriting HTTP://example.com:/a to "http:/a" and erasing the hostname.

Fix

  • Update PORT_RE in src/rfc3986/abnf_regexp.py from "[0-9]{1,5}" to "[0-9]{0,5}" so authorities with an empty port match SUBAUTHORITY_MATCHER.
  • Normalize an empty matched "port" ("") to None in URIMixin.authority_info() (src/rfc3986/_mixin.py), preserving existing uri.port is None semantics (including test_handles_colon_but_no_port) while keeping host and userinfo intact so normalize().unsplit() produces "http://example.com/a".
  • Add parametrized regression tests (test_empty_port_preserves_host_on_normalize) in tests/test_uri.py covering reg-name, userinfo + dot-segment path, and IPv6 literal hosts with empty ports.

Verification

  • Confirmed test_empty_port_preserves_host_on_normalize fails before the fix ("http:/a") and passes after.
  • Passed full test suite with 100% coverage: pytest --cov rfc3986 --cov-fail-under 100 tests/.
  • Verified formatting (line-length = 79) and lint checks cleanly on all modified files.

Allow zero-digit ports in PORT_RE per RFC 3986 section 3.2.3 and normalize empty port matches to None in URIMixin.authority_info() so normalize() omits the colon without dropping the host.
Copilot AI balanced review requested due to automatic review settings November 27, 2026 01:23

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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