/** * 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->getUserInfo() == $this->expected; }
/** * Obfuscates URI if there is an username and a password present * * @param UriInterface $uri * * @return UriInterface */ private static function obfuscateUri($uri) { $userInfo = $uri->getUserInfo(); if (false !== ($pos = strpos($userInfo, ':'))) { return $uri->withUserInfo(substr($userInfo, 0, $pos), '***'); } return $uri; }
private function buildRequest(Command $command, UriInterface $uri) : RequestInterface { $parameters = sprintf('-db=%s&%s', urlencode($this->database), $command); $body = new Stream('php://temp', 'wb+'); $body->write($parameters); $body->rewind(); $request = (new Request($uri->withUserInfo(''), 'POST'))->withAddedHeader('User-agent', 'SimpleFM')->withAddedHeader('Content-type', 'application/x-www-form-urlencoded')->withAddedHeader('Content-length', (string) strlen($parameters))->withBody($body); $credentials = urldecode($uri->getUserInfo()); if ($command->hasIdentity()) { Assertion::notNull($this->identityHandler, 'An identity handler must be set to use identities on commands'); $identity = $command->getIdentity(); $credentials = sprintf('%s:%s', $identity->getUsername(), $this->identityHandler->decryptPassword($identity)); } $this->logger->info(sprintf('%s?%s', (string) $uri->withUserInfo(''), $parameters)); if ('' === $credentials) { return $request; } return $request->withAddedHeader('Authorization', sprintf('Basic %s', base64_encode($credentials))); }