Ejemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function handleRequest(RequestInterface $request, callable $next, callable $first)
 {
     if ($this->replace || $request->getUri()->getHost() === '') {
         $uri = $request->getUri()->withHost($this->host->getHost())->withScheme($this->host->getScheme())->withPort($this->host->getPort());
         $request = $request->withUri($uri);
     }
     return $next($request);
 }
Ejemplo n.º 2
0
 public function withUri(UriInterface $uri, $preserveHost = false)
 {
     $request = clone $this;
     $request->uri = $uri;
     if ($preserveHost || !$uri->getHost()) {
         return $request;
     }
     $host = $uri->getHost();
     if ($uri->getPort()) {
         $host .= ':' . $uri->getPort();
     }
     $request->withHeader('host', $host);
     return $request;
 }
Ejemplo n.º 3
0
 private function updateHostFromUri($host)
 {
     // Ensure Host is the first header.
     // See: http://tools.ietf.org/html/rfc7230#section-5.4
     if ($port = $this->uri->getPort()) {
         $host .= ':' . $port;
     }
     $this->headerLines = array('Host' => array($host)) + $this->headerLines;
     $this->headers = array('host' => array($host)) + $this->headers;
 }
Ejemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 public function withUri(UriInterface $uri, $preserveHost = false)
 {
     $cloneInstance = clone $this;
     $cloneInstance->uri = $uri;
     if ($preserveHost === false) {
         if ($host = $uri->getHost()) {
             if ($port = $this->uri->getPort()) {
                 $host .= ':' . $port;
             }
             $this->headers['host'] = ['name' => 'Host', 'value' => $host];
         }
     }
     return $cloneInstance;
 }
Ejemplo n.º 5
0
 /**
  * @param \Psr\Http\Message\UriInterface $uri
  * @param bool $preserveHost
  * @return static
  */
 public function withUri(UriInterface $uri, $preserveHost = false)
 {
     $new = clone $this;
     $new->uri = $uri;
     if ($preserveHost || '' === ($host = $uri->getHost())) {
         return $new;
     }
     if ('' !== ($port = $uri->getPort())) {
         $host .= ':' . $port;
     }
     $new->headerNames['host'] = 'Host';
     $new->headers['Host'] = [$host];
     return $new;
 }
Ejemplo n.º 6
0
 public function withUri(UriInterface $uri, $preserveHost = false)
 {
     $clone = clone $this;
     $clone->uri = $uri;
     if (!$preserveHost) {
         if ($host = $uri->getHost()) {
             if ($port = $uri->getPort()) {
                 $host .= ':' . $port;
             }
             return $clone->widthHeader('host', $host);
         }
     }
     return $clone;
 }
Ejemplo n.º 7
0
 /**
  * @param  \Psr\Http\Message\UriInterface $uri
  * @return string
  */
 protected function filterBaseurl(UriInterface $uri)
 {
     if ($baseUrl = $this->settings['baseurl']) {
         $reqUri = $uri->getScheme() . '://' . $uri->getHost();
         if ($port = $uri->getPort()) {
             $reqUri .= ':' . $port;
         }
         $url = parse_url($baseUrl);
         $uri = $uri->withScheme($url['scheme'])->withHost($url['host']);
         if ($port || isset($url['port'])) {
             $port = $port == $url['port'] ? $port : $url['port'];
             $uri = $uri->withPort($port);
         }
         return $reqUri !== rtrim($baseUrl, '/');
     }
     return false;
 }
Ejemplo n.º 8
0
 /**
  * Retrieve the host from the URI instance
  */
 private function updateHostFromUri()
 {
     $host = $this->uri->getHost();
     if ($host == '') {
         return;
     }
     if (($port = $this->uri->getPort()) !== null) {
         $host .= ':' . $port;
     }
     if (isset($this->headerNames['host'])) {
         $header = $this->headerNames['host'];
     } else {
         $header = 'Host';
         $this->headerNames['host'] = 'Host';
     }
     // Ensure Host is the first header.
     // See: http://tools.ietf.org/html/rfc7230#section-5.4
     $this->headers = [$header => [$host]] + $this->headers;
 }
