获得指定请求 Uri 对象的请求对象

@param  \Psr\Http\Message\UriInterface   $uri
@param  bool                             $preserveHost

@return \One\Protocol\Contracts\Request
769310
public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface
    {
        $clone = clone $this;
        $clone->uri = $uri;

        if ($uri->getHost() !== '') {
            $clone->headers->set('Host', $uri->getHost());
        }

        return $clone;
    }
527809
protected function formatUri($uri): string
    {
        $scheme = $uri->getScheme();
        if ('' != $scheme) {
            $scheme = $scheme.':';
        }

        $authority = null;
        $host = $uri->getHost();
        if ('' != $host) {
            $user_info = $uri->getUserInfo();
            if ('' != $user_info) {
                $authority .= (new UserInfo())->withContent($user_info)->getContent($this->enc_type).'@';
            }
            $authority .= (new Host($host))->getContent($this->enc_type);
            $port = $uri->getPort();
            if (null !== $port) {
                $authority .= ':'.$port;
            }
        }

        if (null !== $authority) {
            $authority = '//'.$authority;
        }

        $path = (new Path($uri->getPath()))->getContent($this->enc_type);
        if (null !== $authority && '' !== $path && '/' !== $path[0]) {
            $path = '/'.$path;
        }

        $query = $uri->getQuery();
        if ('' != $query || $this->preserve_query) {
            $query = '?'.Uri\build_query(Uri\parse_query($query), $this->query_separator, $this->enc_type);
        }

        $fragment = $uri->getFragment();
        if ('' != $fragment || $this->preserve_fragment) {
            $fragment = '#'.(new Fragment($fragment))->getContent($this->enc_type);
        }

        return $scheme.$authority.$path.$query.$fragment;
    }
418394
public function withUri(UriInterface $uri, $preserveHost = false)
    {
        $new = self::create($uri);
        if ($preserveHost) {
            $new->headers->set('HOST', $this->getHost());
        }

        return $new;
    }
1328703
public function options(string $uri)
    {
        $request = $this->getRequest($uri)
            ->setMethod('OPTIONS');

        return $this->send($request);
    }
243290
protected function prepareUrlForRequest($uri)
    {
        if (Str::startsWith($uri, '/')) {
            $uri = substr($uri, 1);
        }

        if (! Str::startsWith($uri, 'http')) {
            $uri = ($this->baseUrl ?? config('app.url')) . '/' . $uri;
        }

        return trim($uri, '/');
    }
784094
public function setBaseUrl($uri)
    {
        if (is_string($uri)) {
            $this->baseUrl = Uri::createFromString($uri);
        } else {
            $this->baseUrl = $uri;
        }
        return $this;
    }
240948
public function toAbsoluteUri(UriInterface $uri)
    {
        $uri = $uri->withQuery($this->query);
        $uri = (string)$uri;
        $this->session->saveViewData();

        return $this->response
            ->withStatus(302)
            ->withHeader('Location', $uri);
    }
