public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
//If the method is not allowed
if ($request->getMethod() !== 'GET') {
if ($this->continueOnError) {
return $next($request, $response);
}
return $response->withStatus(405);
}
$file = $this->getFilename($request);
//If the file does not exists, check if is gzipped
if (!is_file($file)) {
$file .= '.gz';
if (EncodingNegotiator::getEncoding($request) !== 'gzip' || !is_file($file)) {
if ($this->continueOnError) {
return $next($request, $response);
}
return $response->withStatus(404);
}
$response = $response->withHeader('Content-Encoding', 'gzip');
}
//Handle range header
return $this->range($request, $response->withBody(self::createStream($file, 'r')));
}