Ejemplo n.º 9
0
 /**
  * Get each invalidation request replicated over all HTTP caching servers
  *
  * @return RequestInterface[]
  */
 public function all()
 {
     $requests = [];
     foreach ($this->queue as $request) {
         $uri = $request->getUri();
         // If a base URI is configured, try to make partial invalidation
         // requests complete.
         if ($this->baseUri) {
             if ($uri->getHost()) {
                 // Absolute URI: does it already have a scheme?
                 if (!$uri->getScheme() && $this->baseUri->getScheme() !== '') {
                     $uri = $uri->withScheme($this->baseUri->getScheme());
                 }
             } else {
                 // Relative URI
                 if ($this->baseUri->getHost() !== '') {
                     $uri = $uri->withHost($this->baseUri->getHost());
                 }
                 if ($this->baseUri->getPort()) {
                     $uri = $uri->withPort($this->baseUri->getPort());
                 }
                 // Base path
                 if ($this->baseUri->getPath() !== '') {
                     $path = $this->baseUri->getPath() . '/' . ltrim($uri->getPath(), '/');
                     $uri = $uri->withPath($path);
                 }
             }
         }
         // Close connections to make sure invalidation (PURGE/BAN) requests
         // will not interfere with content (GET) requests.
         $request = $request->withUri($uri)->withHeader('Connection', 'Close');
         // Create a request to each caching proxy server
         foreach ($this->servers as $server) {
             $requests[] = $request->withUri($uri->withScheme($server->getScheme())->withHost($server->getHost())->withPort($server->getPort()), true);
         }
     }
     return $requests;
 }
Ejemplo n.º 10
0
 /**
  * Returns an instance with the provided URI.
  *
  * This method MUST update the Host header of the returned request by
  * default if the URI contains a host component. If the URI does not
  * contain a host component, any pre-existing Host header MUST be carried
  * over to the returned request.
  *
  * You can opt-in to preserving the original state of the Host header by
  * setting `$preserveHost` to `true`. When `$preserveHost` is set to
  * `true`, this method interacts with the Host header in the following ways:
  *
  * - If the the Host header is missing or empty, and the new URI contains
  *   a host component, this method MUST update the Host header in the returned
  *   request.
  * - If the Host header is missing or empty, and the new URI does not contain a
  *   host component, this method MUST NOT update the Host header in the returned
  *   request.
  * - If a Host header is present and non-empty, this method MUST NOT update
  *   the Host header in the returned request.
  *
  * This method MUST be implemented in such a way as to retain the
  * immutability of the message, and MUST return an instance that has the
  * new UriInterface instance.
  *
  * @link http://tools.ietf.org/html/rfc3986#section-4.3
  *
  * @param \Psr\Http\Message\UriInterface $uri New request URI to use.
  * @param bool $preserveHost Preserve the original state of the Host header.
  * @return Request
  */
 public function withUri(UriInterface $uri, $preserveHost = false)
 {
     $clonedObject = clone $this;
     $clonedObject->uri = $uri;
     if ($preserveHost) {
         return $clonedObject;
     }
     if (!$uri->getHost()) {
         return $clonedObject;
     }
     $host = $uri->getHost();
     if ($uri->getPort()) {
         $host .= ':' . $uri->getPort();
     }
     $clonedObject->lowercasedHeaderNames['host'] = 'Host';
     $clonedObject->headers['Host'] = [$host];
     return $clonedObject;
 }
