<?php
/**
* Router for the php cli-server built-in webserver.
- * https://secure.php.net/manual/en/features.commandline.webserver.php
+ * https://www.php.net/manual/en/features.commandline.webserver.php
*
* 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
} 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;
}