From d1a554fba036136f26bb571076f8e12b07fad478 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 24 Jun 2025 23:35:28 +0200 Subject: [PATCH] feat(occ): Add commands to list all routes and match a single one Signed-off-by: Joas Schilling --- core/Command/Router/ListRoutes.php | 129 ++++++++++++++++++++ core/Command/Router/MatchRoute.php | 100 +++++++++++++++ core/register_command.php | 4 + lib/composer/composer/autoload_classmap.php | 2 + lib/composer/composer/autoload_static.php | 2 + lib/private/Route/Router.php | 12 +- 6 files changed, 247 insertions(+), 2 deletions(-) create mode 100644 core/Command/Router/ListRoutes.php create mode 100644 core/Command/Router/MatchRoute.php diff --git a/core/Command/Router/ListRoutes.php b/core/Command/Router/ListRoutes.php new file mode 100644 index 00000000000..b65b01cc7ae --- /dev/null +++ b/core/Command/Router/ListRoutes.php @@ -0,0 +1,129 @@ +setName('router:list') + ->setDescription('Find the target of a route or all routes of an app') + ->addArgument( + 'app', + InputArgument::OPTIONAL | InputArgument::IS_ARRAY, + 'Only list routes of these apps', + ) + ->addOption( + 'ocs', + null, + InputOption::VALUE_NONE, + 'Only list OCS routes', + ) + ->addOption( + 'index', + null, + InputOption::VALUE_NONE, + 'Only list index.php routes', + ) + ; + } + + protected function execute(InputInterface $input, OutputInterface $output): int { + $apps = $input->getArgument('app'); + if (empty($apps)) { + $this->router->loadRoutes(); + } else { + foreach ($apps as $app) { + if ($app === 'core') { + $this->router->loadRoutes($app, false); + continue; + } + + try { + $this->appManager->getAppPath($app); + } catch (AppPathNotFoundException) { + $output->writeln('App ' . $app . ' not found'); + return self::FAILURE; + } + + if (!$this->appManager->isInstalled($app)) { + $output->writeln('App ' . $app . ' is not enabled'); + return self::FAILURE; + } + + $this->router->loadRoutes($app, true); + } + } + + $ocsOnly = $input->getOption('ocs'); + $indexOnly = $input->getOption('index'); + + $rows = []; + $collection = $this->router->getRouteCollection(); + foreach ($collection->all() as $routeName => $route) { + if (str_starts_with($routeName, 'ocs.')) { + if ($indexOnly) { + continue; + } + $routeName = substr($routeName, 4); + } elseif ($ocsOnly) { + continue; + } + + $path = $route->getPath(); + if (str_starts_with($path, '/ocsapp/')) { + $path = '/ocs/v2.php/' . substr($path, strlen('/ocsapp/')); + } + $row = [ + 'route' => $routeName, + 'request' => implode(', ', $route->getMethods()), + 'path' => $path, + ]; + + if ($output->isVerbose()) { + $row['requirements'] = json_encode($route->getRequirements()); + } + + $rows[] = $row; + } + + usort($rows, static function (array $a, array $b): int { + $aRoute = $a['route']; + if (str_starts_with($aRoute, 'ocs.')) { + $aRoute = substr($aRoute, 4); + } + $bRoute = $b['route']; + if (str_starts_with($bRoute, 'ocs.')) { + $bRoute = substr($bRoute, 4); + } + return $aRoute <=> $bRoute; + }); + + $this->writeTableInOutputFormat($input, $output, $rows); + return self::SUCCESS; + } +} diff --git a/core/Command/Router/MatchRoute.php b/core/Command/Router/MatchRoute.php new file mode 100644 index 00000000000..dd3280ed9d1 --- /dev/null +++ b/core/Command/Router/MatchRoute.php @@ -0,0 +1,100 @@ +setName('router:match') + ->setDescription('Match a URL to the target route') + ->addArgument( + 'path', + InputArgument::REQUIRED, + 'Path of the request', + ) + ->addOption( + 'method', + null, + InputOption::VALUE_REQUIRED, + 'HTTP method', + 'GET', + ) + ; + } + + protected function execute(InputInterface $input, OutputInterface $output): int { + $context = new RequestContext(method: strtoupper($input->getOption('method'))); + $this->router->setContext($context); + + $path = $input->getArgument('path'); + if (str_starts_with($path, '/index.php/')) { + $path = substr($path, 10); + } + if (str_starts_with($path, '/ocs/v1.php/') || str_starts_with($path, '/ocs/v2.php/')) { + $path = '/ocsapp' . substr($path, strlen('/ocs/v2.php')); + } + + try { + $route = $this->router->findMatchingRoute($path); + } catch (MethodNotAllowedException) { + $output->writeln('Method not allowed on this path'); + return self::FAILURE; + } catch (ResourceNotFoundException) { + $output->writeln('Path not matched'); + if (preg_match('/\/apps\/([^\/]+)\//', $path, $matches)) { + $appManager = Server::get(IAppManager::class); + if (!$appManager->isInstalled($matches[1])) { + $output->writeln(''); + $output->writeln('App ' . $matches[1] . ' is not enabled'); + } + } + return self::FAILURE; + } + + $row = [ + 'route' => $route['_route'], + 'appid' => $route['caller'][0] ?? null, + 'controller' => $route['caller'][1] ?? null, + 'method' => $route['caller'][2] ?? null, + ]; + + if ($output->isVerbose()) { + $route = $this->router->getRouteCollection()->get($row['route']); + $row['path'] = $route->getPath(); + if (str_starts_with($row['path'], '/ocsapp/')) { + $row['path'] = '/ocs/v2.php/' . substr($row['path'], strlen('/ocsapp/')); + } + $row['requirements'] = json_encode($route->getRequirements()); + } + + $this->writeTableInOutputFormat($input, $output, [$row]); + return self::SUCCESS; + } +} diff --git a/core/register_command.php b/core/register_command.php index a1c0667a2e3..9412562747b 100644 --- a/core/register_command.php +++ b/core/register_command.php @@ -8,6 +8,8 @@ declare(strict_types=1); * SPDX-License-Identifier: AGPL-3.0-only */ use OC\Core\Command; +use OC\Core\Command\Router\ListRoutes; +use OC\Core\Command\Router\MatchRoute; use OCP\IConfig; use OCP\Server; use Stecman\Component\Symfony\Console\BashCompletion\CompletionCommand; @@ -20,6 +22,8 @@ $application->add(Server::get(Command\Integrity\SignApp::class)); $application->add(Server::get(Command\Integrity\SignCore::class)); $application->add(Server::get(Command\Integrity\CheckApp::class)); $application->add(Server::get(Command\Integrity\CheckCore::class)); +$application->add(Server::get(ListRoutes::class)); +$application->add(Server::get(MatchRoute::class)); $config = Server::get(IConfig::class); diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 247de740235..fc0694124f8 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -1288,6 +1288,8 @@ return array( 'OC\\Core\\Command\\Preview\\Generate' => $baseDir . '/core/Command/Preview/Generate.php', 'OC\\Core\\Command\\Preview\\Repair' => $baseDir . '/core/Command/Preview/Repair.php', 'OC\\Core\\Command\\Preview\\ResetRenderedTexts' => $baseDir . '/core/Command/Preview/ResetRenderedTexts.php', + 'OC\\Core\\Command\\Router\\ListRoutes' => $baseDir . '/core/Command/Router/ListRoutes.php', + 'OC\\Core\\Command\\Router\\MatchRoute' => $baseDir . '/core/Command/Router/MatchRoute.php', 'OC\\Core\\Command\\Security\\BruteforceAttempts' => $baseDir . '/core/Command/Security/BruteforceAttempts.php', 'OC\\Core\\Command\\Security\\BruteforceResetAttempts' => $baseDir . '/core/Command/Security/BruteforceResetAttempts.php', 'OC\\Core\\Command\\Security\\ExportCertificates' => $baseDir . '/core/Command/Security/ExportCertificates.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 80af2509303..98d74bd5dce 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -1337,6 +1337,8 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OC\\Core\\Command\\Preview\\Generate' => __DIR__ . '/../../..' . '/core/Command/Preview/Generate.php', 'OC\\Core\\Command\\Preview\\Repair' => __DIR__ . '/../../..' . '/core/Command/Preview/Repair.php', 'OC\\Core\\Command\\Preview\\ResetRenderedTexts' => __DIR__ . '/../../..' . '/core/Command/Preview/ResetRenderedTexts.php', + 'OC\\Core\\Command\\Router\\ListRoutes' => __DIR__ . '/../../..' . '/core/Command/Router/ListRoutes.php', + 'OC\\Core\\Command\\Router\\MatchRoute' => __DIR__ . '/../../..' . '/core/Command/Router/MatchRoute.php', 'OC\\Core\\Command\\Security\\BruteforceAttempts' => __DIR__ . '/../../..' . '/core/Command/Security/BruteforceAttempts.php', 'OC\\Core\\Command\\Security\\BruteforceResetAttempts' => __DIR__ . '/../../..' . '/core/Command/Security/BruteforceResetAttempts.php', 'OC\\Core\\Command\\Security\\ExportCertificates' => __DIR__ . '/../../..' . '/core/Command/Security/ExportCertificates.php', diff --git a/lib/private/Route/Router.php b/lib/private/Route/Router.php index 3277b983d0f..91cf2b9fade 100644 --- a/lib/private/Route/Router.php +++ b/lib/private/Route/Router.php @@ -74,6 +74,14 @@ class Router implements IRouter { $this->root = $this->getCollection('root'); } + public function setContext(RequestContext $context): void { + $this->context = $context; + } + + public function getRouteCollection() { + return $this->root; + } + /** * Get the files to load the routes from * @@ -102,7 +110,7 @@ class Router implements IRouter { * * @param null|string $app */ - public function loadRoutes($app = null) { + public function loadRoutes(?string $app = null, bool $skipLoadingCore = false): void { if (is_string($app)) { $app = $this->appManager->cleanAppId($app); } @@ -161,7 +169,7 @@ class Router implements IRouter { } } - if (!isset($this->loadedApps['core'])) { + if (!$skipLoadingCore && !isset($this->loadedApps['core'])) { $this->loadedApps['core'] = true; $this->useCollection('root'); $this->setupRoutes($this->getAttributeRoutes('core'), 'core');