Ejemplo n.º 11
0
 /**
  * Resolve a base URI with a relative URI and return a new URI.
  *
  * @param UriInterface $base Base URI
  * @param string       $rel  Relative URI
  *
  * @return UriInterface
  */
 public static function resolve(UriInterface $base, $rel)
 {
     if ($rel === null || $rel === '') {
         return $base;
     }
     if ($rel instanceof UriInterface) {
         $relParts = ['scheme' => $rel->getScheme(), 'host' => $rel->getHost(), 'port' => $rel->getPort(), 'path' => $rel->getPath(), 'query' => $rel->getQuery(), 'fragment' => $rel->getFragment()];
     } else {
         $relParts = parse_url($rel) + ['scheme' => '', 'host' => '', 'port' => '', 'path' => '', 'query' => '', 'fragment' => ''];
     }
     if (!empty($relParts['scheme']) && !empty($relParts['host'])) {
         return $rel instanceof UriInterface ? $rel : self::fromParts($relParts);
     }
     $parts = ['scheme' => $base->getScheme(), 'host' => $base->getHost(), 'port' => $base->getPort(), 'path' => $base->getPath(), 'query' => $base->getQuery(), 'fragment' => $base->getFragment()];
     if (!empty($relParts['host'])) {
         $parts['host'] = $relParts['host'];
         $parts['port'] = $relParts['port'];
         $parts['path'] = self::removeDotSegments($relParts['path']);
         $parts['query'] = $relParts['query'];
         $parts['fragment'] = $relParts['fragment'];
     } elseif (!empty($relParts['path'])) {
         if (substr($relParts['path'], 0, 1) == '/') {
             $parts['path'] = self::removeDotSegments($relParts['path']);
             $parts['query'] = $relParts['query'];
             $parts['fragment'] = $relParts['fragment'];
         } else {
             if (!empty($parts['host']) && empty($parts['path'])) {
                 $mergedPath = '/';
             } else {
                 $mergedPath = substr($parts['path'], 0, strrpos($parts['path'], '/') + 1);
             }
             $parts['path'] = self::removeDotSegments($mergedPath . $relParts['path']);
             $parts['query'] = $relParts['query'];
             $parts['fragment'] = $relParts['fragment'];
         }
     } elseif (!empty($relParts['query'])) {
         $parts['query'] = $relParts['query'];
     } elseif ($relParts['fragment'] != null) {
         $parts['fragment'] = $relParts['fragment'];
     }
     return static::fromParts($parts);
 }
Ejemplo n.º 12
0
 /**
  * Returns an instance with the provided URI.
  *
  * This method MUST update the Host header of the returned request by
  * default if the URI contains a host component. If the URI does not
  * contain a host component, any pre-existing Host header MUST be carried
  * over to the returned request.
  *
  * You can opt-in to preserving the original state of the Host header by
  * setting `$preserveHost` to `true`. When `$preserveHost` is set to
  * `true`, this method interacts with the Host header in the following ways:
  *
  * - If the the Host header is missing or empty, and the new URI contains
  *   a host component, this method MUST update the Host header in the returned
  *   request.
  * - If the Host header is missing or empty, and the new URI does not contain a
  *   host component, this method MUST NOT update the Host header in the returned
  *   request.
  * - If a Host header is present and non-empty, this method MUST NOT update
  *   the Host header in the returned request.
  *
  * @link http://tools.ietf.org/html/rfc3986#section-4.3
  *
  * @param  UriInterface $uri          New request URI to use.
  * @param  bool         $preserveHost Preserve the original state of the Host header.
  * @return self
  */
 public function withUri(UriInterface $uri, $preserveHost = false)
 {
     $request = clone $this;
     $request->scheme($uri->getScheme());
     $userInfo = $uri->getUserInfo();
     $parts = explode(':', $userInfo);
     $request->username($parts[0] ?: null);
     $request->password(!empty($parts[1]) ? $parts[1] : null);
     $request->port($uri->getPort());
     if ($preserveHost) {
         $host = $request->headers['Host'];
         $request->host($uri->getHost());
         $request->headers['Host'] = $host;
     } else {
         $request->host($uri->getHost());
     }
     $request->path($uri->getPath());
     $request->query($uri->getQuery());
     $request->fragment($uri->getFragment());
     return $request;
 }
Ejemplo n.º 13
0
 /**
  * @param UriInterface $baseUri
  */
 protected function setBaseUriFromObject(UriInterface $baseUri)
 {
     $this->baseUri = ['scheme' => $baseUri->getScheme(), 'host' => $baseUri->getHost(), 'port' => $baseUri->getPort(), 'path' => rtrim($baseUri->getPath(), '/')];
 }
