Skip to content

Commit 9c65814

Browse files
committed
Merge pull request #128 from lolautruche/fixHostHeader
Fix Host header for proxy client requests
2 parents 63cb099 + 25a7a49 commit 9c65814

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, as Apache chokes on this. See #128 for discussion.
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)