Ejemplo n.º 1
1
 public function isFiltered(UriInterface $currentUri, UriInterface $startUri)
 {
     /* @var $currentUri Uri */
     /* @var $startUri Uri */
     $startDomainElements = explode('.', $startUri->getHost());
     $currentDomainElements = explode('.', $currentUri->getHost());
     $startDomainLength = count($startDomainElements);
     $currentDomainLength = count($currentDomainElements);
     if ($currentDomainLength < $startDomainLength) {
         return true;
     }
     return $currentUri->getHost($startDomainLength) !== $startUri->getHost($startDomainLength);
 }
Ejemplo n.º 2
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.º 3
0
 function withUri(UriInterface $uri, $preserveHost = FALSE) : self
 {
     $headers = $this->headers;
     $headers['HOST'] = $headers['HOST'] ?? [];
     if (!$preserveHost && $uri->getHost()) {
         $headers['HOST'] = [$uri->getHost()];
     }
     $new = clone $this;
     $new->headers = $headers;
     $new->headerNames['HOST'] = 'Host';
     $new->uri = parse_url((string) $uri);
     return $new;
 }
Ejemplo n.º 4
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.º 5
0
 public function withUri(Psr7UriInterface $uri, $preserveHost = false)
 {
     $new = clone $this;
     $new->uri = $uri;
     $uriHost = $uri->getHost();
     $requestHost = $new->getHeaderLine('Host');
     if (!$preserveHost && $uriHost !== $requestHost) {
         return $new->withHeader('Host', $uriHost);
     }
     return $new;
 }
Ejemplo n.º 6
0
 public function withUri(\Psr\Http\Message\UriInterface $uri, $preserveHost = false)
 {
     $request = clone $this;
     $request->uri = $uri;
     if (!$preserveHost or !$this->hasHeader('Host')) {
         $host = $uri->getHost();
         if ($host != '') {
             $request->setHeader('Host', $host);
         }
     }
     return $request;
 }
Ejemplo n.º 7
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.º 8
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.º 9
0
 public function withUri(\Psr\Http\Message\UriInterface $uri, $preserveHost = false)
 {
     if ($uri === $this->uri) {
         return $this;
     }
     $new = clone $this;
     $new->uri = $uri;
     if ($preserveHost) {
         return $new;
     }
     if ($host = $uri->getHost()) {
         $new->updateFromUri($host);
     }
     return $new;
 }
Ejemplo n.º 10
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.º 11
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.º 12
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.º 13
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.º 14
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.º 15
0
 /**
  * {@inheritDoc}
  */
 public function checkUrl(UriInterface $uriInterface)
 {
     return preg_match('/imdb\\.com$/', (string) $uriInterface->getHost());
 }
Ejemplo n.º 16
0
 protected function runMatches(UriInterface $uri)
 {
     return $uri->getHost() == $this->expected;
 }
Ejemplo n.º 17
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.º 18
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.º 19
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;
 }
Ejemplo n.º 20
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.º 21
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 (false === $preserveHost) {
         $host = $uri->getHost();
         if (null !== $host) {
             $this->withHeader('Host', $host);
         }
     } else {
         $hostHeader = $this->getHeader('host');
         if (null === $hostHeader) {
             $host = $uri->getHost();
             $this->withHeader('host', $host);
         }
     }
 }
Ejemplo n.º 22
0
 /**
  * Format a URI authority according to the Formatter properties
  *
  * @param UriInterface|Uri $uri
  *
  * @return string
  */
 protected function formatAuthority($uri)
 {
     if ('' == $uri->getHost()) {
         return '';
     }
     static $parser;
     if (!$parser) {
         $parser = new UriParser();
     }
     $components = $parser((string) $uri);
     $port = $components['port'];
     if (null !== $port) {
         $port = ':' . $port;
     }
     return '//' . $this->buildUserInfo($components['user'], $components['pass']) . $this->formatHost(new Host($components['host'])) . $port;
 }
Ejemplo n.º 23
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.º 24
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.º 25
0
 public function withUri(UriInterface $uri, $preserveHost = false)
 {
     if ($uri === $this->uri) {
         return $this;
     }
     $new = clone $this;
     $new->uri = $uri;
     if (!$preserveHost) {
         if ($host = $uri->getHost()) {
             $new->updateHostFromUri($host);
         }
     }
     return $new;
 }
Ejemplo n.º 26
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.º 27
0
 /**
  * กำหนดค่า Uri
  *
  * @param Uri $uri
  * @param boolean $preserveHost
  * @return \static
  */
 public function withUri(UriInterface $uri, $preserveHost = false)
 {
     $clone = clone $this;
     $clone->uri = $uri;
     if (!$preserveHost) {
         if ($uri->getHost() !== '') {
             $clone->headers['Host'] = $uri->getHost();
         }
     } else {
         if ($this->uri->getHost() !== '' && (!$this->hasHeader('Host') || $this->getHeader('Host') === null)) {
             $clone->headers['Host'] = $uri->getHost();
         }
     }
     return $clone;
 }
Ejemplo n.º 28
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.º 29
0
 /**
  * Returns an instance with the provided URI.
  *
  * This method updates 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`, 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 will 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 will not update the Host header in the returned
  *   request.
  * - If a Host header is present and non-empty, this method will 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;
     $newHost = $uri->getHost();
     $oldHost = isset($request->headers["Host"]) ? $request->headers["Host"] : "";
     if ($preserveHost === false) {
         // Update Host
         if ($newHost && $newHost !== $oldHost) {
             unset($request->headers["Host"]);
             $request->headers["Host"] = $newHost;
         }
     } else {
         // Preserve Host
         if (!$oldHost && $newHost) {
             $request->headers["Host"] = $newHost;
         }
     }
     $request->uri = $uri;
     return $request;
 }
Ejemplo n.º 30
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;
 }