芝麻web文件管理V1.00
编辑当前文件:/home/mgatv524/public_html/mctv/lib/Middleware/HttpCache.php
. */ namespace Xibo\Middleware; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Server\MiddlewareInterface as Middleware; use Psr\Http\Server\RequestHandlerInterface as RequestHandler; /** * Class HttpCache * Http cache * @package Xibo\Middleware */ class HttpCache implements Middleware { /** * Cache-Control type (public or private) * * @var string */ protected $type; /** * Cache-Control max age in seconds * * @var int */ protected $maxAge; /** * Cache-Control includes must-revalidate flag * * @var bool */ protected $mustRevalidate; public function __construct($type = 'private', $maxAge = 86400, $mustRevalidate = false) { $this->type = $type; $this->maxAge = $maxAge; $this->mustRevalidate = $mustRevalidate; } public function process(Request $request, RequestHandler $handler): Response { $response = $handler->handle($request); // Cache-Control header if (!$response->hasHeader('Cache-Control')) { if ($this->maxAge === 0) { $response = $response->withHeader('Cache-Control', sprintf( '%s, no-cache%s', $this->type, $this->mustRevalidate ? ', must-revalidate' : '' )); } else { $response = $response->withHeader('Cache-Control', sprintf( '%s, max-age=%s%s', $this->type, $this->maxAge, $this->mustRevalidate ? ', must-revalidate' : '' )); } } // ETag header and conditional GET check $etag = $response->getHeader('ETag'); $etag = reset($etag); if ($etag) { $ifNoneMatch = $request->getHeaderLine('If-None-Match'); if ($ifNoneMatch) { $etagList = preg_split('@\s*,\s*@', $ifNoneMatch); if (in_array($etag, $etagList) || in_array('*', $etagList)) { return $response->withStatus(304); } } } // Last-Modified header and conditional GET check $lastModified = $response->getHeaderLine('Last-Modified'); if ($lastModified) { if (!is_integer($lastModified)) { $lastModified = strtotime($lastModified); } $ifModifiedSince = $request->getHeaderLine('If-Modified-Since'); if ($ifModifiedSince && $lastModified <= strtotime($ifModifiedSince)) { return $response->withStatus(304); } } return $response; } }