forked from slimphp/Slim-HttpCache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCache.php
More file actions
147 lines (126 loc) · 4.24 KB
/
Cache.php
File metadata and controls
147 lines (126 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/**
* Slim Framework (https://www.slimframework.com)
*
* @license https://github.com/slimphp/Slim-HttpCache/blob/master/LICENSE.md (MIT License)
*/
declare(strict_types=1);
namespace Slim\HttpCache;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use function in_array;
use function is_array;
use function is_numeric;
use function preg_split;
use function reset;
use function sprintf;
use function strtotime;
class Cache implements MiddlewareInterface
{
/**
* 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;
/**
* @var StreamFactoryInterface|null
*/
protected $streamFactory;
/**
* Create new HTTP cache
*
* @param string $type The cache type: "public" or "private"
* @param int $maxAge The maximum age of client-side cache
* @param bool $mustRevalidate must-revalidate
* @param StreamFactoryInterface|null $streamFactory Will clear body of a 304 if provided
*/
public function __construct(
string $type = 'private',
int $maxAge = 86400,
bool $mustRevalidate = false,
?StreamFactoryInterface $streamFactory = null
) {
$this->type = $type;
$this->maxAge = $maxAge;
$this->mustRevalidate = $mustRevalidate;
$this->streamFactory = $streamFactory;
}
/**
* {@inheritDoc}
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$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 (is_array($etagList) && (in_array($etag, $etagList) || in_array('*', $etagList))) {
$response = $response->withStatus(304);
if ($this->streamFactory) {
$response = $response->withBody($this->streamFactory->createStream(''));
}
return $response;
}
}
}
// Last-Modified header and conditional GET check
$lastModified = $response->getHeaderLine('Last-Modified');
if ($lastModified) {
if (!is_numeric($lastModified)) {
$lastModified = strtotime($lastModified);
}
$ifModifiedSince = $request->getHeaderLine('If-Modified-Since');
if ($ifModifiedSince && $lastModified <= strtotime($ifModifiedSince)) {
$response = $response->withStatus(304);
if ($this->streamFactory) {
$response = $response->withBody($this->streamFactory->createStream(''));
}
return $response;
}
}
return $response;
}
}