Ejemplo n.º 14
0
 /**
  * @inheritdoc
  */
 public function withUri(UriInterface $uri, $preserveHost = false)
 {
     $new = clone $this;
     $new->url = $uri;
     if ($preserveHost || !$uri->getHost()) {
         return $new;
     }
     $host = $uri->getHost();
     if ($uri->getPort()) {
         $host .= ':' . $uri->getPort();
     }
     return $new->withHeader('Host', $host);
 }
Ejemplo n.º 15
0
 /**
  * Returns an instance with the provided URI.
  *
  * This method MUST update the Host header of the returned request by
  * default if the URI contains a host component. If the URI does not
  * contain a host component, any pre-existing Host header MUST be carried
  * over to the returned request.
  *
  * You can opt-in to preserving the original state of the Host header by
  * setting `$preserveHost` to `true`. When `$preserveHost` is set to
  * `true`, this method interacts with the Host header in the following ways:
  *
  * - If the the Host header is missing or empty, and the new URI contains
  *   a host component, this method MUST update the Host header in the returned
  *   request.
  * - If the Host header is missing or empty, and the new URI does not contain a
  *   host component, this method MUST NOT update the Host header in the returned
  *   request.
  * - If a Host header is present and non-empty, this method MUST NOT update
  *   the Host header in the returned request.
  *
  * This method MUST be implemented in such a way as to retain the
  * immutability of the message, and MUST return an instance that has the
  * new UriInterface instance.
  *
  * @link http://tools.ietf.org/html/rfc3986#section-4.3
  *
  * @param \Psr\Http\Message\UriInterface $uri New request URI to use.
  * @param bool $preserveHost Preserve the original state of the Host header.
  * @return Request
  */
 public function withUri(UriInterface $uri, $preserveHost = FALSE)
 {
     $clonedObject = clone $this;
     $clonedObject->uri = $uri;
     if ($preserveHost) {
         return $clonedObject;
     }
     if (!$uri->getHost()) {
         return $clonedObject;
     }
     $host = $uri->getHost();
     if ($uri->getPort()) {
         $host .= ':' . $uri->getPort();
     }
     $clonedObject->headerNames['host'] = 'Host';
     $clonedObject->headers['Host'] = array($host);
     return $clonedObject;
 }
Ejemplo n.º 16
0
 /**
  * Set the uri.
  *
  * @param UriInterface $uri
  * @param boolean $preserveHost = false
  * @return self
  */
 private function setUri(UriInterface $uri, $preserveHost = false)
 {
     $this->uri = $uri;
     if (!$preserveHost && ($host = $uri->getHost())) {
         if ($uri->getPort() !== null) {
             $host .= URI::DELIMITER_PORT . $uri->getPort();
         }
         $this->setHeader('Host', $host);
     }
     return $this;
 }
Ejemplo n.º 17
0
 /**
  * {@inheritdoc}
  */
 public function withUri(UriInterface $uri, $preserveHost = false)
 {
     $newInstance = clone $this;
     $newInstance->uri = $uri;
     if ($preserveHost) {
         return $newInstance;
     }
     if ($uri->getHost() && !$this->hasHeader('Host')) {
         $host = $uri->getHost();
         $host .= !is_null($uri->getPort()) ? ':' . $uri->getPort() : '';
         $newInstance->headerNames['host'] = 'Host';
         $newInstance->headerValues['host'] = $host;
     }
     return $newInstance;
 }
Ejemplo n.º 18
0
 /**
  * Returns an instance with the provided URI.
  *
  * This method MUST update the Host header of the returned request by
  * default if the URI contains a host component. If the URI does not
  * contain a host component, any pre-existing Host header MUST be carried
  * over to the returned request.
  *
  * You can opt-in to preserving the original state of the Host header by
  * setting `$preserveHost` to `true`. When `$preserveHost` is set to
  * `true`, this method interacts with the Host header in the following ways:
  *
  * - If the the Host header is missing or empty, and the new URI contains
  *   a host component, this method MUST update the Host header in the returned
  *   request.
  * - If the Host header is missing or empty, and the new URI does not contain a
  *   host component, this method MUST NOT update the Host header in the returned
  *   request.
  * - If a Host header is present and non-empty, this method MUST NOT update
  *   the Host header in the returned request.
  *
  * This method MUST be implemented in such a way as to retain the
  * immutability of the message, and MUST return an instance that has the
  * new UriInterface instance.
  *
  * @link http://tools.ietf.org/html/rfc3986#section-4.3
  * @param UriInterface $uri New request URI to use.
  * @param bool $preserveHost Preserve the original state of the Host header.
  * @return self
  */
 public function withUri(UriInterface $uri, $preserveHost = false)
 {
     if ($uri === $this->_uri) {
         return $this;
     }
     $new = clone $this;
     $new->_uri = $uri;
     if (!$preserveHost) {
         $host = $uri->getHost();
         if (\CoreTyson\isEmpty($host)) {
             if (($port = $uri->getPort()) !== null) {
                 $host .= ':' . $port;
             }
             $this->withoutHeader('host');
             // Ensure Host is the first header.
             // See: http://tools.ietf.org/html/rfc7230#section-5.4
             $this->_headers = ['host' => [$host]] + $this->_headers;
         }
     }
     return $new;
 }
