[HttpCache] fix: do not cache OPTIONS request

This commit is contained in:
David Maicher 2016-10-13 22:14:35 +02:00
parent df138fb636
commit c43de7f21a
5 changed files with 53 additions and 2 deletions

View File

@ -1473,6 +1473,16 @@ class Request
return in_array($this->getMethod(), array('GET', 'HEAD', 'OPTIONS', 'TRACE')); return in_array($this->getMethod(), array('GET', 'HEAD', 'OPTIONS', 'TRACE'));
} }
/**
* Checks whether the method is cachaeble or not.
*
* @return bool
*/
public function isMethodCacheable()
{
return in_array($this->getMethod(), array('GET', 'HEAD'));
}
/** /**
* Returns the request body content. * Returns the request body content.
* *

View File

@ -1948,6 +1948,32 @@ class RequestTest extends \PHPUnit_Framework_TestCase
array('CONNECT', false), array('CONNECT', false),
); );
} }
/**
* @dataProvider methodCacheableProvider
*/
public function testMethodCacheable($method, $chacheable)
{
$request = new Request();
$request->setMethod($method);
$this->assertEquals($chacheable, $request->isMethodCacheable());
}
public function methodCacheableProvider()
{
return array(
array('HEAD', true),
array('GET', true),
array('POST', false),
array('PUT', false),
array('PATCH', false),
array('DELETE', false),
array('PURGE', false),
array('OPTIONS', false),
array('TRACE', false),
array('CONNECT', false),
);
}
} }
class RequestContentProxy extends Request class RequestContentProxy extends Request

View File

@ -204,7 +204,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
if (!$request->isMethodSafe()) { if (!$request->isMethodSafe()) {
$response = $this->invalidate($request, $catch); $response = $this->invalidate($request, $catch);
} elseif ($request->headers->has('expect')) { } elseif ($request->headers->has('expect') || !$request->isMethodCacheable()) {
$response = $this->pass($request, $catch); $response = $this->pass($request, $catch);
} else { } else {
$response = $this->lookup($request, $catch); $response = $this->lookup($request, $catch);

View File

@ -1264,6 +1264,21 @@ class HttpCacheTest extends HttpCacheTestCase
$this->assertNull($this->response->getETag()); $this->assertNull($this->response->getETag());
$this->assertNull($this->response->getLastModified()); $this->assertNull($this->response->getLastModified());
} }
public function testDoesNotCacheOptionsRequest()
{
$this->setNextResponse(200, array('Cache-Control' => 'public, s-maxage=60'), 'get');
$this->request('GET', '/');
$this->assertHttpKernelIsCalled();
$this->setNextResponse(200, array('Cache-Control' => 'public, s-maxage=60'), 'options');
$this->request('OPTIONS', '/');
$this->assertHttpKernelIsCalled();
$this->request('GET', '/');
$this->assertHttpKernelIsNotCalled();
$this->assertSame('get', $this->response->getContent());
}
} }
class TestKernel implements HttpKernelInterface class TestKernel implements HttpKernelInterface

View File

@ -18,7 +18,7 @@
"require": { "require": {
"php": ">=5.3.9", "php": ">=5.3.9",
"symfony/event-dispatcher": "~2.6,>=2.6.7", "symfony/event-dispatcher": "~2.6,>=2.6.7",
"symfony/http-foundation": "~2.7.15|~2.8.8", "symfony/http-foundation": "~2.7.20|~2.8.13",
"symfony/debug": "~2.6,>=2.6.2", "symfony/debug": "~2.6,>=2.6.2",
"psr/log": "~1.0" "psr/log": "~1.0"
}, },