/** * Forward the request to the target url and return the response. * * @param string $target * @throws UnexpectedValueException * @return Response */ public function to($target) { if (is_null($this->request)) { throw new UnexpectedValueException('Missing request instance.'); } $target = new Uri($target); // Overwrite target scheme and host. $uri = $this->request->getUri()->withScheme($target->getScheme())->withHost($target->getHost()); // Check for custom port. if ($port = $target->getPort()) { $uri = $uri->withPort($port); } // Check for subdirectory. if ($path = $target->getPath()) { $uri = $uri->withPath(rtrim($path, '/') . '/' . ltrim($uri->getPath(), '/')); } $request = $this->request->withUri($uri); $stack = $this->filters; $stack[] = function (RequestInterface $request, ResponseInterface $response, callable $next) { $response = $this->adapter->send($request); return $next($request, $response); }; $relay = (new RelayBuilder())->newInstance($stack); return $relay($request, new Response()); }
/** * Forward the request to the target url and return the response. * * @param string $target * @throws UnexpectedValueException * @return Response */ public function to($target) { if (is_null($this->request)) { throw new UnexpectedValueException('Missing request instance.'); } $uri = $this->request->getUri()->withScheme(parse_url($target, PHP_URL_SCHEME))->withHost(parse_url($target, PHP_URL_HOST)); $request = $this->request->withUri($uri); $stack = $this->filters; $stack[] = function (RequestInterface $request, ResponseInterface $response, callable $next) use($target) { $response = $this->adapter->send($request, $target); return $next($request, $response); }; $relay = (new RelayBuilder())->newInstance($stack); return $relay($request, new Response()); }
public function testWithUriTrue() { /** * 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. */ $mockUri = Mockery::mock(UriInterface::class); $mockUri->shouldReceive('getHost')->andReturn('localhost'); $mockUri->shouldReceive('getPort')->andReturn(8888); $mockUri->shouldReceive('getPath')->andReturn('/'); $mockUri->shouldReceive('getQuery')->andReturn(''); $requestWithUri = $this->request->withUri($mockUri); $this->assertEquals('/', $requestWithUri->getRequestTarget()); $this->assertEquals('localhost:8888', $requestWithUri->getHeaderLine('host')); }
/** * Adds authentication credentials to given request. * * @param RequestInterface $request * * @return RequestInterface */ protected function authenticateRequest(RequestInterface $request) { $uri = $request->getUri(); parse_str($uri->getQuery(), $query); $query['key'] = Configuration::get('key'); $query['token'] = Configuration::get('token'); $uri = $uri->withQuery(http_build_query($query)); return $request->withUri($uri); }
/** * @param RequestInterface $request * * @return RequestInterface */ public function addAuthentificationInfo(RequestInterface $request) { $uri = $request->getUri(); $qs = $uri->getQuery(); parse_str($qs, $query_data); $query_data[$this->param_name] = $this->value; $qs = http_build_query($query_data); $uri = $uri->withQuery($qs); return $request->withUri($uri); }
private function transformToUploadUrl() { $parts = parse_url((string) $this->request->getUri()); if (!isset($parts['path'])) { $parts['path'] = ''; } $parts['path'] = '/upload' . $parts['path']; $uri = Uri::fromParts($parts); $this->request = $this->request->withUri($uri); }
/** * {@inheritdoc} */ public function authenticate(RequestInterface $request) { $uri = $request->getUri(); $query = $uri->getQuery(); $params = []; parse_str($query, $params); $params = array_merge($params, $this->params); $query = http_build_query($params); $uri = $uri->withQuery($query); return $request->withUri($uri); }
private function modifyRequest(RequestInterface $request, CommandInterface $command) { $uri = $request->getUri(); $path = $uri->getPath(); $bucket = $command['Bucket']; $path = $this->removeBucketFromPath($path, $bucket); // Modify the Key to make sure the key is encoded, but slashes are not. if ($command['Key']) { $path = S3Client::encodeKey(rawurldecode($path)); } return $request->withUri($uri->withPath($path)); }
/** * @param RequestInterface $request * @param bool $forceNew * * @return RequestInterface */ public function signRequest(RequestInterface $request, $forceNew = false) { try { // force-get a new token if it was requested, if not, the regular // caching mechanism will be used so the call is not necessary here. if (true === $forceNew) { $this->pool->getToken($forceNew); } // create a new request with the new uri and the token added to the headers $uri = Uri::resolve(new Uri($this->pool->getEndpointUrl()), $request->getUri()); return $request->withUri($uri)->withHeader('X-Auth-Token', $this->pool->getTokenId()); } catch (TokenException $e) { throw new ClientException('Could not obtain token', $request, null, $e); } }
/** * Execute the middleware. * * @param RequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next) { $uri = $request->getUri(); $host = $uri->getHost(); if ($this->addWww) { if ($this->canAddWww($host)) { $host = "www.{$host}"; } } elseif (strpos($host, 'www.') === 0) { $host = substr($host, 4); } //redirect if (is_int($this->redirectStatus) && $uri->getHost() !== $host) { return self::getRedirectResponse($this->redirectStatus, $uri->withHost($host), $response); } return $next($request->withUri($uri->withHost($host)), $response); }
/** * Execute the middleware. * * @param RequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next) { $uri = $request->getUri(); $path = $uri->getPath(); if ($this->addSlash) { if (strlen($path) > 1 && substr($path, -1) !== '/' && !pathinfo($path, PATHINFO_EXTENSION)) { $path .= '/'; } } else { if (strlen($path) > 1 && substr($path, -1) === '/') { $path = substr($path, 0, -1); } } //Ensure the path has one "/" if (empty($path) || $path === $this->basePath) { $path .= '/'; } //redirect if (is_int($this->redirectStatus) && $uri->getPath() !== $path) { return self::getRedirectResponse($this->redirectStatus, $uri->withPath($path), $response); } return $next($request->withUri($uri->withPath($path)), $response); }
/** * @param UriInterface $uri * @param bool|false $preserveHost * @return RequestInterface */ public function withUri(UriInterface $uri, $preserveHost = false) { $this->request = $this->request->withUri($uri, $preserveHost); return $this; }
public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next = null) { $path = preg_replace('#^' . $this->location . '#', '', $request->getUri()->getPath()); $uri = $request->getUri()->withPath($path); return $next($request->withUri($uri), $response); }
/** * @param array $params * * @return void * @author Mario Mueller */ public function setQueryParams(array $params) { $uri = $this->guzzleRequest->getUri()->withQuery(\GuzzleHttp\Psr7\build_query($params)); $this->guzzleRequest = $this->guzzleRequest->withUri($uri); }
public function sendAsync(RequestInterface $request, array $options = []) { // Merge the base URI into the request URI if needed. $options = $this->prepareDefaults($options); return $this->transfer($request->withUri($this->buildUri($request->getUri(), $options), $request->hasHeader('Host')), $options); }
/** * Adds the `lang` query parameter to the request for localized endpoints. * * @param RequestInterface $request * * @return \Psr\Http\Message\RequestInterface */ public function onRequest(RequestInterface $request) { $new_uri = Uri::withQueryValue($request->getUri(), 'lang', $this->getEndpoint()->getLang()); return $request->withUri($new_uri); }
/** * Strip the route from the request path * * @param RequestInterface $request * @param string $route * @return RequestInterface */ private function stripRouteFromPath(RequestInterface $request, $route) { $this->removed = $route; $uri = $request->getUri(); $path = $this->getTruncatedPath($route, $uri->getPath()); $new = $uri->withPath($path); // Root path of route is treated differently if ($path === '/' && '/' === substr($uri->getPath(), -1)) { $this->removed .= '/'; } return $request->withUri($new); }
private function applyQuery(RequestInterface $request, $query) { $uri = $request->getUri(); if (!is_array($query)) { $query = GuzzlePsr7\parse_query($query); } $newQuery = array_merge(GuzzlePsr7\parse_query($uri->getQuery()), $query); return $request->withUri($uri->withQuery(http_build_query($newQuery, null, '&'))); }