bug #20205 [HttpCache] fix: do not cache OPTIONS request (dmaicher)

This PR was merged into the 2.7 branch.

Discussion
----------

[HttpCache] fix: do not cache OPTIONS request

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | https://github.com/symfony/symfony/issues/20202
| License       | MIT
| Doc PR        | -

The HttpCache should not cache any OPTIONS request as they are by spec not cacheable (mentioned here https://github.com/symfony/symfony/issues/20202#issuecomment-253033144 by @xabbuh).

Commits
-------

c43de7f [HttpCache] fix: do not cache OPTIONS request
This commit is contained in:
Fabien Potencier 2016-10-13 18:28:24 -07:00
commit 55c3f89ef1
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'));
}
/**
* 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.
*

View File

@ -1948,6 +1948,32 @@ class RequestTest extends \PHPUnit_Framework_TestCase
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

View File

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

View File

@ -1264,6 +1264,21 @@ class HttpCacheTest extends HttpCacheTestCase
$this->assertNull($this->response->getETag());
$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

View File

@ -18,7 +18,7 @@
"require": {
"php": ">=5.3.9",
"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",
"psr/log": "~1.0"
},