all
Some checks failed
Publish Docker Image / build-and-push (push) Failing after 2m13s

This commit is contained in:
Sam Kintop
2026-04-30 09:44:02 -05:00
parent 0a30837e3d
commit bf3870ccca
111 changed files with 8383 additions and 0 deletions

85
src/Api/ContentParser.php Normal file
View File

@@ -0,0 +1,85 @@
<?php
namespace Aternos\Mclogs\Api;
use Aternos\Mclogs\Api\Response\ApiError;
use Aternos\Mclogs\Config\Config;
use Aternos\Mclogs\Config\ConfigKey;
/**
* Utility class for reading log content from the http request
*/
class ContentParser
{
protected const int MAX_ENCODING_STEPS = 5;
/**
* Get all supported content encodings
* @return string[]
*/
public static function getSupportedEncodings(): array
{
return ["deflate", "gzip", "x-gzip"];
}
/**
* Get the content from the http request
*
* @return array|ApiError An array with the content or an ApiError on failure
*/
public function getContent(): array|ApiError
{
$limit = Config::getInstance()->get(ConfigKey::STORAGE_LIMIT_BYTES) * 2;
$body = file_get_contents('php://input', length: $limit + 1);
if ($body === false) {
return new ApiError(500, "Failed to read request body.");
}
if (strlen($body) > $limit) {
return new ApiError(413, "Request body exceeds maximum allowed size.");
}
$encodingHeader = $_SERVER['HTTP_CONTENT_ENCODING'] ?? '';
if ($encodingHeader) {
$encodingSteps = explode(',', $encodingHeader);
if (count($encodingSteps) > static::MAX_ENCODING_STEPS) {
return new ApiError(400, "Too many Content-Encoding steps.");
}
foreach (array_reverse($encodingSteps) as $step) {
switch (trim(strtolower($step))) {
case "deflate":
$body = @gzinflate($body, $limit);
break;
case "x-gzip":
case "gzip":
$body = @gzdecode($body, $limit);
break;
default:
return new ApiError(415, "Unsupported Content-Encoding: " . htmlspecialchars($step));
}
if ($body === false) {
return new ApiError(400, "Failed to decode request body with encoding: " . htmlspecialchars($step));
}
}
}
$contentTypeHeader = $_SERVER['CONTENT_TYPE'] ?? '';
if ($pos = strpos($contentTypeHeader, ';')) {
$contentTypeHeader = substr($contentTypeHeader, 0, $pos);
}
switch ($contentTypeHeader) {
case "application/x-www-form-urlencoded":
parse_str($body, $data);
break;
case "application/json":
$data = @json_decode($body, true);
if (!is_array($data)) {
return new ApiError(400, "Failed to parse JSON body.");
}
break;
default:
return new ApiError(415, "Unsupported Content-Type: " . htmlspecialchars($contentTypeHeader));
}
return $data;
}
}