644974
public function prepareHttp(string $uri)
    {
        $this->uri = $uri;

        // first, check the URI for duplicate slashes - they are not allowed
        // if you must pass duplicate slashes in URL, then urlencode them
        $redirect = null;
        if (strpos($this->uri, '//') !== false) {
            $redirect = str_replace('//', '/', $this->uri);
        }

        if (strlen($this->uri) > 1 && substr($this->uri, -1) == '/') {
            // ending slash is not needed, unless you have a namespace
            // if you need to pass slash on the end of URI, then urlencode it
            if ($redirect === null) {
                $redirect = substr($this->uri, 0, -1);
            } else {
                $redirect = substr($redirect, 0, -1);
            }
        }

        if ($redirect !== null) {
            header('Location: ' . Application::getDomainWithSchema() . $redirect);
            exit(0);
        }

        $questionPos = strpos($this->uri, '?');
        if ($questionPos !== false) {
            $this->uri = substr($this->uri, 0, $questionPos);
        }

        $ds = DS;

        $this->uri = explode('/', $this->uri);
        if (!isset($this->uri[1])) {
            $this->uri[1] = '';
        }

        // There are two possible scenarios:
        // 1. The first part of URL leads to the module controller
        // 2. The first part of URL leads to the default controller

        if ($this->uri[1] == '') {
            $this->controllerUrl = 'index';
            $this->controllerClass = 'IndexController';
        } else {
            $this->controllerUrl = strtolower($this->uri[1]);
            $this->controllerClass = str_replace(' ', '', ucwords(str_replace(['-', '.'], ' ', $this->controllerUrl))) . 'Controller';
        }

        // Now we have the controller class name detected, but, should it be
        // taken from module or from default controllers?

        $moduleDir = Application::getModulePath($this->controllerUrl);

        if (is_dir($moduleDir)) {
            Application::setCurrentModule($this->controllerUrl);

            // ok, it is a module with module/controller/action path
            $moduleUrl = $this->controllerUrl;
            $this->moduleUrl = $moduleUrl;

            if (isset($this->uri[2]) && $this->uri[2] != '') {
                $this->controllerUrl = strtolower($this->uri[2]);
                $this->controllerClass = str_replace(' ', '', ucwords(str_replace(['-', '.'], ' ', $this->controllerUrl))) . 'Controller';
            } else {
                $this->controllerUrl = 'index';
                $this->controllerClass = 'IndexController';
            }

            $this->controllerPath = "{$moduleDir}controllers{$ds}{$this->controllerClass}.php";
            $mainControllerExists = true;

            if (!is_file($this->controllerPath)) {
                // lets try with default controller when requested one is not here
                $this->controllerPath = Application::getModulePath($moduleUrl) . "controllers{$ds}IndexController.php";

                if (!is_file($this->controllerPath)) {
                    // Even IndexController is missing. Can not resolve that.
                    throw new NotFoundException("Can not find {$this->controllerClass} nor IndexController in {$moduleDir}{$ds}controllers");
                }

                $mainControllerExists = false;
                $this->controllerClass = 'IndexController';
            }

            if ($mainControllerExists) {
                if (!isset($this->uri[3]) || $this->uri[3] == '') {
                    $this->actionUrl = 'index';
                    $this->actionMethod = 'index';
                } else {
                    $this->actionUrl = strtolower($this->uri[3]);
                    $this->actionMethod = ucwords(str_replace(['-', '.'], ' ', $this->actionUrl));
                    $this->actionMethod = str_replace(' ', '', $this->actionMethod);
                    $this->actionMethod = strtolower(substr($this->actionMethod, 0, 1)) . substr($this->actionMethod, 1);
                }
            } else if (isset($this->uri[2]) && $this->uri[2] != '') {
                $this->actionUrl = strtolower($this->uri[2]);
                $this->actionMethod = ucwords(str_replace(['-', '.'], ' ', $this->actionUrl));
                $this->actionMethod = str_replace(' ', '', $this->actionMethod);
                $this->actionMethod = strtolower(substr($this->actionMethod, 0, 1)) . substr($this->actionMethod, 1);
            } else {
                $this->actionUrl = 'index';
                $this->actionMethod = 'index';
            }

        } else {

            // ok, it is the default controller/action
            $this->controllerPath = Application::getApplicationPath("controllers{$ds}{$this->controllerClass}.php");

            $mainControllerExists = true;

            if (!is_file($this->controllerPath)) {
                $this->controllerPath = Application::getApplicationPath("controllers{$ds}IndexController.php");

                if (!is_file($this->controllerPath)) {
                    // Even IndexController is missing. Can not resolve that.
                    throw new NotFoundException("Can not find {$this->controllerClass} nor IndexController in " . Application::getApplicationPath('controllers'));
                }

                $mainControllerExists = false;
                $this->controllerClass = 'IndexController';
            }

            if ($mainControllerExists) {
                if (!isset($this->uri[2]) || $this->uri[2] == '') {
                    $this->actionUrl = 'index';
                    $this->actionMethod = 'index';
                } else {
                    $this->actionUrl = strtolower($this->uri[2]);
                    $this->actionMethod = ucwords(str_replace(['-', '.'], ' ', $this->actionUrl));
                    $this->actionMethod = str_replace(' ', '', $this->actionMethod);
                    $this->actionMethod = strtolower(substr($this->actionMethod, 0, 1)) . substr($this->actionMethod, 1);
                }
            } else {
                $this->actionUrl = strtolower($this->uri[1]);
                $this->actionMethod = ucwords(str_replace(['-', '.'], ' ', $this->actionUrl));
                $this->actionMethod = str_replace(' ', '', $this->actionMethod);
                $this->actionMethod = strtolower(substr($this->actionMethod, 0, 1)) . substr($this->actionMethod, 1);
            }

            // and now, configure the include paths according to the case
            $basePath = Application::getApplicationPath();
            Application::addIncludePath($basePath . 'controllers',    // so you can extend abstract controllers in the same directory if needed,
              $basePath . 'library'      // the place where you can define your own classes and methods
            );
        }

        $this->isAjax = (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST' && isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') || (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'],
              'application/json') !== false);

        $controllerClassName = $this->getControllerClass();

        if (!class_exists($controllerClassName)) {
            throw new Exception("Can not find class {$controllerClassName}; current include path is:\n" . get_include_path());
        }

        $controllerInstance = new $controllerClassName();

        $alwaysRestful = (isset($this->config['always_restful'])) && $this->config['always_restful'] === true;

        if ($alwaysRestful || (property_exists($controllerInstance, 'restful') && $controllerInstance::$restful === true)) {
            // it is restful
            $this->actionMethod = strtolower($_SERVER['REQUEST_METHOD']) . ucfirst($this->actionMethod);

        } else if ($this->isAjax) {
            $this->actionMethod .= 'Ajax';

        } else {
            $this->actionMethod .= 'Action';

        }

        $this->controllerInstance = $controllerInstance;
    }
