Skip to content

Commit

Permalink
[BUGFIX] Fix edge cases with resolving frontend domains (#576)
Browse files Browse the repository at this point in the history
Hardening replacing frontend domains for edge case configurations.
  • Loading branch information
twoldanski authored Apr 11, 2023
1 parent 6cec289 commit e3da161
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
17 changes: 9 additions & 8 deletions Classes/Utility/UrlUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use function array_merge;
use function count;
use function rtrim;
use function str_replace;
use function strpos;

class UrlUtility implements LoggerAwareInterface, HeadlessFrontendUrlInterface
Expand Down Expand Up @@ -96,19 +95,21 @@ public function getFrontendUrlForPage(string $url, int $pageUid, string $returnF
$frontendBase = GeneralUtility::makeInstance(Uri::class, $this->sanitizeBaseUrl($frontendBaseUrl));
$frontBase = $frontendBase->getHost();
$frontPort = $frontendBase->getPort();
$targetUri = new Uri($this->sanitizeBaseUrl($url));

if (strpos($url, $base) !== false) {
$url = str_replace($base, $frontBase, $url);
$targetUri = $targetUri->withHost($frontBase);
}

if ($port === $frontPort) {
return $url;
return (string)$targetUri;
}
return str_replace(
$frontBase . ($port ? ':' . $port : ''),
$frontBase . ($frontPort ? ':' . $frontPort : ''),
$url
);

if ($frontPort) {
$targetUri = $targetUri->withPort($frontPort);
}

return (string)$targetUri;
} catch (SiteNotFoundException $e) {
$this->logError($e->getMessage());
}
Expand Down
36 changes: 36 additions & 0 deletions Tests/Unit/Utility/UrlUtilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,42 @@ public function testFrontendUrlForPage(): void
);
}

public function testFrontendUrlForPageWithAlreadyFrontendUrlResolved(): void
{
$site = $this->prophesize(Site::class);
$site->getConfiguration()->shouldBeCalled(2)->willReturn([
'base' => 'https://www.typo3.org',
'languages' => [],
'baseVariants' => [
[
'base' => 'https://api.tld',
'condition' => 'applicationContext == "Development"',
'frontendBase' => 'https://front.api.tld',
]
]
]);

$uri = new Uri('https://api.tld');

$site->getBase()->shouldBeCalled(2)->willReturn($uri);

$resolver = $this->prophesize(Resolver::class);
$resolver->evaluate(Argument::any())->willReturn(true);

$siteFinder = $this->prophesize(SiteFinder::class);
$siteFinder->getSiteByPageId(Argument::is(1))->shouldBeCalledOnce()->willReturn($site->reveal());

$urlUtility = new UrlUtility(null, $resolver->reveal(), $siteFinder->reveal());
$urlUtility = $urlUtility->withSite($site->reveal());

// flag is enabled
$GLOBALS['TYPO3_CONF_VARS']['SYS']['features']['headless.frontendUrls'] = true;
self::assertSame(
'https://front.api.tld/test-page',
$urlUtility->getFrontendUrlForPage('https://front.api.tld/test-page', 1)
);
}

public function testFrontendUrlForPageWithPortsOnFrontendSide(): void
{
$site = $this->prophesize(Site::class);
Expand Down

0 comments on commit e3da161

Please sign in to comment.