Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ public function run($op, $uuid) {
// call the operation
return $this->{$op}($uuid);
}

/**
* Return header for an OPTIONS request
*/
public function options(): array {
return [
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => 'DELETE, GET, HEAD, OPTIONS, PATCH, POST, PULL',
];
}
}

?>
5 changes: 5 additions & 0 deletions lib/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class Router implements HttpKernelInterface {
'POST' => 'add',
'DELETE' => 'delete',
'GET' => 'get',
'HEAD' => 'get',
'OPTIONS' => 'options',
'PATCH' => 'edit',
'PULL' => 'edit' // not REST-conform
);
Expand Down Expand Up @@ -200,6 +202,9 @@ function handler(Request $request, $context, $uuid) {
if (null === ($operation = $request->query->get('operation'))) {
$operation = self::$operationMapping[$request->getMethod()];
}
if ($operation === 'options') {
$this->view = new View\Options($request);
}

$class = self::$controllerMapping[$context];
$controller = new $class($request, $this->em, $this->view);
Expand Down
60 changes: 60 additions & 0 deletions lib/View/Options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* @copyright Copyright (c) 2025, The volkszaehler.org project
* @license https://www.gnu.org/licenses/gpl-3.0.txt GNU General Public License version 3
*/
/*
* This file is part of volkzaehler.org
*
* volkzaehler.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* volkzaehler.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
*/

namespace Volkszaehler\View;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* HTTP OPTIONS request response view
*/
class Options extends View {

public function __construct(Request $request) {
parent::__construct($request);
$this->response = new Response(
null,
Response::HTTP_NO_CONTENT
);
}

protected function render() {
throw new \LogicException('Not needed here');
}

/**
* Render response and send it to the client
*/
public function send(): Response {
return $this->response;
}

/**
* Add headers to response
*/
public function add($headers) {
foreach ($headers as $key => $value) {
$this->response->headers->set($key, $value);
}
}
}