Skip to content

Commit 888e3c0

Browse files
lulhumclue
authored andcommitted
Allow underscore 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
1 parent f2b8bf3 commit 888e3c0

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/Message/Uri.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function __construct($uri)
5555
}
5656
// @codeCoverageIgnoreEnd
5757

58-
if ($parts === false || (isset($parts['scheme']) && !\preg_match('#^[a-z]+$#i', $parts['scheme'])) || (isset($parts['host']) && \preg_match('#[\s_%+]#', $parts['host']))) {
58+
if ($parts === false || (isset($parts['scheme']) && !\preg_match('#^[a-z]+$#i', $parts['scheme'])) || (isset($parts['host']) && \preg_match('#[\s%+]#', $parts['host']))) {
5959
throw new \InvalidArgumentException('Invalid URI given');
6060
}
6161

@@ -173,7 +173,7 @@ public function withHost($host)
173173
return $this;
174174
}
175175

176-
if (\preg_match('#[\s_%+]#', $host) || ($host !== '' && \parse_url('http://' . $host, \PHP_URL_HOST) !== $host)) {
176+
if (\preg_match('#[\s%+]#', $host) || ($host !== '' && \parse_url('http://' . $host, \PHP_URL_HOST) !== $host)) {
177177
throw new \InvalidArgumentException('Invalid URI host given');
178178
}
179179

tests/Message/UriTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ public static function provideValidUris()
120120
),
121121
array(
122122
'http://user%20name:pass%20word@localhost/path%20name?query%20name#frag%20ment'
123+
),
124+
array(
125+
'http://docker_container/'
123126
)
124127
);
125128
}
@@ -338,6 +341,16 @@ public function testWithHostReturnsNewInstanceWhenHostIsChanged()
338341
$this->assertEquals('localhost', $uri->getHost());
339342
}
340343

344+
public function testWithHostReturnsNewInstanceWhenHostIsChangedWithUnderscore()
345+
{
346+
$uri = new Uri('http://localhost');
347+
348+
$new = $uri->withHost('docker_container');
349+
$this->assertNotSame($uri, $new);
350+
$this->assertEquals('docker_container', $new->getHost());
351+
$this->assertEquals('localhost', $uri->getHost());
352+
}
353+
341354
public function testWithHostReturnsNewInstanceWithHostToLowerCaseWhenHostIsChangedWithUpperCase()
342355
{
343356
$uri = new Uri('http://localhost');

0 commit comments

Comments
 (0)