From 7bdcd5c43653b9b9ceb64b0f9f4a6002e4a3f1a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 1 Jul 2024 06:57:18 +0000 Subject: [PATCH] Multipoart support --- app/controllers/general.php | 9 +- src/Appwrite/Utopia/Fetch/BodyMultipart.php | 152 ++++++++++++++++++++ src/Executor/Executor.php | 20 ++- 3 files changed, 172 insertions(+), 9 deletions(-) create mode 100644 src/Appwrite/Utopia/Fetch/BodyMultipart.php diff --git a/app/controllers/general.php b/app/controllers/general.php index d4217477fb..75429af8ed 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -274,6 +274,8 @@ function router(App $utopia, Database $dbForConsole, callable $getProjectDB, Swo requestTimeout: 30 ); + \var_dump($executionResponse); + $headersFiltered = []; foreach ($executionResponse['headers'] as $key => $value) { if (\in_array(\strtolower($key), FUNCTION_ALLOWLIST_HEADERS_RESPONSE)) { @@ -325,13 +327,6 @@ function router(App $utopia, Database $dbForConsole, callable $getProjectDB, Swo $body = $execution['responseBody'] ?? ''; - $encodingKey = \array_search('x-open-runtimes-encoding', \array_column($execution['responseHeaders'], 'name')); - if ($encodingKey !== false) { - if (($execution['responseHeaders'][$encodingKey]['value'] ?? '') === 'base64') { - $body = \base64_decode($body); - } - } - $contentType = 'text/plain'; foreach ($execution['responseHeaders'] as $header) { if (\strtolower($header['name']) === 'content-type') { diff --git a/src/Appwrite/Utopia/Fetch/BodyMultipart.php b/src/Appwrite/Utopia/Fetch/BodyMultipart.php new file mode 100644 index 0000000000..ed009ed8da --- /dev/null +++ b/src/Appwrite/Utopia/Fetch/BodyMultipart.php @@ -0,0 +1,152 @@ + $parts + */ + private array $parts = []; + private string $boundary = ""; + + public function __construct(string $boundary = null) + { + if (is_null($boundary)) { + $this->boundary = self::generateBoundary(); + } else { + $this->boundary = $boundary; + } + } + + public static function generateBoundary(): string + { + return '-----------------------------' . \uniqid(); + } + + public function load(string $body): self + { + $eol = "\r\n"; + + $sections = \explode('--' . $this->boundary, $body); + + foreach ($sections as $section) { + if (empty($section)) { + continue; + } + + if (strpos($section, $eol) === 0) { + $section = substr($section, \strlen($eol)); + } + + if (substr($section, -2) === $eol) { + $section = substr($section, 0, -1 * \strlen($eol)); + } + + if ($section == '--') { + continue; + } + + $partChunks = \explode($eol . $eol, $section, 2); + + if (\count($partChunks) < 2) { + continue; // Broken part + } + + [ $partHeaders, $partBody ] = $partChunks; + $partHeaders = \explode($eol, $partHeaders); + + $partName = ""; + foreach ($partHeaders as $partHeader) { + if (!empty($partName)) { + break; + } + + $partHeaderArray = \explode(':', $partHeader, 2); + + $partHeaderName = \strtolower($partHeaderArray[0] ?? ''); + $partHeaderValue = $partHeaderArray[1] ?? ''; + if ($partHeaderName == "content-disposition") { + $dispositionChunks = \explode("; ", $partHeaderValue); + foreach ($dispositionChunks as $dispositionChunk) { + $dispositionChunkValues = \explode("=", $dispositionChunk, 2); + if (\count($dispositionChunkValues) >= 2) { + if ($dispositionChunkValues[0] === "name") { + $partName = \trim($dispositionChunkValues[1], "\""); + break; + } + } + } + } + } + + if (!empty($partName)) { + $this->parts[$partName] = $partBody; + } + } + return $this; + } + + /** + * @return array + */ + public function getParts(): array + { + return $this->parts ?? []; + } + + public function getPart(string $key, mixed $default = ''): mixed + { + return $this->parts[$key] ?? $default; + } + + public function setPart(string $key, mixed $value): self + { + $this->parts[$key] = $value; + return $this; + } + + public function getBoundary(): string + { + return $this->boundary; + } + + public function setBoundary(string $boundary): self + { + $this->boundary = $boundary; + return $this; + } + + public function exportHeader(): string + { + return 'multipart/form-data; boundary=' . $this->boundary; + } + + public function exportBody(): string + { + $eol = "\r\n"; + $query = '--' . $this->boundary; + + foreach ($this->parts as $key => $value) { + $query .= $eol . 'Content-Disposition: form-data; name="' . $key . '"'; + + if (\is_array($value)) { + $query .= $eol . 'Content-Type: application/json'; + $value = \json_encode($value); + } else { + $isBinary = ! mb_check_encoding($value, 'UTF-8'); + if ($isBinary) { + $query .= $eol . 'Content-Transfer-Encoding: binary'; + } + } + + $query .= $eol . $eol; + $query .= $value . $eol; + $query .= '--' . $this->boundary; + } + + $query .= "--" . $eol; + + return $query; + } +} \ No newline at end of file diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index d80b0037af..ab225b67b1 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -2,6 +2,7 @@ namespace Executor; +use Appwrite\Utopia\Fetch\BodyMultipart; use Exception; use Utopia\System\System; @@ -250,7 +251,13 @@ class Executor break; case 'multipart/form-data': - $query = $this->flatten($params); + $multipart = new BodyMultipart(); + foreach ($params as $key => $value) { + $multipart->setPart($key, $value); + } + + $headers['content-type'] = $multipart->exportHeader(); + $query = $multipart->exportBody(); break; default: @@ -315,7 +322,16 @@ class Executor $responseStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($decode) { - switch (substr($responseType, 0, strpos($responseType, ';'))) { + $strpos = strpos($responseType, ';'); + $strpos = \is_bool($strpos) ? \strlen($responseType) : $strpos; + switch (substr($responseType, 0, $strpos)) { + case 'multipart/form-data': + $boundary = \explode('boundary=', $responseHeaders['content-type'] ?? '')[1] ?? ''; + $multipartResponse = new BodyMultipart($boundary); + $multipartResponse->load(\is_bool($responseBody) ? '' : $responseBody); + + $responseBody = $multipartResponse->getParts(); + break; case 'application/json': $json = json_decode($responseBody, true);