HTTP compression for the XP Framework, implemented as filter.
The following shows how to enable on-the-fly compression for HTTP responses. Depending on the Accept-Encoding header the client sends, the server-supported compression algorithms and the length of the content, the response is compressed, saving bandwidth.
use web\{Application, Filters};
use web\filters\CompressResponses;
class Service extends Application {
public function routes() {
return new Filters([new CompressResponses()], function($req, $res) {
$content= /* ... */;
$res->answer(200);
$res->send($content, 'text/html; charset=utf-8');
});
}
}