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 <david@grudl.com>
  • Loading branch information
Izolex and dg committed Nov 14, 2023
1 parent 04fbf19 commit 9a14e6e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
51 changes: 29 additions & 22 deletions src/Http/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ 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->parseHost($_SERVER[$tmp])
) {
$url->setHost(rtrim(strtolower($pair[1]), '.'));
if (isset($pair[2])) {
$url->setPort((int) substr($pair[2], 1));
} elseif ($tmp === 'SERVER_NAME' && isset($_SERVER['SERVER_PORT'])) {
$url->setHost($pair[0]);
if (isset($pair[1])) {
$url->setPort($pair[1]);
} elseif (isset($_SERVER['SERVER_PORT'])) {
$url->setPort((int) $_SERVER['SERVER_PORT']);
}
}
Expand Down Expand Up @@ -309,22 +309,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->parseHost($proxyParams['host'][0])
) {
$url->setHost($pair[0]);
if (isset($pair[1])) {
$url->setPort($pair[1]);
}
}
return $remoteAddr ?? null;
Expand Down Expand Up @@ -365,7 +356,23 @@ private function useNonstandardProxy(Url $url): ?string
}


/** @deprecated use fromGlobals() */
/**
* @return array{}|array{0: string}|array{0: string, 1: int}
*/
private function parseHost(string $host): array
{
$pair = [];
if (preg_match('#^([a-z0-9_.-]+|\[[a-f0-9:]+\])(:\d+)?$#Di', $host, $matches)) {
$pair[] = rtrim(strtolower($matches[1]), '.');
if (isset($matches[2])) {
$pair[] = (int) substr($matches[2], 1);
}
}
return $pair;
}


/** @deprecated */
public function createHttpRequest(): Request
{
trigger_error(__METHOD__ . '() is deprecated, use fromGlobals()', E_USER_DEPRECATED);
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::null(@$factory->fromGlobals()->getRemoteHost()); // deprecated

$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 9a14e6e

Please sign in to comment.