/**
* Execute the middleware.
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param callable $next
*
* @return ResponseInterface
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
//If basePath does not match
if (!$this->testBasePath($request->getUri()->getPath())) {
return $next($request, $response);
}
//If the method is not allowed
if ($request->getMethod() !== 'GET') {
return $response->withStatus(405);
}
$body = Middleware::createStream();
$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)) {
return $response->withStatus(404);
}
$response = $response->withHeader('Content-Encoding', 'gzip');
}
self::readFile($file, $body);
$response = $response->withBody($body);
//Handle range header
$response = $this->range($request, $response);
return $next($request, $response);
}