2017-03-18 22:30:36 -04:00
|
|
|
<?php
|
2017-11-16 20:23:18 -05:00
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
2017-03-18 22:30:36 -04:00
|
|
|
declare(strict_types=1);
|
2019-12-05 13:02:02 -05:00
|
|
|
namespace JKingWeb\Arsse\REST\NextcloudNews;
|
2017-08-29 10:50:31 -04:00
|
|
|
|
2018-01-04 23:08:53 -05:00
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2020-01-20 10:40:05 -05:00
|
|
|
use Laminas\Diactoros\Response\JsonResponse as Response;
|
|
|
|
use Laminas\Diactoros\Response\EmptyResponse;
|
2017-03-18 22:30:36 -04:00
|
|
|
|
2017-07-07 08:13:03 -04:00
|
|
|
class Versions implements \JKingWeb\Arsse\REST\Handler {
|
2017-08-29 10:50:31 -04:00
|
|
|
public function __construct() {
|
2017-04-06 21:41:21 -04:00
|
|
|
}
|
2017-03-18 22:30:36 -04:00
|
|
|
|
2018-01-04 23:08:53 -05:00
|
|
|
public function dispatch(ServerRequestInterface $req): ResponseInterface {
|
2021-06-24 11:58:50 -04:00
|
|
|
if (!preg_match("<^/?$>D", $req->getRequestTarget())) {
|
2018-01-04 23:08:53 -05:00
|
|
|
// if the request path is more than an empty string or a slash, the client is probably trying a version we don't support
|
2018-01-03 23:13:08 -05:00
|
|
|
return new EmptyResponse(404);
|
2018-01-04 23:08:53 -05:00
|
|
|
}
|
|
|
|
switch ($req->getMethod()) {
|
|
|
|
case "OPTIONS":
|
|
|
|
// if the request method is OPTIONS, respond accordingly
|
|
|
|
return new EmptyResponse(204, ['Allow' => "HEAD,GET"]);
|
|
|
|
case "GET":
|
|
|
|
// otherwise return the supported versions
|
|
|
|
$out = [
|
|
|
|
'apiLevels' => [
|
|
|
|
'v1-2',
|
2020-03-01 15:16:50 -05:00
|
|
|
],
|
2018-01-04 23:08:53 -05:00
|
|
|
];
|
|
|
|
return new Response($out);
|
|
|
|
default:
|
|
|
|
// if any other method was used, this is an error
|
|
|
|
return new EmptyResponse(405, ['Allow' => "HEAD,GET"]);
|
2017-04-06 21:41:21 -04:00
|
|
|
}
|
|
|
|
}
|
2017-08-29 10:50:31 -04:00
|
|
|
}
|