Skip to content

Commit

Permalink
RequestFactory: unify and fix host parsing (#229)
Browse files Browse the repository at this point in the history
Co-authored-by: David Grudl <[email protected]>
  • Loading branch information
Izolex and dg committed Nov 14, 2023
1 parent 54d7971 commit 87e6182
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
43 changes: 23 additions & 20 deletions src/Http/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ private function getServer(Url $url): void

if (
(isset($_SERVER[$tmp = 'HTTP_HOST']) || isset($_SERVER[$tmp = 'SERVER_NAME']))
&& preg_match('#^([a-z0-9_.-]+|\[[a-f0-9:]+\])(:\d+)?$#Di', $_SERVER[$tmp], $pair)
&& ($pair = $this->parseHostAndPort($_SERVER[$tmp]))
) {
$url->setHost(rtrim(strtolower($pair[1]), '.'));
if (isset($pair[2])) {
$url->setPort((int) substr($pair[2], 1));
$url->setHost($pair[0]);
if (isset($pair[1])) {
$url->setPort($pair[1]);
} elseif ($tmp === 'SERVER_NAME' && isset($_SERVER['SERVER_PORT'])) {
$url->setPort((int) $_SERVER['SERVER_PORT']);
}
Expand Down Expand Up @@ -321,22 +321,13 @@ private function useForwardedProxy(Url $url): ?string
$url->setPort($url->getScheme() === 'https' ? 443 : 80);
}

if (isset($proxyParams['host']) && count($proxyParams['host']) === 1) {
$host = $proxyParams['host'][0];
$startingDelimiterPosition = strpos($host, '[');
if ($startingDelimiterPosition === false) { //IPv4
$pair = explode(':', $host);
$url->setHost($pair[0]);
if (isset($pair[1])) {
$url->setPort((int) $pair[1]);
}
} else { //IPv6
$endingDelimiterPosition = strpos($host, ']');
$url->setHost(substr($host, strpos($host, '[') + 1, $endingDelimiterPosition - 1));
$pair = explode(':', substr($host, $endingDelimiterPosition));
if (isset($pair[1])) {
$url->setPort((int) $pair[1]);
}
if (
isset($proxyParams['host']) && count($proxyParams['host']) === 1
&& ($pair = $this->parseHostAndPort($proxyParams['host'][0]))
) {
$url->setHost($pair[0]);
if (isset($pair[1])) {
$url->setPort($pair[1]);
}
}
return $remoteAddr ?? null;
Expand Down Expand Up @@ -378,6 +369,18 @@ private function useNonstandardProxy(Url $url): ?string
}


/** @return array{string, ?int}|null */
private function parseHostAndPort(string $s): ?array
{
return preg_match('#^([a-z0-9_.-]+|\[[a-f0-9:]+])(:\d+)?$#Di', $s, $matches)
? [
rtrim(strtolower($matches[1]), '.'),
isset($matches[2]) ? (int) substr($matches[2], 1) : null,
]
: null;
}


/** @deprecated */
public function createHttpRequest(): Request
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Http/RequestFactory.proxy.forwarded.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ test('', function () {
Assert::same('2001:db8:cafe::17', $factory->fromGlobals()->getRemoteHost());

$url = $factory->fromGlobals()->getUrl();
Assert::same('2001:db8:cafe::18', $url->getHost());
Assert::same('[2001:db8:cafe::18]', $url->getHost());
});

test('', function () {
Expand All @@ -83,7 +83,7 @@ test('', function () {

$url = $factory->fromGlobals()->getUrl();
Assert::same(47832, $url->getPort());
Assert::same('2001:db8:cafe::18', $url->getHost());
Assert::same('[2001:db8:cafe::18]', $url->getHost());
});


Expand Down

0 comments on commit 87e6182

Please sign in to comment.