protected function setUri($uri, $preserveHost = false)
    {
        if (is_string($uri)) {
            $uri = new Uri($uri);
        }
        if (! $uri instanceof UriInterface) {
            throw new InvalidArgumentException(sprintf(
                'Invalid URI provided. Must be null, a string, or '
                . 'Psr\Http\Message\UriInterface; received "%s".',
                is_object($uri) ? get_class($uri) : gettype($uri)
            ));
        }
        $this->uri = $uri;

        if ($preserveHost && $this->hasHeader('Host')) {
            return $this;
        }

        if (! $uri->getHost()) {
            return $this;
        }

        $host = $uri->getHost();
        if ($uri->getPort()) {
            $host .= ':' . $uri->getPort();
        }
        $this->setHeader('Host', $host);

        return $this;
    }