Skip to content

Commit

Permalink
TASK: Widget view helper explicitly merge things
Browse files Browse the repository at this point in the history
`replaceHttpResponse` is unsafe to use and has hard to tell behaviour.
Instead, we up-merge the headers explicitly
  • Loading branch information
mhsdesign committed Mar 2, 2024
1 parent 2120f98 commit 1ed9617
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions Neos.FluidAdaptor/Classes/Core/Widget/AbstractWidgetViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,19 +239,41 @@ protected function initiateSubRequest()
$subRequest->setControllerObjectName($this->widgetContext->getControllerObjectName());
try {
$subResponse = $this->controller->processRequest($subRequest);

// We need to make sure to not merge content up into the parent ActionResponse because that _could_ break the parent response.
$content = $subResponse->getBody()->getContents();
$subResponse = $subResponse->withBody(Utils::streamFor(''));
} catch (StopActionException $exception) {
$subResponse = $exception->response;
$this->controllerContext->getResponse()->replaceHttpResponse($subResponse);
throw $exception;
$parentResponse = $this->controllerContext->getResponse()->buildHttpResponse();

// transfer possible headers that have been set dynamically
foreach ($subResponse->getHeaders() as $name => $values) {
$parentResponse = $parentResponse->withHeader($name, $values);
}
// if the status code is 200 we assume it's the default and will not overrule it
if ($subResponse->getStatusCode() !== 200) {
$parentResponse = $parentResponse->withStatus($subResponse->getStatusCode());
}

if ($subResponse->getBody()->getSize() !== 0) {
$parentResponse = $parentResponse->withBody($subResponse->getBody());
}

throw StopActionException::createForResponse($parentResponse, 'Intercepted from widget view helper.');
} catch (ForwardException $exception) {
$subRequest = $exception->nextRequest;
continue;
}
$this->controllerContext->getResponse()->replaceHttpResponse($subResponse);

// We need to make sure to not merge content up into the parent ActionResponse because that _could_ break the parent response.
$content = $subResponse->getBody()->getContents();

// hacky, but part of the deal. We have to manipulate the global response to redirect for example.
// transfer possible headers that have been set dynamically
foreach ($subResponse->getHeaders() as $name => $values) {
$this->controllerContext->getResponse()->setHttpHeader($name, $values);
}
// if the status code is 200 we assume it's the default and will not overrule it
if ($subResponse->getStatusCode() !== 200) {
$this->controllerContext->getResponse()->setStatusCode($subResponse->getStatusCode());
}
}

return $content;
Expand Down

0 comments on commit 1ed9617

Please sign in to comment.