| @@ -59,13 +59,13 @@ | ||
| 59 | 59 | } |
| 60 | 60 | private function getServer(Url $url) : void |
| 61 | 61 | { |
| 62 | 62 | $url->setScheme(!empty($_SERVER['HTTPS']) && \strcasecmp($_SERVER['HTTPS'], 'off') ? 'https' : 'http'); |
| 63 | - if ((isset($_SERVER[$tmp = 'HTTP_HOST']) || isset($_SERVER[$tmp = 'SERVER_NAME'])) && \preg_match('#^([a-z0-9_.-]+|\\[[a-f0-9:]+\\])(:\\d+)?$#Di', $_SERVER[$tmp], $pair)) { | |
| 64 | - $url->setHost(\rtrim(\strtolower($pair[1]), '.')); | |
| 65 | - if (isset($pair[2])) { | |
| 66 | - $url->setPort((int) \substr($pair[2], 1)); | |
| 67 | - } elseif (isset($_SERVER['SERVER_PORT'])) { | |
| 63 | + if ((isset($_SERVER[$tmp = 'HTTP_HOST']) || isset($_SERVER[$tmp = 'SERVER_NAME'])) && ($pair = $this->parseHostAndPort($_SERVER[$tmp]))) { | |
| 64 | + $url->setHost($pair[0]); | |
| 65 | + if (isset($pair[1])) { | |
| 66 | + $url->setPort($pair[1]); | |
| 67 | + } elseif ($tmp === 'SERVER_NAME' && isset($_SERVER['SERVER_PORT'])) { | |
| 68 | 68 | $url->setPort((int) $_SERVER['SERVER_PORT']); |
| 69 | 69 | } |
| 70 | 70 | } |
| 71 | 71 | } |
| @@ -221,26 +221,12 @@ | ||
| 221 | 221 | if (isset($proxyParams['proto']) && \count($proxyParams['proto']) === 1) { |
| 222 | 222 | $url->setScheme(\strcasecmp($proxyParams['proto'][0], 'https') === 0 ? 'https' : 'http'); |
| 223 | 223 | $url->setPort($url->getScheme() === 'https' ? 443 : 80); |
| 224 | 224 | } |
| 225 | - if (isset($proxyParams['host']) && \count($proxyParams['host']) === 1) { | |
| 226 | - $host = $proxyParams['host'][0]; | |
| 227 | - $startingDelimiterPosition = \strpos($host, '['); | |
| 228 | - if ($startingDelimiterPosition === \false) { | |
| 229 | - //IPv4 | |
| 230 | - $pair = \explode(':', $host); | |
| 231 | - $url->setHost($pair[0]); | |
| 232 | - if (isset($pair[1])) { | |
| 233 | - $url->setPort((int) $pair[1]); | |
| 234 | - } | |
| 235 | - } else { | |
| 236 | - //IPv6 | |
| 237 | - $endingDelimiterPosition = \strpos($host, ']'); | |
| 238 | - $url->setHost(\substr($host, \strpos($host, '[') + 1, $endingDelimiterPosition - 1)); | |
| 239 | - $pair = \explode(':', \substr($host, $endingDelimiterPosition)); | |
| 240 | - if (isset($pair[1])) { | |
| 241 | - $url->setPort((int) $pair[1]); | |
| 242 | - } | |
| 225 | + if (isset($proxyParams['host']) && \count($proxyParams['host']) === 1 && ($pair = $this->parseHostAndPort($proxyParams['host'][0]))) { | |
| 226 | + $url->setHost($pair[0]); | |
| 227 | + if (isset($pair[1])) { | |
| 228 | + $url->setPort($pair[1]); | |
| 243 | 229 | } |
| 244 | 230 | } |
| 245 | 231 | return $remoteAddr ?? null; |
| 246 | 232 | } |
| @@ -270,8 +256,13 @@ | ||
| 270 | 256 | $url->setHost(\trim($xForwardedHost[$xForwardedForRealIpKey])); |
| 271 | 257 | } |
| 272 | 258 | } |
| 273 | 259 | return $remoteAddr ?? null; |
| 260 | + } | |
| 261 | + /** @return array{string, ?int}|null */ | |
| 262 | + private function parseHostAndPort(string $s) : ?array | |
| 263 | + { | |
| 264 | + 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; | |
| 274 | 265 | } |
| 275 | 266 | /** @deprecated */ |
| 276 | 267 | public function createHttpRequest() : Request |
| 277 | 268 | { |