Skip to content

Commit 4e227b4

Browse files
committed
Merge pull request #134 from FriendsOfSymfony/fix-accept-header-handling
fix accept header handling when client sends no accept header
2 parents 480615b + e894523 commit 4e227b4

File tree

6 files changed

+63
-16
lines changed

6 files changed

+63
-16
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Changelog
2+
=========
3+
4+
1.1.2
5+
-----
6+
7+
* **2014-11-17** Fixed documentation for user context varnish configuration to also work when
8+
client omits the `Accept` HTTP header.

tests/.htaccess

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
deny from all
2+

tests/Functional/Fixtures/varnish-3/user_context.vcl

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ sub vcl_recv {
1414
&& (req.http.cookie || req.http.authorization)
1515
&& (req.request == "GET" || req.request == "HEAD")
1616
) {
17-
set req.http.x-fos-original-url = req.url;
18-
set req.http.x-fos-original-accept = req.http.accept;
19-
20-
set req.http.accept = "application/vnd.fos.user-context-hash";
17+
set req.http.x-fos-original-url = req.url;
18+
# Backup accept header, if set
19+
if (req.http.accept) {
20+
set req.http.x-fos-original-accept = req.http.accept;
21+
}
22+
set req.http.accept = "application/vnd.fos.user-context-hash";
2123

2224
# A little hack for testing all scenarios. Choose one for your application.
2325
if ("failure" == req.http.x-cache-hash) {
@@ -38,11 +40,15 @@ sub vcl_recv {
3840
if (req.restarts > 0
3941
&& req.http.accept == "application/vnd.fos.user-context-hash"
4042
) {
41-
set req.url = req.http.x-fos-original-url;
42-
set req.http.accept = req.http.x-fos-original-accept;
43-
43+
set req.url = req.http.x-fos-original-url;
4444
unset req.http.x-fos-original-url;
45-
unset req.http.x-fos-original-accept;
45+
if (req.http.x-fos-original-accept) {
46+
set req.http.accept = req.http.x-fos-original-accept;
47+
unset req.http.x-fos-original-accept;
48+
} else {
49+
# If accept header was not set in original request, remove the header here.
50+
unset req.http.accept;
51+
}
4652

4753
# Force the lookup, the backend must tell not to cache or vary on the
4854
# user hash to properly separate cached data.

tests/Functional/Fixtures/varnish-4/user_context.vcl

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ sub vcl_recv {
1616
&& (req.http.cookie || req.http.authorization)
1717
&& (req.method == "GET" || req.method == "HEAD")
1818
) {
19-
set req.http.x-fos-original-url = req.url;
20-
set req.http.x-fos-original-accept = req.http.accept;
21-
22-
set req.http.accept = "application/vnd.fos.user-context-hash";
19+
set req.http.x-fos-original-url = req.url;
20+
# Backup accept header, if set
21+
if (req.http.accept) {
22+
set req.http.x-fos-original-accept = req.http.accept;
23+
}
24+
set req.http.accept = "application/vnd.fos.user-context-hash";
2325

2426
# A little hack for testing all scenarios. Choose one for your application.
2527
if ("failure" == req.http.x-cache-hash) {
@@ -40,11 +42,15 @@ sub vcl_recv {
4042
if (req.restarts > 0
4143
&& req.http.accept == "application/vnd.fos.user-context-hash"
4244
) {
43-
set req.url = req.http.x-fos-original-url;
44-
set req.http.accept = req.http.x-fos-original-accept;
45-
45+
set req.url = req.http.x-fos-original-url;
4646
unset req.http.x-fos-original-url;
47-
unset req.http.x-fos-original-accept;
47+
if (req.http.x-fos-original-accept) {
48+
set req.http.accept = req.http.x-fos-original-accept;
49+
unset req.http.x-fos-original-accept;
50+
} else {
51+
# If accept header was not set in original request, remove the header here.
52+
unset req.http.accept;
53+
}
4854

4955
# Force the lookup, the backend must tell not to cache or vary on the
5056
# user hash to properly separate cached data.

tests/Functional/Fixtures/web/user_context.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111

1212
header('X-Cache-Debug: 1');
1313

14+
if (isset($_GET['accept'])) {
15+
if ($_GET['accept'] != $_SERVER['HTTP_ACCEPT']) {
16+
header('HTTP/1.1 500 Wrong accept header "' . $_SERVER['HTTP_ACCEPT'] . '", expected "' . $_GET['accept'] . '"');
17+
exit;
18+
}
19+
} elseif (isset($_SERVER['HTTP_ACCEPT'])) {
20+
header('HTTP/1.1 500 Expected no accept header ' . $_SERVER['HTTP_ACCEPT']);
21+
exit;
22+
}
23+
1424
if ('POST' == strtoupper($_SERVER['REQUEST_METHOD'])) {
1525
echo "POST";
1626
exit;

tests/Functional/Varnish/UserContextTestCase.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ abstract class UserContextTestCase extends VarnishTestCase
2727
*/
2828
abstract protected function assertContextCache($hashCache);
2929

30+
/**
31+
* Sending requests without an Accept: header so none should arrive at the
32+
* backend for the actual request.
33+
*/
3034
public function testUserContextHash()
3135
{
3236
$response1 = $this->getResponse('/user_context.php', array(), array('cookies' => array('foo')));
@@ -64,6 +68,17 @@ public function testUserContextHash()
6468
$this->assertHit($headResponse2);
6569
}
6670

71+
public function testAcceptHeader()
72+
{
73+
$response1 = $this->getResponse(
74+
'/user_context.php?accept=text/plain',
75+
array('Accept' => 'text/plain'),
76+
array('cookies' => array('foo'))
77+
);
78+
$this->assertEquals('foo', $response1->getBody(true));
79+
80+
}
81+
6782
public function testUserContextUnauthorized()
6883
{
6984
try {

0 commit comments

Comments
 (0)