From 4b844e8f06699dc60e3f5abd8bca5710ed43719f Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 16 Oct 2024 12:07:15 +0100 Subject: [PATCH 01/21] feat: utopia compression --- src/Http/Http.php | 17 +++++++ src/Http/Response.php | 102 ++++++++++++++++++++++++++++++++---------- 2 files changed, 96 insertions(+), 23 deletions(-) diff --git a/src/Http/Http.php b/src/Http/Http.php index 3a091c6a..68713c82 100755 --- a/src/Http/Http.php +++ b/src/Http/Http.php @@ -49,6 +49,9 @@ class Http extends Base protected string|null $requestClass = null; protected string|null $responseClass = null; + protected bool $compression = false; + protected int $compressionLevel = 0; + /** * Http * @@ -79,6 +82,15 @@ public function setRequestClass(string $requestClass) $this->requestClass = $requestClass; } + /** + * Set Compression + */ + public function setCompression(bool $compression, int $compressionLevel = 0) + { + $this->compression = $compression; + $this->compressionLevel = $compressionLevel; + } + /** * GET * @@ -317,6 +329,11 @@ public function start() if (!\is_null($this->responseClass)) { $response = new $this->responseClass($response); + + if ($this->compression) { + $response->setAcceptEncoding($request->getHeader('accept-encoding') ?? ''); + $response->setCompressionLevel($this->compressionLevel ?? 1); + } } $context = clone $this->container; diff --git a/src/Http/Response.php b/src/Http/Response.php index 66a0eeaa..72cce0f2 100755 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -245,6 +245,16 @@ abstract class Response */ protected int $size = 0; + /** + * @var string + */ + protected string $acceptEncoding = ''; + + /** + * @var int + */ + protected int $compressionLevel = 0; + /** * Response constructor. * @@ -270,6 +280,19 @@ public function setContentType(string $type, string $charset = ''): static return $this; } + /** + * Set accept encoding + * + * Set HTTP accept encoding header. + * + * @param string $acceptEncoding + */ + public function setAcceptEncoding(string $acceptEncoding): static + { + $this->acceptEncoding = $acceptEncoding; + return $this; + } + /** * Get content type * @@ -459,6 +482,25 @@ public function getCookies(): array return $this->cookies; } + public function getEncoding(): ?string + { + if (empty($this->acceptEncoding || isset($this->compressed[$this->contentType]))) { + return null; + } + + if (strpos($this->acceptEncoding, 'br') !== false && function_exists('brotli_compress')) { + return 'br'; + } + + if (strpos($this->acceptEncoding, 'gzip') !== false && function_exists('gzencode')) { + return 'gzip'; + } + + if (strpos($this->acceptEncoding, 'deflate') !== false && function_exists('gzdeflate')) { + return 'deflate'; + } + } + /** * Output response * @@ -475,37 +517,51 @@ public function send(string $body = ''): void $this->sent = true; - $this - ->addHeader('Server', array_key_exists('Server', $this->headers) ? $this->headers['Server'] : 'Utopia/Http') - ->addHeader('X-Debug-Speed', (string) (\microtime(true) - $this->startTime)) - ; - - $this - ->appendCookies() - ->appendHeaders(); - - if (!$this->disablePayload) { - $length = strlen($body); + $serverHeader = $this->headers['Server'] ?? 'Utopia/Http'; + $this->addHeader('Server', $serverHeader); + $this->addHeader('X-Debug-Speed', (string) (microtime(true) - $this->startTime)); - $this->size = $this->size + strlen(implode("\n", $this->headers)) + $length; + $this->appendCookies()->appendHeaders(); - if (array_key_exists( - $this->contentType, - $this->compressed - ) && ($length <= self::CHUNK_SIZE)) { // Dont compress with GZIP / Brotli if header is not listed and size is bigger than 2mb - $this->end($body); - } else { - for ($i = 0; $i < ceil($length / self::CHUNK_SIZE); $i++) { - $this->write(substr($body, ($i * self::CHUNK_SIZE), min(self::CHUNK_SIZE, $length - ($i * self::CHUNK_SIZE)))); - } + // Send response + if ($this->disablePayload) { + $this->end(); + return; + } - $this->end(); + // Compress body + $encoding = $this->getEncoding(); + if ($encoding) { + switch($encoding) { + case 'br': + $body = brotli_compress($body, $this->compressionLevel); + break; + case 'gzip': + $body = gzencode($body, $this->compressionLevel); + break; + case 'deflate': + $body = gzdeflate($body, $this->compressionLevel); + break; } + $this->addHeader('Content-Encoding', $encoding); + $this->addHeader('Vary', 'Accept-Encoding'); + } + + $headerSize = strlen(implode("\n", $this->headers)); + $bodyLength = strlen($body); + $this->size += $headerSize + $bodyLength; - $this->disablePayload(); + if ($bodyLength <= self::CHUNK_SIZE) { + $this->end($body); } else { + $chunks = str_split($body, self::CHUNK_SIZE); + foreach ($chunks as $chunk) { + $this->write($chunk); + } $this->end(); } + + $this->disablePayload(); } /** From f0cd76fe9ab548df4fb1da6f61a991583907a29e Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 16 Oct 2024 12:09:59 +0100 Subject: [PATCH 02/21] chore: lint errors --- src/Http/Response.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Http/Response.php b/src/Http/Response.php index 72cce0f2..f3f6dacb 100755 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -482,9 +482,9 @@ public function getCookies(): array return $this->cookies; } - public function getEncoding(): ?string + public function getEncoding(): string|null { - if (empty($this->acceptEncoding || isset($this->compressed[$this->contentType]))) { + if (empty($this->acceptEncoding || !isset($this->compressed[$this->contentType]))) { return null; } @@ -495,10 +495,12 @@ public function getEncoding(): ?string if (strpos($this->acceptEncoding, 'gzip') !== false && function_exists('gzencode')) { return 'gzip'; } - + if (strpos($this->acceptEncoding, 'deflate') !== false && function_exists('gzdeflate')) { return 'deflate'; } + + return null; } /** @@ -532,7 +534,7 @@ public function send(string $body = ''): void // Compress body $encoding = $this->getEncoding(); if ($encoding) { - switch($encoding) { + switch ($encoding) { case 'br': $body = brotli_compress($body, $this->compressionLevel); break; From 5e119bf2578d99cc1c586abd984f9affef9937a1 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Thu, 17 Oct 2024 13:27:14 +0100 Subject: [PATCH 03/21] feat: use utopia compression --- composer.json | 9 ++- composer.lock | 168 ++++++++++++++++++++++++++++-------------- src/Http/Http.php | 5 +- src/Http/Response.php | 56 ++++---------- 4 files changed, 138 insertions(+), 100 deletions(-) diff --git a/composer.json b/composer.json index 0cdace99..540833d2 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,14 @@ "require": { "php": ">=8.0", "ext-swoole": "*", - "utopia-php/servers": "0.1.*" + "utopia-php/servers": "0.1.*", + "utopia-php/compression": "dev-feat-utopia-compression" + }, + "repositories": { + "utopia-php/compression": { + "type": "git", + "url": "https://github.com/utopia-php/compression" + } }, "require-dev": { "ext-xdebug": "*", diff --git a/composer.lock b/composer.lock index 0a04d66b..78207535 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,63 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f765427b1ac629c58be95cd57ffacda3", + "content-hash": "2768a94b89de6af19ca17a6b2f056c83", "packages": [ + { + "name": "utopia-php/compression", + "version": "dev-feat-utopia-compression", + "source": { + "type": "git", + "url": "https://github.com/utopia-php/compression", + "reference": "a41c2c769ecaab5dc4b7efecf005457a11f29f87" + }, + "require": { + "ext-brotli": "*", + "ext-fileinfo": "*", + "ext-lz4": "*", + "ext-snappy": "*", + "ext-xz": "*", + "ext-zlib": "*", + "ext-zstd": "*", + "php": ">=8.0" + }, + "require-dev": { + "laravel/pint": "1.2.*", + "phpunit/phpunit": "^9.3", + "vimeo/psalm": "4.0.1" + }, + "type": "library", + "autoload": { + "psr-4": { + "Utopia\\Compression\\": "src/Compression" + } + }, + "autoload-dev": { + "psr-4": { + "Utopia\\Tests\\Compression\\": "tests/Compression" + } + }, + "scripts": { + "lint": [ + "./vendor/bin/pint --test" + ], + "format": [ + "./vendor/bin/pint" + ] + }, + "license": [ + "MIT" + ], + "description": "A simple Compression library to handle file compression", + "keywords": [ + "compression", + "framework", + "php", + "upf", + "utopia" + ], + "time": "2024-10-17T12:19:31+00:00" + }, { "name": "utopia-php/di", "version": "0.1.0", @@ -334,16 +389,16 @@ }, { "name": "laravel/pint", - "version": "v1.17.3", + "version": "v1.18.1", "source": { "type": "git", "url": "https://github.com/laravel/pint.git", - "reference": "9d77be916e145864f10788bb94531d03e1f7b482" + "reference": "35c00c05ec43e6b46d295efc0f4386ceb30d50d9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/laravel/pint/zipball/9d77be916e145864f10788bb94531d03e1f7b482", - "reference": "9d77be916e145864f10788bb94531d03e1f7b482", + "url": "https://api.github.com/repos/laravel/pint/zipball/35c00c05ec43e6b46d295efc0f4386ceb30d50d9", + "reference": "35c00c05ec43e6b46d295efc0f4386ceb30d50d9", "shasum": "" }, "require": { @@ -396,7 +451,7 @@ "issues": "https://github.com/laravel/pint/issues", "source": "https://github.com/laravel/pint" }, - "time": "2024-09-03T15:00:28+00:00" + "time": "2024-09-24T17:22:50+00:00" }, { "name": "myclabs/deep-copy", @@ -460,16 +515,16 @@ }, { "name": "nikic/php-parser", - "version": "v5.1.0", + "version": "v5.3.1", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "683130c2ff8c2739f4822ff7ac5c873ec529abd1" + "reference": "8eea230464783aa9671db8eea6f8c6ac5285794b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/683130c2ff8c2739f4822ff7ac5c873ec529abd1", - "reference": "683130c2ff8c2739f4822ff7ac5c873ec529abd1", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/8eea230464783aa9671db8eea6f8c6ac5285794b", + "reference": "8eea230464783aa9671db8eea6f8c6ac5285794b", "shasum": "" }, "require": { @@ -512,9 +567,9 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v5.1.0" + "source": "https://github.com/nikic/PHP-Parser/tree/v5.3.1" }, - "time": "2024-07-01T20:03:41+00:00" + "time": "2024-10-08T18:51:32+00:00" }, { "name": "phar-io/manifest", @@ -734,6 +789,7 @@ "issues": "https://github.com/phpbench/dom/issues", "source": "https://github.com/phpbench/dom/tree/0.3.3" }, + "abandoned": true, "time": "2023-03-06T23:46:57+00:00" }, { @@ -837,16 +893,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.12.3", + "version": "1.12.6", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "0fcbf194ab63d8159bb70d9aa3e1350051632009" + "reference": "dc4d2f145a88ea7141ae698effd64d9df46527ae" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/0fcbf194ab63d8159bb70d9aa3e1350051632009", - "reference": "0fcbf194ab63d8159bb70d9aa3e1350051632009", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/dc4d2f145a88ea7141ae698effd64d9df46527ae", + "reference": "dc4d2f145a88ea7141ae698effd64d9df46527ae", "shasum": "" }, "require": { @@ -891,7 +947,7 @@ "type": "github" } ], - "time": "2024-09-09T08:10:35+00:00" + "time": "2024-10-06T15:03:59+00:00" }, { "name": "phpunit/php-code-coverage", @@ -1214,16 +1270,16 @@ }, { "name": "phpunit/phpunit", - "version": "9.6.20", + "version": "9.6.21", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "49d7820565836236411f5dc002d16dd689cde42f" + "reference": "de6abf3b6f8dd955fac3caad3af7a9504e8c2ffa" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/49d7820565836236411f5dc002d16dd689cde42f", - "reference": "49d7820565836236411f5dc002d16dd689cde42f", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/de6abf3b6f8dd955fac3caad3af7a9504e8c2ffa", + "reference": "de6abf3b6f8dd955fac3caad3af7a9504e8c2ffa", "shasum": "" }, "require": { @@ -1238,7 +1294,7 @@ "phar-io/manifest": "^2.0.4", "phar-io/version": "^3.2.1", "php": ">=7.3", - "phpunit/php-code-coverage": "^9.2.31", + "phpunit/php-code-coverage": "^9.2.32", "phpunit/php-file-iterator": "^3.0.6", "phpunit/php-invoker": "^3.1.1", "phpunit/php-text-template": "^2.0.4", @@ -1297,7 +1353,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", "security": "https://github.com/sebastianbergmann/phpunit/security/policy", - "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.20" + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.21" }, "funding": [ { @@ -1313,7 +1369,7 @@ "type": "tidelift" } ], - "time": "2024-07-10T11:45:39+00:00" + "time": "2024-09-19T10:50:18+00:00" }, { "name": "psr/cache", @@ -1419,16 +1475,16 @@ }, { "name": "psr/log", - "version": "3.0.1", + "version": "3.0.2", "source": { "type": "git", "url": "https://github.com/php-fig/log.git", - "reference": "79dff0b268932c640297f5208d6298f71855c03e" + "reference": "f16e1d5863e37f8d8c2a01719f5b34baa2b714d3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-fig/log/zipball/79dff0b268932c640297f5208d6298f71855c03e", - "reference": "79dff0b268932c640297f5208d6298f71855c03e", + "url": "https://api.github.com/repos/php-fig/log/zipball/f16e1d5863e37f8d8c2a01719f5b34baa2b714d3", + "reference": "f16e1d5863e37f8d8c2a01719f5b34baa2b714d3", "shasum": "" }, "require": { @@ -1463,9 +1519,9 @@ "psr-3" ], "support": { - "source": "https://github.com/php-fig/log/tree/3.0.1" + "source": "https://github.com/php-fig/log/tree/3.0.2" }, - "time": "2024-08-21T13:31:24+00:00" + "time": "2024-09-11T13:17:53+00:00" }, { "name": "sebastian/cli-parser", @@ -2538,16 +2594,16 @@ }, { "name": "symfony/console", - "version": "v7.1.4", + "version": "v7.1.5", "source": { "type": "git", "url": "https://github.com/symfony/console.git", - "reference": "1eed7af6961d763e7832e874d7f9b21c3ea9c111" + "reference": "0fa539d12b3ccf068a722bbbffa07ca7079af9ee" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/console/zipball/1eed7af6961d763e7832e874d7f9b21c3ea9c111", - "reference": "1eed7af6961d763e7832e874d7f9b21c3ea9c111", + "url": "https://api.github.com/repos/symfony/console/zipball/0fa539d12b3ccf068a722bbbffa07ca7079af9ee", + "reference": "0fa539d12b3ccf068a722bbbffa07ca7079af9ee", "shasum": "" }, "require": { @@ -2611,7 +2667,7 @@ "terminal" ], "support": { - "source": "https://github.com/symfony/console/tree/v7.1.4" + "source": "https://github.com/symfony/console/tree/v7.1.5" }, "funding": [ { @@ -2627,7 +2683,7 @@ "type": "tidelift" } ], - "time": "2024-08-15T22:48:53+00:00" + "time": "2024-09-20T08:28:38+00:00" }, { "name": "symfony/deprecation-contracts", @@ -2698,16 +2754,16 @@ }, { "name": "symfony/filesystem", - "version": "v7.1.2", + "version": "v7.1.5", "source": { "type": "git", "url": "https://github.com/symfony/filesystem.git", - "reference": "92a91985250c251de9b947a14bb2c9390b1a562c" + "reference": "61fe0566189bf32e8cfee78335d8776f64a66f5a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/filesystem/zipball/92a91985250c251de9b947a14bb2c9390b1a562c", - "reference": "92a91985250c251de9b947a14bb2c9390b1a562c", + "url": "https://api.github.com/repos/symfony/filesystem/zipball/61fe0566189bf32e8cfee78335d8776f64a66f5a", + "reference": "61fe0566189bf32e8cfee78335d8776f64a66f5a", "shasum": "" }, "require": { @@ -2744,7 +2800,7 @@ "description": "Provides basic utilities for the filesystem", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/filesystem/tree/v7.1.2" + "source": "https://github.com/symfony/filesystem/tree/v7.1.5" }, "funding": [ { @@ -2760,7 +2816,7 @@ "type": "tidelift" } ], - "time": "2024-06-28T10:03:55+00:00" + "time": "2024-09-17T09:16:35+00:00" }, { "name": "symfony/finder", @@ -3213,16 +3269,16 @@ }, { "name": "symfony/process", - "version": "v7.1.3", + "version": "v7.1.5", "source": { "type": "git", "url": "https://github.com/symfony/process.git", - "reference": "7f2f542c668ad6c313dc4a5e9c3321f733197eca" + "reference": "5c03ee6369281177f07f7c68252a280beccba847" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/process/zipball/7f2f542c668ad6c313dc4a5e9c3321f733197eca", - "reference": "7f2f542c668ad6c313dc4a5e9c3321f733197eca", + "url": "https://api.github.com/repos/symfony/process/zipball/5c03ee6369281177f07f7c68252a280beccba847", + "reference": "5c03ee6369281177f07f7c68252a280beccba847", "shasum": "" }, "require": { @@ -3254,7 +3310,7 @@ "description": "Executes commands in sub-processes", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/process/tree/v7.1.3" + "source": "https://github.com/symfony/process/tree/v7.1.5" }, "funding": [ { @@ -3270,7 +3326,7 @@ "type": "tidelift" } ], - "time": "2024-07-26T12:44:47+00:00" + "time": "2024-09-19T21:48:23+00:00" }, { "name": "symfony/service-contracts", @@ -3357,16 +3413,16 @@ }, { "name": "symfony/string", - "version": "v7.1.4", + "version": "v7.1.5", "source": { "type": "git", "url": "https://github.com/symfony/string.git", - "reference": "6cd670a6d968eaeb1c77c2e76091c45c56bc367b" + "reference": "d66f9c343fa894ec2037cc928381df90a7ad4306" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/string/zipball/6cd670a6d968eaeb1c77c2e76091c45c56bc367b", - "reference": "6cd670a6d968eaeb1c77c2e76091c45c56bc367b", + "url": "https://api.github.com/repos/symfony/string/zipball/d66f9c343fa894ec2037cc928381df90a7ad4306", + "reference": "d66f9c343fa894ec2037cc928381df90a7ad4306", "shasum": "" }, "require": { @@ -3424,7 +3480,7 @@ "utf8" ], "support": { - "source": "https://github.com/symfony/string/tree/v7.1.4" + "source": "https://github.com/symfony/string/tree/v7.1.5" }, "funding": [ { @@ -3440,7 +3496,7 @@ "type": "tidelift" } ], - "time": "2024-08-12T09:59:40+00:00" + "time": "2024-09-20T08:28:38+00:00" }, { "name": "theseer/tokenizer", @@ -3544,7 +3600,9 @@ ], "aliases": [], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": { + "utopia-php/compression": 20 + }, "prefer-stable": false, "prefer-lowest": false, "platform": { diff --git a/src/Http/Http.php b/src/Http/Http.php index 68713c82..b9ed1f20 100755 --- a/src/Http/Http.php +++ b/src/Http/Http.php @@ -50,7 +50,6 @@ class Http extends Base protected string|null $responseClass = null; protected bool $compression = false; - protected int $compressionLevel = 0; /** * Http @@ -85,10 +84,9 @@ public function setRequestClass(string $requestClass) /** * Set Compression */ - public function setCompression(bool $compression, int $compressionLevel = 0) + public function setCompression(bool $compression) { $this->compression = $compression; - $this->compressionLevel = $compressionLevel; } /** @@ -332,7 +330,6 @@ public function start() if ($this->compression) { $response->setAcceptEncoding($request->getHeader('accept-encoding') ?? ''); - $response->setCompressionLevel($this->compressionLevel ?? 1); } } diff --git a/src/Http/Response.php b/src/Http/Response.php index f3f6dacb..19660e5a 100755 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -1,6 +1,7 @@ cookies; } - public function getEncoding(): string|null - { - if (empty($this->acceptEncoding || !isset($this->compressed[$this->contentType]))) { - return null; - } - - if (strpos($this->acceptEncoding, 'br') !== false && function_exists('brotli_compress')) { - return 'br'; - } - - if (strpos($this->acceptEncoding, 'gzip') !== false && function_exists('gzencode')) { - return 'gzip'; - } - - if (strpos($this->acceptEncoding, 'deflate') !== false && function_exists('gzdeflate')) { - return 'deflate'; - } - - return null; - } - /** * Output response * @@ -532,21 +507,22 @@ public function send(string $body = ''): void } // Compress body - $encoding = $this->getEncoding(); - if ($encoding) { - switch ($encoding) { - case 'br': - $body = brotli_compress($body, $this->compressionLevel); - break; - case 'gzip': - $body = gzencode($body, $this->compressionLevel); - break; - case 'deflate': - $body = gzdeflate($body, $this->compressionLevel); - break; + if ( + !empty($this->acceptEncoding) && + isset($this->compressed[$this->contentType]) && + strlen($body) > 1024 + ) { + $algorithm = Compression::fromAcceptEncoding($this->acceptEncoding, [ + Compression::BROTLI, + Compression::GZIP, + Compression::DEFLATE, + ]); + + if ($algorithm) { + $body = $algorithm->compress($body); + $this->addHeader('Content-Encoding', $algorithm->getContentEncoding()); + $this->addHeader('Vary', 'Accept-Encoding'); } - $this->addHeader('Content-Encoding', $encoding); - $this->addHeader('Vary', 'Accept-Encoding'); } $headerSize = strlen(implode("\n", $this->headers)); From 45e077cea0a0ce1925f94aa9d08ceb39698d3db9 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Thu, 17 Oct 2024 13:28:43 +0100 Subject: [PATCH 04/21] chore: fmt --- src/Http/Response.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Http/Response.php b/src/Http/Response.php index 19660e5a..1d553ab1 100755 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -1,6 +1,7 @@ acceptEncoding) && + !empty($this->acceptEncoding) && isset($this->compressed[$this->contentType]) && strlen($body) > 1024 ) { From 36867d3e7a30c11b424bd722e1331ad3f6be55b4 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Thu, 17 Oct 2024 13:36:58 +0100 Subject: [PATCH 05/21] feat: swoole compression tests --- .github/workflows/test.yml | 5 ++- CONTRIBUTING.md | 1 + Dockerfile.swoole_compression.swoole | 48 +++++++++++++++++++++ docker-compose.yml | 13 ++++++ tests/e2e/ResponseSwooleCompression.php | 17 ++++++++ tests/e2e/server-swoole-compression.php | 57 +++++++++++++++++++++++++ 6 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 Dockerfile.swoole_compression.swoole create mode 100755 tests/e2e/ResponseSwooleCompression.php create mode 100644 tests/e2e/server-swoole-compression.php diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a6f1e642..797e1b87 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -30,5 +30,8 @@ jobs: - name: Run Swoole Tests run: docker compose exec swoole vendor/bin/phpunit --configuration phpunit.xml - - name: Run Swoole Corotuine Tests + - name: Run Swoole Coroutine Tests run: docker compose exec swoole-coroutine vendor/bin/phpunit --configuration phpunit.xml + + - name: Run Swoole Compression Tests + run: docker compose exec swoole-compression vendor/bin/phpunit --configuration phpunit.xml \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 36760e78..e922df85 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -70,6 +70,7 @@ $ git push origin [name_of_your_new_branch] - `docker compose exec fpm vendor/bin/phpunit --configuration phpunit.xml` - `docker compose exec swoole vendor/bin/phpunit --configuration phpunit.xml` - `docker compose exec swoole-coroutine vendor/bin/phpunit --configuration phpunit.xml` +- `docker compose exec swoole-compression vendor/bin/phpunit --configuration phpunit.xml` ## Introducing New Features diff --git a/Dockerfile.swoole_compression.swoole b/Dockerfile.swoole_compression.swoole new file mode 100644 index 00000000..446248bd --- /dev/null +++ b/Dockerfile.swoole_compression.swoole @@ -0,0 +1,48 @@ +FROM composer:2.0 AS step0 + +ARG TESTING=true +ARG DEBUG=false + +ENV TESTING=$TESTING +ENV DEBUG=$DEBUG + +WORKDIR /usr/local/src/ + +COPY composer.* /usr/local/src/ + +RUN composer install --ignore-platform-reqs --optimize-autoloader \ + --no-plugins --no-scripts --prefer-dist \ + `if [ "$TESTING" != "true" ]; then echo "--no-dev"; fi` + +FROM appwrite/base:0.9.0 as final + +ARG TESTING=true +ARG DEBUG=false + +ENV TESTING=$TESTING +ENV DEBUG=$DEBUG + +LABEL maintainer="team@appwrite.io" + +RUN \ + if [ "$DEBUG" == "true" ]; then \ + apk add boost boost-dev; \ + fi + +WORKDIR /usr/src/code + +COPY ./dev /usr/src/code/dev +COPY ./src /usr/src/code/src +COPY ./tests /usr/src/code/tests +COPY ./phpunit.xml /usr/src/code/phpunit.xml +COPY ./phpbench.json /usr/src/code/phpbench.json +COPY --from=step0 /usr/local/src/vendor /usr/src/code/vendor + +# Enable Extensions +RUN if [ "$DEBUG" == "true" ]; then cp /usr/src/code/dev/xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini; fi +RUN if [ "$DEBUG" = "false" ]; then rm -rf /usr/src/code/dev; fi +RUN if [ "$DEBUG" = "false" ]; then rm -f /usr/local/lib/php/extensions/no-debug-non-zts-20220829/xdebug.so; fi + +EXPOSE 80 + +CMD ["php", "tests/e2e/server-swoole-compression.php"] diff --git a/docker-compose.yml b/docker-compose.yml index bd2952c7..d88623ce 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -38,6 +38,19 @@ services: - ./tmp/xdebug:/tmp/xdebug networks: - testing + swoole-compression: + build: + context: . + dockerfile: Dockerfile.swoole_compression + ports: + - "9403:80" + volumes: + - ./dev:/usr/src/code/dev:rw + - ./src:/usr/src/code/src + - ./tests:/usr/src/code/tests + - ./tmp/xdebug:/tmp/xdebug + networks: + - testing mariadb: image: mariadb:10.11 # fix issues when upgrading using: mysql_upgrade -u root -p diff --git a/tests/e2e/ResponseSwooleCompression.php b/tests/e2e/ResponseSwooleCompression.php new file mode 100755 index 00000000..58c78a17 --- /dev/null +++ b/tests/e2e/ResponseSwooleCompression.php @@ -0,0 +1,17 @@ +client = new Client('http://swoole-compression'); + } +} diff --git a/tests/e2e/server-swoole-compression.php b/tests/e2e/server-swoole-compression.php new file mode 100644 index 00000000..e10c14d6 --- /dev/null +++ b/tests/e2e/server-swoole-compression.php @@ -0,0 +1,57 @@ +withHost('mariadb') + ->withPort(3306) + // ->withUnixSocket('/tmp/mysql.sock') + ->withDbName('test') + ->withCharset('utf8mb4') + ->withUsername('user') + ->withPassword('password'), 9000); + + +$dependency = new Dependency(); + +$dependency + ->setName('key') + ->inject('request') + ->setCallback(function (Request $request) { + return $request->getHeader('x-utopia-key', 'unknown'); + }); + +$container->set($dependency); + +$dependency1 = new Dependency(); +$dependency1 + ->setName('pool') + ->setCallback(function () use ($pool) { + return $pool; + }); + +$container->set($dependency1); + +$server = new Server('0.0.0.0', '80', [ + 'open_http2_protocol' => true, + 'http_compression' => false, // disable swoole compression +]); +$http = new Http($server, $container, 'UTC'); + +$http->setCompression(true); // enable utopia compression + +echo "Server started\n"; + +$http->start(); From 33553f3664e184a645084eefc47d29c331985506 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Thu, 17 Oct 2024 13:41:27 +0100 Subject: [PATCH 06/21] chore: rename dockerfile --- ...ile.swoole_compression.swoole => Dockerfile.swoole_compression | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Dockerfile.swoole_compression.swoole => Dockerfile.swoole_compression (100%) diff --git a/Dockerfile.swoole_compression.swoole b/Dockerfile.swoole_compression similarity index 100% rename from Dockerfile.swoole_compression.swoole rename to Dockerfile.swoole_compression From 98e75b4fae665bbaba0f50179e1bc9de691c9f2b Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Thu, 17 Oct 2024 13:57:04 +0100 Subject: [PATCH 07/21] feat: add min size setting --- src/Http/Http.php | 10 ++++++++++ src/Http/Response.php | 20 +++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/Http/Http.php b/src/Http/Http.php index b9ed1f20..3749bd94 100755 --- a/src/Http/Http.php +++ b/src/Http/Http.php @@ -50,6 +50,7 @@ class Http extends Base protected string|null $responseClass = null; protected bool $compression = false; + protected int $minCompressionSize = 1024; /** * Http @@ -89,6 +90,14 @@ public function setCompression(bool $compression) $this->compression = $compression; } + /** + * Set minimum compression size + */ + public function setMinCompressionSize(int $minCompressionSize) + { + $this->minCompressionSize = $minCompressionSize; + } + /** * GET * @@ -330,6 +339,7 @@ public function start() if ($this->compression) { $response->setAcceptEncoding($request->getHeader('accept-encoding') ?? ''); + $response->setMinCompressionSize($this->minCompressionSize); } } diff --git a/src/Http/Response.php b/src/Http/Response.php index 1d553ab1..cae9cac9 100755 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -252,6 +252,11 @@ abstract class Response */ protected string $acceptEncoding = ''; + /** + * @var int + */ + protected int $minCompressionSize = 1024; + /** * Response constructor. * @@ -290,6 +295,19 @@ public function setAcceptEncoding(string $acceptEncoding): static return $this; } + /** + * Set min compression size + * + * Set minimum size for compression to be applied in bytes. + * + * @param int $minCompressionSize + */ + public function setMinCompressionSize(int $minCompressionSize): static + { + $this->minCompressionSize = $minCompressionSize; + return $this; + } + /** * Get content type * @@ -511,7 +529,7 @@ public function send(string $body = ''): void if ( !empty($this->acceptEncoding) && isset($this->compressed[$this->contentType]) && - strlen($body) > 1024 + strlen($body) > $this->minCompressionSize ) { $algorithm = Compression::fromAcceptEncoding($this->acceptEncoding, [ Compression::BROTLI, From 8c71521d2a6af9982dc64ef6d2507477ba9799e1 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Thu, 17 Oct 2024 13:59:46 +0100 Subject: [PATCH 08/21] feat: add const default --- src/Http/Http.php | 10 ++++++---- src/Http/Response.php | 10 +++++----- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/Http/Http.php b/src/Http/Http.php index 3749bd94..340d6d8b 100755 --- a/src/Http/Http.php +++ b/src/Http/Http.php @@ -6,6 +6,8 @@ use Utopia\DI\Dependency; use Utopia\Servers\Base; +const COMPRESSION_MIN_SIZE_DEFAULT = 1024; + class Http extends Base { /** @@ -50,7 +52,7 @@ class Http extends Base protected string|null $responseClass = null; protected bool $compression = false; - protected int $minCompressionSize = 1024; + protected int $compressionMinSize = COMPRESSION_MIN_SIZE_DEFAULT; /** * Http @@ -93,9 +95,9 @@ public function setCompression(bool $compression) /** * Set minimum compression size */ - public function setMinCompressionSize(int $minCompressionSize) + public function setcompressionMinSize(int $compressionMinSize) { - $this->minCompressionSize = $minCompressionSize; + $this->compressionMinSize = $compressionMinSize; } /** @@ -339,7 +341,7 @@ public function start() if ($this->compression) { $response->setAcceptEncoding($request->getHeader('accept-encoding') ?? ''); - $response->setMinCompressionSize($this->minCompressionSize); + $response->setcompressionMinSize($this->compressionMinSize); } } diff --git a/src/Http/Response.php b/src/Http/Response.php index cae9cac9..6b0e0e3e 100755 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -255,7 +255,7 @@ abstract class Response /** * @var int */ - protected int $minCompressionSize = 1024; + protected int $compressionMinSize = COMPRESSION_MIN_SIZE_DEFAULT; /** * Response constructor. @@ -300,11 +300,11 @@ public function setAcceptEncoding(string $acceptEncoding): static * * Set minimum size for compression to be applied in bytes. * - * @param int $minCompressionSize + * @param int $compressionMinSize */ - public function setMinCompressionSize(int $minCompressionSize): static + public function setcompressionMinSize(int $compressionMinSize): static { - $this->minCompressionSize = $minCompressionSize; + $this->compressionMinSize = $compressionMinSize; return $this; } @@ -529,7 +529,7 @@ public function send(string $body = ''): void if ( !empty($this->acceptEncoding) && isset($this->compressed[$this->contentType]) && - strlen($body) > $this->minCompressionSize + strlen($body) > $this->compressionMinSize ) { $algorithm = Compression::fromAcceptEncoding($this->acceptEncoding, [ Compression::BROTLI, From 69232cdcdeadac49ff926743e62abc6716bef43b Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Thu, 17 Oct 2024 14:17:34 +0100 Subject: [PATCH 09/21] chore: move constant to class --- src/Http/Http.php | 7 ++++--- src/Http/Response.php | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Http/Http.php b/src/Http/Http.php index 340d6d8b..d1782c0d 100755 --- a/src/Http/Http.php +++ b/src/Http/Http.php @@ -6,10 +6,10 @@ use Utopia\DI\Dependency; use Utopia\Servers\Base; -const COMPRESSION_MIN_SIZE_DEFAULT = 1024; - class Http extends Base { + public const COMPRESSION_MIN_SIZE_DEFAULT = 1024; + /** * Request method constants */ @@ -21,6 +21,7 @@ class Http extends Base public const REQUEST_METHOD_OPTIONS = 'OPTIONS'; public const REQUEST_METHOD_HEAD = 'HEAD'; + /** * @var Files */ @@ -52,7 +53,7 @@ class Http extends Base protected string|null $responseClass = null; protected bool $compression = false; - protected int $compressionMinSize = COMPRESSION_MIN_SIZE_DEFAULT; + protected int $compressionMinSize = Http::COMPRESSION_MIN_SIZE_DEFAULT; /** * Http diff --git a/src/Http/Response.php b/src/Http/Response.php index 6b0e0e3e..cf5e5475 100755 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -255,7 +255,7 @@ abstract class Response /** * @var int */ - protected int $compressionMinSize = COMPRESSION_MIN_SIZE_DEFAULT; + protected int $compressionMinSize = Http::COMPRESSION_MIN_SIZE_DEFAULT; /** * Response constructor. From a6b915a7d3b1fe82ed1431a30ba013fd3a8c428b Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 21 Oct 2024 10:33:07 +0100 Subject: [PATCH 10/21] chore: composer update --- composer.lock | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/composer.lock b/composer.lock index 78207535..e75181c1 100644 --- a/composer.lock +++ b/composer.lock @@ -12,16 +12,9 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/compression", - "reference": "a41c2c769ecaab5dc4b7efecf005457a11f29f87" + "reference": "cd5974ea5eb0126d31fea55bb631420a0565a838" }, "require": { - "ext-brotli": "*", - "ext-fileinfo": "*", - "ext-lz4": "*", - "ext-snappy": "*", - "ext-xz": "*", - "ext-zlib": "*", - "ext-zstd": "*", "php": ">=8.0" }, "require-dev": { @@ -59,7 +52,7 @@ "upf", "utopia" ], - "time": "2024-10-17T12:19:31+00:00" + "time": "2024-10-17T13:52:54+00:00" }, { "name": "utopia-php/di", @@ -893,16 +886,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.12.6", + "version": "1.12.7", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "dc4d2f145a88ea7141ae698effd64d9df46527ae" + "reference": "dc2b9976bd8b0f84ec9b0e50cc35378551de7af0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/dc4d2f145a88ea7141ae698effd64d9df46527ae", - "reference": "dc4d2f145a88ea7141ae698effd64d9df46527ae", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/dc2b9976bd8b0f84ec9b0e50cc35378551de7af0", + "reference": "dc2b9976bd8b0f84ec9b0e50cc35378551de7af0", "shasum": "" }, "require": { @@ -947,7 +940,7 @@ "type": "github" } ], - "time": "2024-10-06T15:03:59+00:00" + "time": "2024-10-18T11:12:07+00:00" }, { "name": "phpunit/php-code-coverage", From 7da4fb3f31051173272bb8759f7c33534028d7ce Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 23 Oct 2024 11:29:51 +0100 Subject: [PATCH 11/21] chore: bump utopia/compression --- composer.json | 2 +- composer.lock | 12 +++++------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index 540833d2..338a347b 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "php": ">=8.0", "ext-swoole": "*", "utopia-php/servers": "0.1.*", - "utopia-php/compression": "dev-feat-utopia-compression" + "utopia-php/compression": "0.1.*" }, "repositories": { "utopia-php/compression": { diff --git a/composer.lock b/composer.lock index e75181c1..75e06336 100644 --- a/composer.lock +++ b/composer.lock @@ -4,15 +4,15 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "2768a94b89de6af19ca17a6b2f056c83", + "content-hash": "a21c06ac7fd9c0de3c88317678e96685", "packages": [ { "name": "utopia-php/compression", - "version": "dev-feat-utopia-compression", + "version": "0.1.0", "source": { "type": "git", "url": "https://github.com/utopia-php/compression", - "reference": "cd5974ea5eb0126d31fea55bb631420a0565a838" + "reference": "8c6d9bcb5b0972faa27e5bf70923c20403aaf25c" }, "require": { "php": ">=8.0" @@ -52,7 +52,7 @@ "upf", "utopia" ], - "time": "2024-10-17T13:52:54+00:00" + "time": "2024-10-23T10:17:46+00:00" }, { "name": "utopia-php/di", @@ -3593,9 +3593,7 @@ ], "aliases": [], "minimum-stability": "stable", - "stability-flags": { - "utopia-php/compression": 20 - }, + "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, "platform": { From a5ae37bfa153f10f95cad8744ab258982b3a302c Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 23 Oct 2024 11:44:39 +0100 Subject: [PATCH 12/21] fix: typo --- src/Http/Http.php | 4 ++-- src/Http/Response.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Http/Http.php b/src/Http/Http.php index d1782c0d..92d5b5e0 100755 --- a/src/Http/Http.php +++ b/src/Http/Http.php @@ -96,7 +96,7 @@ public function setCompression(bool $compression) /** * Set minimum compression size */ - public function setcompressionMinSize(int $compressionMinSize) + public function setCompressionMinSize(int $compressionMinSize) { $this->compressionMinSize = $compressionMinSize; } @@ -342,7 +342,7 @@ public function start() if ($this->compression) { $response->setAcceptEncoding($request->getHeader('accept-encoding') ?? ''); - $response->setcompressionMinSize($this->compressionMinSize); + $response->setCompressionMinSize($this->compressionMinSize); } } diff --git a/src/Http/Response.php b/src/Http/Response.php index cf5e5475..0f2cab39 100755 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -302,7 +302,7 @@ public function setAcceptEncoding(string $acceptEncoding): static * * @param int $compressionMinSize */ - public function setcompressionMinSize(int $compressionMinSize): static + public function setCompressionMinSize(int $compressionMinSize): static { $this->compressionMinSize = $compressionMinSize; return $this; From dedaf7a5fd6cc75148ab52d359c5b54575afea10 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 23 Oct 2024 11:46:36 +0100 Subject: [PATCH 13/21] fix: spacing --- src/Http/Http.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Http/Http.php b/src/Http/Http.php index 92d5b5e0..2f5d5c21 100755 --- a/src/Http/Http.php +++ b/src/Http/Http.php @@ -21,7 +21,6 @@ class Http extends Base public const REQUEST_METHOD_OPTIONS = 'OPTIONS'; public const REQUEST_METHOD_HEAD = 'HEAD'; - /** * @var Files */ @@ -52,6 +51,9 @@ class Http extends Base protected string|null $requestClass = null; protected string|null $responseClass = null; + /** + * Compression + */ protected bool $compression = false; protected int $compressionMinSize = Http::COMPRESSION_MIN_SIZE_DEFAULT; From fefa0231da15c6acc3107a6322fbda3881d1d360 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Tue, 29 Oct 2024 14:13:14 +0000 Subject: [PATCH 14/21] fix: composer --- composer.json | 6 --- composer.lock | 102 +++++++++++++++++++++++++------------------------- 2 files changed, 50 insertions(+), 58 deletions(-) diff --git a/composer.json b/composer.json index 338a347b..04f15e98 100644 --- a/composer.json +++ b/composer.json @@ -33,12 +33,6 @@ "utopia-php/servers": "0.1.*", "utopia-php/compression": "0.1.*" }, - "repositories": { - "utopia-php/compression": { - "type": "git", - "url": "https://github.com/utopia-php/compression" - } - }, "require-dev": { "ext-xdebug": "*", "phpunit/phpunit": "^9.5.25", diff --git a/composer.lock b/composer.lock index 75e06336..fa31c729 100644 --- a/composer.lock +++ b/composer.lock @@ -4,16 +4,22 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "a21c06ac7fd9c0de3c88317678e96685", + "content-hash": "d32afe850b2efb8780a47961dd5ff634", "packages": [ { "name": "utopia-php/compression", "version": "0.1.0", "source": { "type": "git", - "url": "https://github.com/utopia-php/compression", + "url": "https://github.com/utopia-php/compression.git", "reference": "8c6d9bcb5b0972faa27e5bf70923c20403aaf25c" }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/utopia-php/compression/zipball/8c6d9bcb5b0972faa27e5bf70923c20403aaf25c", + "reference": "8c6d9bcb5b0972faa27e5bf70923c20403aaf25c", + "shasum": "" + }, "require": { "php": ">=8.0" }, @@ -28,19 +34,7 @@ "Utopia\\Compression\\": "src/Compression" } }, - "autoload-dev": { - "psr-4": { - "Utopia\\Tests\\Compression\\": "tests/Compression" - } - }, - "scripts": { - "lint": [ - "./vendor/bin/pint --test" - ], - "format": [ - "./vendor/bin/pint" - ] - }, + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -52,6 +46,10 @@ "upf", "utopia" ], + "support": { + "issues": "https://github.com/utopia-php/compression/issues", + "source": "https://github.com/utopia-php/compression/tree/0.1.0" + }, "time": "2024-10-23T10:17:46+00:00" }, { @@ -2587,16 +2585,16 @@ }, { "name": "symfony/console", - "version": "v7.1.5", + "version": "v7.1.6", "source": { "type": "git", "url": "https://github.com/symfony/console.git", - "reference": "0fa539d12b3ccf068a722bbbffa07ca7079af9ee" + "reference": "bb5192af6edc797cbab5c8e8ecfea2fe5f421e57" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/console/zipball/0fa539d12b3ccf068a722bbbffa07ca7079af9ee", - "reference": "0fa539d12b3ccf068a722bbbffa07ca7079af9ee", + "url": "https://api.github.com/repos/symfony/console/zipball/bb5192af6edc797cbab5c8e8ecfea2fe5f421e57", + "reference": "bb5192af6edc797cbab5c8e8ecfea2fe5f421e57", "shasum": "" }, "require": { @@ -2660,7 +2658,7 @@ "terminal" ], "support": { - "source": "https://github.com/symfony/console/tree/v7.1.5" + "source": "https://github.com/symfony/console/tree/v7.1.6" }, "funding": [ { @@ -2676,7 +2674,7 @@ "type": "tidelift" } ], - "time": "2024-09-20T08:28:38+00:00" + "time": "2024-10-09T08:46:59+00:00" }, { "name": "symfony/deprecation-contracts", @@ -2747,16 +2745,16 @@ }, { "name": "symfony/filesystem", - "version": "v7.1.5", + "version": "v7.1.6", "source": { "type": "git", "url": "https://github.com/symfony/filesystem.git", - "reference": "61fe0566189bf32e8cfee78335d8776f64a66f5a" + "reference": "c835867b3c62bb05c7fe3d637c871c7ae52024d4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/filesystem/zipball/61fe0566189bf32e8cfee78335d8776f64a66f5a", - "reference": "61fe0566189bf32e8cfee78335d8776f64a66f5a", + "url": "https://api.github.com/repos/symfony/filesystem/zipball/c835867b3c62bb05c7fe3d637c871c7ae52024d4", + "reference": "c835867b3c62bb05c7fe3d637c871c7ae52024d4", "shasum": "" }, "require": { @@ -2793,7 +2791,7 @@ "description": "Provides basic utilities for the filesystem", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/filesystem/tree/v7.1.5" + "source": "https://github.com/symfony/filesystem/tree/v7.1.6" }, "funding": [ { @@ -2809,20 +2807,20 @@ "type": "tidelift" } ], - "time": "2024-09-17T09:16:35+00:00" + "time": "2024-10-25T15:11:02+00:00" }, { "name": "symfony/finder", - "version": "v7.1.4", + "version": "v7.1.6", "source": { "type": "git", "url": "https://github.com/symfony/finder.git", - "reference": "d95bbf319f7d052082fb7af147e0f835a695e823" + "reference": "2cb89664897be33f78c65d3d2845954c8d7a43b8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/finder/zipball/d95bbf319f7d052082fb7af147e0f835a695e823", - "reference": "d95bbf319f7d052082fb7af147e0f835a695e823", + "url": "https://api.github.com/repos/symfony/finder/zipball/2cb89664897be33f78c65d3d2845954c8d7a43b8", + "reference": "2cb89664897be33f78c65d3d2845954c8d7a43b8", "shasum": "" }, "require": { @@ -2857,7 +2855,7 @@ "description": "Finds files and directories via an intuitive fluent interface", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/finder/tree/v7.1.4" + "source": "https://github.com/symfony/finder/tree/v7.1.6" }, "funding": [ { @@ -2873,20 +2871,20 @@ "type": "tidelift" } ], - "time": "2024-08-13T14:28:19+00:00" + "time": "2024-10-01T08:31:23+00:00" }, { "name": "symfony/options-resolver", - "version": "v7.1.1", + "version": "v7.1.6", "source": { "type": "git", "url": "https://github.com/symfony/options-resolver.git", - "reference": "47aa818121ed3950acd2b58d1d37d08a94f9bf55" + "reference": "85e95eeede2d41cd146146e98c9c81d9214cae85" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/options-resolver/zipball/47aa818121ed3950acd2b58d1d37d08a94f9bf55", - "reference": "47aa818121ed3950acd2b58d1d37d08a94f9bf55", + "url": "https://api.github.com/repos/symfony/options-resolver/zipball/85e95eeede2d41cd146146e98c9c81d9214cae85", + "reference": "85e95eeede2d41cd146146e98c9c81d9214cae85", "shasum": "" }, "require": { @@ -2924,7 +2922,7 @@ "options" ], "support": { - "source": "https://github.com/symfony/options-resolver/tree/v7.1.1" + "source": "https://github.com/symfony/options-resolver/tree/v7.1.6" }, "funding": [ { @@ -2940,7 +2938,7 @@ "type": "tidelift" } ], - "time": "2024-05-31T14:57:53+00:00" + "time": "2024-09-25T14:20:29+00:00" }, { "name": "symfony/polyfill-ctype", @@ -3262,16 +3260,16 @@ }, { "name": "symfony/process", - "version": "v7.1.5", + "version": "v7.1.6", "source": { "type": "git", "url": "https://github.com/symfony/process.git", - "reference": "5c03ee6369281177f07f7c68252a280beccba847" + "reference": "6aaa189ddb4ff6b5de8fa3210f2fb42c87b4d12e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/process/zipball/5c03ee6369281177f07f7c68252a280beccba847", - "reference": "5c03ee6369281177f07f7c68252a280beccba847", + "url": "https://api.github.com/repos/symfony/process/zipball/6aaa189ddb4ff6b5de8fa3210f2fb42c87b4d12e", + "reference": "6aaa189ddb4ff6b5de8fa3210f2fb42c87b4d12e", "shasum": "" }, "require": { @@ -3303,7 +3301,7 @@ "description": "Executes commands in sub-processes", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/process/tree/v7.1.5" + "source": "https://github.com/symfony/process/tree/v7.1.6" }, "funding": [ { @@ -3319,7 +3317,7 @@ "type": "tidelift" } ], - "time": "2024-09-19T21:48:23+00:00" + "time": "2024-09-25T14:20:29+00:00" }, { "name": "symfony/service-contracts", @@ -3406,16 +3404,16 @@ }, { "name": "symfony/string", - "version": "v7.1.5", + "version": "v7.1.6", "source": { "type": "git", "url": "https://github.com/symfony/string.git", - "reference": "d66f9c343fa894ec2037cc928381df90a7ad4306" + "reference": "61b72d66bf96c360a727ae6232df5ac83c71f626" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/string/zipball/d66f9c343fa894ec2037cc928381df90a7ad4306", - "reference": "d66f9c343fa894ec2037cc928381df90a7ad4306", + "url": "https://api.github.com/repos/symfony/string/zipball/61b72d66bf96c360a727ae6232df5ac83c71f626", + "reference": "61b72d66bf96c360a727ae6232df5ac83c71f626", "shasum": "" }, "require": { @@ -3473,7 +3471,7 @@ "utf8" ], "support": { - "source": "https://github.com/symfony/string/tree/v7.1.5" + "source": "https://github.com/symfony/string/tree/v7.1.6" }, "funding": [ { @@ -3489,7 +3487,7 @@ "type": "tidelift" } ], - "time": "2024-09-20T08:28:38+00:00" + "time": "2024-09-25T14:20:29+00:00" }, { "name": "theseer/tokenizer", @@ -3593,7 +3591,7 @@ ], "aliases": [], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": {}, "prefer-stable": false, "prefer-lowest": false, "platform": { From 83613aca7e395493f25c9510fb884e0999f195d9 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Thu, 31 Oct 2024 11:34:53 +0000 Subject: [PATCH 15/21] feat: supported compression --- src/Http/Http.php | 12 +++++++++++- src/Http/Response.php | 20 +++++++++++++++----- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/Http/Http.php b/src/Http/Http.php index 2f5d5c21..3d93b9ea 100755 --- a/src/Http/Http.php +++ b/src/Http/Http.php @@ -56,6 +56,7 @@ class Http extends Base */ protected bool $compression = false; protected int $compressionMinSize = Http::COMPRESSION_MIN_SIZE_DEFAULT; + protected mixed $compressionSupported; /** * Http @@ -88,7 +89,7 @@ public function setRequestClass(string $requestClass) } /** - * Set Compression + * Set compression */ public function setCompression(bool $compression) { @@ -103,6 +104,14 @@ public function setCompressionMinSize(int $compressionMinSize) $this->compressionMinSize = $compressionMinSize; } + /** + * Set supported compression algorithms + */ + public function setCompressionSupported(mixed $compressionSupported) + { + $this->compressionSupported = $compressionSupported; + } + /** * GET * @@ -345,6 +354,7 @@ public function start() if ($this->compression) { $response->setAcceptEncoding($request->getHeader('accept-encoding') ?? ''); $response->setCompressionMinSize($this->compressionMinSize); + $response->setCompressionSupported($this->compressionSupported); } } diff --git a/src/Http/Response.php b/src/Http/Response.php index 0f2cab39..c314d1e8 100755 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -257,6 +257,11 @@ abstract class Response */ protected int $compressionMinSize = Http::COMPRESSION_MIN_SIZE_DEFAULT; + /** + * @var mixed + */ + protected mixed $compressionSupported; + /** * Response constructor. * @@ -308,6 +313,15 @@ public function setCompressionMinSize(int $compressionMinSize): static return $this; } + /**s + * Set supported compression algorithms + */ + public function setCompressionSupported(mixed $compressionSupported): static + { + $this->compressionSupported = $compressionSupported; + return $this; + } + /** * Get content type * @@ -531,11 +545,7 @@ public function send(string $body = ''): void isset($this->compressed[$this->contentType]) && strlen($body) > $this->compressionMinSize ) { - $algorithm = Compression::fromAcceptEncoding($this->acceptEncoding, [ - Compression::BROTLI, - Compression::GZIP, - Compression::DEFLATE, - ]); + $algorithm = Compression::fromAcceptEncoding($this->acceptEncoding, $this->compressionSupported); if ($algorithm) { $body = $algorithm->compress($body); From 604538751fd3027acdae9eac4e7e6bdbc6a46c0a Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 1 Nov 2024 17:06:18 +0000 Subject: [PATCH 16/21] chore: define compressionSupported --- src/Http/Http.php | 2 +- src/Http/Response.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Http/Http.php b/src/Http/Http.php index 3d93b9ea..8ff0abe7 100755 --- a/src/Http/Http.php +++ b/src/Http/Http.php @@ -56,7 +56,7 @@ class Http extends Base */ protected bool $compression = false; protected int $compressionMinSize = Http::COMPRESSION_MIN_SIZE_DEFAULT; - protected mixed $compressionSupported; + protected mixed $compressionSupported = []; /** * Http diff --git a/src/Http/Response.php b/src/Http/Response.php index c314d1e8..e926a84f 100755 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -260,7 +260,7 @@ abstract class Response /** * @var mixed */ - protected mixed $compressionSupported; + protected mixed $compressionSupported = []; /** * Response constructor. From d91c598ac19b232e6967ed8ff6fe655592b8c69d Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 8 Nov 2024 13:13:00 +0100 Subject: [PATCH 17/21] fix: send headers first --- src/Http/Response.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Http/Response.php b/src/Http/Response.php index e926a84f..741c17ce 100755 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -525,19 +525,11 @@ public function send(string $body = ''): void return; } - $this->sent = true; - $serverHeader = $this->headers['Server'] ?? 'Utopia/Http'; $this->addHeader('Server', $serverHeader); $this->addHeader('X-Debug-Speed', (string) (microtime(true) - $this->startTime)); - $this->appendCookies()->appendHeaders(); - - // Send response - if ($this->disablePayload) { - $this->end(); - return; - } + $this->appendCookies(); // Compress body if ( @@ -554,6 +546,14 @@ public function send(string $body = ''): void } } + $this->appendHeaders(); + + // Send response + if ($this->disablePayload) { + $this->end(); + return; + } + $headerSize = strlen(implode("\n", $this->headers)); $bodyLength = strlen($body); $this->size += $headerSize + $bodyLength; From 74f062784f7a0b2dc220063e4d7987d300a76cc2 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 8 Nov 2024 13:25:36 +0100 Subject: [PATCH 18/21] chore: composer update --- composer.lock | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/composer.lock b/composer.lock index fa31c729..16a5853c 100644 --- a/composer.lock +++ b/composer.lock @@ -8,16 +8,16 @@ "packages": [ { "name": "utopia-php/compression", - "version": "0.1.0", + "version": "0.1.1", "source": { "type": "git", "url": "https://github.com/utopia-php/compression.git", - "reference": "8c6d9bcb5b0972faa27e5bf70923c20403aaf25c" + "reference": "2ac5709e39823dbccb9fa66099ccebe809078c2e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/compression/zipball/8c6d9bcb5b0972faa27e5bf70923c20403aaf25c", - "reference": "8c6d9bcb5b0972faa27e5bf70923c20403aaf25c", + "url": "https://api.github.com/repos/utopia-php/compression/zipball/2ac5709e39823dbccb9fa66099ccebe809078c2e", + "reference": "2ac5709e39823dbccb9fa66099ccebe809078c2e", "shasum": "" }, "require": { @@ -48,9 +48,9 @@ ], "support": { "issues": "https://github.com/utopia-php/compression/issues", - "source": "https://github.com/utopia-php/compression/tree/0.1.0" + "source": "https://github.com/utopia-php/compression/tree/0.1.1" }, - "time": "2024-10-23T10:17:46+00:00" + "time": "2024-11-08T12:22:15+00:00" }, { "name": "utopia-php/di", @@ -884,16 +884,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.12.7", + "version": "1.12.8", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "dc2b9976bd8b0f84ec9b0e50cc35378551de7af0" + "reference": "f6a60a4d66142b8156c9da923f1972657bc4748c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/dc2b9976bd8b0f84ec9b0e50cc35378551de7af0", - "reference": "dc2b9976bd8b0f84ec9b0e50cc35378551de7af0", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/f6a60a4d66142b8156c9da923f1972657bc4748c", + "reference": "f6a60a4d66142b8156c9da923f1972657bc4748c", "shasum": "" }, "require": { @@ -938,7 +938,7 @@ "type": "github" } ], - "time": "2024-10-18T11:12:07+00:00" + "time": "2024-11-06T19:06:49+00:00" }, { "name": "phpunit/php-code-coverage", @@ -2585,16 +2585,16 @@ }, { "name": "symfony/console", - "version": "v7.1.6", + "version": "v7.1.7", "source": { "type": "git", "url": "https://github.com/symfony/console.git", - "reference": "bb5192af6edc797cbab5c8e8ecfea2fe5f421e57" + "reference": "3284aafcac338b6e86fd955ee4d794cbe434151a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/console/zipball/bb5192af6edc797cbab5c8e8ecfea2fe5f421e57", - "reference": "bb5192af6edc797cbab5c8e8ecfea2fe5f421e57", + "url": "https://api.github.com/repos/symfony/console/zipball/3284aafcac338b6e86fd955ee4d794cbe434151a", + "reference": "3284aafcac338b6e86fd955ee4d794cbe434151a", "shasum": "" }, "require": { @@ -2658,7 +2658,7 @@ "terminal" ], "support": { - "source": "https://github.com/symfony/console/tree/v7.1.6" + "source": "https://github.com/symfony/console/tree/v7.1.7" }, "funding": [ { @@ -2674,7 +2674,7 @@ "type": "tidelift" } ], - "time": "2024-10-09T08:46:59+00:00" + "time": "2024-11-05T15:34:55+00:00" }, { "name": "symfony/deprecation-contracts", @@ -3260,16 +3260,16 @@ }, { "name": "symfony/process", - "version": "v7.1.6", + "version": "v7.1.7", "source": { "type": "git", "url": "https://github.com/symfony/process.git", - "reference": "6aaa189ddb4ff6b5de8fa3210f2fb42c87b4d12e" + "reference": "9b8a40b7289767aa7117e957573c2a535efe6585" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/process/zipball/6aaa189ddb4ff6b5de8fa3210f2fb42c87b4d12e", - "reference": "6aaa189ddb4ff6b5de8fa3210f2fb42c87b4d12e", + "url": "https://api.github.com/repos/symfony/process/zipball/9b8a40b7289767aa7117e957573c2a535efe6585", + "reference": "9b8a40b7289767aa7117e957573c2a535efe6585", "shasum": "" }, "require": { @@ -3301,7 +3301,7 @@ "description": "Executes commands in sub-processes", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/process/tree/v7.1.6" + "source": "https://github.com/symfony/process/tree/v7.1.7" }, "funding": [ { @@ -3317,7 +3317,7 @@ "type": "tidelift" } ], - "time": "2024-09-25T14:20:29+00:00" + "time": "2024-11-06T09:25:12+00:00" }, { "name": "symfony/service-contracts", From 0c26c3eb1228ce5bef3e9ae8f61acde650e28884 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 8 Nov 2024 16:01:18 +0100 Subject: [PATCH 19/21] chore: composer update --- composer.lock | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/composer.lock b/composer.lock index 16a5853c..e60a1052 100644 --- a/composer.lock +++ b/composer.lock @@ -8,16 +8,16 @@ "packages": [ { "name": "utopia-php/compression", - "version": "0.1.1", + "version": "0.1.2", "source": { "type": "git", "url": "https://github.com/utopia-php/compression.git", - "reference": "2ac5709e39823dbccb9fa66099ccebe809078c2e" + "reference": "6062f70596415f8d5de40a589367b0eb2a435f98" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/compression/zipball/2ac5709e39823dbccb9fa66099ccebe809078c2e", - "reference": "2ac5709e39823dbccb9fa66099ccebe809078c2e", + "url": "https://api.github.com/repos/utopia-php/compression/zipball/6062f70596415f8d5de40a589367b0eb2a435f98", + "reference": "6062f70596415f8d5de40a589367b0eb2a435f98", "shasum": "" }, "require": { @@ -48,9 +48,9 @@ ], "support": { "issues": "https://github.com/utopia-php/compression/issues", - "source": "https://github.com/utopia-php/compression/tree/0.1.1" + "source": "https://github.com/utopia-php/compression/tree/0.1.2" }, - "time": "2024-11-08T12:22:15+00:00" + "time": "2024-11-08T14:59:54+00:00" }, { "name": "utopia-php/di", From 9e2b284b783dcdffcfeab49722cbd2dae3c0cb71 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 8 Nov 2024 19:30:20 +0100 Subject: [PATCH 20/21] feat: add header --- src/Http/Response.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Http/Response.php b/src/Http/Response.php index 741c17ce..72e3b69b 100755 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -527,7 +527,6 @@ public function send(string $body = ''): void $serverHeader = $this->headers['Server'] ?? 'Utopia/Http'; $this->addHeader('Server', $serverHeader); - $this->addHeader('X-Debug-Speed', (string) (microtime(true) - $this->startTime)); $this->appendCookies(); @@ -542,10 +541,12 @@ public function send(string $body = ''): void if ($algorithm) { $body = $algorithm->compress($body); $this->addHeader('Content-Encoding', $algorithm->getContentEncoding()); + $this->addHeader('X-Utopia-Compression', '1'); $this->addHeader('Vary', 'Accept-Encoding'); } } + $this->addHeader('X-Debug-Speed', (string) (microtime(true) - $this->startTime)); $this->appendHeaders(); // Send response From d999718a6f5555e5579222c5181e494ca275c778 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 8 Nov 2024 19:44:58 +0100 Subject: [PATCH 21/21] chore: update to true --- src/Http/Response.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Http/Response.php b/src/Http/Response.php index 72e3b69b..60c32b1c 100755 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -541,7 +541,7 @@ public function send(string $body = ''): void if ($algorithm) { $body = $algorithm->compress($body); $this->addHeader('Content-Encoding', $algorithm->getContentEncoding()); - $this->addHeader('X-Utopia-Compression', '1'); + $this->addHeader('X-Utopia-Compression', 'true'); $this->addHeader('Vary', 'Accept-Encoding'); } }