diff --git a/module/Rest/config/dependencies.config.php b/module/Rest/config/dependencies.config.php index 6938b6ba..0058b100 100644 --- a/module/Rest/config/dependencies.config.php +++ b/module/Rest/config/dependencies.config.php @@ -37,6 +37,7 @@ return [ Middleware\BodyParserMiddleware::class => InvokableFactory::class, Middleware\CrossDomainMiddleware::class => InvokableFactory::class, Middleware\ShortUrl\CreateShortUrlContentNegotiationMiddleware::class => InvokableFactory::class, + Middleware\ShortUrl\DropDefaultDomainFromQueryMiddleware::class => ConfigAbstractFactory::class, ], ], @@ -72,6 +73,8 @@ return [ Action\Tag\DeleteTagsAction::class => [Service\Tag\TagService::class, LoggerInterface::class], Action\Tag\CreateTagsAction::class => [Service\Tag\TagService::class, LoggerInterface::class], Action\Tag\UpdateTagAction::class => [Service\Tag\TagService::class, LoggerInterface::class], + + Middleware\ShortUrl\DropDefaultDomainFromQueryMiddleware::class => ['config.url_shortener.domain.hostname'], ], ]; diff --git a/module/Rest/config/routes.config.php b/module/Rest/config/routes.config.php index d210f13b..301691aa 100644 --- a/module/Rest/config/routes.config.php +++ b/module/Rest/config/routes.config.php @@ -4,26 +4,25 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Rest; +$contentNegotiationMiddleware = [Middleware\ShortUrl\CreateShortUrlContentNegotiationMiddleware::class]; +$dropDomainMiddleware = [Middleware\ShortUrl\DropDefaultDomainFromQueryMiddleware::class]; + return [ 'routes' => [ Action\HealthAction::getRouteDef(), // Short codes - Action\ShortUrl\CreateShortUrlAction::getRouteDef([ - Middleware\ShortUrl\CreateShortUrlContentNegotiationMiddleware::class, - ]), - Action\ShortUrl\SingleStepCreateShortUrlAction::getRouteDef([ - Middleware\ShortUrl\CreateShortUrlContentNegotiationMiddleware::class, - ]), - Action\ShortUrl\EditShortUrlAction::getRouteDef(), - Action\ShortUrl\DeleteShortUrlAction::getRouteDef(), - Action\ShortUrl\ResolveShortUrlAction::getRouteDef(), + Action\ShortUrl\CreateShortUrlAction::getRouteDef($contentNegotiationMiddleware), + Action\ShortUrl\SingleStepCreateShortUrlAction::getRouteDef($contentNegotiationMiddleware), + Action\ShortUrl\EditShortUrlAction::getRouteDef($dropDomainMiddleware), + Action\ShortUrl\DeleteShortUrlAction::getRouteDef($dropDomainMiddleware), + Action\ShortUrl\ResolveShortUrlAction::getRouteDef($dropDomainMiddleware), Action\ShortUrl\ListShortUrlsAction::getRouteDef(), - Action\ShortUrl\EditShortUrlTagsAction::getRouteDef(), + Action\ShortUrl\EditShortUrlTagsAction::getRouteDef($dropDomainMiddleware), // Visits - Action\Visit\GetVisitsAction::getRouteDef(), + Action\Visit\GetVisitsAction::getRouteDef($dropDomainMiddleware), // Tags Action\Tag\ListTagsAction::getRouteDef(), diff --git a/module/Rest/src/Middleware/ShortUrl/DropDefaultDomainFromQueryMiddleware.php b/module/Rest/src/Middleware/ShortUrl/DropDefaultDomainFromQueryMiddleware.php new file mode 100644 index 00000000..b894e40c --- /dev/null +++ b/module/Rest/src/Middleware/ShortUrl/DropDefaultDomainFromQueryMiddleware.php @@ -0,0 +1,31 @@ +defaultDomain = $defaultDomain; + } + + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + $query = $request->getQueryParams(); + if (isset($query['domain']) && $query['domain'] === $this->defaultDomain) { + unset($query['domain']); + $request = $request->withQueryParams($query); + } + + return $handler->handle($request); + } +}