Skip to content

Commit e29ce02

Browse files
authored
fix: URI authority generation for schemes without default ports (#9605)
* fix: URI authority generation for schemes without default ports * fix typo
1 parent 863faee commit e29ce02

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

system/HTTP/URI.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ public function getAuthority(bool $ignorePort = false): string
376376
}
377377

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

tests/system/HTTP/URITest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,22 @@ public static function provideAuthorityReturnsExceptedValues(): iterable
622622
'http://[email protected]:3000/bar',
623623
624624
],
625+
'rtsp-with-port' => [
626+
'rtsp://localhost:1234/stream',
627+
'localhost:1234',
628+
],
629+
'rtsp-no-port' => [
630+
'rtsp://localhost/stream',
631+
'localhost',
632+
],
633+
'custom-scheme-with-port' => [
634+
'myscheme://server:9999/resource',
635+
'server:9999',
636+
],
637+
'custom-scheme-no-port' => [
638+
'myscheme://server/resource',
639+
'server',
640+
],
625641
];
626642
}
627643

@@ -1226,4 +1242,17 @@ public function testForceGlobalSecureRequestsAndNonHTTPProtocol(): void
12261242

12271243
$this->assertSame($expected, (string) $uri);
12281244
}
1245+
1246+
/**
1247+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/9604
1248+
*/
1249+
public function testAuthorityIncludesPortForCustomSchemes(): void
1250+
{
1251+
$url = 'rtsp://localhost:1234/stream';
1252+
$uri = new URI($url);
1253+
1254+
$this->assertSame('rtsp://localhost:1234/stream', (string) $uri);
1255+
$this->assertSame('localhost:1234', $uri->getAuthority());
1256+
$this->assertSame(1234, $uri->getPort());
1257+
}
12291258
}

user_guide_src/source/changelogs/v4.6.2.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Bugs Fixed
4141
- **Email:** Fixed a bug where ``Email::getHostname()`` failed to use ``$_SERVER['SERVER_ADDR']`` when ``$_SERVER['SERVER_NAME']`` was not set.
4242
- **Security:** Fixed a bug where the ``sanitize_filename()`` function from the Security helper would throw an error when used in CLI requests.
4343
- **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.
44+
- **URI:** Fixed a bug in ``URI::getAuthority()`` where schemes without defined default ports (like ``rtsp://``) would cause issues due to missing array key handling.
4445

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

0 commit comments

Comments
 (0)