]> git.mxchange.org Git - friendica.git/blobdiff - src/App/Router.php
Add OPTIONS endpoint
[friendica.git] / src / App / Router.php
index 90c5bcfad96f46606d9aadfc6f251db448e9dded..0640e589d4eb0b2cbc60d50272a676dc551d56ce 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2022, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -37,9 +37,9 @@ use Friendica\Core\Lock\Capability\ICanLock;
 use Friendica\LegacyModule;
 use Friendica\Module\HTTPException\MethodNotAllowed;
 use Friendica\Module\HTTPException\PageNotFound;
+use Friendica\Module\Special\Options;
 use Friendica\Network\HTTPException;
 use Friendica\Network\HTTPException\MethodNotAllowedException;
-use Friendica\Network\HTTPException\NoContentException;
 use Friendica\Network\HTTPException\NotFoundException;
 use Psr\Log\LoggerInterface;
 
@@ -141,13 +141,6 @@ class Router
 
                $httpMethod = $this->server['REQUEST_METHOD'] ?? self::GET;
 
-               // @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS
-               // @todo Check allowed methods per requested path
-               if ($httpMethod === static::OPTIONS) {
-                       header('Allow: ' . implode(',', Router::ALLOWED_METHODS));
-                       throw new NoContentException();
-               }
-
                $this->httpMethod = in_array($httpMethod, self::ALLOWED_METHODS) ? $httpMethod : self::GET;
 
                $this->routeCollector = isset($routeCollector) ?
@@ -283,7 +276,12 @@ class Router
                        $moduleClass = $routeInfo[1];
                        $this->parameters = $routeInfo[2];
                } elseif ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) {
-                       throw new HTTPException\MethodNotAllowedException($this->l10n->t('Method not allowed for this module. Allowed method(s): %s', implode(', ', $routeInfo[1])));
+                       if ($this->httpMethod === static::OPTIONS) {
+                               // Default response for HTTP OPTIONS requests in case there is no special treatment
+                               $moduleClass = Options::class;
+                       } else {
+                               throw new HTTPException\MethodNotAllowedException($this->l10n->t('Method not allowed for this module. Allowed method(s): %s', implode(', ', $routeInfo[1])));
+                       }
                } else {
                        throw new HTTPException\NotFoundException($this->l10n->t('Page not found.'));
                }
@@ -342,7 +340,7 @@ class Router
                        if ($this->dice_profiler_threshold > 0) {
                                $dur = floatval(microtime(true) - $stamp);
                                if ($dur >= $this->dice_profiler_threshold) {
-                                       $this->logger->warning('Dice module creation lasts too long.', ['duration' => $dur, 'module' => $module_class, 'parameters' => $module_parameters]);
+                                       $this->logger->warning('Dice module creation lasts too long.', ['duration' => round($dur, 3), 'module' => $module_class, 'parameters' => $module_parameters]);
                                }
                        }
                }