diff --git a/src/ServerRequest.php b/src/ServerRequest.php index 604bf9ae..7a50d848 100644 --- a/src/ServerRequest.php +++ b/src/ServerRequest.php @@ -136,12 +136,27 @@ public static function fromGlobals() public static function getUriFromGlobals() { $uri = new Uri(''); - return $uri - ->withScheme(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http') - ->withHost($_SERVER['SERVER_NAME']) - ->withPort($_SERVER['SERVER_PORT']) - ->withPath(current(explode('?', $_SERVER['REQUEST_URI']))) - ->withQuery(isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : ''); + if (isset($_SERVER['HTTPS'])) { + $uri = $uri->withScheme($_SERVER['HTTPS'] == 'on' ? 'https' : 'http'); + } + + if (isset($_SERVER['SERVER_NAME'])) { + $uri = $uri->withHost($_SERVER['SERVER_NAME']); + } + + if (isset($_SERVER['SERVER_PORT'])) { + $uri = $uri->withPort($_SERVER['SERVER_PORT']); + } + + if (isset($_SERVER['REQUEST_URI'])) { + $uri = $uri->withPath(current(explode('?', $_SERVER['REQUEST_URI']))); + } + + if (isset($_SERVER['QUERY_STRING'])) { + $uri = $uri->withQuery($_SERVER['QUERY_STRING']); + } + + return $uri; } /** diff --git a/tests/ServerRequestTest.php b/tests/ServerRequestTest.php index 8810ee14..66703b30 100644 --- a/tests/ServerRequestTest.php +++ b/tests/ServerRequestTest.php @@ -325,6 +325,10 @@ public function dataGetUriFromGlobals() 'http://www.blakesimpson.co.uk:8324/blog/article.php?id=10&user=foo', array_merge($server, ['SERVER_PORT' => '8324']), ], + 'Empty server variable' => [ + '', + [], + ], ]; }