WellKnownController.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Controller;
  8. use OC\Http\WellKnown\RequestManager;
  9. use OCP\AppFramework\Controller;
  10. use OCP\AppFramework\Http;
  11. use OCP\AppFramework\Http\Attribute\FrontpageRoute;
  12. use OCP\AppFramework\Http\Attribute\OpenAPI;
  13. use OCP\AppFramework\Http\JSONResponse;
  14. use OCP\AppFramework\Http\Response;
  15. use OCP\IRequest;
  16. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  17. class WellKnownController extends Controller {
  18. public function __construct(
  19. IRequest $request,
  20. private RequestManager $requestManager,
  21. ) {
  22. parent::__construct('core', $request);
  23. }
  24. /**
  25. * @PublicPage
  26. * @NoCSRFRequired
  27. *
  28. * @return Response
  29. */
  30. #[FrontpageRoute(verb: 'GET', url: '.well-known/{service}')]
  31. public function handle(string $service): Response {
  32. $response = $this->requestManager->process(
  33. $service,
  34. $this->request
  35. );
  36. if ($response === null) {
  37. $httpResponse = new JSONResponse(["message" => "$service not supported"], Http::STATUS_NOT_FOUND);
  38. } else {
  39. $httpResponse = $response->toHttpResponse();
  40. }
  41. // We add a custom header so that setup checks can detect if their requests are answered by this controller
  42. return $httpResponse->addHeader('X-NEXTCLOUD-WELL-KNOWN', '1');
  43. }
  44. }