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\BanInterface; |
16
|
|
|
use FOS\HttpCache\ProxyClient\Invalidation\PurgeInterface; |
17
|
|
|
use FOS\HttpCache\ProxyClient\Invalidation\RefreshInterface; |
18
|
|
|
use FOS\HttpCache\ProxyClient\Invalidation\TagsInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Varnish HTTP cache invalidator. |
22
|
|
|
* |
23
|
|
|
* Additional constructor options: |
24
|
|
|
* - tags_header Header for tagging responses, defaults to X-Cache-Tags |
25
|
|
|
* - header_length Maximum header length, defaults to 7500 bytes |
26
|
|
|
* - default_ban_headers Map of headers that are set on each ban request, |
27
|
|
|
* merged with the built-in headers |
28
|
|
|
* |
29
|
|
|
* @author David de Boer <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class Varnish extends HttpProxyClient implements BanInterface, PurgeInterface, RefreshInterface, TagsInterface |
32
|
|
|
{ |
33
|
|
|
const HTTP_METHOD_BAN = 'BAN'; |
34
|
|
|
const HTTP_METHOD_PURGE = 'PURGE'; |
35
|
|
|
const HTTP_METHOD_REFRESH = 'GET'; |
36
|
|
|
const HTTP_HEADER_HOST = 'X-Host'; |
37
|
|
|
const HTTP_HEADER_URL = 'X-Url'; |
38
|
|
|
const HTTP_HEADER_CONTENT_TYPE = 'X-Content-Type'; |
39
|
|
|
const DEFAULT_HTTP_HEADER_CACHE_TAGS = 'X-Cache-Tags'; |
40
|
|
|
const DEFAULT_HTTP_PURGE_HEADER_CACHE_TAGS = 'key'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
4 |
|
public function invalidateTags(array $tags) |
46
|
|
|
{ |
47
|
4 |
|
$tagMode = $this->options['tag_mode']; |
48
|
4 |
|
$escapedTags = array_map('preg_quote', $this->escapeTags($tags)); |
49
|
|
|
|
50
|
4 |
|
if ($tagMode === 'purge_single') { |
51
|
|
|
$elems = 1; |
52
|
4 |
|
} elseif (mb_strlen(implode('|', $escapedTags)) >= $this->options['header_length']) { |
53
|
|
|
/* |
54
|
|
|
* estimate the amount of tags to invalidate by dividing the max |
55
|
|
|
* header length by the largest tag (minus 1 for the implode character) |
56
|
|
|
*/ |
57
|
1 |
|
$tagsize = max(array_map('mb_strlen', $escapedTags)); |
58
|
1 |
|
$elems = floor($this->options['header_length'] / ($tagsize - 1)) ?: 1; |
59
|
1 |
|
} else { |
60
|
3 |
|
$elems = count($escapedTags); |
61
|
|
|
} |
62
|
|
|
|
63
|
4 |
|
foreach (array_chunk($escapedTags, $elems) as $tagchunk) { |
64
|
4 |
|
if ($tagMode === 'ban') { |
65
|
4 |
|
$tagExpression = sprintf('(%s)(,.+)?$', implode('|', $tagchunk)); |
66
|
4 |
|
$this->ban([$this->options['tags_header'] => $tagExpression]); |
67
|
4 |
|
} else { |
68
|
|
|
$this->purge('/', [$this->options['tags_purge_header'] => implode(' ', $tagchunk)]); |
69
|
|
|
} |
70
|
4 |
|
} |
71
|
|
|
|
72
|
4 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
1 |
|
public function getTagsHeaderValue(array $tags) |
79
|
|
|
{ |
80
|
1 |
|
return implode(($this->options['tag_mode'] === 'ban' ? ',' : ' '), array_unique($this->escapeTags($tags))); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
1 |
|
public function getTagsHeaderName() |
87
|
|
|
{ |
88
|
1 |
|
return $this->options['tags_header']; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
10 |
|
public function ban(array $headers) |
95
|
|
|
{ |
96
|
10 |
|
$headers = array_merge( |
97
|
10 |
|
$this->options['default_ban_headers'], |
98
|
|
|
$headers |
99
|
10 |
|
); |
100
|
|
|
|
101
|
10 |
|
$this->queueRequest(self::HTTP_METHOD_BAN, '/', $headers); |
102
|
|
|
|
103
|
10 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
4 |
|
public function banPath($path, $contentType = null, $hosts = null) |
110
|
|
|
{ |
111
|
4 |
|
if (is_array($hosts)) { |
112
|
2 |
|
if (!count($hosts)) { |
113
|
1 |
|
throw new InvalidArgumentException('Either supply a list of hosts or null, but not an empty array.'); |
114
|
|
|
} |
115
|
1 |
|
$hosts = '^('.implode('|', $hosts).')$'; |
116
|
1 |
|
} |
117
|
|
|
|
118
|
|
|
$headers = [ |
119
|
3 |
|
self::HTTP_HEADER_URL => $path, |
120
|
3 |
|
]; |
121
|
|
|
|
122
|
3 |
|
if ($contentType) { |
|
|
|
|
123
|
2 |
|
$headers[self::HTTP_HEADER_CONTENT_TYPE] = $contentType; |
124
|
2 |
|
} |
125
|
3 |
|
if ($hosts) { |
|
|
|
|
126
|
1 |
|
$headers[self::HTTP_HEADER_HOST] = $hosts; |
127
|
1 |
|
} |
128
|
|
|
|
129
|
3 |
|
return $this->ban($headers); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritdoc} |
134
|
|
|
*/ |
135
|
4 |
|
public function purge($url, array $headers = []) |
136
|
|
|
{ |
137
|
4 |
|
$this->queueRequest(self::HTTP_METHOD_PURGE, $url, $headers); |
138
|
|
|
|
139
|
4 |
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
3 |
View Code Duplication |
public function refresh($url, array $headers = []) |
|
|
|
|
146
|
|
|
{ |
147
|
3 |
|
$headers = array_merge($headers, ['Cache-Control' => 'no-cache']); |
148
|
3 |
|
$this->queueRequest(self::HTTP_METHOD_REFRESH, $url, $headers); |
149
|
|
|
|
150
|
3 |
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritdoc} |
155
|
|
|
*/ |
156
|
22 |
|
protected function configureOptions() |
157
|
|
|
{ |
158
|
22 |
|
$resolver = parent::configureOptions(); |
159
|
22 |
|
$resolver->setDefaults([ |
160
|
22 |
|
'tags_header' => self::DEFAULT_HTTP_HEADER_CACHE_TAGS, |
161
|
22 |
|
'tags_purge_header' => self::DEFAULT_HTTP_PURGE_HEADER_CACHE_TAGS, |
162
|
22 |
|
'tag_mode' => 'ban', |
163
|
22 |
|
'header_length' => 7500, |
164
|
22 |
|
'default_ban_headers' => [], |
165
|
22 |
|
]); |
166
|
|
|
// tag_mode options: 'ban', 'purge' or 'purge_single' for purge invalidation per tag |
167
|
22 |
|
$resolver->setAllowedValues('tag_mode', ['ban', 'purge', 'purge_single']); |
168
|
22 |
|
$resolver->setNormalizer('default_ban_headers', function ($resolver, $specified) { |
169
|
22 |
|
return array_merge( |
170
|
|
|
[ |
171
|
22 |
|
self::HTTP_HEADER_HOST => self::REGEX_MATCH_ALL, |
172
|
22 |
|
self::HTTP_HEADER_URL => self::REGEX_MATCH_ALL, |
173
|
22 |
|
self::HTTP_HEADER_CONTENT_TYPE => self::REGEX_MATCH_ALL, |
174
|
22 |
|
], |
175
|
|
|
$specified |
176
|
22 |
|
); |
177
|
22 |
|
}); |
178
|
|
|
|
179
|
22 |
|
return $resolver; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
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: