10000 [HttpKernel] Fix missing Request in RequestStack for StreamedResponse · symfony/symfony@ca1c40e · GitHub
[go: up one dir, main page]

Skip to content

Commit ca1c40e

Browse files
Ismail Turannicolas-grekas
Ismail Turan
authored andcommitted
[HttpKernel] Fix missing Request in RequestStack for StreamedResponse
1 parent 4c1d953 commit ca1c40e

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

src/Symfony/Component/HttpFoundation/StreamedResponse.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ public function setCallback(callable $callback): static
5656
return $this;
5757
}
5858

59+
public function getCallback(): \Closure
60+
{
61+
return ($this->callback)(...);
62+
}
63+
5964
/**
6065
* This method only sends the headers once.
6166
*

src/Symfony/Component/HttpKernel/HttpKernel.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\RequestStack;
1717
use Symfony\Component\HttpFoundation\Response;
18+
use Symfony\Component\HttpFoundation\StreamedResponse;
1819
use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
1920
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
2021
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
@@ -70,8 +71,9 @@ public function handle(Request $request, int $type = HttpKernelInterface::MAIN_R
7071
$request->headers->set('X-Php-Ob-Level', (string) ob_get_level());
7172

7273
$this->requestStack->push($request);
74+
$response = null;
7375
try {
74-
return $this->handleRaw($request, $type);
76+
return $response = $this->handleRaw($request, $type);
7577
} catch (\Throwable $e) {
7678
if ($e instanceof \Error && !$this->handleAllThrowables) {
7779
throw $e;
@@ -86,9 +88,23 @@ public function handle(Request $request, int $type = HttpKernelInterface::MAIN_R
8688
throw $e;
8789
}
8890

89-
return $this->handleThrowable($e, $request, $type);
91+
return $response = $this->handleThrowable($e, $request, $type);
9092
} finally {
9193
$this->requestStack->pop();
94+
95+
if ($response instanceof StreamedResponse) {
96+
$callback = $response->getCallback();
97+
$requestStack = $this->requestStack;
98+
99+
$response->setCallback(static function () use ($request, $callback, $requestStack) {
100+
$requestStack->push($request);
101+
try {
102+
$callback();
103+
} finally {
104+
$requestStack->pop();
105+
}
106+
});
107+
}
92108
}
93109
}
94110

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\HttpFoundation\Request;
1919
use Symfony\Component\HttpFoundation\RequestStack;
2020
use Symfony\Component\HttpFoundation\Response;
21+
use Symfony\Component\HttpFoundation\StreamedResponse;
2122
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
2223
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
2324
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
@@ -457,6 +458,23 @@ public function testVerifyRequestStackPushPopDuringHandle()
457458
$kernel->handle($request, HttpKernelInterface::MAIN_REQUEST);
458459
}
459460

461+
public function testVerifyRequestStackPushPopWithStreamedResponse()
462+
{
463+
$request = new Request();
464+
$stack = new RequestStack();
465+
$dispatcher = new EventDispatcher();
466+
$kernel = $this->getHttpKernel($dispatcher, fn () => new StreamedResponse(function () use ($stack) {
467+
echo $stack->getMainRequest()::class;
468+
}), $stack);
469+
470+
$response = $kernel->handle($request, HttpKernelInterface::MAIN_REQUEST);
471+
self::assertNull($stack->getMainRequest());
472+
ob_start();
473+
$response->send();
474+
self::assertSame(Request::class, ob_get_clean());
475+
self::assertNull($stack->getMainRequest());
476+
}
477+
460478
public function testInconsistentClientIpsOnMainRequests()
461479
{
462480
$this->expectException(BadRequestHttpException::class);

src/Symfony/Component/HttpKernel/composer.json

Lines changed: 1 addition & 1 deletion
< 6BC1 th scope="col">Original file line number
Diff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"symfony/deprecation-contracts": "^2.5|^3",
2121
"symfony/error-handler": "^6.3",
2222
"symfony/event-dispatcher": "^5.4|^6.0",
23-
"symfony/http-foundation": "^6.2.7",
23+
"symfony/http-foundation": "^6.3.4",
2424
"symfony/polyfill-ctype": "^1.8",
2525
"psr/log": "^1|^2|^3"
2626
},

0 commit comments

Comments
 (0)
0