Skip to content

Commit

Permalink
Merge pull request #134 from FriendsOfSymfony/fix-accept-header-handling
Browse files Browse the repository at this point in the history
fix accept header handling when client sends no accept header
  • Loading branch information
ddeboer committed Nov 21, 2014
2 parents 480615b + e894523 commit 4e227b4
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 16 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Changelog
=========

1.1.2
-----

* **2014-11-17** Fixed documentation for user context varnish configuration to also work when
client omits the `Accept` HTTP header.
2 changes: 2 additions & 0 deletions tests/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
deny from all

22 changes: 14 additions & 8 deletions tests/Functional/Fixtures/varnish-3/user_context.vcl
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ sub vcl_recv {
&& (req.http.cookie || req.http.authorization)
&& (req.request == "GET" || req.request == "HEAD")
) {
set req.http.x-fos-original-url = req.url;
set req.http.x-fos-original-accept = req.http.accept;

set req.http.accept = "application/vnd.fos.user-context-hash";
set req.http.x-fos-original-url = req.url;
# Backup accept header, if set
if (req.http.accept) {
set req.http.x-fos-original-accept = req.http.accept;
}
set req.http.accept = "application/vnd.fos.user-context-hash";

# A little hack for testing all scenarios. Choose one for your application.
if ("failure" == req.http.x-cache-hash) {
Expand All @@ -38,11 +40,15 @@ sub vcl_recv {
if (req.restarts > 0
&& req.http.accept == "application/vnd.fos.user-context-hash"
) {
set req.url = req.http.x-fos-original-url;
set req.http.accept = req.http.x-fos-original-accept;

set req.url = req.http.x-fos-original-url;
unset req.http.x-fos-original-url;
unset req.http.x-fos-original-accept;
if (req.http.x-fos-original-accept) {
set req.http.accept = req.http.x-fos-original-accept;
unset req.http.x-fos-original-accept;
} else {
# If accept header was not set in original request, remove the header here.
unset req.http.accept;
}

# Force the lookup, the backend must tell not to cache or vary on the
# user hash to properly separate cached data.
Expand Down
22 changes: 14 additions & 8 deletions tests/Functional/Fixtures/varnish-4/user_context.vcl
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ sub vcl_recv {
&& (req.http.cookie || req.http.authorization)
&& (req.method == "GET" || req.method == "HEAD")
) {
set req.http.x-fos-original-url = req.url;
set req.http.x-fos-original-accept = req.http.accept;

set req.http.accept = "application/vnd.fos.user-context-hash";
set req.http.x-fos-original-url = req.url;
# Backup accept header, if set
if (req.http.accept) {
set req.http.x-fos-original-accept = req.http.accept;
}
set req.http.accept = "application/vnd.fos.user-context-hash";

# A little hack for testing all scenarios. Choose one for your application.
if ("failure" == req.http.x-cache-hash) {
Expand All @@ -40,11 +42,15 @@ sub vcl_recv {
if (req.restarts > 0
&& req.http.accept == "application/vnd.fos.user-context-hash"
) {
set req.url = req.http.x-fos-original-url;
set req.http.accept = req.http.x-fos-original-accept;

set req.url = req.http.x-fos-original-url;
unset req.http.x-fos-original-url;
unset req.http.x-fos-original-accept;
if (req.http.x-fos-original-accept) {
set req.http.accept = req.http.x-fos-original-accept;
unset req.http.x-fos-original-accept;
} else {
# If accept header was not set in original request, remove the header here.
unset req.http.accept;
}

# Force the lookup, the backend must tell not to cache or vary on the
# user hash to properly separate cached data.
Expand Down
10 changes: 10 additions & 0 deletions tests/Functional/Fixtures/web/user_context.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@

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

if (isset($_GET['accept'])) {
if ($_GET['accept'] != $_SERVER['HTTP_ACCEPT']) {
header('HTTP/1.1 500 Wrong accept header "' . $_SERVER['HTTP_ACCEPT'] . '", expected "' . $_GET['accept'] . '"');
exit;
}
} elseif (isset($_SERVER['HTTP_ACCEPT'])) {
header('HTTP/1.1 500 Expected no accept header ' . $_SERVER['HTTP_ACCEPT']);
exit;
}

if ('POST' == strtoupper($_SERVER['REQUEST_METHOD'])) {
echo "POST";
exit;
Expand Down
15 changes: 15 additions & 0 deletions tests/Functional/Varnish/UserContextTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ abstract class UserContextTestCase extends VarnishTestCase
*/
abstract protected function assertContextCache($hashCache);

/**
* Sending requests without an Accept: header so none should arrive at the
* backend for the actual request.
*/
public function testUserContextHash()
{
$response1 = $this->getResponse('/user_context.php', array(), array('cookies' => array('foo')));
Expand Down Expand Up @@ -64,6 +68,17 @@ public function testUserContextHash()
$this->assertHit($headResponse2);
}

public function testAcceptHeader()
{
$response1 = $this->getResponse(
'/user_context.php?accept=text/plain',
array('Accept' => 'text/plain'),
array('cookies' => array('foo'))
);
$this->assertEquals('foo', $response1->getBody(true));

}

public function testUserContextUnauthorized()
{
try {
Expand Down

0 comments on commit 4e227b4

Please sign in to comment.