maintenance: Enable gzip in router.php for static files
authorTimo Tijhof <krinklemail@gmail.com>
Thu, 26 Sep 2019 21:57:50 +0000 (22:57 +0100)
committerTimo Tijhof <krinklemail@gmail.com>
Mon, 30 Sep 2019 13:09:35 +0000 (14:09 +0100)
Bug: T233992
Change-Id: Ie401180ac968210c9f923ad920bf15955c8551d7

maintenance/dev/includes/router.php

index d767df0..bb600bf 100644 (file)
@@ -74,10 +74,22 @@ if ( $ext == 'php' ) {
        } else {
                header( "Content-Type: $mime" );
        }
-       header( "Content-Length: " . filesize( $file ) );
-       // Stream that out to the browser
-       $f = fopen( $file, 'rb' );
-       fpassthru( $f );
+
+       $content = file_get_contents( $file );
+
+       header( 'Vary: Accept-Encoding' );
+       $acceptGzip = preg_match( '/\bgzip\b/', $_SERVER['HTTP_ACCEPT_ENCODING'] ?? '' );
+       if ( $acceptGzip &&
+               // Don't compress binary static files (e.g. png)
+               preg_match( '/text|javascript|json|css|xml|svg/', $mime ) &&
+               // Tiny files tend to grow instead of shrink. – <https://gerrit.wikimedia.org/r/537974>
+               strlen( $content ) > 150
+       ) {
+               $content = gzencode( $content, 9 );
+               header( 'Content-Encoding: gzip' );
+       }
+       header( "Content-Length: " . strlen( $content ) );
+       echo $content;
 
        return true;
 }