From ee470bed63024845576b830e69d6030a2f16dda0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Thu, 22 Oct 2020 14:06:33 +0300 Subject: [PATCH 1/3] Add support for compressing post payload --- src/Saver/UploadSaver.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Saver/UploadSaver.php b/src/Saver/UploadSaver.php index 38fbbda..2ff5f71 100644 --- a/src/Saver/UploadSaver.php +++ b/src/Saver/UploadSaver.php @@ -10,8 +10,10 @@ class UploadSaver implements SaverInterface private $url; /** @var int */ private $timeout; + /** @var bool */ + private $compress; - public function __construct($url, $token, $timeout) + public function __construct($url, $token, $timeout, $compress = false) { $this->url = $url; if ($token) { @@ -19,6 +21,7 @@ public function __construct($url, $token, $timeout) } $this->timeout = $timeout; + $this->compress = $compress; } public function isSupported() @@ -29,7 +32,7 @@ public function isSupported() public function save(array $data) { $json = json_encode($data); - $this->submit($this->url, $json); + $this->submit($this->url, $json, $this->compress); return true; } @@ -37,8 +40,9 @@ public function save(array $data) /** * @param string $url * @param string $payload + * @param bool $compress */ - private function submit($url, $payload) + private function submit($url, $payload, $compress) { $ch = curl_init($url); if (!$ch) { @@ -49,13 +53,13 @@ private function submit($url, $payload) // Prefer to receive JSON back 'Accept: application/json', // The sent data is JSON - 'Content-Type: application/json', + $compress ? 'Content-Type: application/json+gzip' : 'Content-Type: application/json', ); $res = curl_setopt_array($ch, array( CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_RETURNTRANSFER => true, - CURLOPT_POSTFIELDS => $payload, + CURLOPT_POSTFIELDS => $compress ? gzencode($payload) : $payload, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_HTTPHEADER => $headers, CURLOPT_TIMEOUT => $this->timeout, From efb6b7ca1c51a12c9cddd1d7a5f16f36d33f5ab7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Thu, 22 Oct 2020 14:13:23 +0300 Subject: [PATCH 2/3] Do not enable compression if not supported --- src/Saver/UploadSaver.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Saver/UploadSaver.php b/src/Saver/UploadSaver.php index 2ff5f71..188e28b 100644 --- a/src/Saver/UploadSaver.php +++ b/src/Saver/UploadSaver.php @@ -32,7 +32,7 @@ public function isSupported() public function save(array $data) { $json = json_encode($data); - $this->submit($this->url, $json, $this->compress); + $this->submit($this->url, $json, $this->hasCompression()); return true; } @@ -84,4 +84,12 @@ private function submit($url, $payload, $compress) throw new ProfilerException($message); } } + + /** + * @return bool + */ + private function hasCompression() + { + return $this->compress && function_exists('gzencode'); + } } From 3729e1e3dc7273aabe6c091f87a5772216c815e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Thu, 22 Oct 2020 14:13:51 +0300 Subject: [PATCH 3/3] Support compress option from config --- README.md | 2 ++ src/Saver/UploadSaver.php | 2 +- src/SaverFactory.php | 3 ++- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bfaed64..2239c10 100644 --- a/README.md +++ b/README.md @@ -169,6 +169,8 @@ Example config: 'timeout' => 3, // the token must match 'upload.token' config in XHGui 'token' => 'token', + // whether to gzip compress the payload + 'compress' => true, ), ``` diff --git a/src/Saver/UploadSaver.php b/src/Saver/UploadSaver.php index 188e28b..f05743d 100644 --- a/src/Saver/UploadSaver.php +++ b/src/Saver/UploadSaver.php @@ -13,7 +13,7 @@ class UploadSaver implements SaverInterface /** @var bool */ private $compress; - public function __construct($url, $token, $timeout, $compress = false) + public function __construct($url, $token, $timeout, $compress) { $this->url = $url; if ($token) { diff --git a/src/SaverFactory.php b/src/SaverFactory.php index 9a0dd6f..5d22b11 100644 --- a/src/SaverFactory.php +++ b/src/SaverFactory.php @@ -32,10 +32,11 @@ public static function create($saveHandler, array $config = array()) 'url' => null, 'token' => null, 'timeout' => 3, + 'compress' => false, ); $userConfig = isset($config['save.handler.upload']) && is_array($config['save.handler.upload']) ? $config['save.handler.upload'] : array(); $saverConfig = array_merge($defaultConfig, $userConfig); - $saver = new Saver\UploadSaver($saverConfig['url'] ?: $saverConfig['uri'], $saverConfig['token'], $saverConfig['timeout']); + $saver = new Saver\UploadSaver($saverConfig['url'] ?: $saverConfig['uri'], $saverConfig['token'], $saverConfig['timeout'], $saverConfig['compress']); break; case Profiler::SAVER_STACK: