Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit caead56

Browse filesBrowse files
committed
[HttpCache] fix: do not cache OPTIONS request
1 parent df138fb commit caead56
Copy full SHA for caead56

File tree

Expand file treeCollapse file tree

5 files changed

+53
-2
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+53
-2
lines changed

‎src/Symfony/Component/HttpFoundation/Request.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Request.php
+10Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
+26Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
+1-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php
+15Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/composer.json
+1-1Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.