Skip to content

Commit 1a07976

Browse files
her-cathexianghuilimingxinleo
authored
Fixed bug that method Request::parseHost not working when host is invalid. (#5142)
Co-authored-by: hexianghui <[email protected]> Co-authored-by: 李铭昕 <[email protected]>
1 parent 992a16b commit 1a07976

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/Server/Request.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,9 @@ private static function getUriFromGlobals(Swoole\Http\Request $swooleRequest): U
591591
private static function parseHost(string $httpHost): array
592592
{
593593
$parts = parse_url('//' . $httpHost);
594+
if (! isset($parts['host'])) {
595+
throw new InvalidArgumentException('Invalid host: ' . $httpHost);
596+
}
594597

595598
return [$parts['host'], $parts['port'] ?? null];
596599
}

tests/ServerRequestTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Hyperf\Utils\Codec\Xml;
2323
use HyperfTest\HttpMessage\Stub\ParserStub;
2424
use HyperfTest\HttpMessage\Stub\Server\RequestStub;
25+
use InvalidArgumentException;
2526
use Mockery;
2627
use PHPUnit\Framework\TestCase;
2728
use Psr\Container\ContainerInterface;
@@ -174,6 +175,10 @@ public function testParseHost()
174175
$resIPv6 = $method->invokeArgs(null, [$hostStrIPv6]);
175176
$this->assertSame('[fe80::a464:1aff:fe88:7b5a]', $resIPv6[0]);
176177
$this->assertSame(9502, $resIPv6[1]);
178+
179+
$this->expectException(InvalidArgumentException::class);
180+
$this->expectExceptionMessage('Invalid host: ');
181+
$method->invokeArgs(null, ['']);
177182
}
178183

179184
/**

0 commit comments

Comments
 (0)