Skip to content

Commit 596a734

Browse files
committed
Fix Host header for proxy client requests
When using `ban` requests with Varnish proxy client, requests are queued and sent with `flush()`. Queued requests are registered without host, making Guzzle create an empty `Host` header. When flushing, Guzzle request is re-created starting with the original one (so without host), with all registered headers. The problem is that the empty `Host` header is also copied, and thus not re-created by Guzzle with the server URL. In the backend, cURL sees it and takes it into account, overriding passed URL silently. This causes requests with empty host being fired by Guzzle, which is kind of annoying 😃.
1 parent 1e0a515 commit 596a734

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/ProxyClient/AbstractProxyClient.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,16 @@ private function sendRequests(array $requests)
161161
$allRequests = array();
162162

163163
foreach ($requests as $request) {
164+
$headers = $request->getHeaders()->toArray();
165+
// Force to re-create Host header if empty.
166+
if (empty($headers['Host'])) {
167+
unset( $headers['Host'] );
168+
}
164169
foreach ($this->servers as $server) {
165170
$proxyRequest = $this->client->createRequest(
166171
$request->getMethod(),
167172
$server . $request->getResource(),
168-
$request->getHeaders()
173+
$headers
169174
);
170175
$allRequests[] = $proxyRequest;
171176
}

tests/Unit/ProxyClient/VarnishTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,23 @@ public function testBanEverything()
4747
$this->assertEquals('fos.lo', $headers->get('Host'));
4848
}
4949

50+
public function testBanEverythingNoBaseUrl()
51+
{
52+
$varnish = new Varnish(array('http://127.0.0.1:123'), null, $this->client);
53+
$varnish->ban(array())->flush();
54+
55+
$requests = $this->getRequests();
56+
$this->assertCount(1, $requests);
57+
$this->assertEquals('BAN', $requests[0]->getMethod());
58+
59+
$headers = $requests[0]->getHeaders();
60+
$this->assertEquals('.*', $headers->get('X-Host'));
61+
$this->assertEquals('.*', $headers->get('X-Url'));
62+
$this->assertEquals('.*', $headers->get('X-Content-Type'));
63+
// Ensure host header matches the Varnish server one.
64+
$this->assertEquals(array('127.0.0.1:123'), $headers->get('Host')->toArray());
65+
}
66+
5067
public function testBanHeaders()
5168
{
5269
$varnish = new Varnish(array('http://127.0.0.1:123'), 'fos.lo', $this->client);

0 commit comments

Comments
 (0)