@@ -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 );
0 commit comments