From a65d40052b3cd88b32e2e5688f4aa2cf1c197f36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ana=C3=AFs=20Babel?= Date: Mon, 22 Apr 2024 13:22:45 +0200 Subject: [PATCH] Allow undescore character in Uri host I don't understand why this validity check has be added. Has per rfc 2181 (https://datatracker.ietf.org/doc/html/rfc2181#section-11), underscore are valid character to use in an uri host. For my specific usage, it broke for requests using docker internal hostnames. added test to prevent regression on URI containing underscore in host --- src/Message/Uri.php | 4 ++-- tests/Message/UriTest.php | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Message/Uri.php b/src/Message/Uri.php index 4309bbed..1eaf24fe 100644 --- a/src/Message/Uri.php +++ b/src/Message/Uri.php @@ -55,7 +55,7 @@ public function __construct($uri) } // @codeCoverageIgnoreEnd - if ($parts === false || (isset($parts['scheme']) && !\preg_match('#^[a-z]+$#i', $parts['scheme'])) || (isset($parts['host']) && \preg_match('#[\s_%+]#', $parts['host']))) { + if ($parts === false || (isset($parts['scheme']) && !\preg_match('#^[a-z]+$#i', $parts['scheme'])) || (isset($parts['host']) && \preg_match('#[\s%+]#', $parts['host']))) { throw new \InvalidArgumentException('Invalid URI given'); } @@ -173,7 +173,7 @@ public function withHost($host) return $this; } - if (\preg_match('#[\s_%+]#', $host) || ($host !== '' && \parse_url('http://' . $host, \PHP_URL_HOST) !== $host)) { + if (\preg_match('#[\s%+]#', $host) || ($host !== '' && \parse_url('http://' . $host, \PHP_URL_HOST) !== $host)) { throw new \InvalidArgumentException('Invalid URI host given'); } diff --git a/tests/Message/UriTest.php b/tests/Message/UriTest.php index 05eec723..1ed6abf2 100644 --- a/tests/Message/UriTest.php +++ b/tests/Message/UriTest.php @@ -120,7 +120,10 @@ public static function provideValidUris() ), array( 'http://user%20name:pass%20word@localhost/path%20name?query%20name#frag%20ment' - ) + ), + array( + 'http://docker_container/' + ), ); }