/** * {@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); }
/** * @param $nodes * @param $uri * @return mixed * @throws UnableToDetermineNodesException * @throws UnknownNodeTypeException */ protected function fetchNode(array $nodes, UriInterface $uri) { foreach ($nodes as $nodeId => $node) { if (!isset($node['type']) || !isset($node['pattern'])) { continue; } if (strpos($node['type'], 'url') !== 0) { continue; } if (strpos($uri->getScheme(), 'http') !== 0) { continue; } $type = $node['type']; $pattern = $node['pattern']; $subject = NULL; //check if https is required if (isset($node['secure'])) { if ($node['secure'] && strpos($uri->getScheme(), 'https') !== 0) { continue; } } //determine url part by type switch ($type) { case "url": $subject = $uri; break; case "url_path": $subject = $uri->getPath(); break; case "url_host": $subject = $uri->getHost(); break; case "url_query": $subject = $uri->getQuery(); break; default: throw new UnknownNodeTypeException($type); } if (preg_match_all('#' . $pattern . '#i', $subject)) { return [$nodeId, $node]; } } throw new UnableToDetermineNodesException(); }
/** * @param LeagueUriInterface|UriInterface $relative * * @return LeagueUriInterface|UriInterface */ protected function generate($relative) { $scheme = $relative->getScheme(); if (!empty($scheme) && $scheme != $this->uri->getScheme()) { return $relative; } if (!empty($relative->getAuthority())) { return $relative->withScheme($this->uri->getScheme()); } return $this->resolveRelative($relative)->withFragment($relative->getFragment()); }
/** * @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; }
/** * 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; }
/** * Check if the given URL has an https:// protocol scheme. * * @param \Psr\Http\Message\UriInterface $uri * @return bool */ protected function isSecure(UriInterface $uri) { return strtolower($uri->getScheme()) === 'https'; }
/** * Format an Uri according to the Formatter properties * * @param Uri|UriInterface $uri * * @return string */ protected function formatUri($uri) { $scheme = $uri->getScheme(); if ('' !== $scheme) { $scheme .= ':'; } $auth = $this->formatAuthority($uri); return $scheme . $auth . $this->formatPath($uri->getPath(), $auth) . $this->formatQuery($uri) . $this->formatFragment($uri); }
/** * Returns the target URI as a relative reference from the base URI. * * This method is the counterpart to resolve(): * * (string) $target === (string) UriResolver::resolve($base, UriResolver::relativize($base, $target)) * * One use-case is to use the current request URI as base URI and then generate relative links in your documents * to reduce the document size or offer self-contained downloadable document archives. * * $base = new Uri('http://example.com/a/b/'); * echo UriResolver::relativize($base, new Uri('http://example.com/a/b/c')); // prints 'c'. * echo UriResolver::relativize($base, new Uri('http://example.com/a/x/y')); // prints '../x/y'. * echo UriResolver::relativize($base, new Uri('http://example.com/a/b/?q')); // prints '?q'. * echo UriResolver::relativize($base, new Uri('http://example.org/a/b/')); // prints '//example.org/a/b/'. * * This method also accepts a target that is already relative and will try to relativize it further. Only a * relative-path reference will be returned as-is. * * echo UriResolver::relativize($base, new Uri('/a/b/c')); // prints 'c' as well * * @param UriInterface $base Base URI * @param UriInterface $target Target URI * * @return UriInterface The relative URI reference */ public static function relativize(UriInterface $base, UriInterface $target) { if ($target->getScheme() !== '' && ($base->getScheme() !== $target->getScheme() || $target->getAuthority() === '' && $base->getAuthority() !== '')) { return $target; } if (Uri::isRelativePathReference($target)) { // As the target is already highly relative we return it as-is. It would be possible to resolve // the target with `$target = self::resolve($base, $target);` and then try make it more relative // by removing a duplicate query. But let's not do that automatically. return $target; } if ($target->getAuthority() !== '' && $base->getAuthority() !== $target->getAuthority()) { return $target->withScheme(''); } // We must remove the path before removing the authority because if the path starts with two slashes, the URI // would turn invalid. And we also cannot set a relative path before removing the authority, as that is also // invalid. $emptyPathUri = $target->withScheme('')->withPath('')->withUserInfo('')->withPort(null)->withHost(''); if ($base->getPath() !== $target->getPath()) { return $emptyPathUri->withPath(self::getRelativePath($base, $target)); } if ($base->getQuery() === $target->getQuery()) { // Only the target fragment is left. And it must be returned even if base and target fragment are the same. return $emptyPathUri->withQuery(''); } // If the base URI has a query but the target has none, we cannot return an empty path reference as it would // inherit the base query component when resolving. if ($target->getQuery() === '') { $segments = explode('/', $target->getPath()); $lastSegment = end($segments); return $emptyPathUri->withPath($lastSegment === '' ? './' : $lastSegment); } return $emptyPathUri; }
/** * Resolves the given relative or absolute $uri by appending it behind $this base URI * * The given $uri parameter can be either a relative or absolute URI and * as such can not contain any URI template placeholders. * * As such, the outcome of this method represents a valid, absolute URI * which will be returned as an instance implementing `UriInterface`. * * If the given $uri is a relative URI, it will simply be appended behind $base URI. * * If the given $uri is an absolute URI, it will simply be verified to * be *below* the given $base URI. * * @param UriInterface $uri * @param UriInterface $base * @return UriInterface * @throws \UnexpectedValueException * @see Browser::resolve() */ public function expandBase(UriInterface $uri, UriInterface $base) { if ($uri->getScheme() !== '') { if (strpos((string) $uri, (string) $base) !== 0) { throw new \UnexpectedValueException('Invalid base, "' . $uri . '" does not appear to be below "' . $base . '"'); } return $uri; } $uri = (string) $uri; $base = (string) $base; if ($uri !== '' && substr($base, -1) !== '/' && substr($uri, 0, 1) !== '?') { $base .= '/'; } if (isset($uri[0]) && $uri[0] === '/') { $uri = substr($uri, 1); } return $this->uri($base . $uri); }
/** * Whether the URI is a relative-path reference. * * A relative reference that does not begin with a slash character is termed a relative-path reference. * * @param UriInterface $uri * * @return bool * @link https://tools.ietf.org/html/rfc3986#section-4.2 */ public static function isRelativePathReference(UriInterface $uri) { return $uri->getScheme() === '' && $uri->getAuthority() === '' && (!isset($uri->getPath()[0]) || $uri->getPath()[0] !== '/'); }
/** * {@inheritdoc} */ public function join(UriInterface $uriToJoin) { // other host if ($uriToJoin->getScheme() !== '' || $uriToJoin->getHost() !== '') { return clone $uriToJoin; } $uriToReturn = clone $this; // current path. if ($uriToJoin->getPath() === '' || $uriToJoin->getPath() === '.') { return $uriToReturn; } $newPathItems = explode('/', $uriToReturn->path); $pathItemToJoin = explode('/', $uriToJoin->getPath()); if (isset($pathItemToJoin[0])) { array_pop($newPathItems); } $newPathItems = array_merge($newPathItems, $pathItemToJoin); $pathItemsToReturn = []; foreach ($newPathItems as $item) { if ($item === '') { $pathItemsToReturn = [$item]; } elseif ($item === '.') { continue; } elseif ($item === '..') { array_pop($pathItemsToReturn); } else { array_push($pathItemsToReturn, $item); } } if (isset($pathItemsToReturn[0]) && $pathItemsToReturn[0] !== '') { array_unshift($pathItemsToReturn, ''); } $uriToReturn->path = implode('/', $pathItemsToReturn); $uriToReturn->query = $uriToJoin->getQuery(); $uriToReturn->fragment = $uriToJoin->getFragment(); return $uriToReturn; }
/** * Ensures that the url of the certificate is one belonging to AWS, and not * just something from the amazonaws domain, which includes S3 buckets. * * @param UriInterface $uri * * @throws MessageValidatorException if the cert url is invalid */ private function validateUrl(UriInterface $uri) { // The cert URL must be https, a .pem, and match the following pattern. $hostPattern = '/^sns\\.[a-zA-Z0-9\\-]{3,}\\.amazonaws\\.com(\\.cn)?$/'; if ($uri->getScheme() !== 'https' || substr($uri, -4) !== '.pem' || !preg_match($hostPattern, $uri->getHost())) { throw new MessageValidatorException('The certificate is located ' . 'on an invalid domain.'); } }
/** * 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); }
/** * Resolve a base URI with a relative URI and return a new URI. * * @param UriInterface $base Base URI * @param string|UriInterface $rel Relative URI * * @return UriInterface * @link http://tools.ietf.org/html/rfc3986#section-5.2 */ public static function resolve(UriInterface $base, $rel) { if (!$rel instanceof UriInterface) { $rel = new self($rel); } if ((string) $rel === '') { // we can simply return the same base URI instance for this same-document reference return $base; } if ($rel->getScheme() != '') { return $rel->withPath(self::removeDotSegments($rel->getPath())); } if ($rel->getAuthority() != '') { $targetAuthority = $rel->getAuthority(); $targetPath = self::removeDotSegments($rel->getPath()); $targetQuery = $rel->getQuery(); } else { $targetAuthority = $base->getAuthority(); if ($rel->getPath() === '') { $targetPath = $base->getPath(); $targetQuery = $rel->getQuery() != '' ? $rel->getQuery() : $base->getQuery(); } else { if ($rel->getPath()[0] === '/') { $targetPath = $rel->getPath(); } else { if ($targetAuthority != '' && $base->getPath() === '') { $targetPath = '/' . $rel->getPath(); } else { $lastSlashPos = strrpos($base->getPath(), '/'); if ($lastSlashPos === false) { $targetPath = $rel->getPath(); } else { $targetPath = substr($base->getPath(), 0, $lastSlashPos + 1) . $rel->getPath(); } } } $targetPath = self::removeDotSegments($targetPath); $targetQuery = $rel->getQuery(); } } return new self(self::createUriString($base->getScheme(), $targetAuthority, $targetPath, $targetQuery, $rel->getFragment())); }
/** * @param $urlString * @param UriInterface $originUrl * @return UriInterface */ private function createAbsoulteUrl(UriInterface $uri, UriInterface $originUrl) { // @example href="" if ((string) $uri == "" || strpos((string) $uri, "#") === 0) { return $originUrl; } // @example href="?cat=1" if (strpos((string) $uri, "?") === 0) { return new Uri($originUrl->getScheme() . "://" . $originUrl->getHost() . $originUrl->getPath() . (string) $uri); } if ($uri->getScheme() === '') { if ($uri->getQuery() !== '') { $query = '?' . $uri->getQuery(); } else { $query = ''; } if ($uri->getHost() !== '') { $uriString = $originUrl->getScheme() . '://' . $uri->getHost() . $uri->getPath() . $query; } else { if (strpos($uri->getPath(), '/') === 0) { // absolute path $uriString = $originUrl->getScheme() . '://' . $originUrl->getHost() . $uri->getPath() . $query; } else { // relative path $pathParts = pathinfo($originUrl->getPath()); if (array_key_exists('dirname', $pathParts)) { $dirname = $pathParts['dirname']; if ($dirname != "/") { $dirname .= "/"; } } else { $dirname = "/"; } $uriString = $originUrl->getScheme() . '://' . $originUrl->getHost() . $dirname . $uri->getPath() . $query; } } $resultUri = new Uri($uriString); } else { $resultUri = $uri; } return $resultUri; }
/** * * Converts a uri object to a string in the format <scheme>://<server_address>/<path>?<query_string>#<fragment> * * @param \Psr\Http\Message\UriInterface $uri uri object to be converted to a string * * @return string the string represntation of the uri object. * Eg. http://someserver.com/controller/action */ function s3MVC_UriToString(\Psr\Http\Message\UriInterface $uri) { $scheme = $uri->getScheme(); $authority = $uri->getAuthority(); $basePath = s3MVC_GetBaseUrlPath(); $path = $uri->getPath(); $query = $uri->getQuery(); $fragment = $uri->getFragment(); $path = $basePath . '/' . ltrim($path, '/'); return ($scheme ? $scheme . ':' : '') . ($authority ? '//' . $authority : '') . $path . ($query ? '?' . $query : '') . ($fragment ? '#' . $fragment : ''); }
/** * @param UriInterface $uri * * @return array */ private static function normalizeUri(UriInterface $uri) { return ['schema' => $uri->getScheme(), 'host' => $uri->getHost(), 'path' => $uri->getPath()]; }
/** * @param UriInterface $baseUri */ protected function setBaseUriFromObject(UriInterface $baseUri) { $this->baseUri = ['scheme' => $baseUri->getScheme(), 'host' => $baseUri->getHost(), 'port' => $baseUri->getPort(), 'path' => rtrim($baseUri->getPath(), '/')]; }
/** * 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; }
protected function runMatches(UriInterface $uri) { return $uri->getScheme() == $this->expected; }
/** * Resolve a base URI with a relative URI and return a new URI. * * @param UriInterface $base Base URI * @param null|string|UriInterface $rel Relative URI * * @throws \InvalidArgumentException * * @return UriInterface */ public static function resolve(UriInterface $base, $rel = null) { if ($rel === null || $rel === '') { return $base; } if (!$rel instanceof UriInterface) { $rel = new self($rel); } // Return the relative uri as-is if it has a scheme. if ($rel->getScheme()) { return $rel->withPath(static::removeDotSegments($rel->getPath())); } $relParts = ['scheme' => $rel->getScheme(), 'authority' => $rel->getAuthority(), 'path' => $rel->getPath(), 'query' => $rel->getQuery(), 'fragment' => $rel->getFragment()]; $parts = ['scheme' => $base->getScheme(), 'authority' => $base->getAuthority(), 'path' => $base->getPath(), 'query' => $base->getQuery(), 'fragment' => $base->getFragment()]; if (!empty($relParts['path'])) { if (strpos($relParts['path'], '/') === 0) { $parts['path'] = self::removeDotSegments($relParts['path']); $parts['query'] = $relParts['query']; $parts['fragment'] = $relParts['fragment']; } 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 new self(static::createUriString($parts['scheme'], $parts['authority'], $parts['path'], $parts['query'], $parts['fragment'])); }
/** * Indicates if the route matches with the URI. * * @param UriInterface $uri * @param array $attributes * * @return bool */ public final function matchesWithUri(UriInterface $uri, &$attributes = []) { $matched = ('' === ($scheme = $this->getScheme()) or $scheme === $uri->getScheme()) && ('' === ($host = $this->getHost()) or preg_match($this->compileHost(), $uri->getHost(), $attrs)) && preg_match($this->compilePath(), $uri->getPath(), $attrs); $attributes = array_filter($attrs, 'is_string'); return $matched; }
/** * Format a Interfaces\Schemes\Uri according to the Formatter properties * * @param Uri|UriInterface $uri * * @return string */ protected function formatUri($uri) { $scheme = $uri->getScheme(); if (!empty($scheme)) { $scheme .= ':'; } $auth = $this->formatAuthority($uri); $query = $this->formatUriPart(new Query($uri->getQuery())); if (!empty($query)) { $query = '?' . $query; } $fragment = $uri->getFragment(); if (!empty($fragment)) { $fragment = '#' . $fragment; } return $scheme . $auth . $this->formatPath($uri->getPath(), !empty($auth)) . $query . $fragment; }
public function scheme() { return $this->uri->getScheme(); }