Skip to content

Commit

Permalink
do not force a host in the uri for ban requests
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Jun 23, 2017
1 parent c9e50d3 commit 4beeec7
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 13 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ Changelog

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

2.0.1
-----

### Fixed

* Ban requests now work even when no base URI is configured.

2.0.0
-----

Expand Down
7 changes: 5 additions & 2 deletions src/ProxyClient/HttpDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,13 @@ public function __construct(
* Queue invalidation request.
*
* @param RequestInterface $invalidationRequest
* @param bool $validateHost If false, do not validate that we either have a
* base uri or the invalidation request specifies
* the host
*/
public function invalidate(RequestInterface $invalidationRequest)
public function invalidate(RequestInterface $invalidationRequest, $validateHost = true)
{
if (!$this->baseUri && !$invalidationRequest->getUri()->getHost()) {
if ($validateHost && !$this->baseUri && !$invalidationRequest->getUri()->getHost()) {
throw MissingHostException::missingHost((string) $invalidationRequest->getUri());
}

Expand Down
6 changes: 4 additions & 2 deletions src/ProxyClient/HttpProxyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,13 @@ protected function configureOptions()
* @param string $method
* @param string|UriInterface $url
* @param array $headers
* @param bool $validateHost see HttpDispatcher::invalidate
*/
protected function queueRequest($method, $url, array $headers)
protected function queueRequest($method, $url, array $headers, $validateHost = true)
{
$this->httpDispatcher->invalidate(
$this->requestFactory->createRequest($method, $url, $headers)
$this->requestFactory->createRequest($method, $url, $headers),
$validateHost
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/ProxyClient/Varnish.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function ban(array $headers)
$headers
);

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

return $this;
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/ProxyClient/HttpDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ public function testMissingHostExceptionIsThrown()
$httpDispatcher->invalidate($request);
}

public function testBanWithoutBaseUri()
{
$httpDispatcher = new HttpDispatcher(
['127.0.0.1:123'],
'',
$this->httpClient
);

$request = $this->messageFactory->createRequest('BAN', '/', ['X-Url' => '/foo/.*']);
$httpDispatcher->invalidate($request, false);
}

public function testSetBasePathWithHost()
{
$httpDispatcher = new HttpDispatcher(
Expand Down
5 changes: 3 additions & 2 deletions tests/Unit/ProxyClient/SymfonyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function (RequestInterface $request) {

return true;
}
)
), true
);

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

return true;
}
)
),
true
);

$symfony->refresh('/fresh');
Expand Down
12 changes: 6 additions & 6 deletions tests/Unit/ProxyClient/VarnishTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function (RequestInterface $request) {

return true;
}
)
), false
);

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

return true;
}
)
), false
);
$hosts = ['fos.lo', 'fos2.lo'];
$varnish->banPath('/articles/.*', 'text/html', $hosts);
Expand Down Expand Up @@ -113,7 +113,7 @@ function (RequestInterface $request) {

return true;
}
)
), false
);

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

return true;
}
)
), false
);

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

return true;
}
)
), true
);

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

return true;
}
)
), true
);

$varnish->refresh('/fresh');
Expand Down

0 comments on commit 4beeec7

Please sign in to comment.