Skip to content
Snippets Groups Projects
Commit 37ac6abc authored by catch's avatar catch
Browse files

Issue #2603788 by Wim Leers, dawehner: HtmlResponseSubscriber does not call...

Issue #2603788 by Wim Leers, dawehner: HtmlResponseSubscriber does not call HtmlResponseAttachmentsProcessor on subrequests
parent 1f4013c5
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -147,7 +147,12 @@ protected function makeSubrequest(GetResponseForExceptionEvent $event, $url, $st ...@@ -147,7 +147,12 @@ protected function makeSubrequest(GetResponseForExceptionEvent $event, $url, $st
} }
$response = $this->httpKernel->handle($sub_request, HttpKernelInterface::SUB_REQUEST); $response = $this->httpKernel->handle($sub_request, HttpKernelInterface::SUB_REQUEST);
$response->setStatusCode($status_code); // Only 2xx responses should have their status code overridden; any
// other status code should be passed on: redirects (3xx), error (5xx)…
// @see https://www.drupal.org/node/2603788#comment-10504916
if ($response->isSuccessful()) {
$response->setStatusCode($status_code);
}
// Persist any special HTTP headers that were set on the exception. // Persist any special HTTP headers that were set on the exception.
if ($exception instanceof HttpExceptionInterface) { if ($exception instanceof HttpExceptionInterface) {
......
...@@ -48,10 +48,6 @@ public function __construct(PlaceholderStrategyInterface $placeholder_strategy) ...@@ -48,10 +48,6 @@ public function __construct(PlaceholderStrategyInterface $placeholder_strategy)
* The event to process. * The event to process.
*/ */
public function onRespond(FilterResponseEvent $event) { public function onRespond(FilterResponseEvent $event) {
if (!$event->isMasterRequest()) {
return;
}
$response = $event->getResponse(); $response = $event->getResponse();
if (!$response instanceof HtmlResponse) { if (!$response instanceof HtmlResponse) {
return; return;
......
...@@ -42,10 +42,6 @@ public function __construct(AttachmentsResponseProcessorInterface $html_response ...@@ -42,10 +42,6 @@ public function __construct(AttachmentsResponseProcessorInterface $html_response
* The event to process. * The event to process.
*/ */
public function onRespond(FilterResponseEvent $event) { public function onRespond(FilterResponseEvent $event) {
if (!$event->isMasterRequest()) {
return;
}
$response = $event->getResponse(); $response = $event->getResponse();
if (!$response instanceof HtmlResponse) { if (!$response instanceof HtmlResponse) {
return; return;
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
*/ */
namespace Drupal\test_page_test\Controller; namespace Drupal\test_page_test\Controller;
use Symfony\Component\HttpKernel\Exception\HttpException;
/** /**
* Defines a test controller for page titles. * Defines a test controller for page titles.
...@@ -75,4 +76,14 @@ public function renderPage() { ...@@ -75,4 +76,14 @@ public function renderPage() {
); );
} }
/**
* Throws a HTTP exception.
*
* @param int $code
* The status code.
*/
public function httpResponseException($code) {
throw new HttpException($code);
}
} }
...@@ -42,3 +42,11 @@ test_page_test.admin_render_title: ...@@ -42,3 +42,11 @@ test_page_test.admin_render_title:
_controller: '\Drupal\test_page_test\Controller\Test::renderTitle' _controller: '\Drupal\test_page_test\Controller\Test::renderTitle'
requirements: requirements:
_access: 'TRUE' _access: 'TRUE'
test_page_test.http_response_exception:
path: '/test-http-response-exception/{code}'
defaults:
_controller: '\Drupal\test_page_test\Controller\Test::httpResponseException'
code: 200
requirements:
_access: 'TRUE'
<?php
/**
* @file
* Contains \Drupal\KernelTests\RequestProcessing\RedirectOnExceptionTest.
*/
namespace Drupal\KernelTests\RequestProcessing;
use Drupal\KernelTests\KernelTestBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Tests redirects on exception pages.
*
* @group request_processing
*/
class RedirectOnExceptionTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['system', 'test_page_test'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installSchema('system', ['router', 'url_alias']);
\Drupal::service('router.builder')->rebuild();
}
public function testRedirectOn404() {
\Drupal::configFactory()->getEditable('system.site')
->set('page.404', '/test-http-response-exception/' . Response::HTTP_PERMANENTLY_REDIRECT)
->save();
/** @var \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel */
$http_kernel = \Drupal::service('http_kernel');
// Foo doesn't exist, so this triggers the 404 page.
$request = Request::create('/foo');
$response = $http_kernel->handle($request);
$this->assertEquals(Response::HTTP_PERMANENTLY_REDIRECT, $response->getStatusCode());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment