/** * @param string|UriInterface $baseUri * @return ApiUri * @throws \InvalidArgumentException */ public function withBaseUri($baseUri) { if (empty($baseUri)) { throw new \InvalidArgumentException('You must provide a non-empty string or PSR-7 UserInterface'); } elseif (is_string($baseUri)) { $baseUri = new Psr7\Uri($baseUri); } elseif (!$baseUri instanceof UriInterface) { throw new \InvalidArgumentException('Invalid format provided; should either be a string or PSR-7 UriInterface'); } if (!in_array($baseUri->getScheme(), ['http', 'https'])) { throw new \InvalidArgumentException('Only http & https schemes are allowed'); } // always store the base URI with a final / if (!preg_match('{/$}', $baseUri->getPath())) { $baseUri = $baseUri->withPath($baseUri->getPath() . '/'); } /** @var ApiUri $new */ if ('' === (string) $this) { $new = new static($baseUri); } else { $new = clone $this; } $new->baseUri = $baseUri; return $new; }
public function register(Container $pimple) { // File extensions used to determine whether a URI points to an asset // or an HTML file. $pimple['html_extensions'] = static::$html_extensions; $pimple['asset_extensions'] = static::$asset_extensions; // Matches any URI we think points to an HTML page. $pimple['matcher.html'] = function () use($pimple) { return Matcher::all()->pathExtensionIs($pimple['html_extensions']); }; // Matches any URI we think points to an asset file. $pimple['matcher.asset'] = function () use($pimple) { return Matcher::all()->pathExtensionIs($pimple['asset_extensions']); }; // Matches any URI that is considered "in scope." $pimple['matcher.internal'] = function () use($pimple) { $uri = new Uri($pimple['base_url']); return Matcher::all()->schemeIs($uri->getScheme())->hostIs($uri->getHost())->pathMatches('~^' . preg_quote($uri->getPath(), '~') . '~'); }; // Matches any URI that is both internal and HTML. $pimple['matcher.internal_html'] = function () use($pimple) { return Matcher::all()->add($pimple['matcher.internal'])->add($pimple['matcher.html']); }; // Matches any URI that is both internal and an asset. $pimple['matcher.internal_asset'] = function () use($pimple) { return Matcher::all()->add($pimple['matcher.internal'])->add($pimple['matcher.asset']); }; }
/** * Initialize the stream client and resources * * @param string $path */ protected function initialize($path) { $uri = new Uri($path); if (!ctype_digit($uri->getHost())) { throw new \InvalidArgumentException(sprintf('path "%s" must be of type integer', $path)); } $this->length = (int) $uri->getHost(); }
/** * {@inheritdoc} */ public function perform(OperationInterface $operation, ConfigurationInterface $configuration) { $preparedRequestParams = $this->prepareRequestParams($operation, $configuration); $queryString = $this->buildQueryString($preparedRequestParams, $configuration); $uri = new Uri(sprintf($this->requestTemplate, $configuration->getCountry(), $queryString)); $request = new \GuzzleHttp\Psr7\Request('GET', $uri->withScheme($this->scheme), ['User-Agent' => 'ApaiIO [' . ApaiIO::VERSION . ']']); $result = $this->client->send($request); return $result->getBody()->getContents(); }
public static function appendPath($url, $path) { $uri = new Psr7Uri($url); $cutUrl = (string) $uri->withQuery('')->withFragment(''); if ($path === '' || $path[0] === '#') { return $cutUrl . $path; } else { return rtrim($cutUrl, '/') . '/' . ltrim($path, '/'); } }
/** * @param array $params { * @var string $resource Flora resource * @var int|string $id optional Unique item identifier * @var string $format optional Output format (default json) * @var string $action optional API action (default: retrieve) * @var string $select optional Retrieve only specified attributes * @var string $filter optional Filter items by criteria * @var int $limit optional Limit result set * @var int $page optional Paginate through result set * @var string $search optional Search items by full-text search * @var bool $cache optional En-/disable caching (default: true) * @var bool $authenticate optional Use authentication provider to add some authentication information to request * @var string $httpMethod optional Explicitly set/override HTTP (GET, POST,...) method * @var array|\stdClass $data optional Send $data as JSON * } * @return \stdClass * @throws \Flora\Exception */ public function execute(array $params) { if (!isset($params['resource']) || empty($params['resource'])) { throw new Exception('Resource must be set'); } $uri = $this->uri->withPath($this->getPath($params)); foreach (['resource', 'id', 'format'] as $param) { // remove path params from request params if (isset($params[$param])) { unset($params[$param]); } } if (array_key_exists('cache', $params)) { if ((bool) $params['cache'] === false) { $params['_'] = time(); } unset($params['cache']); } if (isset($params['action']) && $params['action'] == 'retrieve') { unset($params['action']); } $httpMethod = $this->getHttpMethod($params); $request = new Request($httpMethod, $uri, ['Referer' => $this->getCurrentUri()]); if (isset($params['authenticate'])) { if ((bool) $params['authenticate']) { if ($this->authProvider === null) { throw new Exception('Authentication provider is not configured'); } $request = $this->authProvider->authenticate($request); } unset($params['authenticate']); } if (!empty($this->defaultParams)) { $params = array_merge($this->defaultParams, $params); } if (!empty($params)) { $request = $this->applyParameters($request, $params); } try { $response = $this->httpClient->send($request, $this->httpOptions); } catch (RequestException $e) { throw new Exception($e->getMessage()); } $result = $response->getBody(); $contentType = $response->getHeaderLine('Content-Type'); if (strpos($contentType, 'application/json') !== false) { $result = json_decode($result); } $statusCode = $response->getStatusCode(); if ($statusCode < 400) { return $result; } $this->throwError($statusCode, $result->error); }
/** * 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; }
/** * {@inheritdoc} */ public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { // Only apply to a full URL. if (is_string($value) && strpos($value, '://') > 0) { // URL encode everything after the hostname. $parsed_url = parse_url($value); // Fail on seriously malformed URLs. if ($parsed_url === FALSE) { throw new MigrateException("Value '{$value}' is not a valid URL"); } // Iterate over specific pieces of the URL rawurlencoding each one. $url_parts_to_encode = array('path', 'query', 'fragment'); foreach ($parsed_url as $parsed_url_key => $parsed_url_value) { if (in_array($parsed_url_key, $url_parts_to_encode)) { // urlencode() would convert spaces to + signs. $urlencoded_parsed_url_value = rawurlencode($parsed_url_value); // Restore special characters depending on which part of the URL this is. switch ($parsed_url_key) { case 'query': $urlencoded_parsed_url_value = str_replace('%26', '&', $urlencoded_parsed_url_value); break; case 'path': $urlencoded_parsed_url_value = str_replace('%2F', '/', $urlencoded_parsed_url_value); break; } $parsed_url[$parsed_url_key] = $urlencoded_parsed_url_value; } } $value = (string) Uri::fromParts($parsed_url); } return $value; }
/** * Convert relative links, images scr and form actions to absolute * * @param ElementFinder $page * @param string $affectedUrl */ public static function convertUrlsToAbsolute(ElementFinder $page, $affectedUrl) { $affected = new Uri($affectedUrl); $srcElements = $page->element('//*[@src] | //*[@href] | //form[@action]'); $baseUrl = $page->value('//base/@href')->getFirst(); foreach ($srcElements as $element) { $attributeName = 'href'; if ($element->hasAttribute('action') === true and $element->tagName === 'form') { $attributeName = 'action'; } else { if ($element->hasAttribute('src') === true) { $attributeName = 'src'; } } $relative = $element->getAttribute($attributeName); # don`t change javascript in href if (preg_match('!^\\s*javascript\\s*:\\s*!', $relative)) { continue; } if (parse_url($relative) === false) { continue; } if (!empty($baseUrl) and !preg_match('!^(/|http)!i', $relative)) { $relative = Uri::resolve(new Uri($baseUrl), $relative); } $url = Uri::resolve($affected, (string) $relative); $element->setAttribute($attributeName, (string) $url); } }
/** * @param RequestInterface $request * @return RequestInterface */ public function __invoke(RequestInterface $request) { foreach ($this->params as $param => $value) { $request = $request->withUri(Uri::withQueryValue($request->getUri(), $param, $value)); } return $request; }
public function getForm($formId, $params, $headers) { /** @var DOMElement $form */ $dom = new DomDocument(); libxml_use_internal_errors(true); $dom->loadHTML($this->response()); $xpath = new DOMXpath($dom); $form = $xpath->query("//form[@id='{$formId}']")->item(0); $elements = $xpath->query('//input'); $form_params = []; $allowedTypes = ["hidden", "text", "password"]; foreach ($elements as $element) { /** @var DOMElement $element */ $type = $element->getAttribute("type"); if (in_array($type, $allowedTypes)) { $name = $element->getAttribute("name"); $value = $element->getAttribute("value"); $form_params[$name] = $value; } } $headers = array_merge(["Referer" => $this->baseUri], $headers); $url = Uri::resolve(new Uri($this->baseUri), $form->getAttribute("action"))->__toString(); $method = strtoupper($form->getAttribute("method")); return ["method" => $method, "url" => $url, "headers" => $headers, "params" => array_merge($form_params, $params)]; }
/** * 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; }
/** * @param string $filename * @param string $version * * @return \Psr\Http\Message\UriInterface */ public function uriFor($filename, $version = '') { $filename = original_version($filename); if ($version) { $filename .= ":{$version}"; } return Uri::resolve($this->baseUri, $filename); }
/** * @param RequestInterface $request * * @return RequestInterface */ private function appendQueryParams(RequestInterface $request) { $queryParams = $this->auth->getQueryParameters(); foreach ($queryParams as $key => $value) { $uri = Uri::withQueryValue($request->getUri(), $key, $value); $request = $request->withUri($uri); } return $request; }
/** * @param RequestInterface $request * * @return ResponseInterface * * @throws ApiException | SearchLimitException */ public function research(RequestInterface $request) { try { $response = $this->httpClient->request('GET', Uri::withQueryValue($request->getUri(), 'apikey', $this->apiKey)); return new Response($response); } catch (\GuzzleHttp\Exception\ClientException $ex) { throw ExceptionFactory::createThrowable($ex); } }
private function executeKohanaRequest(ServerRequestInterface $request, $redirect = 0) { if ($redirect >= self::MAX_REDIRECTS) { throw new LogicException(sprintf('Maximum Number of redirects (%s) for url %s', self::MAX_REDIRECTS, $request->getUri())); } Request::$initial = $kohanaRequest = $this->getKohanaRequest($request); Request::$user_agent = self::USER_AGENT; $_FILES = $request->getAttribute('FILES'); $kohanaResponse = $kohanaRequest->execute(); $this->current = $request; $status = $kohanaResponse->status(); if ($status >= 300 && $status < 400) { $redirectUri = new Psr7\Uri($kohanaResponse->headers('location')); $localUri = $redirectUri->withScheme(null)->withUserInfo(null)->withHost(null)->withPort(null); return $this->executeKohanaRequest(new Psr7\ServerRequest('GET', $localUri), $redirect + 1); } return $kohanaResponse; }
/** * {@inheritDoc} */ public function __invoke(RequestInterface $request, array $options) { $next = $this->nextHandler; if (!$this->config->has('access_token') || $this->config->get('access_token') === null) { return $next($request, $options); } $uri = Uri::withQueryValue($request->getUri(), 'access_token', $this->config->get('access_token')->getToken()); return parent::__invoke($request->withUri($uri)->withHeader('Content-Type', 'application/json'), $options); }
public function __construct(Options $options, OutputInterface $ouput) { $timeout = $options->getTimeout(); $client = new ClientGuzzle(['timeout' => $timeout]); $url = $options->getUrl(); $response = $client->get($url); if ($response->getStatusCode() != 200) { throw new RuntimeException(sprintf('Cannot access this url: "%s"', $url)); } $uri = new Uri($url); $baseUri = $uri->getScheme() . '://' . $uri->getHost(); if (!empty($uri->getPort())) { $baseUri .= ':' . $uri->getPort(); } $options->setBaseUri($baseUri); $this->options = $options; $this->output = $ouput; }
public static function get(array $options, $key) { $stack = HandlerStack::create(); $stack->unshift(Middleware::mapRequest(function (RequestInterface $request) use($key) { return $request->withUri(Uri::withQueryValue($request->getUri(), 'key', $key)); })); $options['handler'] = $stack; return new Client($options); }
/** * @param $url * @param array $params * * @return \Psr\Http\Message\UriInterface */ private function makeUri($url, $params = array()) { $uri = \GuzzleHttp\uri_template($this->endpoint, array_merge($this->defaults, $params)); $uri = new Psr7\Uri($uri); // All arguments must be urlencoded (as per RFC 1738). $query = Psr7\build_query($params, PHP_QUERY_RFC1738); $uri = $uri->withQuery($query); return Psr7\Uri::withQueryValue($uri, 'url', $url); }
/** * Uri constructor. * Overridden constructor to achieve every * request to the ZOHO should have Auth Token * * @param string $uri * @param ConfigurationInterface $configuration */ public function __construct($uri = '', ConfigurationInterface $configuration) { if ($uri != null) { $parts = parse_url($uri); if ($parts === false || !array_key_exists('query', $parts)) { return parent::__construct($uri . '?authtoken=' . $configuration->getAuthToken()); } return parent::__construct(str_replace($parts['query'], 'authtoken=' . $configuration->getAuthToken() . '&' . $parts['query'], $uri)); } }
/** * Inject credentials information into the query parameters * * @param RequestInterface $request * @param array $options * * @return GuzzleResponseInterface */ public function __invoke(RequestInterface $request, array $options) { if ($request->getUri()->getScheme() == 'https' && $options['auth'] == static::AUTH_NAME) { $uri = Uri::withQueryValue($request->getUri(), 'client_id', $this->userKey ?: $this->apiKey); $uri = Uri::withQueryValue($uri, 'client_secret', $this->secret); $request = $request->withUri($uri); } $fn = $this->nextHandler; return $fn($request, $options); }
/** * @param null|string $method HTTP method for the request. * @param null|string $uri URI for the request. * @param array $headers Headers for the message. * @param string|resource|StreamInterface $body Message body. * @param string $protocolVersion HTTP protocol version. * * @throws InvalidArgumentException for an invalid URI */ public function __construct($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1') { if (is_string($uri)) { $uri = new Uri($uri); } elseif (!$uri instanceof UriInterface) { throw new \InvalidArgumentException('URI must be a string or Psr\\Http\\Message\\UriInterface'); } $this->method = strtoupper($method); $this->uri = $uri; $this->setHeaders($headers); $this->protocol = $protocolVersion; $host = $uri->getHost(); if ($host && !$this->hasHeader('Host')) { $this->updateHostFromUri($host); } if ($body) { $this->stream = stream_for($body); } }
/** * {@inheritDoc} */ public function __invoke(RequestInterface $request, array $options) { $next = $this->nextHandler; if (!$this->config->get('secure_requests')) { return $next($request, $options); } $uri = $request->getUri(); $sig = $this->generateSig($this->getPath($uri), $this->getQueryParams($uri), $this->config->get('client_secret')); $uri = Uri::withQueryValue($uri, 'sig', $sig); return parent::__invoke($request->withUri($uri), $options); }
public function get($url, array $options = [], \Closure $processor = null) { $url = Psr7\Uri::resolve($this->baseUri, $url); $response = $this->httpClient->get($url, $options); if (null !== $processor) { $response = $processor($response); } $crawler = new Crawler(null, (string) $url, (string) $this->baseUri); $crawler->addContent($response->getBody()); return $crawler; }
/** * Moves the URI of the queue to the URI in the input parameter. * * @return callable */ private function queueUrl() { return static function (callable $handler) { return function (CommandInterface $c, RequestInterface $r = null) use($handler) { if ($c->hasParam('QueueUrl')) { $uri = Uri::resolve($r->getUri(), $c['QueueUrl']); $r = $r->withUri($uri); } return $handler($c, $r); }; }; }
/** * @param string $attribute * @param UriInterface $base */ private function resolveLinkAttribute($attribute, UriInterface $base) { $elements = $this->xpath->query("//*[@{$attribute} and not(contains(@{$attribute}, \"://\"))]"); foreach ($elements as $element) { try { $resolved = Uri::resolve($base, $element->getAttribute($attribute)); $element->setAttribute($attribute, $resolved->__toString()); } catch (InvalidArgumentException $e) { // Tolerate invalid urls } } }
private function extractHeaders(BrowserKitRequest $request) { $headers = []; $server = $request->getServer(); $uri = new Uri($request->getUri()); $server['HTTP_HOST'] = $uri->getHost(); $port = $uri->getPort(); if ($port !== null && $port !== 443 && $port != 80) { $server['HTTP_HOST'] .= ':' . $port; } $contentHeaders = array('Content-Length' => true, 'Content-Md5' => true, 'Content-Type' => true); foreach ($server as $header => $val) { $header = implode('-', array_map('ucfirst', explode('-', strtolower(str_replace('_', '-', $header))))); if (strpos($header, 'Http-') === 0) { $headers[substr($header, 5)] = $val; } elseif (isset($contentHeaders[$header])) { $headers[$header] = $val; } } return $headers; }
/** * 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; }
/** * @test */ public function invoke_QueryParameters_Success() { $this->auth->expects($this->once())->method('getHeaders')->willReturn([]); $this->auth->expects($this->once())->method('getQueryParameters')->willReturn(['jwt' => 'YYYY', 'otherParam' => 'YYYY']); $this->request->expects($this->exactly(3))->method('getUri')->willReturn($this->uri); $this->request->expects($this->exactly(2))->method('withUri')->withConsecutive(Uri::withQueryValue($this->uri, 'jwt', 'YYYY'), Uri::withQueryValue($this->uri, 'otherParam', 'YYYY'))->willReturnSelf(); $middleware = new ConnectMiddleware($this->auth, $this->appContext); $callable = $middleware(function (RequestInterface $actualRequest, array $options) { $this->assertEquals($this->request, $actualRequest); $this->assertEquals(['Hello World'], $options); }); $callable($this->request, ['Hello World']); }