Skip to content

Commit

Permalink
add format-suffix handling
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes committed Aug 3, 2023
1 parent ea6267e commit 50acd25
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Controller/WebsiteRedirectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ public function redirect(Request $request, RedirectRouteInterface $redirectRoute

$queryString = http_build_query($request->query->all());

$requestFormat = $request->getRequestFormat(null);
$formatSuffix = $requestFormat ? ('.' . $requestFormat) : '';

$url = [
$redirectRoute->getTarget(),
$formatSuffix,
false === strpos($redirectRoute->getTarget(), '?') ? '?' : '&',
$queryString,
];
Expand Down
3 changes: 2 additions & 1 deletion Routing/RedirectRouteProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ public function getRouteCollectionForRequest(Request $request): RouteCollection
// server encodes the url and symfony does not encode it
// symfony decodes this data here https://github.com/symfony/symfony/blob/v5.2.3/src/Symfony/Component/Routing/Matcher/UrlMatcher.php#L88
$pathInfo = rawurldecode($request->getPathInfo());
$path = \str_replace('.' . $request->getRequestFormat(), '', $pathInfo);
$host = $request->getHost();

$routeCollection = new RouteCollection();
if (!$redirectRoute = $this->redirectRouteRepository->findEnabledBySource($pathInfo, $host)) {
if (!$redirectRoute = $this->redirectRouteRepository->findEnabledBySource($path, $host)) {
return $routeCollection;
}

Expand Down
15 changes: 14 additions & 1 deletion Tests/Functional/Routing/RedirectRouteProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function testRoute(
string $source,
int $statusCode,
string $target = '',
string $targetUrl = '',
?string $sourceHost = null
) {
// setup models
Expand All @@ -61,7 +62,7 @@ public function testRoute(

if ($target) {
$this->assertInstanceOf(RedirectResponse::class, $response);
$this->assertSame($target, $response->getTargetUrl());
$this->assertSame($targetUrl, $response->getTargetUrl());
}
}

Expand All @@ -72,13 +73,15 @@ public function routeDataProvider(): \Generator
'/test-301',
301,
'/test2',
'/test2',
];

yield [
'/test-302',
'/test-302',
302,
'/test2',
'/test2',
];

yield [
Expand All @@ -92,6 +95,7 @@ public function routeDataProvider(): \Generator
'/test-domain-redirect',
301,
'/',
'/',
'with-domain.com',
];

Expand All @@ -100,6 +104,15 @@ public function routeDataProvider(): \Generator
'/test-emoticon-🎉',
301,
'/',
'/',
];

yield [
'/source.json', // browsers will encode the url and be provided this way to symfony getPathInfo
'/source',
301,
'/target',
'/target.json',
];
}
}

0 comments on commit 50acd25

Please sign in to comment.