/** * Sends a redirect to the client. * * In case a relative URI is given, it will be resolved using the current base URI. * * @param \Ableron\Lib\Net\Uri $redirectUri The URI to redirect to * @param int|null $statusCode Status code of the redirect, e.g. relevant for HTTP redirects * @return void */ public function sendRedirect(Uri $redirectUri, $statusCode = HttpResponse::STATUS_SEE_OTHER) { // because it is not possible to guess an absolute URL from a relative one because of the flexible module paths, relative URLs are not allowed if (!$redirectUri->isAbsolute()) { // log error Application::getLogManager()->error(sprintf('Unable to perform redirect: Redirect URL must be absolute. Relative URL given: %s', $redirectUri->toString())); // redirect to base URL $redirectUri = EnvironmentUtil::getBaseUrl(); } // set response $this->setResponse(new HttpResponse($statusCode, null, '', null, array(new LocationHeaderField($redirectUri)))); }
/** * Resolves this relative URI using the given absolute base URI. * * Does not change this Uri object in case this URI is already absolute. * * @param \Ableron\Lib\Net\Uri $baseUri The absolute base URI used to resolve this relative URI * @throws \Ableron\Core\Exception\SystemException * @return \Ableron\Lib\Net\Uri */ public function resolve(Uri $baseUri) { // check whether base URI is absolute if (!$baseUri->isAbsolute()) { throw new SystemException(sprintf('Unable to resolve URI using base URI "%s" - Base URI must be absolute!', $baseUri->toString()), 0, E_USER_NOTICE, __FILE__, __LINE__); } // check whether URI is already absolute if ($this->isAbsolute()) { $this->setPath(self::removeDotSegments($this->path)); return $this; } // resolve scheme specific part if ($this->host !== null) { $this->setPath(self::removeDotSegments($this->path)); } else { // resolve authority part $this->setUserInfo($baseUri->getRawUserInfo()); $this->setHost($baseUri->getRawHost()); $this->setPort($baseUri->getPort()); // resolve path and query if ($this->path === '') { $this->setPath($baseUri->getRawPath()); if ($this->query === null) { $this->setQuery($baseUri->getRawQuery()); } } else { if (StringUtil::startsWith($this->path, '/')) { $this->setPath(self::removeDotSegments($this->path)); } else { $this->setPath(self::removeDotSegments($baseUri->getRawHost() !== null && $baseUri->getRawPath() === '' ? '/' : StringUtil::getSubstring($baseUri->getRawPath(), 0, StringUtil::getLastIndexOf($baseUri->getRawPath(), '/') + 1) . $this->path)); } } } // resolve scheme $this->setScheme($baseUri->getScheme()); // return reference of this object return $this; }
/** * Tests whether parser and getter are working correctly. * * @dataProvider dataProviderTestParserAndGetter * @return void */ public function testParserAndGetter($uriString, $expectedResult) { $uri = new Uri($uriString); $this->assertSame($expectedResult['scheme'], $uri->getScheme(), 'getScheme()'); $this->assertSame($expectedResult['authority'], $uri->getAuthority(), 'getAuthority()'); $this->assertSame($expectedResult['rawAuthority'], $uri->getRawAuthority(), 'getRawAuthority()'); $this->assertSame($expectedResult['userInfo'], $uri->getUserInfo(), 'getUserInfo()'); $this->assertSame($expectedResult['rawUserInfo'], $uri->getRawUserInfo(), 'getRawUserInfo()'); $this->assertSame($expectedResult['host'], $uri->getHost(), 'getHost()'); $this->assertSame($expectedResult['rawHost'], $uri->getRawHost(), 'getRawHost()'); $this->assertSame($expectedResult['port'], $uri->getPort(), 'getPort()'); $this->assertSame($expectedResult['path'], $uri->getPath(), 'getPath()'); $this->assertSame($expectedResult['rawPath'], $uri->getRawPath(), 'getRawPath()'); $this->assertSame($expectedResult['query'], $uri->getQuery(), 'getQuery()'); $this->assertSame($expectedResult['rawQuery'], $uri->getRawQuery(), 'getRawQuery()'); $this->assertSame($expectedResult['queryParameters'], $uri->getQueryParameters(), 'getQueryParameters()'); $this->assertSame($expectedResult['fragment'], $uri->getFragment(), 'getFragment()'); $this->assertSame($expectedResult['rawFragment'], $uri->getRawFragment(), 'getRawFragment()'); $this->assertSame($expectedResult['isAbsolute'], $uri->isAbsolute(), 'isAbsolute()'); $this->assertSame($expectedResult['externalForm'], $uri->toString(), 'toString()'); $this->assertSame($expectedResult['normalized'], $uri->normalize()->toString(), 'normalize()'); }