Skip to content

Commit ecc4582

Browse files
authored
Merge pull request #361 from FriendsOfSymfony/ban-without-host
do not force a host in the uri for ban requests
2 parents c9e50d3 + 4beeec7 commit ecc4582

File tree

7 files changed

+38
-13
lines changed

7 files changed

+38
-13
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ Changelog
33

44
See also the [GitHub releases page](https://github.com/FriendsOfSymfony/FOSHttpCache/releases).
55

6+
2.0.1
7+
-----
8+
9+
### Fixed
10+
11+
* Ban requests now work even when no base URI is configured.
12+
613
2.0.0
714
-----
815

src/ProxyClient/HttpDispatcher.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,13 @@ public function __construct(
111111
* Queue invalidation request.
112112
*
113113
* @param RequestInterface $invalidationRequest
114+
* @param bool $validateHost If false, do not validate that we either have a
115+
* base uri or the invalidation request specifies
116+
* the host
114117
*/
115-
public function invalidate(RequestInterface $invalidationRequest)
118+
public function invalidate(RequestInterface $invalidationRequest, $validateHost = true)
116119
{
117-
if (!$this->baseUri && !$invalidationRequest->getUri()->getHost()) {
120+
if ($validateHost && !$this->baseUri && !$invalidationRequest->getUri()->getHost()) {
118121
throw MissingHostException::missingHost((string) $invalidationRequest->getUri());
119122
}
120123

src/ProxyClient/HttpProxyClient.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,13 @@ protected function configureOptions()
8686
* @param string $method
8787
* @param string|UriInterface $url
8888
* @param array $headers
89+
* @param bool $validateHost see HttpDispatcher::invalidate
8990
*/
90-
protected function queueRequest($method, $url, array $headers)
91+
protected function queueRequest($method, $url, array $headers, $validateHost = true)
9192
{
9293
$this->httpDispatcher->invalidate(
93-
$this->requestFactory->createRequest($method, $url, $headers)
94+
$this->requestFactory->createRequest($method, $url, $headers),
95+
$validateHost
9496
);
9597
}
9698

src/ProxyClient/Varnish.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function ban(array $headers)
8787
$headers
8888
);
8989

90-
$this->queueRequest(self::HTTP_METHOD_BAN, '/', $headers);
90+
$this->queueRequest(self::HTTP_METHOD_BAN, '/', $headers, false);
9191

9292
return $this;
9393
}

tests/Unit/ProxyClient/HttpDispatcherTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,18 @@ public function testMissingHostExceptionIsThrown()
157157
$httpDispatcher->invalidate($request);
158158
}
159159

160+
public function testBanWithoutBaseUri()
161+
{
162+
$httpDispatcher = new HttpDispatcher(
163+
['127.0.0.1:123'],
164+
'',
165+
$this->httpClient
166+
);
167+
168+
$request = $this->messageFactory->createRequest('BAN', '/', ['X-Url' => '/foo/.*']);
169+
$httpDispatcher->invalidate($request, false);
170+
}
171+
160172
public function testSetBasePathWithHost()
161173
{
162174
$httpDispatcher = new HttpDispatcher(

tests/Unit/ProxyClient/SymfonyTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function (RequestInterface $request) {
4242

4343
return true;
4444
}
45-
)
45+
), true
4646
);
4747

4848
$symfony->purge('/url', ['X-Foo' => 'bar']);
@@ -62,7 +62,8 @@ function (RequestInterface $request) {
6262

6363
return true;
6464
}
65-
)
65+
),
66+
true
6667
);
6768

6869
$symfony->refresh('/fresh');

tests/Unit/ProxyClient/VarnishTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function (RequestInterface $request) {
5252

5353
return true;
5454
}
55-
)
55+
), false
5656
);
5757

5858
$varnish->ban([
@@ -74,7 +74,7 @@ function (RequestInterface $request) {
7474

7575
return true;
7676
}
77-
)
77+
), false
7878
);
7979
$hosts = ['fos.lo', 'fos2.lo'];
8080
$varnish->banPath('/articles/.*', 'text/html', $hosts);
@@ -113,7 +113,7 @@ function (RequestInterface $request) {
113113

114114
return true;
115115
}
116-
)
116+
), false
117117
);
118118

119119
$varnish->invalidateTags(['mytag', 'othertag']);
@@ -134,7 +134,7 @@ function (RequestInterface $request) {
134134

135135
return true;
136136
}
137-
)
137+
), false
138138
);
139139

140140
$varnish->invalidateTags(['post-1', 'post,type-3']);
@@ -162,7 +162,7 @@ function (RequestInterface $request) {
162162

163163
return true;
164164
}
165-
)
165+
), true
166166
);
167167

168168
$varnish->purge('/url', ['X-Foo' => 'bar']);
@@ -180,7 +180,7 @@ function (RequestInterface $request) {
180180

181181
return true;
182182
}
183-
)
183+
), true
184184
);
185185

186186
$varnish->refresh('/fresh');

0 commit comments

Comments
 (0)