public function testAllowsFalseyUrlParts() { $url = new Uri('http://a:1/0?0#0'); $this->assertSame('a', $url->getHost()); $this->assertEquals(1, $url->getPort()); $this->assertSame('/0', $url->getPath()); $this->assertEquals('0', (string) $url->getQuery()); $this->assertSame('0', $url->getFragment()); $this->assertEquals('http://a:1/0?0#0', (string) $url); $url = new Uri(''); $this->assertSame('', (string) $url); $url = new Uri('0'); $this->assertSame('/0', (string) $url); }
/** * Create a signed Amazon CloudFront URL. * * Keep in mind that URLs meant for use in media/flash players may have * different requirements for URL formats (e.g. some require that the * extension be removed, some require the file name to be prefixed * - mp4:<path>, some require you to add "/cfx/st" into your URL). * * @param string $url URL to sign (can include query * string string and wildcards) * @param string|integer|null $expires UTC Unix timestamp used when signing * with a canned policy. Not required * when passing a custom $policy. * @param string $policy JSON policy. Use this option when * creating a signed URL for a custom * policy. * * @return string The file URL with authentication parameters * @throws \InvalidArgumentException if the URL provided is invalid * @link http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/WorkingWithStreamingDistributions.html */ public function getSignedUrl($url, $expires = null, $policy = null) { // Determine the scheme of the url $urlSections = explode('://', $url); if (count($urlSections) < 2) { throw new \InvalidArgumentException("Invalid URL: {$url}"); } // Get the real scheme by removing wildcards from the scheme $scheme = str_replace('*', '', $urlSections[0]); $uri = new Uri($scheme . '://' . $urlSections[1]); $query = Psr7\parse_query($uri->getQuery(), PHP_QUERY_RFC3986); $signature = $this->signer->getSignature($this->createResource($scheme, (string) $uri), $expires, $policy); $uri = $uri->withQuery(http_build_query($query + $signature, null, '&', PHP_QUERY_RFC3986)); return $scheme === 'rtmp' ? $this->createRtmpUrl($uri) : (string) $uri; }
protected function assertUriEquals($expectedUri, $actualUri) { if (is_string($expectedUri)) { $expectedUri = new Psr7\Uri($expectedUri); } if (is_string($actualUri)) { $actualUri = new Psr7\Uri($actualUri); } $this->assertEquals($expectedUri->getScheme(), $actualUri->getScheme()); $this->assertEquals($expectedUri->getPort(), $actualUri->getPort()); $this->assertEquals($expectedUri->getHost(), $actualUri->getHost()); $this->assertEquals($expectedUri->getPath(), $actualUri->getPath()); $expectedQueryParts = Psr7\parse_query($expectedUri->getQuery()); $actualQueryParts = Psr7\parse_query($actualUri->getQuery()); $this->assertEquals($expectedQueryParts, $actualQueryParts); }
/** * Generate a base string for a HMAC-SHA1 signature * based on the given a url, method, and any parameters. * * @param Url $url * @param string $method * @param array $parameters * * @return string */ protected function baseString(Uri $url, $method = 'POST', array $parameters = array()) { $baseString = rawurlencode($method) . '&'; $schemeHostPath = Uri::fromParts(array('scheme' => $url->getScheme(), 'host' => $url->getHost(), 'path' => $url->getPath())); $baseString .= rawurlencode($schemeHostPath) . '&'; $data = array(); parse_str($url->getQuery(), $query); $data = array_merge($query, $parameters); // normalize data key/values array_walk_recursive($data, function (&$key, &$value) { $key = rawurlencode(rawurldecode($key)); $value = rawurlencode(rawurldecode($value)); }); ksort($data); $baseString .= $this->queryStringFromData($data); return $baseString; }
/** * Create a signed Amazon CloudFront URL. * * Keep in mind that URLs meant for use in media/flash players may have * different requirements for URL formats (e.g. some require that the * extension be removed, some require the file name to be prefixed * - mp4:<path>, some require you to add "/cfx/st" into your URL). * * @param string $url URL to sign (can include query * string string and wildcards * @param string|integer|null $expires UTC Unix timestamp used when signing * with a canned policy. Not required * when passing a custom $policy. * @param string $policy JSON policy. Use this option when * creating a signed URL for a custom * policy. * * @return string The file URL with authentication parameters * @throws \InvalidArgumentException if the URL provided is invalid * @link http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/WorkingWithStreamingDistributions.html */ public function getSignedUrl($url, $expires = null, $policy = null) { // Determine the scheme of the url $urlSections = explode('://', $url); if (count($urlSections) < 2) { throw new \InvalidArgumentException("Invalid URL: {$url}"); } // Get the real scheme by removing wildcards from the scheme $scheme = str_replace('*', '', $urlSections[0]); if ($policy) { $isCustom = true; } else { $isCustom = false; $policy = $this->createCannedPolicy($scheme, $url, $expires); } $policy = str_replace(' ', '', $policy); $uri = new Uri($scheme . '://' . $urlSections[1]); parse_str($uri->getQuery(), $query); $query = $this->prepareQuery($isCustom, $policy, $query, $expires); $uri = $uri->withQuery(http_build_query($query, null, '&', PHP_QUERY_RFC3986)); return $scheme === 'rtmp' ? $this->createRtmpUrl($uri) : (string) $uri; }
protected function getFilters(Request $request) { $filters = []; $facetConfigs = $this->config->get('sunrise.products.facets'); $uri = new Uri($request->getRequestUri()); $queryParams = \GuzzleHttp\Psr7\parse_query($uri->getQuery()); $category = $this->getCategory($request); if ($category instanceof Category) { $filters['filter'][] = Filter::of()->setName('categories.id')->setValue($category->getId()); } foreach ($queryParams as $filterName => $params) { if (!isset($facetConfigs[$filterName])) { continue; } $facetConfig = $facetConfigs[$filterName]; if ($facetConfig['multi']) { if (!is_array($params)) { $params = [$params]; } $filter = Filter::ofType('array'); } else { $filter = Filter::of(); } switch ($facetConfig['type']) { case 'text': $filter = $filter->setName('variants.attributes.' . $facetConfig['attribute'])->setValue($params); $filters['filter'][] = $filters['filter.facets'][] = $filter; break; case 'enum': $filter = $filter->setName('variants.attributes.' . $facetConfig['attribute'] . '.key')->setValue($params); $filters['filter'][] = $filters['filter.facets'][] = $filter; break; default: throw new \InvalidArgumentException('Facet type not implemented'); } } return $filters; }
/** * Retrieve /path?query#fragment part of URL * @param $url * @return string */ public static function retrieveUri($url) { $uri = new Psr7Uri($url); return (string) (new Psr7Uri())->withPath($uri->getPath())->withQuery($uri->getQuery())->withFragment($uri->getFragment()); }
public static function hasQueryValue(Uri $uri, $key) { $current = $uri->getQuery(); $key = strtr($key, self::$replaceQuery); if (!$current) { $result = []; } else { $result = []; foreach (explode('&', $current) as $part) { if (explode('=', $part)[0] === $key) { return true; } } } return false; }
public function testDefaultReturnValuesOfGetters() { $uri = new Uri(); $this->assertSame('', $uri->getScheme()); $this->assertSame('', $uri->getAuthority()); $this->assertSame('', $uri->getUserInfo()); $this->assertSame('', $uri->getHost()); $this->assertNull($uri->getPort()); $this->assertSame('', $uri->getPath()); $this->assertSame('', $uri->getQuery()); $this->assertSame('', $uri->getFragment()); }