Skip to content

Conversation

Copy link

Copilot AI commented Oct 24, 2025

Problem

Gratuitous leading zeros in port numbers were not being stripped when calling canonical() on URI objects. This resulted in non-canonical port representations:

# Before this fix:
perl -MURI -e "say URI->new('http://xyz:08080')->canonical->port"
# Output: 08080  (expected: 8080)

While default ports with leading zeros were correctly removed (due to Perl's numeric comparison in the existing code), non-default ports retained their leading zeros:

# These worked correctly before:
perl -MURI -e "say URI->new('http://xyz:080')->canonical->port"
# Output: 80 (port removed from URI as it's the default)

perl -MURI -e "say URI->new('https://xyz:0443')->canonical->port"
# Output: 443 (port removed from URI as it's the default)

Solution

Modified the canonical() method in URI::_server to detect and normalize port numbers with leading zeros. The fix:

  1. Detects ports with leading zeros using the regex /^0\d/ (matches "0" followed by any digit)
  2. Normalizes such ports by converting to integer with int($port), which strips leading zeros
  3. Avoids normalizing port "0" alone (handled correctly by the regex)
  4. Preserves existing behavior for default port removal

Changes

  • lib/URI/_server.pm: Added port normalization logic (3 lines)
  • t/port-leading-zeros.t: New comprehensive test suite with 15 tests

Testing

The fix handles various edge cases:

  • Non-default ports: 080808080, 012341234
  • Default ports: 08080 (removed), 0443443 (removed)
  • Multiple leading zeros: 00443443
  • Edge case: 0 remains 0, 000
  • Works across all protocols (HTTP, HTTPS, FTP, etc.)

All existing tests continue to pass.

Fixes #[issue-number]

Original prompt

This section details on the original issue you should resolve

<issue_title>Leading Zeros in Port Numbers</issue_title>
<issue_description>Gratuitous leading zeros in port numbers should be stripped off. But the behavior of URI version 1.76 is:

$ perl -MURI -e "CORE::say URI->new('http://xyz:08080')->canonical->port"
08080

It already works as expected though for default ports:

$ perl -MURI -e "CORE::say URI->new('http://xyz:080')->canonical->port"
80
$ perl -MURI -e "CORE::say URI->new('https://xyz:0443')->canonical->port"
443
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

Fixes #69


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits October 24, 2025 04:17
Co-authored-by: oalders <96205+oalders@users.noreply.github.com>
…f port 0

Co-authored-by: oalders <96205+oalders@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix leading zeros in port numbers handling Strip leading zeros from port numbers in canonical URIs Oct 24, 2025
Copilot finished work on behalf of oalders October 24, 2025 04:22
Copilot AI requested a review from oalders October 24, 2025 04:22
@codecov
Copy link

codecov bot commented Oct 29, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.09%. Comparing base (185b40a) to head (4f7d836).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #162      +/-   ##
==========================================
+ Coverage   87.03%   87.09%   +0.06%     
==========================================
  Files          64       64              
  Lines        2091     2093       +2     
  Branches      511      512       +1     
==========================================
+ Hits         1820     1823       +3     
  Misses        109      109              
+ Partials      162      161       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

Leading Zeros in Port Numbers

2 participants