Ejemplo n.º 19
0
 /**
  * Returns an instance with the provided URI.
  *
  * This method MUST update the Host header of the returned request by
  * default if the URI contains a host component. If the URI does not
  * contain a host component, any pre-existing Host header MUST be carried
  * over to the returned request.
  *
  * You can opt-in to preserving the original state of the Host header by
  * setting `$preserveHost` to `true`. When `$preserveHost` is set to
  * `true`, this method interacts with the Host header in the following ways:
  *
  * - If the the Host header is missing or empty, and the new URI contains
  *   a host component, this method MUST update the Host header in the returned
  *   request.
  * - If the Host header is missing or empty, and the new URI does not contain a
  *   host component, this method MUST NOT update the Host header in the returned
  *   request.
  * - If a Host header is present and non-empty, this method MUST NOT update
  *   the Host header in the returned request.
  *
  * This method MUST be implemented in such a way as to retain the
  * immutability of the message, and MUST return an instance that has the
  * new UriInterface instance.
  *
  * @link http://tools.ietf.org/html/rfc3986#section-4.3
  *
  * @param UriInterface $uri          New request URI to use.
  * @param bool         $preserveHost Preserve the original state of the Host header.
  *
  * @return static
  */
 public function withUri(UriInterface $uri, $preserveHost = false)
 {
     $new = clone $this;
     $new->uri = $uri;
     if ($preserveHost) {
         return $new;
     }
     if (!$uri->getHost()) {
         return $new;
     }
     $host = $uri->getHost();
     if ($uri->getPort()) {
         $host .= ':' . $uri->getPort();
     }
     $new->headerNames['host'] = 'Host';
     $new->headers['Host'] = array($host);
     return $new;
 }
Ejemplo n.º 20
0
 /**
  * Sets the request URI.
  *
  * @param \Psr\Http\Message\UriInterface $uri
  * @param bool                           $preserveHost
  */
 protected final function setUri(UriInterface $uri, $preserveHost = false)
 {
     $this->uri = $uri;
     if ($preserveHost and $this->hasHeader('Host')) {
         return;
     }
     if (!$uri->getHost()) {
         return;
     }
     $host = $uri->getHost();
     if ($port = $uri->getPort()) {
         $host .= ':' . $port;
     }
     $this->headerBag->setHeader('Host', $host);
 }
Ejemplo n.º 21
0
 protected function runMatches(UriInterface $uri)
 {
     return $uri->getPort() === $this->expected;
 }
Ejemplo n.º 22
0
 /**
  * Return config and uri specific cookie domain.
  *
  * @param UriInterface $uri
  * @return string
  */
 public function cookiesDomain(UriInterface $uri)
 {
     $host = $uri->getHost();
     $pattern = $this->config['cookies']['domain'];
     if (filter_var($host, FILTER_VALIDATE_IP)) {
         //We can't use sub domains
         $pattern = ltrim($pattern, '.');
     }
     if (!empty($port = $uri->getPort())) {
         $host = $host . ':' . $port;
     }
     if (strpos($pattern, '%s') === false) {
         //Forced domain
         return $pattern;
     }
     return sprintf($pattern, $host);
 }