370044
private function setHostFromUri()
    {
        $this->hostFromUri = true;

        $host = $this->uri->getHost();

        if (!empty($host)) { // Do not set Host header if URI has no host.
            $port = $this->uri->getPort();
            if (null !== $port) {
                $host = sprintf('%s:%d', $host, $port);
            }

            parent::setHeader('Host', $host);
        }
    }
1064704
public function withHost($host)
        {
            if(!is_string($host)){
                throw new InvalidArgumentException('Uri host must be a string');
            }

            $clone = clone $this;
            $clone->host = $this->removePortFromHost($host);
            return $clone;
        }
823926
private function createUri($uri)
    {
        if ($this->hasBaseUri() && (stripos($uri, $baseUri = (string) $this->getBaseUri()) === false)) {
            return $baseUri.$uri;
        }

        return $uri;
    }
295852
protected function _parseUri($uri)
    {
        $parts = parse_url($uri);

        if (false === $parts) {
            throw new \InvalidArgumentException(
                'The source URI string appears to be malformed'
            );
        }

        $this->_scheme    = isset($parts['scheme'])   ? $this->_filterScheme($parts['scheme']) : '';
        $this->_userInfo  = isset($parts['user'])     ? $parts['user']     : '';
        $this->_host      = isset($parts['host'])     ? $parts['host']     : '';
        $this->_port      = isset($parts['port'])     ? $parts['port']     : null;
        $this->_path      = isset($parts['path'])     ? $this->_filterPath($parts['path']) : '';
        $this->_query     = isset($parts['query'])    ? $this->_filterQuery($parts['query']) : '';
        $this->_fragment  = isset($parts['fragment']) ? $this->_filterFragment($parts['fragment']) : '';

        if (isset($parts['pass'])) {
            $this->_userInfo .= ':' . $parts['pass'];
        }
    }
917673
protected function isOpenSymphonyUrl(UriInterface $uri)
    {
        $host = $uri->getHost();
        $host = strtolower($host);
        $host = trim($host);

        $flag = self::VALID_URI === $host;

        return $flag;
    }
821223
protected function generateAbsoluteUrl($uri, RequestContext $requestContext)
    {
        $scheme = $requestContext->getScheme();
        $port = '';
        if ($scheme === 'http' && $requestContext->getHttpPort() != 80) {
            $port = ':' . $requestContext->getHttpPort();
        } elseif ($scheme === 'https' && $requestContext->getHttpsPort() != 443) {
            $port = ':' . $requestContext->getHttpsPort();
        }

        return $scheme . '://' . $requestContext->getHost() . $port . $uri;
    }
1123214
public function withHost(string $host): UriInterface
    {
        $instance = clone $this;
        $instance->host = $this->marshalHost($host);

        return $instance;
    }
939550
private function compareUris($redirect_uri, $clientRedirectUri)
    {
        if (!$redirect_uri || !$clientRedirectUri) {
            return false;
        }

        $wanted = array_fill_keys(['scheme', 'host', 'port', 'path'], true);

        // gets only scheme, host, port, path from uri
        $redirect_uri = array_intersect_key(parse_url($redirect_uri), $wanted);
        $clientRedirectUri = array_intersect_key(parse_url($clientRedirectUri), $wanted);

        return strcmp(implode($redirect_uri), implode($clientRedirectUri)) === 0;
    }
1009622
protected function _normalizedUrl($uri)
    {
        $out = $uri->getScheme() . '://';
        $out .= strtolower($uri->getHost());
        $out .= $uri->getPath();

        return $out;
    }