Skip to content

Commit 80f145c

Browse files
committed
Merge pull request #118 from FriendsOfSymfony/purge-headers
Add optional headers to purge call
2 parents 060ca17 + 271ce63 commit 80f145c

File tree

5 files changed

+46
-7
lines changed

5 files changed

+46
-7
lines changed

doc/proxy-clients.rst

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,21 @@ path::
103103
->flush()
104104
;
105105

106+
You can specify HTTP headers as the second argument to ``purge()``.
107+
For instance::
108+
109+
$client
110+
->purge('/some/path', array('X-Foo' => 'bar')
111+
->flush()
112+
;
113+
114+
Please note that purge will invalidate all variants, so you do not have to
115+
send any headers that you vary on, such as ``Accept``.
116+
117+
The above allows you to pass headers that are different between purge requests.
118+
If you want to send headers for all purge requests, such as ``Authorization``,
119+
use a :ref:`custom Guzzle client <custom Guzzle client>` instead.
120+
106121
Refresh
107122
~~~~~~~
108123

@@ -163,3 +178,23 @@ Varnish client::
163178

164179
Make sure to add any headers that you want to ban on to your
165180
:doc:`Varnish configuration <varnish-configuration>`.
181+
182+
.. _custom guzzle client:
183+
184+
Custom Guzzle Client
185+
--------------------
186+
187+
By default, the proxy clients instantiate a `Guzzle client`_ to communicate
188+
with the caching proxy. If you need to customize the requests, for example to
189+
send a basic authentication header, you can inject a custom Guzzle client::
190+
191+
use FOS\HttpCache\ProxyClient\Varnish;
192+
use Guzzle\Http\Client;
193+
194+
$client = new Client();
195+
$client->setDefaultOption('auth', array('username', 'password', 'Digest'));
196+
197+
$servers = array('10.0.0.1');
198+
$varnish = new Varnish($servers, '/baseUrl', $client);
199+
200+
.. _Guzzle client: http://guzzle3.readthedocs.org/

src/ProxyClient/Invalidation/PurgeInterface.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ interface PurgeInterface extends ProxyClientInterface
3030
* If the $url is just a path, the proxy client class will add a default
3131
* host name.
3232
*
33-
* @param string $url Path or URL to purge.
33+
* @param string $url Path or URL to purge.
34+
* @param array $headers Extra HTTP headers to send to the caching proxy
35+
* (optional)
3436
*
3537
* @return $this
3638
*/
37-
public function purge($url);
39+
public function purge($url, array $headers = array());
3840
}

src/ProxyClient/Nginx.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ public function refresh($url, array $headers = array())
7474
/**
7575
* {@inheritdoc}
7676
*/
77-
public function purge($url)
77+
public function purge($url, array $headers = array())
7878
{
7979
$purgeUrl = $this->purgeLocation.$url;
80-
$this->queueRequest(self::HTTP_METHOD_PURGE, $purgeUrl);
80+
$this->queueRequest(self::HTTP_METHOD_PURGE, $purgeUrl, $headers);
8181

8282
return $this;
8383
}

src/ProxyClient/Varnish.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ public function banPath($path, $contentType = null, $hosts = null)
109109
/**
110110
* {@inheritdoc}
111111
*/
112-
public function purge($url)
112+
public function purge($url, array $headers = array())
113113
{
114-
$this->queueRequest(self::HTTP_METHOD_PURGE, $url);
114+
$this->queueRequest(self::HTTP_METHOD_PURGE, $url, $headers);
115115

116116
return $this;
117117
}

tests/Unit/ProxyClient/VarnishTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,11 @@ function ($requests) use ($self) {
120120
$self->assertEquals('127.0.0.1', $requests[2]->getHost());
121121
$self->assertEquals('8080', $requests[2]->getPort());
122122
$self->assertEquals('/url/two', $requests[2]->getPath());
123+
$self->assertEquals('bar', $requests[2]->getHeader('X-Foo'));
123124

124125
$self->assertEquals('123.123.123.2', $requests[3]->getHost());
125126
$self->assertEquals('/url/two', $requests[3]->getPath());
127+
$self->assertEquals('bar', $requests[3]->getHeader('X-Foo'));
126128

127129
return true;
128130
}
@@ -138,7 +140,7 @@ function ($requests) use ($self) {
138140
$varnish = new Varnish($ips, 'my_hostname.dev', $client);
139141

140142
$varnish->purge('/url/one');
141-
$varnish->purge('/url/two');
143+
$varnish->purge('/url/two', array('X-Foo' => 'bar'));
142144

143145
$varnish->flush();
144146
}

0 commit comments

Comments
 (0)