Ejemplo n.º 23
0
 /**
  * Returns the host header.
  *
  * @param UriInterface $uri
  *
  * @return string
  */
 private function buildHost(UriInterface $uri) : string
 {
     return $uri->getHost() . ($uri->getPort() !== null ? ':' . $uri->getPort() : '');
 }
Ejemplo n.º 24
0
 /**
  * Whether the URI has the default port of the current scheme.
  *
  * `Psr\Http\Message\UriInterface::getPort` may return null or the standard port. This method can be used
  * independently of the implementation.
  *
  * @param UriInterface $uri
  *
  * @return bool
  */
 public static function isDefaultPort(UriInterface $uri)
 {
     return $uri->getPort() === null || isset(self::$defaultPorts[$uri->getScheme()]) && $uri->getPort() === self::$defaultPorts[$uri->getScheme()];
 }
Ejemplo n.º 25
0
 /**
  * Must work exactly as Oxygen_Util::getUrlSlug, or unexpected behaviour might occur.
  *
  * @param UriInterface $url
  *
  * @return string
  *
  * @see Oxygen_Util::getUrlSlug
  */
 private function getUrlSlug(UriInterface $url)
 {
     return sprintf('%s%s%s', $url->getHost(), $url->getPort() ? ':' . $url->getPort() : '', rtrim($url->getPath(), '/'));
 }
Ejemplo n.º 26
0
 /**
  * Retrieve the host from the URI instance
  *
  * @return string
  */
 private function getHostFromUri()
 {
     $host = $this->uri->getHost();
     $host .= $this->uri->getPort() ? ':' . $this->uri->getPort() : '';
     return $host;
 }
Ejemplo n.º 27
0
 /**
  * Returns an instance with the provided URI.
  *
  * {@inheritdoc}
  *
  * @see http://tools.ietf.org/html/rfc3986#section-4.3
  * @param UriInterface $uri     New request URI to use.
  * @param bool $preserveHost    Preserve the original state of the Host header.
  * @return self
  */
 public function withUri(UriInterface $uri, $preserveHost = false)
 {
     $clone = clone $this;
     $clone->uri = $uri;
     if ($preserveHost && $this->hasHeader('Host')) {
         return $clone;
     }
     if (!$uri->getHost()) {
         return $clone;
     }
     $host = $uri->getHost();
     if ($uri->getPort()) {
         $host .= ':' . $uri->getPort();
     }
     // Remove an existing host header if present
     foreach (array_keys($clone->headers) as $header) {
         if (strtolower($header) === 'host') {
             unset($clone->headers[$header]);
         }
     }
     $clone->headers['Host'] = [$host];
     return $clone;
 }
Ejemplo n.º 28
0
 /**
  * Returns an instance with the provided URI.
  *
  * This method will update the Host header of the returned request by
  * default if the URI contains a host component. If the URI does not
  * contain a host component, any pre-existing Host header will be carried
  * over to the returned request.
  *
  * You can opt-in to preserving the original state of the Host header by
  * setting `$preserveHost` to `true`. When `$preserveHost` is set to
  * `true`, the returned request will not update the Host header of the
  * returned message -- even if the message contains no Host header. This
  * means that a call to `getHeader('Host')` on the original request MUST
  * equal the return value of a call to `getHeader('Host')` on the returned
  * request.
  *
  * This method MUST be implemented in such a way as to retain the
  * immutability of the message, and MUST return an instance that has the
  * new UriInterface instance.
  *
  * @link http://tools.ietf.org/html/rfc3986#section-4.3
  * @param UriInterface $uri New request URI to use.
  * @param bool $preserveHost Preserve the original state of the Host header.
  * @return static
  */
 public function withUri(UriInterface $uri, $preserveHost = false)
 {
     $new = clone $this;
     $new->uri = $uri;
     if ($preserveHost && $this->message->hasHeader('Host')) {
         return $new;
     }
     if (!$uri->getHost()) {
         return $new;
     }
     $host = $uri->getHost();
     if ($uri->getPort()) {
         $host .= ':' . $uri->getPort();
     }
     $new->message = $this->message->withHeader('Host', $host);
     return $new;
 }
 public function port()
 {
     return $this->uri->getPort();
 }