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 34e7b95

Browse filesBrowse files
[HttpKernel] Deprecate checking for cacheable HTTP methods in Request::isMethodSafe()
1 parent 59f9949 commit 34e7b95
Copy full SHA for 34e7b95

File tree

Expand file treeCollapse file tree

6 files changed

+27
-6
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+27
-6
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/BinaryFileResponse.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public function prepare(Request $request)
190190

191191
if (!$this->headers->has('Accept-Ranges')) {
192192
// Only accept ranges on safe HTTP methods
193-
$this->headers->set('Accept-Ranges', $request->isMethodSafe() ? 'bytes' : 'none');
193+
$this->headers->set('Accept-Ranges', $request->isMethodSafe(false) ? 'bytes' : 'none');
194194
}
195195

196196
if (!$this->headers->has('Content-Type')) {

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Request.php
+11-1Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1475,10 +1475,20 @@ public function isMethod($method)
14751475
/**
14761476
* Checks whether or not the method is safe.
14771477
*
1478+
* @param bool $andCacheable Adds the additional condition that the method should be cacheable. True by default.
1479+
*
14781480
* @return bool
14791481
*/
1480-
public function isMethodSafe()
1482+
public function isMethodSafe(/* $andCacheable = true */)
14811483
{
1484+
if (!func_num_args() || func_get_arg(0)) {
1485+
// This deprecation should be turned into a BadMethodCallException in 4.0 (without adding the argument in the signature)
1486+
// then setting $andCacheable to false should be deprecated in 4.1
1487+
@trigger_error('Checking only for cacheable HTTP methods with Symfony\Component\HttpFoundation\Request::isMethodSafe() is deprecated since version 3.2 and will throw an exception in 4.0. Disable checking only for cacheable methods by calling the method with `false` as first argument or use the Request::isMethodCacheable() instead.', E_USER_DEPRECATED);
1488+
1489+
return in_array($this->getMethod(), array('GET', 'HEAD'));
1490+
}
1491+
14821492
return in_array($this->getMethod(), array('GET', 'HEAD', 'OPTIONS', 'TRACE'));
14831493
}
14841494

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
+12-1Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1994,7 +1994,7 @@ public function testMethodSafe($method, $safe)
19941994
{
19951995
$request = new Request();
19961996
$request->setMethod($method);
1997-
$this->assertEquals($safe, $request->isMethodSafe());
1997+
$this->assertEquals($safe, $request->isMethodSafe(false));
19981998
}
19991999

20002000
public function methodSafeProvider()
@@ -2013,6 +2013,17 @@ public function methodSafeProvider()
20132013
);
20142014
}
20152015

2016+
/**
2017+
* @group legacy
2018+
* @expectedDeprecation Checking only for cacheable HTTP methods with Symfony\Component\HttpFoundation\Request::isMethodSafe() is deprecated since version 3.2 and will throw an exception in 4.0. Disable checking only for cacheable methods by calling the method with `false` as first argument or use the Request::isMethodCacheable() instead.
2019+
*/
2020+
public function testMethodSafeChecksCacheable()
2021+
{
2022+
$request = new Request();
2023+
$request->setMethod('OPTION');
2024+
$this->assertFalse($request->isMethodSafe());
2025+
}
2026+
20162027
/**
20172028
* @dataProvider methodCacheableProvider
20182029
*/

‎src/Symfony/Component/HttpKernel/EventListener/FragmentListener.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/EventListener/FragmentListener.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function onKernelRequest(GetResponseEvent $event)
8181
protected function validateRequest(Request $request)
8282
{
8383
// is the Request safe?
84-
if (!$request->isMethodSafe()) {
84+
if (!$request->isMethodSafe(false)) {
8585
throw new AccessDeniedHttpException();
8686
}
8787

‎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
@@ -182,7 +182,7 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
182182
}
183183
$this->traces[$request->getMethod().' '.$path] = array();
184184

185-
if (!$request->isMethodSafe()) {
185+
if (!$request->isMethodSafe(false)) {
186186
$response = $this->invalidate($request, $catch);
187187
} elseif ($request->headers->has('expect') || !$request->isMethodCacheable()) {
188188
$response = $this->pass($request, $catch);

‎src/Symfony/Component/Security/Http/Firewall/ExceptionListener.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Http/Firewall/ExceptionListener.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ private function startAuthentication(Request $request, AuthenticationException $
220220
protected function setTargetPath(Request $request)
221221
{
222222
// session isn't required when using HTTP basic authentication mechanism for example
223-
if ($request->hasSession() && $request->isMethodSafe() && !$request->isXmlHttpRequest()) {
223+
if ($request->hasSession() && $request->isMethodSafe(false) && !$request->isXmlHttpRequest()) {
224224
$this->saveTargetPath($request->getSession(), $this->providerKey, $request->getUri());
225225
}
226226
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.