src/Http/RequestFactory.php
protected function createUriFromSuperglobals(array $server): Uri
    {
        $isUsingTrustedProxy = $this->isUsingTrustedProxy($server);

        if ($isUsingTrustedProxy && isset($server[$this->trustedHeaderNames['HTTP_CLIENT_PROTO']])) {
            $protoString = $server[$this->trustedHeaderNames['HTTP_CLIENT_PROTO']];
            $protoArray = explode(',', $protoString);
            $isSecure = count($protoArray) > 0 && in_array(strtolower($protoArray[0]), ['https', 'ssl', 'on'], true);
        } else {
            $isSecure = isset($server['HTTPS']) && $server['HTTPS'] !== 'off';
        }

        $rawProtocol = isset($server['SERVER_PROTOCOL']) ? strtolower($server['SERVER_PROTOCOL']) : 'http/1.1';
        $scheme = substr($rawProtocol, 0, strpos($rawProtocol, '/')) . ($isSecure ? 's' : '');
        $user = $server['PHP_AUTH_USER'] ?? null;
        $password = $server['PHP_AUTH_PW'] ?? null;
        $port = null;

        if ($isUsingTrustedProxy) {
            if (isset($server[$this->trustedHeaderNames['HTTP_CLIENT_PORT']])) {
                $port = (int)$server[$this->trustedHeaderNames['HTTP_CLIENT_PORT']];
            } elseif (
                isset($server[$this->trustedHeaderNames['HTTP_CLIENT_PROTO']]) &&
                $server[$this->trustedHeaderNames['HTTP_CLIENT_PROTO']] === 'https'
            ) {
                $port = 443;
            }
        }

        if ($port === null) {
            $port = isset($server['SERVER_PORT']) ? (int)$server['SERVER_PORT'] : null;
        }

        if ($isUsingTrustedProxy && isset($server[$this->trustedHeaderNames['HTTP_CLIENT_HOST']])) {
            $hostWithPort = explode(',', $server[$this->trustedHeaderNames['HTTP_CLIENT_HOST']]);
            $hostWithPort = trim(end($hostWithPort));
        } else {
            $hostWithPort = $server['HTTP_HOST'] ?? $server['SERVER_NAME'] ?? $server['SERVER_ADDR'] ?? '';
        }

        // Remove the port from the host
        $host = strtolower(preg_replace("/:\d+$/", '', trim($hostWithPort)));

        // Check for forbidden characters
        if (!empty($host) && !empty(preg_replace("/(?:^\[)?[a-zA-Z0-9-:\]_]+\.?/", '', $host))) {
            throw new InvalidArgumentException("Invalid host \"$host\"");
        }

        $path = parse_url('http://foo.com' . ($server['REQUEST_URI'] ?? ''), PHP_URL_PATH);
        $path = $path === false ? '' : ($path ?? '');
        $queryString = $server['QUERY_STRING'] ?? '';

        if ($queryString === '') {
            $queryString = parse_url('http://foo.com' . ($server['REQUEST_URI'] ?? ''), PHP_URL_QUERY);
            $queryString = $queryString === false || $queryString === null ? '' : $queryString;
        }

        // The "?" is simply the separator for the query string, not actually part of the query string
        $queryString = ltrim($queryString, '?');
        $uriString = "$scheme://";

        if ($user !== null) {
            $uriString .= "$user:" . ($password ?? '') . '@';
        }

        $uriString .= $host . ($port === null ? '' : ":$port") . "{$path}?{$queryString}";

        return new Uri($uriString);
    }
protected function createUriFromSuperglobals(array $server): Uri
    {
        $isUsingTrustedProxy = $this->isUsingTrustedProxy($server);

        if ($isUsingTrustedProxy && isset($server[$this->trustedHeaderNames['HTTP_CLIENT_PROTO']])) {
            $protoString = $server[$this->trustedHeaderNames['HTTP_CLIENT_PROTO']];
            $protoArray = explode(',', $protoString);
            $isSecure = count($protoArray) > 0 && in_array(strtolower($protoArray[0]), ['https', 'ssl', 'on'], true);
        } else {
            $isSecure = isset($server['HTTPS']) && $server['HTTPS'] !== 'off';
        }

        $rawProtocol = isset($server['SERVER_PROTOCOL']) ? strtolower($server['SERVER_PROTOCOL']) : 'http/1.1';
        $scheme = substr($rawProtocol, 0, strpos($rawProtocol, '/')) . ($isSecure ? 's' : '');
        $user = $server['PHP_AUTH_USER'] ?? null;
        $password = $server['PHP_AUTH_PW'] ?? null;
        $port = null;

        if ($isUsingTrustedProxy) {
            if (isset($server[$this->trustedHeaderNames['HTTP_CLIENT_PORT']])) {
                $port = (int)$server[$this->trustedHeaderNames['HTTP_CLIENT_PORT']];
            } elseif (
                isset($server[$this->trustedHeaderNames['HTTP_CLIENT_PROTO']]) &&
                $server[$this->trustedHeaderNames['HTTP_CLIENT_PROTO']] === 'https'
            ) {
                $port = 443;
            }
        }

        if ($port === null) {
            $port = isset($server['SERVER_PORT']) ? (int)$server['SERVER_PORT'] : null;
        }

        if ($isUsingTrustedProxy && isset($server[$this->trustedHeaderNames['HTTP_CLIENT_HOST']])) {
            $hostWithPort = explode(',', $server[$this->trustedHeaderNames['HTTP_CLIENT_HOST']]);
            $hostWithPort = trim(end($hostWithPort));
        } else {
            $hostWithPort = $server['HTTP_HOST'] ?? $server['SERVER_NAME'] ?? $server['SERVER_ADDR'] ?? '';
        }

        // Remove the port from the host
        $host = strtolower(preg_replace("/:\d+$/", '', trim($hostWithPort)));

        // Check for forbidden characters
        if (!empty($host) && !empty(preg_replace("/(?:^\[)?[a-zA-Z0-9-:\]_]+\.?/", '', $host))) {
            throw new InvalidArgumentException("Invalid host \"$host\"");
        }

        $path = parse_url('http://foo.com' . ($server['REQUEST_URI'] ?? ''), PHP_URL_PATH);
        $path = $path === false ? '' : ($path ?? '');
        $queryString = $server['QUERY_STRING'] ?? '';

        if ($queryString === '') {
            $queryString = parse_url('http://foo.com' . ($server['REQUEST_URI'] ?? ''), PHP_URL_QUERY);
            $queryString = $queryString === false || $queryString === null ? '' : $queryString;
        }

        // The "?" is simply the separator for the query string, not actually part of the query string
        $queryString = ltrim($queryString, '?');
        $uriString = "$scheme://";

        if ($user !== null) {
            $uriString .= "$user:" . ($password ?? '') . '@';
        }

        $uriString .= $host . ($port === null ? '' : ":$port") . "{$path}?{$queryString}";

        return new Uri($uriString);
    }
Creates a URI from PHP globals

@param array $server The global server array
@return Uri The URI
@throws InvalidArgumentException Thrown if the host is malformed