From cf3f813c0d535cdbad915114f52fd70cc489d68d Mon Sep 17 00:00:00 2001 From: Alexis LAURIER Date: Mon, 22 Mar 2021 09:28:57 +0100 Subject: [PATCH 1/5] rest api - manage data compression --- htdocs/api/index.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/htdocs/api/index.php b/htdocs/api/index.php index bb74144a40d..e651cd41ee3 100644 --- a/htdocs/api/index.php +++ b/htdocs/api/index.php @@ -310,9 +310,24 @@ if (!empty($reg[1]) && ($reg[1] != 'explorer' || ($reg[2] != '/swagger.json' && // Call API (we suppose we found it). // The handle will use the file api/temp/routes.php to get data to run the API. If the file exists and the entry for API is not found, it will return 404. -//Luracast\Restler\Defaults::$returnResponse = true; +Luracast\Restler\Defaults::$returnResponse = true; //print $api->r->handle(); -$api->r->handle(); +$result = $api->r->handle(); +if(isset($_SERVER['HTTP_ACCEPT_ENCODING'])) { + if(strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'br') !== false && is_callable('brotli_compress')) { + header('Content-Encoding: br'); + $result = brotli_compress($result, 11, BROTLI_TEXT); + } + elseif(strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'bz') !== false && is_callable('bzcompress')) { + header('Content-Encoding: bz'); + $result = bzcompress($result, 9); + } + elseif(strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false && is_callable('gzencode')) { + header('Content-Encoding: gzip'); + $result = gzencode($result, 9); + } +} +echo $result; //session_destroy(); From bcb326567809ba7031c29250c2107c3ddfbcb87b Mon Sep 17 00:00:00 2001 From: Alexis LAURIER Date: Mon, 22 Mar 2021 09:37:25 +0100 Subject: [PATCH 2/5] rest api - manage data compression --- htdocs/api/index.php | 1 + 1 file changed, 1 insertion(+) diff --git a/htdocs/api/index.php b/htdocs/api/index.php index e651cd41ee3..abed66f138a 100644 --- a/htdocs/api/index.php +++ b/htdocs/api/index.php @@ -2,6 +2,7 @@ /* Copyright (C) 2015 Jean-François Ferry * Copyright (C) 2016 Laurent Destailleur * Copyright (C) 2017 Regis Houssin + * Copyright (C) 2021 Alexis LAURIER * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by From 5463a705c76a0c9ce4ae5292e886899d5643d0e0 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Mon, 22 Mar 2021 08:40:06 +0000 Subject: [PATCH 3/5] Fixing style errors. --- htdocs/api/index.php | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/htdocs/api/index.php b/htdocs/api/index.php index abed66f138a..aad5232043a 100644 --- a/htdocs/api/index.php +++ b/htdocs/api/index.php @@ -315,19 +315,17 @@ Luracast\Restler\Defaults::$returnResponse = true; //print $api->r->handle(); $result = $api->r->handle(); -if(isset($_SERVER['HTTP_ACCEPT_ENCODING'])) { - if(strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'br') !== false && is_callable('brotli_compress')) { - header('Content-Encoding: br'); - $result = brotli_compress($result, 11, BROTLI_TEXT); - } - elseif(strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'bz') !== false && is_callable('bzcompress')) { - header('Content-Encoding: bz'); - $result = bzcompress($result, 9); - } - elseif(strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false && is_callable('gzencode')) { - header('Content-Encoding: gzip'); - $result = gzencode($result, 9); - } +if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) { + if (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'br') !== false && is_callable('brotli_compress')) { + header('Content-Encoding: br'); + $result = brotli_compress($result, 11, BROTLI_TEXT); + } elseif (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'bz') !== false && is_callable('bzcompress')) { + header('Content-Encoding: bz'); + $result = bzcompress($result, 9); + } elseif (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false && is_callable('gzencode')) { + header('Content-Encoding: gzip'); + $result = gzencode($result, 9); + } } echo $result; From 69d57f48fd20c0bb6d6ec59581523d9731cf2c83 Mon Sep 17 00:00:00 2001 From: Alexis LAURIER Date: Thu, 25 Mar 2021 09:20:31 +0100 Subject: [PATCH 4/5] api compression - add API_DISABLE_COMPRESSION --- htdocs/api/index.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/htdocs/api/index.php b/htdocs/api/index.php index aad5232043a..739db3e465d 100644 --- a/htdocs/api/index.php +++ b/htdocs/api/index.php @@ -308,14 +308,14 @@ if (!empty($reg[1]) && ($reg[1] != 'explorer' || ($reg[2] != '/swagger.json' && //var_dump($api->r->apiVersionMap); //exit; +//We do not want that restler return data if we want to compress it +Luracast\Restler\Defaults::$returnResponse = empty($conf->global->API_DISABLE_COMPRESSION); + // Call API (we suppose we found it). // The handle will use the file api/temp/routes.php to get data to run the API. If the file exists and the entry for API is not found, it will return 404. - -Luracast\Restler\Defaults::$returnResponse = true; -//print $api->r->handle(); - $result = $api->r->handle(); -if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) { +if (empty($conf->global->API_DISABLE_COMPRESSION) && isset($_SERVER['HTTP_ACCEPT_ENCODING'])) { + //We try to compress data if (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'br') !== false && is_callable('brotli_compress')) { header('Content-Encoding: br'); $result = brotli_compress($result, 11, BROTLI_TEXT); @@ -326,7 +326,12 @@ if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) { header('Content-Encoding: gzip'); $result = gzencode($result, 9); } + +} + +if(Luracast\Restler\Defaults::$returnResponse) { + //Restler did not output data,we return it + echo $result; } -echo $result; //session_destroy(); From 94b98403aba7cf7aa7733496de090af9f870fc9b Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Thu, 25 Mar 2021 12:52:35 +0100 Subject: [PATCH 5/5] Update index.php --- htdocs/api/index.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/htdocs/api/index.php b/htdocs/api/index.php index 739db3e465d..2ed7d2e08f8 100644 --- a/htdocs/api/index.php +++ b/htdocs/api/index.php @@ -308,14 +308,15 @@ if (!empty($reg[1]) && ($reg[1] != 'explorer' || ($reg[2] != '/swagger.json' && //var_dump($api->r->apiVersionMap); //exit; -//We do not want that restler return data if we want to compress it -Luracast\Restler\Defaults::$returnResponse = empty($conf->global->API_DISABLE_COMPRESSION); +// We do not want that restler output data if we use native compression (default behaviour) but we want to have it returned into a string. +Luracast\Restler\Defaults::$returnResponse = (empty($conf->global->API_DISABLE_COMPRESSION) && !empty($_SERVER['HTTP_ACCEPT_ENCODING'])); // Call API (we suppose we found it). // The handle will use the file api/temp/routes.php to get data to run the API. If the file exists and the entry for API is not found, it will return 404. $result = $api->r->handle(); -if (empty($conf->global->API_DISABLE_COMPRESSION) && isset($_SERVER['HTTP_ACCEPT_ENCODING'])) { - //We try to compress data + +if (Luracast\Restler\Defaults::$returnResponse) { + // We try to compress data if (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'br') !== false && is_callable('brotli_compress')) { header('Content-Encoding: br'); $result = brotli_compress($result, 11, BROTLI_TEXT); @@ -327,10 +328,7 @@ if (empty($conf->global->API_DISABLE_COMPRESSION) && isset($_SERVER['HTTP_ACCEPT $result = gzencode($result, 9); } -} - -if(Luracast\Restler\Defaults::$returnResponse) { - //Restler did not output data,we return it + // Restler did not output data yet, we return it now echo $result; }