|
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 = 'X-Cache-Tags'; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
4 |
|
*/ |
|
45
|
|
|
public function invalidateTags(array $tags) |
|
46
|
4 |
|
{ |
|
47
|
|
|
$purgeInvalidation = $this->options['tags_invalidation_by_purge']; |
|
48
|
4 |
|
$escapedTags = array_map('preg_quote', $this->escapeTags($tags)); |
|
49
|
|
|
|
|
50
|
|
|
if ($purgeInvalidation === 'single') { |
|
51
|
|
|
$elems = 1; |
|
52
|
|
|
} else if (mb_strlen(implode('|', $escapedTags)) >= $this->options['header_length']) { |
|
53
|
1 |
|
/* |
|
54
|
1 |
|
* 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
|
3 |
|
*/ |
|
57
|
|
|
$tagsize = max(array_map('mb_strlen', $escapedTags)); |
|
58
|
|
|
$elems = floor($this->options['header_length'] / ($tagsize - 1)) ?: 1; |
|
59
|
4 |
|
} else { |
|
60
|
4 |
|
$elems = count($escapedTags); |
|
61
|
4 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
foreach (array_chunk($escapedTags, $elems) as $tagchunk) { |
|
64
|
4 |
|
if ($purgeInvalidation) { |
|
65
|
|
|
$this->purge('/', [$this->options['tags_purge_header'] => $tagchunk]); |
|
66
|
|
|
} else { |
|
67
|
|
|
$tagExpression = sprintf('(%s)(,.+)?$', implode('|', $tagchunk)); |
|
68
|
|
|
$this->ban([$this->options['tags_purge_header'] => $tagExpression]); |
|
69
|
|
|
} |
|
70
|
1 |
|
|
|
71
|
|
|
} |
|
72
|
1 |
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc} |
|
78
|
1 |
|
*/ |
|
79
|
|
|
public function getTagsHeaderValue(array $tags) |
|
80
|
1 |
|
{ |
|
81
|
|
|
return implode($this->options['tags_separator'], array_unique($this->escapeTags($tags))); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* {@inheritdoc} |
|
86
|
10 |
|
*/ |
|
87
|
|
|
public function getTagsHeaderName() |
|
88
|
10 |
|
{ |
|
89
|
10 |
|
return $this->options['tags_header']; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
10 |
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
10 |
|
public function ban(array $headers) |
|
96
|
|
|
{ |
|
97
|
|
|
$headers = array_merge( |
|
98
|
|
|
$this->options['default_ban_headers'], |
|
99
|
|
|
$headers |
|
100
|
|
|
); |
|
101
|
4 |
|
|
|
102
|
|
|
$this->queueRequest(self::HTTP_METHOD_BAN, '/', $headers); |
|
103
|
4 |
|
|
|
104
|
2 |
|
return $this; |
|
105
|
1 |
|
} |
|
106
|
|
|
|
|
107
|
1 |
|
/** |
|
108
|
|
|
* {@inheritdoc} |
|
109
|
|
|
*/ |
|
110
|
|
|
public function banPath($path, $contentType = null, $hosts = null) |
|
111
|
3 |
|
{ |
|
112
|
|
|
if (is_array($hosts)) { |
|
113
|
|
|
if (!count($hosts)) { |
|
114
|
3 |
|
throw new InvalidArgumentException('Either supply a list of hosts or null, but not an empty array.'); |
|
115
|
2 |
|
} |
|
116
|
|
|
$hosts = '^('.implode('|', $hosts).')$'; |
|
117
|
3 |
|
} |
|
118
|
1 |
|
|
|
119
|
|
|
$headers = [ |
|
120
|
|
|
self::HTTP_HEADER_URL => $path, |
|
121
|
3 |
|
]; |
|
122
|
|
|
|
|
123
|
|
|
if ($contentType) { |
|
|
|
|
|
|
124
|
|
|
$headers[self::HTTP_HEADER_CONTENT_TYPE] = $contentType; |
|
125
|
|
|
} |
|
126
|
|
|
if ($hosts) { |
|
|
|
|
|
|
127
|
4 |
|
$headers[self::HTTP_HEADER_HOST] = $hosts; |
|
128
|
|
|
} |
|
129
|
4 |
|
|
|
130
|
|
|
return $this->ban($headers); |
|
131
|
4 |
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* {@inheritdoc} |
|
135
|
|
|
*/ |
|
136
|
|
|
public function purge($url, array $headers = []) |
|
137
|
3 |
|
{ |
|
138
|
|
|
$this->queueRequest(self::HTTP_METHOD_PURGE, $url, $headers); |
|
139
|
3 |
|
|
|
140
|
3 |
|
return $this; |
|
141
|
|
|
} |
|
142
|
3 |
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* {@inheritdoc} |
|
145
|
|
|
*/ |
|
146
|
|
View Code Duplication |
public function refresh($url, array $headers = []) |
|
|
|
|
|
|
147
|
|
|
{ |
|
148
|
22 |
|
$headers = array_merge($headers, ['Cache-Control' => 'no-cache']); |
|
149
|
|
|
$this->queueRequest(self::HTTP_METHOD_REFRESH, $url, $headers); |
|
150
|
22 |
|
|
|
151
|
22 |
|
return $this; |
|
152
|
22 |
|
} |
|
153
|
22 |
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* {@inheritdoc} |
|
156
|
22 |
|
*/ |
|
157
|
22 |
|
protected function configureOptions() |
|
158
|
|
|
{ |
|
159
|
22 |
|
$resolver = parent::configureOptions(); |
|
160
|
22 |
|
$resolver->setDefaults([ |
|
161
|
22 |
|
'tags_header' => self::DEFAULT_HTTP_HEADER_CACHE_TAGS, |
|
162
|
|
|
'tags_purge_header' => self::DEFAULT_HTTP_PURGE_HEADER_CACHE_TAGS, |
|
163
|
|
|
'header_length' => 7500, |
|
164
|
|
|
'tags_invalidation_by_purge' => false,// false, true or "single" for purge invalidation with a purge per tag |
|
165
|
22 |
|
'tags_separator' => ',', |
|
166
|
|
|
'default_ban_headers' => [], |
|
167
|
22 |
|
]); |
|
168
|
|
|
$resolver->setNormalizer('default_ban_headers', function ($resolver, $specified) { |
|
169
|
|
|
return array_merge( |
|
170
|
|
|
[ |
|
171
|
|
|
self::HTTP_HEADER_HOST => self::REGEX_MATCH_ALL, |
|
172
|
|
|
self::HTTP_HEADER_URL => self::REGEX_MATCH_ALL, |
|
173
|
|
|
self::HTTP_HEADER_CONTENT_TYPE => self::REGEX_MATCH_ALL, |
|
174
|
|
|
], |
|
175
|
|
|
$specified |
|
176
|
|
|
); |
|
177
|
|
|
}); |
|
178
|
|
|
|
|
179
|
|
|
return $resolver; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: