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); }
/** * @param UriInterface $uri Has to contain a host name and cans have a path. * @param array $hostConfig Config for AddHostPlugin. @see AddHostPlugin::configureOptions */ public function __construct(UriInterface $uri, array $hostConfig = []) { $this->addHostPlugin = new AddHostPlugin($uri, $hostConfig); if (rtrim($uri->getPath(), '/')) { $this->addPathPlugin = new AddPathPlugin($uri); } }
/** * @param string $specification * @return string */ private function createUrlFromUri($specification) { preg_match('%^(?P<path>[^?#]*)(?:(?:\\?(?P<query>[^#]*))?(?:\\#(?P<fragment>.*))?)$%', (string) $specification, $matches); $path = $matches['path']; $query = isset($matches['query']) ? $matches['query'] : ''; $fragment = isset($matches['fragment']) ? $matches['fragment'] : ''; $uri = $this->uri->withQuery('')->withFragment(''); // Relative path if (!empty($path) && '/' !== $path[0]) { $path = rtrim($this->uri->getPath(), '/') . '/' . $path; } // Path present; set on URI if (!empty($path)) { $uri = $uri->withPath($path); } // Query present; set on URI if (!empty($query)) { $uri = $uri->withQuery($query); } // Fragment present; set on URI if (!empty($fragment)) { $uri = $uri->withFragment($fragment); } return (string) $uri; }
public function setBase(UriInterface $uri) { $base = (string) $uri->withPath('')->withQuery('')->withFragment(''); if ($base and $base !== $this->base) { $this->base = $base; } }
/** * Retrieves the message's request target. * * Retrieves the message's request-target either as it will appear (for * clients), as it appeared at request (for servers), or as it was * specified for the instance (see withRequestTarget()). * * In most cases, this will be the origin-form of the composed URI, * unless a value was provided to the concrete implementation (see * withRequestTarget() below). * * If no URI is available, and no request-target has been specifically * provided, this method MUST return the string "/". * * @return string */ public function getRequestTarget() : string { if ($this->target !== null) { return $this->target; } return $this->uri->getPath() === '' ? '/' : $this->uri->getPath(); }
/** * @param AccessToken $accessToken * @param UriInterface $destination * @return Response */ public function handle(AccessToken $accessToken, UriInterface $destination) { $claims = $this->userService->getUserClaims($accessToken)->toArray(); $jwt = $this->encoderService->encode($claims); $q = $destination->getQuery(); $q .= ($q ? '&' : '') . 'jwt=' . $jwt; return new RedirectResponse((string) $destination->withQuery($q)); }
public function updateBaseUri(UriInterface $uri) { $base = (string) $uri->withPath('')->withQuery('')->withFragment(''); $oldBase = (string) $this->getClient()->getConfig('base_uri'); if ($base and $base !== $oldBase) { $this->client = new Client(['base_uri' => $base]); } }
/** * {@inheritdoc} */ public function sameValueAs(UriInterface $url) { try { return static::createFromComponents($this->schemeRegistry, static::Parse($url->__toString()))->toAscii()->normalize()->ksortQuery()->__toString() === $this->toAscii()->normalize()->ksortQuery()->__toString(); } catch (InvalidArgumentException $e) { return false; } }
/** * Builds URI for provided route instance. * * @param RouteContract $route * @param array $variables * @param array $query * @return UriInterface */ private function buildRouteUri(RouteContract $route, array $variables = [], array $query = []) : UriInterface { $uri = $this->uri->withScheme($route->getScheme() ?: $this->request->getUri()->getScheme())->withHost($route->getHost() ?: $this->request->getUri()->getHost())->withPath($route->compilePath($variables)); if ($query) { $uri = $uri->withQuery(http_build_query($query)); } return $uri; }
/** * {@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); }
private function createRtmpUrl(UriInterface $uri) { // Use a relative URL when creating Flash player URLs $result = ltrim($uri->getPath(), '/'); if ($query = $uri->getQuery()) { $result .= '?' . $query; } return $result; }
/** * Merges additional query parameters with current query parameters (possibly overwriting (some of) them) * * @param UriInterface $uri * @param array $queryParameters Query parameters to add / overwrite * @return UriInterface */ public static function addQueryParameters(UriInterface $uri, array $queryParameters) { $originalQuery = $uri->getQuery(); $originalQueryParameters = []; parse_str($originalQuery, $originalQueryParameters); $newQueryParameters = array_replace_recursive($originalQueryParameters, $queryParameters); $newQuery = http_build_query($newQueryParameters); $newUri = $uri->withQuery($newQuery); return $newUri; }
/** * Gets the query parameters as an array from a ``UriInterface`` instance. * * @param UriInterface $uri * @param array $exclude * @param array $params * * @return array */ public function getQueryParams(UriInterface $uri, $exclude = ['sig'], $params = []) { parse_str($uri->getQuery(), $params); foreach ($exclude as $excludedParam) { if (array_key_exists($excludedParam, $params)) { unset($params[$excludedParam]); } } return $params; }
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; }
private function parseVirtualHosted(UriInterface $url, array $matches) { $result = self::$defaultResult; $result['path_style'] = false; // Remove trailing "." from the prefix to get the bucket $result['bucket'] = substr($matches[1], 0, -1); $path = $url->getPath(); // Check if a key was present, and if so, removing the leading "/" $result['key'] = !$path || $path == '/' ? null : substr($path, 1); return $result; }
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; }
/** @test */ public function shouldCatchInvalidBody() { $body = $this->getMock('\\Psr\\Http\\Message\\StreamInterface'); $body->method('getContents')->willReturn('{"title":"Aaa"}'); $this->request->method('getMethod')->willReturn('post'); $this->uri->method('getPath')->willReturn('/songs'); $this->request->method('getHeaderLine')->with('Content-Type')->willReturn('application/json'); $this->request->method('getBody')->willReturn($body); $this->setExpectedException('\\Raml\\Validator\\ValidatorRequestException', 'title (minLength), artist (required)'); $validator = $this->getValidatorForSchema(__DIR__ . '/../fixture/validator/requestBody.raml'); $validator->validateRequest($this->request); }
public function getSelectedValues(UriInterface $uri) { $queryParams = Psr7\parse_query($uri->getQuery()); $selectedValues = []; foreach ($queryParams as $paramName => $params) { if (!isset($this->paramFacets[$paramName])) { continue; } $facetName = $this->paramFacets[$paramName]; $selectedValues[$facetName] = $params; } return $selectedValues; }
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; }
/** * @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; }
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; }
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; }
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; }
/** * Gets the request target from current URI * * @return string */ private function targetFromUri() { $target = $this->uri->getPath(); $target .= '' === $this->uri->getQuery() ? '' : '?' . $this->uri->getQuery(); $target = empty($target) ? '/' : $target; return $target; }
/** * @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(); }
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; }
/** * @param UriInterface $uri * @return UriInterface * @throws \ErrorException */ protected function forward_aliases(UriInterface $uri) { // strip beginning / $path = substr($uri->getPath(), 1); $aliases = $this->db->conn()->prepare(<<<SQL SELECT `alias_go`, `alias_forward_to` FROM `{$this->db->getPrefix()}aliases` WHERE `alias_active` = 1 AND `alias_go` = ? SQL ); $aliases->execute(array($path)); $aliases = $aliases->fetchAll(\PDO::FETCH_ASSOC); foreach ($aliases as $alias) { if ($path == $alias['alias_go']) { $path = $alias['alias_forward_to']; } } return $uri->withPath('/' . $path); }
protected function processPortHeader(ServerRequestInterface $request, UriInterface $uri) { if ($request->hasHeader('X-Forwarded-Port')) { $port = trim(current(explode(',', $request->getHeaderLine('X-Forwarded-Port')))); if (preg_match('/^\\d+\\z/', $port)) { return $uri->withPort((int) $port); } } return $uri; }
/** * returns the resolve URI * * @param LeagueUriInterface|UriInterface $relative the relative URI * * @return LeagueUriInterface|UriInterface */ protected function resolveRelative($relative) { $path = $relative->getPath(); if (!empty($path)) { return $this->resolveRelativePath($relative, $path, $relative->getQuery()); } $query = $relative->getQuery(); if (!empty($query)) { return $this->getBaseUri($relative)->withPath($this->uri->getPath())->withQuery($query); } return $this->getBaseUri($relative)->withPath($this->uri->getPath())->withQuery($this->uri->getQuery()); }
/** * Retrieve query string arguments. * * Retrieves the deserialized query string arguments, if any. * * Note: the query params might not be in sync with the URI or server * params. If you need to ensure you are only getting the original * values, you may need to parse the query string from `getUri()->getQuery()` * or from the `QUERY_STRING` server param. * * @return array */ public function getQueryParams() { if ($this->queryParams) { return $this->queryParams; } if ($this->uri === null) { return []; } parse_str($this->uri->getQuery(), $this->queryParams); // <-- URL decodes data return $this->queryParams; }