Skip to content

Commit c43de7f

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

File tree

5 files changed

+53
-2
lines changed

5 files changed

+53
-2
lines changed

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 10 additions & 0 deletions
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

Lines changed: 26 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 15 additions & 0 deletions
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

src/Symfony/Component/HttpKernel/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"require": {
1919
"php": ">=5.3.9",
2020
"symfony/event-dispatcher": "~2.6,>=2.6.7",
21-
"symfony/http-foundation": "~2.7.15|~2.8.8",
21+
"symfony/http-foundation": "~2.7.20|~2.8.13",
2222
"symfony/debug": "~2.6,>=2.6.2",
2323
"psr/log": "~1.0"
2424
},

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy