Browse Source

dev 2020-11

Max F 5 years ago
parent
commit
43580b8b47

+ 28 - 8
src/App/Middlewares/GzipEncoding.php

@@ -14,21 +14,41 @@ class GzipEncoding
 		$response = $next($request, $response);
 
 
-        $content_type = $response->getContentType();
-        $content_type_short = explode(';', $content_type)[0];
+		$content_type = $response->getContentType();
+		$content_type_short = explode(';', $content_type)[0];
+
+		$contents_types = [
+			'text/html',
+			'text/plain',
+			'text/xml',
+			'text/css',
+			'application/x-javascript',
+			'application/javascript',
+			'application/ecmascript',
+			'application/json',
+			'application/xml',
+			'image/svg+xml',
+		];
+
+		if (empty($content_types) || ! in_array($content_type_short, $content_types)) {
+			return $response;
+		}
 
-        if ($content_type_short !== 'text/html') {
-            return $response;
-        }
+		$content_length = $response->getContentLength();
 
 
-		$content = (string) gzencode($response->getBody());
+		if ($content_length > 1000) {
+			$content_minimified = (string) gzencode($response->getBody());
+			$response->setBody($content_minimified);
+			$content_minimified_length = $response->getContentLength();
 
-		if (strlen($content) > 1000) {
-			$response->setBody($content);
 
 			$response->addHeader('Content-Encoding', 'gzip');
 			$response->addHeader('X-Encoding', 'gzip');
+
+			// add information headers
+			$response->addHeader('X-Before-Encoding-Content-Length', $content_length);
+			$response->addHeader('X-After-Encoding-Content-Length', $content_minimified_length);
 		}
 
 		return $response;

+ 7 - 5
src/App/Middlewares/MinimifierHtml.php

@@ -14,12 +14,11 @@ class MinimifierHtml
     protected $content_types;
 
     
-    public function __construct($minimify_html=true, $minimify_external_js=true, $minimify_external_css=true, $content_types=['text/html'])
+    public function __construct($minimify_html=true, $minimify_external_js=true, $minimify_external_css=true)
     {
         $this->minimify_html = $minimify_html;
         $this->minimify_external_js = $minimify_external_js;
         $this->minimify_external_css = $minimify_external_css;
-        $this->content_types = $content_types;
     }
 
 
@@ -31,8 +30,11 @@ class MinimifierHtml
         $content_type = $response->getContentType();
         $content_type_short = explode(';', $content_type)[0];
 
+        $content_types = [
+            'text/html',
+        ];
 
-        if (! empty($this->content_types) && ! in_array($content_type_short, $this->content_types)) {
+        if (empty($content_types) || ! in_array($content_type_short, $content_types)) {
             // restriction to the selected content_types
             return $response;
         }
@@ -70,7 +72,7 @@ class MinimifierHtml
                 }
             }
 
-            $response->setContent($content);
+            $response->setBody($content);
         }
 
 
@@ -80,7 +82,7 @@ class MinimifierHtml
             $content_length = $response->getContentLength();
 
             $content_minimified = self::minify_html($content);
-            $response->setContent($content_minimified);
+            $response->setBody($content_minimified);
             $content_minimified_length = $response->getContentLength();
 
             // add information headers

+ 8 - 0
src/App/Middlewares/UrlRouter.php

@@ -50,6 +50,14 @@ class UrlRouter
 
 			$response->prepend($content); // on ajoute ici le texte capturé pendant l'execution de la route
 
+			$content_type = $response->getContentType();
+			if (empty($content_type)) {
+				$content_type = 'text/html; charset=utf-8';
+				$response->setContentType($content_type);
+			}
+
+			$response->addHeader('X-Content-Type', $content_type);
+
 			$response = $next($request, $response);
 
 		} catch (\Exception $e) {