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 75d9d7c

Browse filesBrowse files
author
James Halsall
committed
[HttpKernel] Deprecate X-Status-Code for better alternative
This marks the X-Status-Code header method of setting a custom response status code in exception listeners as deprecated. Instead there is now a new method on the GetResponseForExceptionEvent that allows successful status codes in the response sent to the client.
1 parent 3521105 commit 75d9d7c
Copy full SHA for 75d9d7c

File tree

7 files changed

+90
-4
lines changed
Filter options

7 files changed

+90
-4
lines changed

‎UPGRADE-4.0.md

Copy file name to clipboardExpand all lines: UPGRADE-4.0.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ HttpKernel
132132
have your own `ControllerResolverInterface` implementation, you should
133133
inject an `ArgumentResolverInterface` instance.
134134

135+
* The `X-Status-Code` header method of setting a custom status code in the response
136+
when handling exceptions has been removed. There is now a new
137+
`GetResponseForExceptionEvent::setAllowSuccessfulResponse()` method instead, which
138+
will tell the Kernel to use the response code set on the event's response object.
139+
135140
Serializer
136141
----------
137142

‎src/Symfony/Component/HttpKernel/Event/GetResponseForExceptionEvent.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Event/GetResponseForExceptionEvent.php
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,17 @@ class GetResponseForExceptionEvent extends GetResponseEvent
3636
*/
3737
private $exception;
3838

39+
/**
40+
* @var bool
41+
*/
42+
private $allowSuccessfulResponse;
43+
3944
public function __construct(HttpKernelInterface $kernel, Request $request, $requestType, \Exception $e)
4045
{
4146
parent::__construct($kernel, $request, $requestType);
4247

4348
$this->setException($e);
49+
$this->allowSuccessfulResponse = false;
4450
}
4551

4652
/**
@@ -64,4 +70,24 @@ public function setException(\Exception $exception)
6470
{
6571
$this->exception = $exception;
6672
}
73+
74+
/**
75+
* Mark the event as allowing a successful response code.
76+
*
77+
* @param bool $allowSuccessfulResponse
78+
*/
79+
public function setAllowSuccessfulResponse($allowSuccessfulResponse)
80+
{
81+
$this->allowSuccessfulResponse = $allowSuccessfulResponse;
82+
}
83+
84+
/**
85+
* Returns true if the event allows a successful response code.
86+
*
87+
* @return bool
88+
*/
89+
public function allowSuccessfulResponse()
90+
{
91+
return $this->allowSuccessfulResponse;
92+
}
6793
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/HttpKernel.php
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,12 @@ private function handleException(\Exception $e, $request, $type)
242242

243243
// the developer asked for a specific status code
244244
if ($response->headers->has('X-Status-Code')) {
245+
@trigger_error(sprintf('Using the X-Status-Code header is deprecated, use %s::setAllowSuccessfulResponse() instead.', GetResponseForExceptionEvent::class), E_USER_DEPRECATED);
246+
245247
$response->setStatusCode($response->headers->get('X-Status-Code'));
246248

247249
$response->headers->remove('X-Status-Code');
248-
} elseif (!$response->isClientError() && !$response->isServerError() && !$response->isRedirect()) {
250+
} elseif (!$event->allowSuccessfulResponse() && !$response->isClientError() && !$response->isServerError() && !$response->isRedirect()) {
249251
// ensure that we actually have an error response
250252
if ($e instanceof HttpExceptionInterface) {
251253
// keep the HTTP status code and headers
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Tests\Event;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
16+
use Symfony\Component\HttpKernel\Tests\TestHttpKernel;
17+
18+
class GetResponseForExceptionEventTest extends \PHPUnit_Framework_TestCase
19+
{
20+
public function testAllowSuccessfulResponseIsFalseByDefault()
21+
{
22+
$event = new GetResponseForExceptionEvent(new TestHttpKernel(), new Request(), 1, new \Exception());
23+
24+
$this->assertFalse($event->allowSuccessfulResponse());
25+
}
26+
}

‎src/Symfony/Component/HttpKernel/Tests/HttpKernelTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Tests/HttpKernelTest.php
+28-1Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
1717
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
1818
use Symfony\Component\HttpKernel\Event\FilterControllerArgumentsEvent;
19+
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
1920
use Symfony\Component\HttpKernel\HttpKernel;
2021
use Symfony\Component\HttpKernel\HttpKernelInterface;
2122
use Symfony\Component\HttpKernel\KernelEvents;
@@ -112,7 +113,7 @@ public function testHandleHttpException()
112113
/**
113114
* @dataProvider getStatusCodes
114115
*/
115-
public function testHandleWhenAnExceptionIsHandledWithASpecificStatusCode($responseStatusCode, $expectedStatusCode)
116+
public function testLegacyHandleWhenAnExceptionIsHandledWithASpecificStatusCode($responseStatusCode, $expectedStatusCode)
116117
{
117118
$dispatcher = new EventDispatcher();
118119
$dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) use ($responseStatusCode, $expectedStatusCode) {
@@ -136,6 +137,32 @@ public function getStatusCodes()
136137
);
137138
}
138139

140+
/**
141+
* @dataProvider getStatusCodes
142+
*/
143+
public function testHandleWhenAnExceptionIsHandledWithASpecificStatusCode($expectedStatusCode)
144+
{
145+
$dispatcher = new EventDispatcher();
146+
$dispatcher->addListener(KernelEvents::EXCEPTION, function (GetResponseForExceptionEvent $event) use ($expectedStatusCode) {
147+
$event->setAllowSuccessfulResponse(true);
148+
$event->setResponse(new Response('', $expectedStatusCode));
149+
});
150+
151+
$kernel = $this->getHttpKernel($dispatcher, function () { throw new \RuntimeException(); });
152+
$response = $kernel->handle(new Request());
153+
154+
$this->assertEquals($expectedStatusCode, $response->getStatusCode());
155+
}
156+
157+
public function getSuccessfulStatusCodes()
158+
{
159+
return array(
160+
array(200),
161+
array(204),
162+
array(201),
163+
);
164+
}
165+
139166
public function testHandleWhenAListenerReturnsAResponse()
140167
{
141168
$dispatcher = new EventDispatcher();

‎src/Symfony/Component/Security/Http/EntryPoint/FormAuthenticationEntryPoint.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Http/EntryPoint/FormAuthenticationEntryPoint.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function start(Request $request, AuthenticationException $authException =
5454

5555
$response = $this->httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
5656
if (200 === $response->getStatusCode()) {
57-
$response->headers->set('X-Status-Code', 401);
57+
$response->setStatusCode(401);
5858
}
5959

6060
return $response;

‎src/Symfony/Component/Security/Http/Tests/EntryPoint/FormAuthenticationEntryPointTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Http/Tests/EntryPoint/FormAuthenticationEntryPointTest.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@ public function testStartWithUseForward()
6363
$entryPointResponse = $entryPoint->start($request);
6464

6565
$this->assertEquals($response, $entryPointResponse);
66-
$this->assertEquals(401, $entryPointResponse->headers->get('X-Status-Code'));
66+
$this->assertEquals(401, $entryPointResponse->getStatusCode());
6767
}
6868
}

0 commit comments

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