fixed too big responses
This commit is contained in:
1 parent
601be6719f
commit
782e92a5e6
5 files changed
+299
-94
No files matched your search
+119
-3
@@ -14,6 +14,10 @@ class NextFloor extends IPSModuleStrict
|
||||
private const MAX_BUILDING_BYTES = 8_000_000;
|
||||
private const MAX_IMAGE_BYTES = 8_000_000;
|
||||
private const MAX_HISTORY = 20;
|
||||
private const MAX_API_RESPONSE_BYTES = 256 * 1024;
|
||||
private const TRANSFER_CHUNK_BYTES = 128 * 1024;
|
||||
private const MAX_TRANSFER_BYTES = 64 * 1024 * 1024;
|
||||
private const TRANSFER_TTL = 120;
|
||||
|
||||
public function Create(): void
|
||||
{
|
||||
@@ -304,9 +308,11 @@ class NextFloor extends IPSModuleStrict
|
||||
'backup/export' => ['format' => 'nextfloor-backup', 'version' => 1, 'building' => $this->building(), 'packs' => $this->importedPacks()],
|
||||
'backup/import' => $this->backupImport($this->arrayValue($request, 'building'), $request['packs'] ?? []),
|
||||
'variables/action' => $this->variableAction((string) ($request['domain'] ?? ''), (string) ($request['service'] ?? ''), $this->arrayValue($request, 'data')),
|
||||
'transport/read' => $this->readTransfer((string) ($request['transfer_id'] ?? ''), (int) ($request['index'] ?? -1)),
|
||||
'transport/release' => $this->releaseTransfer((string) ($request['transfer_id'] ?? '')),
|
||||
default => throw new RuntimeException('unknown_action')
|
||||
};
|
||||
$this->sendJson($result);
|
||||
$this->sendApiResult($result, ($request['transport'] ?? '') === 'nextfloor-chunks-v1');
|
||||
} catch (InvalidArgumentException $error) {
|
||||
$this->sendError(400, $error->getMessage());
|
||||
} catch (RuntimeException $error) {
|
||||
@@ -522,7 +528,7 @@ class NextFloor extends IPSModuleStrict
|
||||
|
||||
private function stateRevision(array $objects): int
|
||||
{
|
||||
$hash = hash('sha256', json_encode($objects, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
|
||||
$hash = hash('sha256', $this->encodeResponse($objects));
|
||||
if (!hash_equals($this->ReadAttributeString('StateHash'), $hash)) {
|
||||
$this->WriteAttributeString('StateHash', $hash);
|
||||
$this->WriteAttributeInteger('StateRevision', $this->ReadAttributeInteger('StateRevision') + 1);
|
||||
@@ -827,9 +833,119 @@ class NextFloor extends IPSModuleStrict
|
||||
|
||||
private function sendJson(array $data): void
|
||||
{
|
||||
$encoded = $this->encodeResponse($data);
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
header('Cache-Control: no-store');
|
||||
echo json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
||||
echo $encoded;
|
||||
}
|
||||
|
||||
private function encodeResponse(array $data): string
|
||||
{
|
||||
return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE | JSON_THROW_ON_ERROR);
|
||||
}
|
||||
|
||||
/** Never emit a JSON response near the kernel's default 1 MiB PHP output limit. */
|
||||
private function sendApiResult(array $data, bool $supportsChunks): void
|
||||
{
|
||||
$encoded = $this->encodeResponse($data);
|
||||
if (strlen($encoded) <= self::MAX_API_RESPONSE_BYTES) {
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
header('Cache-Control: no-store');
|
||||
echo $encoded;
|
||||
return;
|
||||
}
|
||||
if (!$supportsChunks) {
|
||||
$this->sendError(409, 'client_update_required: Bitte den Designer neu laden.');
|
||||
return;
|
||||
}
|
||||
$this->sendJson(['_nextfloor_transfer' => $this->createTransfer($encoded)]);
|
||||
}
|
||||
|
||||
/** Large results use private, short-lived snapshots, not the module's size-limited SetBuffer. */
|
||||
private function transferDirectory(): string
|
||||
{
|
||||
$directory = rtrim(sys_get_temp_dir(), '/\\') . DIRECTORY_SEPARATOR . 'nextfloor-transfers-' . $this->InstanceID;
|
||||
if (is_link($directory) || (!is_dir($directory) && !mkdir($directory, 0700, true) && !is_dir($directory))) {
|
||||
throw new RuntimeException('transfer_storage_unavailable');
|
||||
}
|
||||
return $directory;
|
||||
}
|
||||
|
||||
private function transferFile(string $id): string
|
||||
{
|
||||
if (preg_match('/^[a-f0-9]{32}$/D', $id) !== 1) {
|
||||
throw new InvalidArgumentException('invalid_transfer_id');
|
||||
}
|
||||
return $this->transferDirectory() . DIRECTORY_SEPARATOR . $id . '.json';
|
||||
}
|
||||
|
||||
private function createTransfer(string $encoded): array
|
||||
{
|
||||
$bytes = strlen($encoded);
|
||||
if ($bytes > self::MAX_TRANSFER_BYTES) {
|
||||
throw new InvalidArgumentException('response_too_large');
|
||||
}
|
||||
$active = 0;
|
||||
foreach (glob($this->transferDirectory() . DIRECTORY_SEPARATOR . '*.json') ?: [] as $file) {
|
||||
if (preg_match('/^[a-f0-9]{32}\.json$/D', basename($file)) !== 1 || is_link($file)) continue;
|
||||
if ((int) filemtime($file) < time() - self::TRANSFER_TTL) {
|
||||
unlink($file);
|
||||
} else {
|
||||
$active++;
|
||||
}
|
||||
}
|
||||
if ($active >= 8) throw new RuntimeException('transfer_busy: Bitte kurz warten und erneut laden.');
|
||||
$id = bin2hex(random_bytes(16));
|
||||
$file = $this->transferFile($id);
|
||||
$handle = fopen($file, 'xb');
|
||||
if ($handle === false) throw new RuntimeException('transfer_storage_unavailable');
|
||||
chmod($file, 0600);
|
||||
try {
|
||||
$written = 0;
|
||||
while ($written < $bytes) {
|
||||
$count = fwrite($handle, substr($encoded, $written));
|
||||
if ($count === false || $count === 0) throw new RuntimeException('transfer_write_failed');
|
||||
$written += $count;
|
||||
}
|
||||
} catch (Throwable $error) {
|
||||
fclose($handle);
|
||||
unlink($file);
|
||||
throw $error;
|
||||
}
|
||||
fclose($handle);
|
||||
return ['id' => $id, 'bytes' => $bytes, 'chunk_bytes' => self::TRANSFER_CHUNK_BYTES, 'chunks' => (int) ceil($bytes / self::TRANSFER_CHUNK_BYTES)];
|
||||
}
|
||||
|
||||
private function readTransfer(string $id, int $index): array
|
||||
{
|
||||
$file = $this->transferFile($id);
|
||||
if (!is_file($file) || is_link($file)) throw new RuntimeException('transfer_not_found');
|
||||
if ((int) filemtime($file) < time() - self::TRANSFER_TTL) {
|
||||
unlink($file);
|
||||
throw new RuntimeException('transfer_expired: Bitte erneut laden.');
|
||||
}
|
||||
$bytes = (int) filesize($file);
|
||||
if ($index < 0 || $index >= (int) ceil($bytes / self::TRANSFER_CHUNK_BYTES)) {
|
||||
throw new InvalidArgumentException('invalid_transfer_index');
|
||||
}
|
||||
$handle = fopen($file, 'rb');
|
||||
if ($handle === false) throw new RuntimeException('transfer_not_found');
|
||||
try {
|
||||
if (fseek($handle, $index * self::TRANSFER_CHUNK_BYTES) !== 0) throw new RuntimeException('transfer_read_failed');
|
||||
$data = fread($handle, self::TRANSFER_CHUNK_BYTES);
|
||||
if ($data === false) throw new RuntimeException('transfer_read_failed');
|
||||
} finally {
|
||||
fclose($handle);
|
||||
}
|
||||
touch($file); // Active slow connections keep their immutable snapshot alive.
|
||||
return ['index' => $index, 'data' => base64_encode($data)];
|
||||
}
|
||||
|
||||
private function releaseTransfer(string $id): array
|
||||
{
|
||||
$file = $this->transferFile($id);
|
||||
if (is_file($file) && !is_link($file)) unlink($file);
|
||||
return [];
|
||||
}
|
||||
|
||||
private function sendError(int $status, string $error): void
|
||||
|
||||
Reference in new issue
Block a user