Skip to content

Commit 155671b

Browse files
authored
Merge pull request #402 from datafactory/anonymous_request_matcher
Create AnonymousRequestMatcher with isAnonymous Logic to share it with FOSHttpCacheBundle
2 parents 56ea762 + 3525017 commit 155671b

File tree

2 files changed

+55
-16
lines changed

2 files changed

+55
-16
lines changed

src/SymfonyCache/UserContextSubscriber.php

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

1212
namespace FOS\HttpCache\SymfonyCache;
1313

14+
use FOS\HttpCache\UserContext\AnonymousRequestMatcher;
1415
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1516
use Symfony\Component\HttpFoundation\Request;
1617
use Symfony\Component\HttpFoundation\Response;
@@ -185,23 +186,9 @@ private function getUserHash(HttpKernelInterface $kernel, Request $request)
185186
*/
186187
private function isAnonymous(Request $request)
187188
{
188-
// You might have to enable rewriting of the Authorization header in your server config or .htaccess:
189-
// RewriteEngine On
190-
// RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
191-
if ($request->server->has('AUTHORIZATION') ||
192-
$request->server->has('HTTP_AUTHORIZATION') ||
193-
$request->server->has('PHP_AUTH_USER')
194-
) {
195-
return false;
196-
}
197-
198-
foreach ($request->cookies as $name => $value) {
199-
if ($this->isSessionName($name)) {
200-
return false;
201-
}
202-
}
189+
$anonymousRequestMatcher = new AnonymousRequestMatcher($this->options['session_name_prefix']);
203190

204-
return true;
191+
return $anonymousRequestMatcher->matches($request);
205192
}
206193

207194
/**
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSHttpCache package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\HttpCache\UserContext;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
16+
17+
/**
18+
* Matches anonymous requests using a list of identification headers.
19+
*/
20+
class AnonymousRequestMatcher implements RequestMatcherInterface
21+
{
22+
private $sessionNamePrefix;
23+
24+
/**
25+
* @param string $sessionNamePrefix Prefix for session cookies. Must match your PHP session configuration
26+
*/
27+
public function __construct($sessionNamePrefix)
28+
{
29+
$this->sessionNamePrefix = $sessionNamePrefix;
30+
}
31+
32+
public function matches(Request $request)
33+
{
34+
// You might have to enable rewriting of the Authorization header in your server config or .htaccess:
35+
// RewriteEngine On
36+
// RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
37+
if ($request->server->has('AUTHORIZATION') ||
38+
$request->server->has('HTTP_AUTHORIZATION') ||
39+
$request->server->has('PHP_AUTH_USER')
40+
) {
41+
return false;
42+
}
43+
44+
foreach ($request->cookies as $name => $value) {
45+
if (0 === strpos($name, $this->sessionNamePrefix)) {
46+
return false;
47+
}
48+
}
49+
50+
return true;
51+
}
52+
}

0 commit comments

Comments
 (0)