Commit c21e0999 authored by alexpott's avatar alexpott

Issue #2477461 by borisson_, googletorp, Wim Leers, Crell: Move X-Generator...

Issue #2477461 by borisson_, googletorp, Wim Leers, Crell: Move X-Generator header to its own listener
parent ea37327b
......@@ -958,6 +958,10 @@ services:
tags:
- { name: event_subscriber }
arguments: ['@language_manager', '@config.factory', '@page_cache_request_policy', '@page_cache_response_policy', '@cache_contexts_manager']
response_generator_subscriber:
class: Drupal\Core\EventSubscriber\ResponseGeneratorSubscriber
tags:
- { name: event_subscriber }
redirect_response_subscriber:
class: Drupal\Core\EventSubscriber\RedirectResponseSubscriber
arguments: ['@url_generator', '@router.request_context']
......
<?php
/**
* @file
* Contains \Drupal\Core\EventSubscriber\ResponseGeneratorSubscriber.
*/
namespace Drupal\Core\EventSubscriber;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Response subscriber to add X-Generator header tag.
*/
class ResponseGeneratorSubscriber implements EventSubscriberInterface {
/**
* Sets extra X-Generator header on successful responses.
*
* @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
* The event to process.
*/
public function onRespond(FilterResponseEvent $event) {
if (!$event->isMasterRequest()) {
return;
}
$response = $event->getResponse();
// Set the generator in the HTTP header.
list($version) = explode('.', \Drupal::VERSION, 2);
$response->headers->set('X-Generator', 'Drupal ' . $version . ' (https://www.drupal.org)');
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[KernelEvents::RESPONSE][] = ['onRespond'];
return $events;
}
}
......@@ -136,12 +136,8 @@ public function renderResponse(array $main_content, Request $request, RouteMatch
}
$content = $this->renderer->render($html);
// Set the generator in the HTTP header.
list($version) = explode('.', \Drupal::VERSION, 2);
$response = new CacheableResponse($content, 200,[
'Content-Type' => 'text/html; charset=UTF-8',
'X-Generator' => 'Drupal ' . $version . ' (https://www.drupal.org)'
]);
// Bubble the cacheability metadata associated with the rendered render
......
<?php
/**
* @file
* Definition of Drupal\system\Tests\System\ResponseGeneratorTest.
*/
namespace Drupal\system\Tests\System;
use Drupal\rest\Tests\RESTTestBase;
/**
* Tests to see if generator header is added.
*
* @group system
*/
class ResponseGeneratorTest extends RESTTestBase {
/**
* Modules to install.
*
* @var array
*/
public static $modules = array('hal', 'rest', 'node');
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
$permissions = $this->entityPermissions('node', 'view');
$permissions[] = 'restful get entity:node';
$account = $this->drupalCreateUser($permissions);
$this->drupalLogin($account);
}
/**
* Test to see if generator header is added.
*/
function testGeneratorHeaderAdded() {
$node = $this->drupalCreateNode();
list($version) = explode('.', \Drupal::VERSION, 2);
$expectedGeneratorHeader = 'Drupal ' . $version . ' (https://www.drupal.org)';
// Check to see if the header is added when viewing a normal content page
$this->drupalGet($node->urlInfo());
$this->assertResponse(200);
$this->assertEqual('text/html; charset=UTF-8', $this->drupalGetHeader('Content-Type'));
$this->assertEqual($expectedGeneratorHeader, $this->drupalGetHeader('X-Generator'));
// Check to see if the header is also added for a non-successful response
$this->drupalGet('llama');
$this->assertResponse(404);
$this->assertEqual('text/html; charset=UTF-8', $this->drupalGetHeader('Content-Type'));
$this->assertEqual($expectedGeneratorHeader, $this->drupalGetHeader('X-Generator'));
// Enable rest API for nodes
$this->enableService('entity:node', 'GET', 'json');
// Tests to see if this also works for a non-html request
$this->httpRequest($node->urlInfo(), 'GET', NULL, 'application/json');
$this->assertResponse(200);
$this->assertEqual('application/json', $this->drupalGetHeader('Content-Type'));
$this->assertEqual($expectedGeneratorHeader, $this->drupalGetHeader('X-Generator'));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment