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
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
4 |
|
public function invalidateTags(array $tags) |
45
|
|
|
{ |
46
|
4 |
|
$escapedTags = array_map('preg_quote', $this->escapeTags($tags)); |
47
|
|
|
|
48
|
4 |
|
if (mb_strlen(implode('|', $escapedTags)) >= $this->options['header_length']) { |
49
|
|
|
/* |
50
|
|
|
* estimate the amount of tags to invalidate by dividing the max |
51
|
|
|
* header length by the largest tag (minus 1 for the implode character) |
52
|
|
|
*/ |
53
|
1 |
|
$tagsize = max(array_map('mb_strlen', $escapedTags)); |
54
|
1 |
|
$elems = floor($this->options['header_length'] / ($tagsize - 1)) ?: 1; |
55
|
1 |
|
} else { |
56
|
3 |
|
$elems = count($escapedTags); |
57
|
|
|
} |
58
|
|
|
|
59
|
4 |
|
foreach (array_chunk($escapedTags, $elems) as $tagchunk) { |
60
|
4 |
|
$tagExpression = sprintf('(%s)(,.+)?$', implode('|', $tagchunk)); |
61
|
4 |
|
$this->ban([$this->options['tags_header'] => $tagExpression]); |
62
|
4 |
|
} |
63
|
|
|
|
64
|
4 |
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
1 |
|
public function getTagsHeaderValue(array $tags) |
71
|
|
|
{ |
72
|
1 |
|
return implode(',', array_unique($this->escapeTags($tags))); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
1 |
|
public function getTagsHeaderName() |
79
|
|
|
{ |
80
|
1 |
|
return $this->options['tags_header']; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
10 |
|
public function ban(array $headers) |
87
|
|
|
{ |
88
|
10 |
|
$headers = array_merge( |
89
|
10 |
|
$this->options['default_ban_headers'], |
90
|
|
|
$headers |
91
|
10 |
|
); |
92
|
|
|
|
93
|
10 |
|
$this->queueRequest(self::HTTP_METHOD_BAN, '/', $headers); |
94
|
|
|
|
95
|
10 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
4 |
|
public function banPath($path, $contentType = null, $hosts = null) |
102
|
|
|
{ |
103
|
4 |
|
if (is_array($hosts)) { |
104
|
2 |
|
if (!count($hosts)) { |
105
|
1 |
|
throw new InvalidArgumentException('Either supply a list of hosts or null, but not an empty array.'); |
106
|
|
|
} |
107
|
1 |
|
$hosts = '^('.implode('|', $hosts).')$'; |
108
|
1 |
|
} |
109
|
|
|
|
110
|
|
|
$headers = [ |
111
|
3 |
|
self::HTTP_HEADER_URL => $path, |
112
|
3 |
|
]; |
113
|
|
|
|
114
|
3 |
|
if ($contentType) { |
|
|
|
|
115
|
2 |
|
$headers[self::HTTP_HEADER_CONTENT_TYPE] = $contentType; |
116
|
2 |
|
} |
117
|
3 |
|
if ($hosts) { |
|
|
|
|
118
|
1 |
|
$headers[self::HTTP_HEADER_HOST] = $hosts; |
119
|
1 |
|
} |
120
|
|
|
|
121
|
3 |
|
return $this->ban($headers); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
4 |
|
public function purge($url, array $headers = []) |
128
|
|
|
{ |
129
|
4 |
|
$this->queueRequest(self::HTTP_METHOD_PURGE, $url, $headers); |
130
|
|
|
|
131
|
4 |
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
3 |
View Code Duplication |
public function refresh($url, array $headers = []) |
|
|
|
|
138
|
|
|
{ |
139
|
3 |
|
$headers = array_merge($headers, ['Cache-Control' => 'no-cache']); |
140
|
3 |
|
$this->queueRequest(self::HTTP_METHOD_REFRESH, $url, $headers); |
141
|
|
|
|
142
|
3 |
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
22 |
|
protected function configureOptions() |
149
|
|
|
{ |
150
|
22 |
|
$resolver = parent::configureOptions(); |
151
|
22 |
|
$resolver->setDefaults([ |
152
|
22 |
|
'tags_header' => self::DEFAULT_HTTP_HEADER_CACHE_TAGS, |
153
|
22 |
|
'header_length' => 7500, |
154
|
22 |
|
'default_ban_headers' => [], |
155
|
22 |
|
]); |
156
|
22 |
|
$resolver->setNormalizer('default_ban_headers', function ($resolver, $specified) { |
157
|
22 |
|
return array_merge( |
158
|
|
|
[ |
159
|
22 |
|
self::HTTP_HEADER_HOST => self::REGEX_MATCH_ALL, |
160
|
22 |
|
self::HTTP_HEADER_URL => self::REGEX_MATCH_ALL, |
161
|
22 |
|
self::HTTP_HEADER_CONTENT_TYPE => self::REGEX_MATCH_ALL, |
162
|
22 |
|
], |
163
|
|
|
$specified |
164
|
22 |
|
); |
165
|
22 |
|
}); |
166
|
|
|
|
167
|
22 |
|
return $resolver; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
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: