Skip to content

Commit

Permalink
Merge branch 'next-4863/punycode-domains' into 'master'
Browse files Browse the repository at this point in the history
NEXT-4863 punycode domains

See merge request shopware/6/product/platform!315
  • Loading branch information
janbuecker committed Sep 9, 2019
2 parents 46c1369 + a81a8b3 commit 907e544
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -91,6 +91,7 @@
"symfony/twig-bundle": "4.3.4",
"symfony/validator": "4.3.4",
"symfony/yaml": "4.3.4",
"true/punycode": "2.1.1",
"twig/extensions": "1.5.4",
"twig/twig": "2.11.3",
"voku/stop-words": "2.0.1"
Expand Down
1 change: 1 addition & 0 deletions src/Core/composer.json
Expand Up @@ -87,6 +87,7 @@
"symfony/twig-bundle": "4.3.4",
"symfony/validator": "4.3.4",
"symfony/yaml": "4.3.4",
"true/punycode": "2.1.1",
"twig/extensions": "1.5.4",
"twig/twig": "2.11.3",
"voku/stop-words": "2.0.1"
Expand Down
18 changes: 15 additions & 3 deletions src/Storefront/Framework/Routing/RequestTransformer.php
Expand Up @@ -12,6 +12,7 @@
use Shopware\Storefront\Framework\Routing\Exception\SalesChannelMappingException;
use Shopware\Storefront\Framework\Seo\SeoResolver;
use Symfony\Component\HttpFoundation\Request;
use TrueBV\Punycode;

class RequestTransformer implements RequestTransformerInterface
{
Expand Down Expand Up @@ -41,10 +42,16 @@ class RequestTransformer implements RequestTransformerInterface
'/admin/',
];

/**
* @var Punycode
*/
private $punycode;

public function __construct(RequestTransformerInterface $decorated, Connection $connection)
{
$this->connection = $connection;
$this->decorated = $decorated;
$this->punycode = new Punycode();
}

public function transform(Request $request): Request
Expand All @@ -62,7 +69,7 @@ public function transform(Request $request): Request
throw new SalesChannelMappingException($request->getUri());
}

$absoluteBaseUrl = $request->getSchemeAndHttpHost() . $request->getBaseUrl();
$absoluteBaseUrl = $this->getSchemeAndHttpHost($request) . $request->getBaseUrl();
$baseUrl = str_replace($absoluteBaseUrl, '', $salesChannel['url']);

$resolved = $this->resolveSeoUrl($request, $baseUrl, $salesChannel['languageId'], $salesChannel['salesChannelId']);
Expand Down Expand Up @@ -121,7 +128,7 @@ public function transform(Request $request): Request
$clone->attributes->set(SalesChannelRequest::ATTRIBUTE_THEME_BASE_NAME, $salesChannel['parentThemeName']);

if (isset($resolved['canonicalPathInfo'])) {
$clone->attributes->set(SalesChannelRequest::ATTRIBUTE_CANONICAL_LINK, $request->getSchemeAndHttpHost() . $baseUrl . $resolved['canonicalPathInfo']);
$clone->attributes->set(SalesChannelRequest::ATTRIBUTE_CANONICAL_LINK, $this->getSchemeAndHttpHost($request) . $baseUrl . $resolved['canonicalPathInfo']);
}

$clone->headers->add($request->headers->all());
Expand Down Expand Up @@ -177,7 +184,7 @@ private function findSalesChannel(Request $request): ?array
}

// domain urls and request uri should be in same format, all with trailing slash
$requestUrl = rtrim($request->getSchemeAndHttpHost() . $request->getBasePath() . $request->getPathInfo(), '/') . '/';
$requestUrl = rtrim($this->getSchemeAndHttpHost($request) . $request->getBasePath() . $request->getPathInfo(), '/') . '/';

// direct hit
if (array_key_exists($requestUrl, $domains)) {
Expand Down Expand Up @@ -239,4 +246,9 @@ private function resolveSeoUrl(Request $request, string $baseUrl, string $langua

return ['pathInfo' => '/' . trim($seoPathInfo, '/')];
}

private function getSchemeAndHttpHost(Request $request): string
{
return $request->getScheme() . '://' . $this->punycode->decode($request->getHttpHost());
}
}
10 changes: 10 additions & 0 deletions src/Storefront/Test/Framework/Routing/RequestTransformerTest.php
Expand Up @@ -202,6 +202,16 @@ public function domainProvider(): array
new ExpectedRequest('http://inactive.test/foobar', null, null, null, null, null, null, null, null, SalesChannelMappingException::class),
],
],
'punycode' => [
[
$this->getGermanSalesChannel($germanId, $gerDomainId, 'http://würmer.test'),
],
[
new ExpectedRequest('http://xn--wrmer-kva.test', '', $gerDomainId, $germanId, true, self::LOCALE_DE_DE_ISO, Defaults::CURRENCY, $this->deLanguageId, $snippetSetDE),
new ExpectedRequest('http://xn--wrmer-kva.test/', '', $gerDomainId, $germanId, true, self::LOCALE_DE_DE_ISO, Defaults::CURRENCY, $this->deLanguageId, $snippetSetDE),
new ExpectedRequest('http://xn--wrmer-kva.test/foobar', '', $gerDomainId, $germanId, true, self::LOCALE_DE_DE_ISO, Defaults::CURRENCY, $this->deLanguageId, $snippetSetDE),
],
],
];
}

Expand Down

0 comments on commit 907e544

Please sign in to comment.