Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 96a8caa

Browse files
James Halsalljameshalsall
James Halsall
authored andcommitted
[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 f9bb4ab commit 96a8caa

File tree

7 files changed

+90
-4
lines changed

7 files changed

+90
-4
lines changed

UPGRADE-4.0.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ HttpKernel
160160

161161
* The `DataCollector::varToString()` method has been removed in favor of `cloneVar()`.
162162

163+
* The `X-Status-Code` header method of setting a custom status code in the response
164+
when handling exceptions has been removed. There is now a new
165+
`GetResponseForExceptionEvent::setAllowCustomResponseCode()` method instead, which
166+
will tell the Kernel to use the response code set on the event's response object.
167+
163168
Serializer
164169
----------
165170

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ class GetResponseForExceptionEvent extends GetResponseEvent
3636
*/
3737
private $exception;
3838

39+
/**
40+
* @var bool
41+
*/
42+
private $allowCustomResponseCode = false;
43+
3944
public function __construct(HttpKernelInterface $kernel, Request $request, $requestType, \Exception $e)
4045
{
4146
parent::__construct($kernel, $request, $requestType);
@@ -64,4 +69,24 @@ public function setException(\Exception $exception)
6469
{
6570
$this->exception = $exception;
6671
}
72+
73+
/**
74+
* Mark the event as allowing a custom, non-500 response code.
75+
*
76+
* @param bool $allowCustomResponseCode
77+
*/
78+
public function setAllowCustomResponseCode($allowCustomResponseCode)
79+
{
80+
$this->allowCustomResponseCode = $allowCustomResponseCode;
81+
}
82+
83+
/**
84+
* Returns true if the event allows a custom, non-500 response code.
85+
*
86+
* @return bool
87+
*/
88+
public function allowCustomResponseCode()
89+
{
90+
return $this->allowCustomResponseCode;
91+
}
6792
}

src/Symfony/Component/HttpKernel/HttpKernel.php

Lines 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::setAllowCustomResponseCode() 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->allowCustomResponseCode() && !$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
Lines 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 <[email protected]>
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->allowCustomResponseCode());
25+
}
26+
}

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

Lines changed: 29 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;
@@ -110,9 +111,10 @@ public function testHandleHttpException()
110111
}
111112

112113
/**
114+
* @group legacy
113115
* @dataProvider getStatusCodes
114116
*/
115-
public function testHandleWhenAnExceptionIsHandledWithASpecificStatusCode($responseStatusCode, $expectedStatusCode)
117+
public function testLegacyHandleWhenAnExceptionIsHandledWithASpecificStatusCode($responseStatusCode, $expectedStatusCode)
116118
{
117119
$dispatcher = new EventDispatcher();
118120
$dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) use ($responseStatusCode, $expectedStatusCode) {
@@ -136,6 +138,32 @@ public function getStatusCodes()
136138
);
137139
}
138140

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

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

Lines 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

Lines 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)