Skip to content

Commit 9a14e6e

Browse files
Izolexdg
andauthored
RequestFactory: unify and fix host parsing (#229)
Co-authored-by: David Grudl <david@grudl.com>
1 parent 04fbf19 commit 9a14e6e

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

src/Http/RequestFactory.php

+29-22
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ private function getServer(Url $url): void
8181

8282
if (
8383
(isset($_SERVER[$tmp = 'HTTP_HOST']) || isset($_SERVER[$tmp = 'SERVER_NAME']))
84-
&& preg_match('#^([a-z0-9_.-]+|\[[a-f0-9:]+\])(:\d+)?$#Di', $_SERVER[$tmp], $pair)
84+
&& $pair = $this->parseHost($_SERVER[$tmp])
8585
) {
86-
$url->setHost(rtrim(strtolower($pair[1]), '.'));
87-
if (isset($pair[2])) {
88-
$url->setPort((int) substr($pair[2], 1));
89-
} elseif ($tmp === 'SERVER_NAME' && isset($_SERVER['SERVER_PORT'])) {
86+
$url->setHost($pair[0]);
87+
if (isset($pair[1])) {
88+
$url->setPort($pair[1]);
89+
} elseif (isset($_SERVER['SERVER_PORT'])) {
9090
$url->setPort((int) $_SERVER['SERVER_PORT']);
9191
}
9292
}
@@ -309,22 +309,13 @@ private function useForwardedProxy(Url $url): ?string
309309
$url->setPort($url->getScheme() === 'https' ? 443 : 80);
310310
}
311311

312-
if (isset($proxyParams['host']) && count($proxyParams['host']) === 1) {
313-
$host = $proxyParams['host'][0];
314-
$startingDelimiterPosition = strpos($host, '[');
315-
if ($startingDelimiterPosition === false) { //IPv4
316-
$pair = explode(':', $host);
317-
$url->setHost($pair[0]);
318-
if (isset($pair[1])) {
319-
$url->setPort((int) $pair[1]);
320-
}
321-
} else { //IPv6
322-
$endingDelimiterPosition = strpos($host, ']');
323-
$url->setHost(substr($host, strpos($host, '[') + 1, $endingDelimiterPosition - 1));
324-
$pair = explode(':', substr($host, $endingDelimiterPosition));
325-
if (isset($pair[1])) {
326-
$url->setPort((int) $pair[1]);
327-
}
312+
if (
313+
isset($proxyParams['host']) && count($proxyParams['host']) === 1
314+
&& $pair = $this->parseHost($proxyParams['host'][0])
315+
) {
316+
$url->setHost($pair[0]);
317+
if (isset($pair[1])) {
318+
$url->setPort($pair[1]);
328319
}
329320
}
330321
return $remoteAddr ?? null;
@@ -365,7 +356,23 @@ private function useNonstandardProxy(Url $url): ?string
365356
}
366357

367358

368-
/** @deprecated use fromGlobals() */
359+
/**
360+
* @return array{}|array{0: string}|array{0: string, 1: int}
361+
*/
362+
private function parseHost(string $host): array
363+
{
364+
$pair = [];
365+
if (preg_match('#^([a-z0-9_.-]+|\[[a-f0-9:]+\])(:\d+)?$#Di', $host, $matches)) {
366+
$pair[] = rtrim(strtolower($matches[1]), '.');
367+
if (isset($matches[2])) {
368+
$pair[] = (int) substr($matches[2], 1);
369+
}
370+
}
371+
return $pair;
372+
}
373+
374+
375+
/** @deprecated */
369376
public function createHttpRequest(): Request
370377
{
371378
trigger_error(__METHOD__ . '() is deprecated, use fromGlobals()', E_USER_DEPRECATED);

tests/Http/RequestFactory.proxy.forwarded.phpt

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ test('', function () {
6565
Assert::null(@$factory->fromGlobals()->getRemoteHost()); // deprecated
6666

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

7171
test('', function () {
@@ -83,7 +83,7 @@ test('', function () {
8383

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

8989

0 commit comments

Comments
 (0)