1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSHttpCache package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\HttpCache\ProxyClient; |
13
|
|
|
|
14
|
|
|
use FOS\HttpCache\Exception\InvalidArgumentException; |
15
|
|
|
use FOS\HttpCache\ProxyClient\Invalidation\BanCapable; |
16
|
|
|
use FOS\HttpCache\ProxyClient\Invalidation\PurgeCapable; |
17
|
|
|
use FOS\HttpCache\ProxyClient\Invalidation\RefreshCapable; |
18
|
|
|
use FOS\HttpCache\ProxyClient\Invalidation\TagCapable; |
19
|
|
|
use Symfony\Component\OptionsResolver\Options; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Varnish HTTP cache invalidator. |
23
|
|
|
* |
24
|
|
|
* Additional constructor options: |
25
|
|
|
* - tag_mode Whether to use ban or the xkey extension for cache tagging. |
26
|
|
|
* - tags_header Header for sending tag invalidation requests to |
27
|
|
|
* Varnish, if tag_mode is ban, defaults to X-Cache-Tags, |
28
|
|
|
* otherwise defaults to xkey-softpurge. |
29
|
|
|
* - header_length Maximum header length when invalidating tags. If there |
30
|
|
|
* are more tags to invalidate than fit into the header, |
31
|
|
|
* the invalidation request is split into several requests. |
32
|
|
|
* Defaults to 7500 |
33
|
|
|
* - default_ban_headers Map of header name => header value that have to be set |
34
|
|
|
* on each ban request, merged with the built-in headers |
35
|
|
|
* |
36
|
|
|
* @author David de Boer <[email protected]> |
37
|
|
|
*/ |
38
|
|
|
class Varnish extends HttpProxyClient implements BanCapable, PurgeCapable, RefreshCapable, TagCapable |
39
|
|
|
{ |
40
|
|
|
const HTTP_METHOD_BAN = 'BAN'; |
41
|
|
|
|
42
|
|
|
const HTTP_METHOD_PURGE = 'PURGE'; |
43
|
|
|
|
44
|
|
|
const HTTP_METHOD_PURGEKEYS = 'PURGEKEYS'; |
45
|
|
|
|
46
|
|
|
const HTTP_METHOD_REFRESH = 'GET'; |
47
|
|
|
|
48
|
|
|
const HTTP_HEADER_HOST = 'X-Host'; |
49
|
|
|
|
50
|
|
|
const HTTP_HEADER_URL = 'X-Url'; |
51
|
|
|
|
52
|
|
|
const HTTP_HEADER_CONTENT_TYPE = 'X-Content-Type'; |
53
|
|
|
|
54
|
|
|
const TAG_BAN = 'ban'; |
55
|
|
|
|
56
|
|
|
const TAG_XKEY = 'purgekeys'; |
57
|
4 |
|
|
58
|
|
|
/** |
59
|
4 |
|
* Default name of the header used to invalidate content with specific tags. |
60
|
|
|
* |
61
|
4 |
|
* This happens to be the same as TagHeaderFormatter::DEFAULT_HEADER_NAME |
62
|
|
|
* but does not technically need to be the same. |
63
|
|
|
* |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
1 |
|
const DEFAULT_HTTP_HEADER_CACHE_TAGS = 'X-Cache-Tags'; |
67
|
1 |
|
|
68
|
|
|
const DEFAULT_HTTP_HEADER_CACHE_XKEY = 'xkey-softpurge'; |
69
|
3 |
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
4 |
|
*/ |
73
|
4 |
|
public function invalidateTags(array $tags) |
74
|
4 |
|
{ |
75
|
|
|
$banMode = self::TAG_BAN === $this->options['tag_mode']; |
76
|
|
|
$escapedTags = array_map('preg_quote', $this->escapeTags($tags)); |
77
|
4 |
|
|
78
|
|
|
$chunkSize = $this->determineTagsPerHeader($escapedTags, $banMode ? '|' : ' '); |
79
|
|
|
|
80
|
|
|
foreach (array_chunk($escapedTags, $chunkSize) as $tagchunk) { |
81
|
|
|
if ($banMode) { |
82
|
|
|
$this->invalidateByBan($tagchunk); |
83
|
10 |
|
} else { |
84
|
|
|
$this->invalidateByPurgekeys($tagchunk); |
85
|
10 |
|
} |
86
|
10 |
|
} |
87
|
10 |
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
10 |
|
|
91
|
|
|
/** |
92
|
10 |
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function ban(array $headers) |
95
|
|
|
{ |
96
|
|
|
$headers = array_merge( |
97
|
|
|
$this->options['default_ban_headers'], |
98
|
4 |
|
$headers |
99
|
|
|
); |
100
|
4 |
|
|
101
|
2 |
|
$this->queueRequest(self::HTTP_METHOD_BAN, '/', $headers, false); |
102
|
1 |
|
|
103
|
|
|
return $this; |
104
|
1 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
3 |
|
*/ |
109
|
|
|
public function banPath($path, $contentType = null, $hosts = null) |
110
|
|
|
{ |
111
|
3 |
|
if (is_array($hosts)) { |
112
|
2 |
|
if (!count($hosts)) { |
113
|
|
|
throw new InvalidArgumentException('Either supply a list of hosts or null, but not an empty array.'); |
114
|
3 |
|
} |
115
|
1 |
|
$hosts = '^('.implode('|', $hosts).')$'; |
116
|
|
|
} |
117
|
|
|
|
118
|
3 |
|
$headers = [ |
119
|
|
|
self::HTTP_HEADER_URL => $path, |
120
|
|
|
]; |
121
|
|
|
|
122
|
|
|
if ($contentType) { |
|
|
|
|
123
|
|
|
$headers[self::HTTP_HEADER_CONTENT_TYPE] = $contentType; |
124
|
4 |
|
} |
125
|
|
|
if ($hosts) { |
|
|
|
|
126
|
4 |
|
$headers[self::HTTP_HEADER_HOST] = $hosts; |
127
|
|
|
} |
128
|
4 |
|
|
129
|
|
|
return $this->ban($headers); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritdoc} |
134
|
3 |
|
*/ |
135
|
|
|
public function purge($url, array $headers = []) |
136
|
3 |
|
{ |
137
|
3 |
|
$this->queueRequest(self::HTTP_METHOD_PURGE, $url, $headers); |
138
|
|
|
|
139
|
3 |
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
19 |
View Code Duplication |
public function refresh($url, array $headers = []) |
|
|
|
|
146
|
|
|
{ |
147
|
19 |
|
$headers = array_merge($headers, ['Cache-Control' => 'no-cache']); |
148
|
19 |
|
$this->queueRequest(self::HTTP_METHOD_REFRESH, $url, $headers); |
149
|
19 |
|
|
150
|
19 |
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
19 |
|
/** |
154
|
19 |
|
* {@inheritdoc} |
155
|
|
|
*/ |
156
|
19 |
|
protected function configureOptions() |
157
|
19 |
|
{ |
158
|
19 |
|
$resolver = parent::configureOptions(); |
159
|
|
|
$resolver->setDefaults([ |
160
|
19 |
|
'tags_header' => self::DEFAULT_HTTP_HEADER_CACHE_TAGS, |
161
|
|
|
'tag_mode' => self::TAG_BAN, |
162
|
19 |
|
'header_length' => 7500, |
163
|
|
|
'default_ban_headers' => [], |
164
|
19 |
|
]); |
165
|
|
|
$resolver->setDefault('tags_header', function (Options $options) { |
166
|
|
|
if (self::TAG_BAN === $options['tag_mode']) { |
167
|
|
|
return self::DEFAULT_HTTP_HEADER_CACHE_TAGS; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return self::DEFAULT_HTTP_HEADER_CACHE_XKEY; |
171
|
|
|
}); |
172
|
|
|
$resolver->setAllowedValues('tag_mode', [self::TAG_BAN, self::TAG_XKEY]); |
173
|
|
|
$resolver->setNormalizer('default_ban_headers', function ($resolver, $specified) { |
174
|
|
|
return array_merge( |
175
|
|
|
[ |
176
|
|
|
self::HTTP_HEADER_HOST => self::REGEX_MATCH_ALL, |
177
|
|
|
self::HTTP_HEADER_URL => self::REGEX_MATCH_ALL, |
178
|
|
|
self::HTTP_HEADER_CONTENT_TYPE => self::REGEX_MATCH_ALL, |
179
|
|
|
], |
180
|
|
|
$specified |
181
|
|
|
); |
182
|
|
|
}); |
183
|
|
|
|
184
|
|
|
return $resolver; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
private function invalidateByBan(array $tagchunk) |
188
|
|
|
{ |
189
|
|
|
$tagExpression = sprintf('(^|,)(%s)(,|$)', implode('|', $tagchunk)); |
190
|
|
|
$this->ban([$this->options['tags_header'] => $tagExpression]); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
private function invalidateByPurgekeys(array $tagchunk) |
194
|
|
|
{ |
195
|
|
|
$this->queueRequest( |
196
|
|
|
self::HTTP_METHOD_PURGEKEYS, |
197
|
|
|
'/', |
198
|
|
|
[$this->options['tags_header'] => implode(' ', $tagchunk)], |
199
|
|
|
false |
200
|
|
|
); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: