Try to unify API responses

This commit is contained in:
2024-07-21 16:10:02 +02:00
parent a01515aa9c
commit b76c434216
5 changed files with 59 additions and 38 deletions

View File

@ -5,8 +5,8 @@ function http_401_unauthorized(): void {
header('Status: 401'); header('Status: 401');
header('WWW-Authenticate: Basic realm="Elwig"'); header('WWW-Authenticate: Basic realm="Elwig"');
header('Content-Type: text/plain; charset=UTF-8'); header('Content-Type: text/plain; charset=UTF-8');
header('Content-Length: 20'); header('Content-Length: 17');
exit("401 Unauthorized :(\n"); exit("401 Unauthorized\n");
} }
function authenticate(): void { function authenticate(): void {

View File

@ -7,10 +7,12 @@ function get_fmt(): string {
if ($fmt === 'ascii') { if ($fmt === 'ascii') {
header('Status: 303'); header('Status: 303');
header('Location: ?format=text'); header('Location: ?format=text');
exit(); header('Content-Length: 14');
exit("303 See Other\n");
} else if ($fmt !== 'json' && $fmt !== 'html' && $fmt !== 'text') { } else if ($fmt !== 'json' && $fmt !== 'html' && $fmt !== 'text') {
header('Status: 300'); header('Status: 300');
header('Content-Type: text/html; charset=UTF-8'); header('Content-Type: text/html; charset=UTF-8');
header('Content-Length: 162');
echo "<!DOCTYPE html><html><head></head><body>\n<a href='?format=html'>HTML</a><br/>\n<a href='?format=json'>JSON</a><br/>\n<a href='?format=text'>Text</a>\n</body></html>\n"; echo "<!DOCTYPE html><html><head></head><body>\n<a href='?format=html'>HTML</a><br/>\n<a href='?format=json'>JSON</a><br/>\n<a href='?format=text'>Text</a>\n</body></html>\n";
exit(); exit();
} }

View File

@ -72,10 +72,12 @@ if ($path == '') {
header('Allow: GET'); header('Allow: GET');
if ($format === 'text') { if ($format === 'text') {
header('Content-Type: text/plain; charset=UTF-8'); header('Content-Type: text/plain; charset=UTF-8');
echo "405 Method Not Allowed :(\n"; header('Content-Length: 23');
echo "405 Method Not Allowed\n";
} else if ($format === 'json') { } else if ($format === 'json') {
header('Content-Type: application/json; charset=UTF-8'); header('Content-Type: application/json; charset=UTF-8');
echo "{\"status\": \"error\", \"errors\": [{\"message\": \"Method not allowed\"}]}\n"; header('Content-Length: 48');
echo "{\"errors\": [{\"message\": \"Method not allowed\"}]}\n";
} else { } else {
header('Content-Type: text/html; charset=UTF-8'); header('Content-Type: text/html; charset=UTF-8');
header('Content-Length: 0'); header('Content-Length: 0');
@ -89,7 +91,7 @@ if ($path == '') {
echo "$c\n"; echo "$c\n";
} else if ($format === 'json') { } else if ($format === 'json') {
header('Content-Type: application/json; charset=UTF-8'); header('Content-Type: application/json; charset=UTF-8');
echo "{\"status\": \"success\", \"data\": ["; echo "{\"data\": [";
$first = true; $first = true;
foreach ($clients as $c) { foreach ($clients as $c) {
if (!$first) echo ","; if (!$first) echo ",";
@ -140,18 +142,20 @@ foreach ($clients as $c) {
authenticate_client($c); authenticate_client($c);
if ($path === "/$c") { if ($path === "/$c") {
header("Location: $c/");
header('Status: 303'); header('Status: 303');
exit("303 See Other :)\n"); header("Location: $c/");
header('Content-Length: 14');
exit("303 See Other\n");
} elseif ($path === "/$c/") { } elseif ($path === "/$c/") {
if ($_SERVER['REQUEST_METHOD'] !== 'GET') { if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
header("Status: 405"); header("Status: 405");
header("Allow: GET"); header("Allow: GET");
exit("405 Method Not Allowed :(\n"); header('Content-Length: 23');
exit("405 Method Not Allowed\n");
} }
header('Content-Type: application/json; charset=UTF-8'); header('Content-Type: application/json; charset=UTF-8');
echo "{\"status\": \"success\", \"data\": [\n"; echo "{\"data\": [\n";
$first = true; $first = true;
foreach (scandir(".data/clients/$c/") as $file) { foreach (scandir(".data/clients/$c/") as $file) {
if (str_starts_with($file, ".") || str_ends_with($file, ".php")) continue; if (str_starts_with($file, ".") || str_ends_with($file, ".php")) continue;
@ -183,13 +187,15 @@ foreach ($clients as $c) {
$file = substr($path, strlen("/$c/")); $file = substr($path, strlen("/$c/"));
$path = ".data/clients/$c/$file"; $path = ".data/clients/$c/$file";
if (str_contains($file, '/')) { if (str_contains($file, '/')) {
header("Status: 400"); header('Status: 400');
exit("400 Bad Request :(\n"); header('Content-Length: 16');
exit("400 Bad Request\n");
} elseif ($_SERVER['REQUEST_METHOD'] === 'GET') { } elseif ($_SERVER['REQUEST_METHOD'] === 'GET') {
$size = filesize($path); $size = filesize($path);
if ($size === false) { if ($size === false) {
header("Status: 404"); header('Status: 404');
exit("404 Not Found :(\n"); header('Content-Length: 14');
exit("404 Not Found\n");
} }
$type = mime_content_type($path); $type = mime_content_type($path);
header("Content-Type: $type"); header("Content-Type: $type");
@ -201,24 +207,29 @@ foreach ($clients as $c) {
$fp = fopen($path, 'wb'); $fp = fopen($path, 'wb');
if ($fp === false) { if ($fp === false) {
header("Status: 500"); header("Status: 500");
exit("500 Internal Server Error :(\n"); header("Content-Length: 26");
exit("500 Internal Server Error\n");
} }
while ($data = fread($putdata, 4096)) while ($data = fread($putdata, 4096))
fwrite($fp, $data); fwrite($fp, $data);
fclose($fp); fclose($fp);
fclose($putdata); fclose($putdata);
header("Status: 201"); header("Status: 201");
exit("201 Created :)\n"); header('Content-Length: 12');
exit("201 Created\n");
} elseif ($_SERVER['REQUEST_METHOD'] === 'DELETE') { } elseif ($_SERVER['REQUEST_METHOD'] === 'DELETE') {
if (unlink($path) === false) { if (unlink($path) === false) {
header("Status: 500"); header("Status: 500");
exit("500 Internal Server Error :(\n"); header("Content-Length: 26");
exit("500 Internal Server Error\n");
} }
exit("200 OK :)\n"); header('Content-Length: 6');
exit("200 OK\n");
} else { } else {
header("Status: 405"); header("Status: 405");
header("Allow: GET, PUT, DELETE"); header("Allow: GET, PUT, DELETE");
exit("405 Method Not Allowed :(\n"); header("Content-Length: 23");
exit("405 Method Not Allowed\n");
} }
exit(); exit();
@ -227,10 +238,12 @@ foreach ($clients as $c) {
header("Status: 404"); header("Status: 404");
if ($format === 'text') { if ($format === 'text') {
header('Content-Type: text/plain; charset=UTF-8'); header('Content-Type: text/plain; charset=UTF-8');
echo "404 Not Found :(\n"; header('Content-Length: 14');
echo "404 Not Found\n";
} else if ($format === 'json') { } else if ($format === 'json') {
header('Content-Type: application/json; charset=UTF-8'); header('Content-Type: application/json; charset=UTF-8');
echo "{\"status\": \"error\", \"errors\": [{\"message\": \"Not found\"}]}\n"; header('Content-Length: 39');
echo "{\"errors\": [{\"message\": \"Not found\"}]}\n";
} else { } else {
header('Content-Type: text/html; charset=UTF-8'); header('Content-Type: text/html; charset=UTF-8');
header('Content-Length: 0'); header('Content-Length: 0');

View File

@ -1,12 +1,10 @@
<?php <?php
require "../.php/auth.inc";
require "../.php/credentials.inc"; require "../.php/credentials.inc";
global $GITEA_TOKEN; global $GITEA_TOKEN;
if ($_SERVER['PHP_AUTH_USER'] !== 'elwig' || $_SERVER['PHP_AUTH_PW'] !== 'ganzGeheim123!') { if ($_SERVER['PHP_AUTH_USER'] !== 'elwig' || $_SERVER['PHP_AUTH_PW'] !== 'ganzGeheim123!') {
header('Status: 401'); http_401_unauthorized();
header('WWW-Authenticate: Basic realm="Elwig"');
header('Content-Type: text/plain');
exit("401 Unauthorized :(\n");
} }
$repo = "winzer/elwig-misc.git"; $repo = "winzer/elwig-misc.git";
@ -15,11 +13,18 @@ $schema_version = $_GET['v'];
shell_exec("cd .repos; git clone $url; cd elwig-misc; git checkout main; git pull -f --rebase"); shell_exec("cd .repos; git clone $url; cd elwig-misc; git checkout main; git pull -f --rebase");
if ($schema_version) { if (!$schema_version || !ctype_digit($schema_version)) {
header('Status: 400');
header('Content-Type: text/plain; charset=UTF-8');
header('Content-Length: 16');
exit("400 Bad Request\n");
} else {
$commit = shell_exec("cd .repos/elwig-misc; git log -p sql/v01/99.schema_version.sql | grep '+PRAGMA schema_version = ${schema_version}00;' -B 14 | head -n 1 | grep -oE '[^ ]+$'"); $commit = shell_exec("cd .repos/elwig-misc; git log -p sql/v01/99.schema_version.sql | grep '+PRAGMA schema_version = ${schema_version}00;' -B 14 | head -n 1 | grep -oE '[^ ]+$'");
if (!$commit) { if (!$commit) {
header('Status: 404'); header('Status: 404');
exit("404 Specified schema version not found :(\n"); header('Content-Type: text/plain; charset=UTF-8');
header('Content-Length: 39');
exit("404 Specified Schema Version Not Found\n");
} }
shell_exec("cd .repos/elwig-misc; git checkout $commit"); shell_exec("cd .repos/elwig-misc; git checkout $commit");
} }

View File

@ -8,9 +8,9 @@ if ($_SERVER['REQUEST_METHOD'] === 'PUT') {
$name = substr($_SERVER['PATH_INFO'], 1); $name = substr($_SERVER['PATH_INFO'], 1);
if (str_contains($name, "..") || str_contains($name, "/")) { if (str_contains($name, "..") || str_contains($name, "/")) {
header('Status: 403'); header('Status: 403');
header('Content-Type: text/plain'); header('Content-Type: text/plain; charset=UTF-8');
header('Content-Length: 17'); header('Content-Length: 14');
exit("403 Forbidden :(\n"); exit("403 Forbidden\n");
} }
$upload = fopen("php://input", "r"); $upload = fopen("php://input", "r");
@ -19,16 +19,16 @@ if ($_SERVER['REQUEST_METHOD'] === 'PUT') {
fclose($fp); fclose($fp);
fclose($upload); fclose($upload);
header('Status: 200'); header('Status: 201');
header('Content-Type: text/plain'); header('Content-Type: text/plain; charset=UTF-8');
header('Content-Length: 10'); header('Content-Length: 12');
exit("200 OK :)\n"); exit("201 Created\n");
} else if ($_SERVER['REQUEST_METHOD'] !== 'GET' && $_SERVER['REQUEST_METHOD'] !== 'HEAD') { } else if ($_SERVER['REQUEST_METHOD'] !== 'GET' && $_SERVER['REQUEST_METHOD'] !== 'HEAD') {
header('Status: 405'); header('Status: 405');
header('Allow: GET, HEAD, PUT'); header('Allow: GET, HEAD, PUT');
header('Content-Type: text/plain'); header('Content-Type: text/plain; charset=UTF-8');
header('Content-Length: 26'); header('Content-Length: 23');
exit("405 Method Not Allowed :(\n"); exit("405 Method Not Allowed\n");
} }
global $getProd; global $getProd;
@ -95,7 +95,7 @@ $entities = array_filter($entities, function($e) {
$format = get_fmt(); $format = get_fmt();
if ($format === 'json') { if ($format === 'json') {
header('Content-Type: application/json; charset=UTF-8'); header('Content-Type: application/json; charset=UTF-8');
echo "{\"status\": \"success\", \"data\": [\n"; echo "{\"data\": [\n";
$first = true; $first = true;
foreach ($entities as $name => [$prod, $vers, $url, $size, $mtime, $ctime, $mod, $cre]) { foreach ($entities as $name => [$prod, $vers, $url, $size, $mtime, $ctime, $mod, $cre]) {
if (!$first) echo ",\n"; if (!$first) echo ",\n";
@ -112,6 +112,7 @@ if ($format === 'json') {
if (isset($getProd) && isset($getVers) && sizeof($entities) === 1) { if (isset($getProd) && isset($getVers) && sizeof($entities) === 1) {
header('Status: 303'); header('Status: 303');
header('Location: ' . $entities[array_key_first($entities)][2]); header('Location: ' . $entities[array_key_first($entities)][2]);
header('Content-Length: 0');
exit(); exit();
} }
header('Content-Type: application/xhtml+xml; charset=UTF-8'); header('Content-Type: application/xhtml+xml; charset=UTF-8');