Skip to content

fix: URI authority generation for schemes without default ports #9605

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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion system/HTTP/URI.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public function getAuthority(bool $ignorePort = false): string
}

// Don't add port if it's a standard port for this scheme
if ((int) $this->port !== 0 && ! $ignorePort && $this->port !== $this->defaultPorts[$this->scheme]) {
if ((int) $this->port !== 0 && ! $ignorePort && $this->port !== ($this->defaultPorts[$this->scheme] ?? null)) {
$authority .= ':' . $this->port;
}

Expand Down
29 changes: 29 additions & 0 deletions tests/system/HTTP/URITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,22 @@ public static function provideAuthorityReturnsExceptedValues(): iterable
'http://me@foo.com:3000/bar',
'me@foo.com:3000',
],
'rtsp-with-port' => [
'rtsp://localhost:1234/stream',
'localhost:1234',
],
'rtsp-no-port' => [
'rtsp://localhost/stream',
'localhost',
],
'custom-scheme-with-port' => [
'myscheme://server:9999/resource',
'server:9999',
],
'custom-scheme-no-port' => [
'myscheme://server/resource',
'server',
],
];
}

Expand Down Expand Up @@ -1226,4 +1242,17 @@ public function testForceGlobalSecureRequestsAndNonHTTPProtocol(): void

$this->assertSame($expected, (string) $uri);
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/9604
*/
public function testAuthorityIncludesPortForCustomSchemes(): void
{
$url = 'rtsp://localhost:1234/stream';
$uri = new URI($url);

$this->assertSame('rtsp://localhost:1234/stream', (string) $uri);
$this->assertSame('localhost:1234', $uri->getAuthority());
$this->assertSame(1234, $uri->getPort());
}
}
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.6.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Bugs Fixed
- **Email:** Fixed a bug where ``Email::getHostname()`` failed to use ``$_SERVER['SERVER_ADDR']`` when ``$_SERVER['SERVER_NAME']`` was not set.
- **Security:** Fixed a bug where the ``sanitize_filename()`` function from the Security helper would throw an error when used in CLI requests.
- **Session:** Fixed a bug where using the ``DatabaseHandler`` with an unsupported database driver (such as ``SQLSRV``, ``OCI8``, or ``SQLite3``) did not throw an appropriate error.
- **URI:** Fixed a bug in ``URI::getAuthority()`` where schemes without defined default ports (like ``rtsp://``) would cause issues due to missing array key handling.

See the repo's
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
Expand Down
Loading