Skip to content

Commit 52668b3

Browse files
committed
[HttpCache] fix: do not cache OPTIONS request
1 parent df138fb commit 52668b3

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

src/Symfony/Component/HttpFoundation/Request.php

+10
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,16 @@ public function isMethodSafe()
14731473
return in_array($this->getMethod(), array('GET', 'HEAD', 'OPTIONS', 'TRACE'));
14741474
}
14751475

1476+
/**
1477+
* Checks whether the method is cachaeble or not.
1478+
*
1479+
* @return bool
1480+
*/
1481+
public function isMethodCacheable()
1482+
{
1483+
return in_array($this->getMethod(), array('GET', 'HEAD'));
1484+
}
1485+
14761486
/**
14771487
* Returns the request body content.
14781488
*

src/Symfony/Component/HttpFoundation/Tests/RequestTest.php

+26
Original file line numberDiff line numberDiff line change
@@ -1948,6 +1948,32 @@ public function methodSafeProvider()
19481948
array('CONNECT', false),
19491949
);
19501950
}
1951+
1952+
/**
1953+
* @dataProvider methodCacheableProvider
1954+
*/
1955+
public function testMethodCacheable($method, $chacheable)
1956+
{
1957+
$request = new Request();
1958+
$request->setMethod($method);
1959+
$this->assertEquals($chacheable, $request->isMethodCacheable());
1960+
}
1961+
1962+
public function methodCacheableProvider()
1963+
{
1964+
return array(
1965+
array('HEAD', true),
1966+
array('GET', true),
1967+
array('POST', false),
1968+
array('PUT', false),
1969+
array('PATCH', false),
1970+
array('DELETE', false),
1971+
array('PURGE', false),
1972+
array('OPTIONS', false),
1973+
array('TRACE', false),
1974+
array('CONNECT', false),
1975+
);
1976+
}
19511977
}
19521978

19531979
class RequestContentProxy extends Request

src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
204204

205205
if (!$request->isMethodSafe()) {
206206
$response = $this->invalidate($request, $catch);
207-
} elseif ($request->headers->has('expect')) {
207+
} elseif ($request->headers->has('expect') || !$request->isMethodCacheable()) {
208208
$response = $this->pass($request, $catch);
209209
} else {
210210
$response = $this->lookup($request, $catch);

src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,21 @@ public function testEsiCacheRemoveValidationHeadersIfEmbeddedResponses()
12641264
$this->assertNull($this->response->getETag());
12651265
$this->assertNull($this->response->getLastModified());
12661266
}
1267+
1268+
public function testDoesNotCacheOptionsRequest()
1269+
{
1270+
$this->setNextResponse(200, array('Cache-Control' => 'public, s-maxage=60'), 'get');
1271+
$this->request('GET', '/');
1272+
$this->assertHttpKernelIsCalled();
1273+
1274+
$this->setNextResponse(200, array('Cache-Control' => 'public, s-maxage=60'), 'options');
1275+
$this->request('OPTIONS', '/');
1276+
$this->assertHttpKernelIsCalled();
1277+
1278+
$this->request('GET', '/');
1279+
$this->assertHttpKernelIsNotCalled();
1280+
$this->assertSame('get', $this->response->getContent());
1281+
}
12671282
}
12681283

12691284
class TestKernel implements HttpKernelInterface

0 commit comments

Comments
 (0)