From 4beeec7d6d1dc6f299bdb3e3e5107da0288cab44 Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Fri, 23 Jun 2017 15:29:55 +0200 Subject: [PATCH] do not force a host in the uri for ban requests --- CHANGELOG.md | 7 +++++++ src/ProxyClient/HttpDispatcher.php | 7 +++++-- src/ProxyClient/HttpProxyClient.php | 6 ++++-- src/ProxyClient/Varnish.php | 2 +- tests/Unit/ProxyClient/HttpDispatcherTest.php | 12 ++++++++++++ tests/Unit/ProxyClient/SymfonyTest.php | 5 +++-- tests/Unit/ProxyClient/VarnishTest.php | 12 ++++++------ 7 files changed, 38 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dda804e4..30c12ef7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ----- diff --git a/src/ProxyClient/HttpDispatcher.php b/src/ProxyClient/HttpDispatcher.php index c62a8190..96bc2c82 100644 --- a/src/ProxyClient/HttpDispatcher.php +++ b/src/ProxyClient/HttpDispatcher.php @@ -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()); } diff --git a/src/ProxyClient/HttpProxyClient.php b/src/ProxyClient/HttpProxyClient.php index 05346aa8..87846020 100644 --- a/src/ProxyClient/HttpProxyClient.php +++ b/src/ProxyClient/HttpProxyClient.php @@ -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 ); } diff --git a/src/ProxyClient/Varnish.php b/src/ProxyClient/Varnish.php index d3e671ad..f73b89f5 100644 --- a/src/ProxyClient/Varnish.php +++ b/src/ProxyClient/Varnish.php @@ -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; } diff --git a/tests/Unit/ProxyClient/HttpDispatcherTest.php b/tests/Unit/ProxyClient/HttpDispatcherTest.php index 13afd954..c008e0fa 100644 --- a/tests/Unit/ProxyClient/HttpDispatcherTest.php +++ b/tests/Unit/ProxyClient/HttpDispatcherTest.php @@ -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( diff --git a/tests/Unit/ProxyClient/SymfonyTest.php b/tests/Unit/ProxyClient/SymfonyTest.php index dfc06f30..ab52c509 100644 --- a/tests/Unit/ProxyClient/SymfonyTest.php +++ b/tests/Unit/ProxyClient/SymfonyTest.php @@ -42,7 +42,7 @@ function (RequestInterface $request) { return true; } - ) + ), true ); $symfony->purge('/url', ['X-Foo' => 'bar']); @@ -62,7 +62,8 @@ function (RequestInterface $request) { return true; } - ) + ), + true ); $symfony->refresh('/fresh'); diff --git a/tests/Unit/ProxyClient/VarnishTest.php b/tests/Unit/ProxyClient/VarnishTest.php index a5cbf5d7..1f6b3ab0 100644 --- a/tests/Unit/ProxyClient/VarnishTest.php +++ b/tests/Unit/ProxyClient/VarnishTest.php @@ -52,7 +52,7 @@ function (RequestInterface $request) { return true; } - ) + ), false ); $varnish->ban([ @@ -74,7 +74,7 @@ function (RequestInterface $request) { return true; } - ) + ), false ); $hosts = ['fos.lo', 'fos2.lo']; $varnish->banPath('/articles/.*', 'text/html', $hosts); @@ -113,7 +113,7 @@ function (RequestInterface $request) { return true; } - ) + ), false ); $varnish->invalidateTags(['mytag', 'othertag']); @@ -134,7 +134,7 @@ function (RequestInterface $request) { return true; } - ) + ), false ); $varnish->invalidateTags(['post-1', 'post,type-3']); @@ -162,7 +162,7 @@ function (RequestInterface $request) { return true; } - ) + ), true ); $varnish->purge('/url', ['X-Foo' => 'bar']); @@ -180,7 +180,7 @@ function (RequestInterface $request) { return true; } - ) + ), true ); $varnish->refresh('/fresh');