Skip to content

Commit

Permalink
UHF-10819: Removed x-robots-tag header in favor of nginx based solution
Browse files Browse the repository at this point in the history
  • Loading branch information
tuutti committed Oct 24, 2024
1 parent efdf8d4 commit a9a4e0c
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 197 deletions.
2 changes: 0 additions & 2 deletions config/schema/helfi_proxy.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ helfi_proxy.settings:
type: string
session_suffix:
type: string
robots_header_enabled:
type: boolean
prefixes:
type: sequence
sequence:
Expand Down
9 changes: 9 additions & 0 deletions helfi_proxy.install
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,12 @@ function helfi_proxy_update_9002() : void {
$loggers->set('channels', $channels)
->save();
}

/**
* Remove unused 'robots_header_enabled' config.
*/
function helfi_proxy_update_9003() : void {
\Drupal::configFactory()->getEditable('helfi_proxy.settings')
->clear('robots_header_enabled')
->save();
}
60 changes: 8 additions & 52 deletions src/EventSubscriber/RobotsResponseSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@

use Drupal\Core\Cache\CacheableResponseInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\Core\Path\PathMatcherInterface;
use Drupal\helfi_proxy\ProxyManagerInterface;
use Drupal\path_alias\AliasManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

Expand All @@ -21,15 +19,6 @@
*/
final class RobotsResponseSubscriber implements EventSubscriberInterface {

public const X_ROBOTS_TAG_HEADER_NAME = 'DRUPAL_X_ROBOTS_TAG_HEADER';

/**
* The config.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
private ImmutableConfig $config;

/**
* Constructs a new instance.
*
Expand All @@ -43,38 +32,11 @@ final class RobotsResponseSubscriber implements EventSubscriberInterface {
* The path matcher.
*/
public function __construct(
ConfigFactoryInterface $configFactory,
private ConfigFactoryInterface $configFactory,
private CurrentPathStack $pathStack,
private AliasManagerInterface $aliasManager,
private PathMatcherInterface $pathMatcher
) {
$this->config = $configFactory->get('helfi_proxy.settings');
}

/**
* Adds the robots response header.
*
* @param \Symfony\Component\HttpFoundation\Response $response
* The response.
*/
private function addRobotHeader(Response $response) : void {
$response->headers->add(['X-Robots-Tag' => 'noindex, nofollow']);
}

/**
* Checks whether robots header should be present.
*
* @return bool
* TRUE if robots header is enabled for all pages.
*/
private function robotsHeaderEnabled() : bool {
$value = $this->config->get(ProxyManagerInterface::ROBOTS_HEADER_ENABLED);

if ($value !== NULL) {
return (bool) $value;
}
// @todo Remove this in 3.0.0 release.
return (bool) getenv(self::X_ROBOTS_TAG_HEADER_NAME);
}

/**
Expand All @@ -85,26 +47,20 @@ private function robotsHeaderEnabled() : bool {
*/
public function onResponse(ResponseEvent $event) : void {
$response = $event->getResponse();
$config = $this->configFactory->get('helfi_proxy.settings');

if ($response instanceof CacheableResponseInterface) {
$response->addCacheableDependency($this->config);
}

if ($this->robotsHeaderEnabled()) {
$this->addRobotHeader($response);
// No need to check individual paths if robots header should be
// added for every page.
return;
}

if (!$paths = implode("\n", $this->config->get(ProxyManagerInterface::ROBOTS_PATHS) ?? [])) {
if (!$paths = implode("\n", $config->get(ProxyManagerInterface::ROBOTS_PATHS) ?? [])) {
return;
}
$alias = $this->aliasManager
->getAliasByPath($this->pathStack->getPath($event->getRequest()));

if ($this->pathMatcher->matchPath($alias, $paths)) {
$this->addRobotHeader($response);
$response->headers->add(['X-Robots-Tag' => 'noindex, nofollow']);

if ($response instanceof CacheableResponseInterface) {
$response->addCacheableDependency($config);
}
}
}

Expand Down
50 changes: 0 additions & 50 deletions src/Plugin/DebugDataItem/Robots.php

This file was deleted.

1 change: 0 additions & 1 deletion src/ProxyManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ interface ProxyManagerInterface {
public const ROBOTS_PATHS = 'robots_paths';
public const FRONT_PAGE_TITLE = 'front_page_title';
public const SESSION_SUFFIX = 'session_suffix';
public const ROBOTS_HEADER_ENABLED = 'robots_header_enabled';
public const TUNNISTAMO_RETURN_URL = 'tunnistamo_return_url';

/**
Expand Down
44 changes: 0 additions & 44 deletions tests/src/Kernel/Plugin/DebugDataItem/RobotsTest.php

This file was deleted.

48 changes: 0 additions & 48 deletions tests/src/Kernel/RobotsResponseSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,54 +84,6 @@ private function assertResponseEventNoHeader(ResponseEvent $event) : void {
$this->assertNotContains('X-Robots-Tag', $event->getResponse()->headers);
}

/**
* Tests that robots header is present when configuration is set.
*/
public function testConfig() : void {
$this->config('helfi_proxy.settings')
->set(ProxyManagerInterface::ROBOTS_HEADER_ENABLED, FALSE)
->save();
// Make sure setting config to false does not enable the feature.
$event = $this->getResponseEvent();
$this->getSut()->onResponse($event);
$this->assertResponseEventNoHeader($event);

$this->config('helfi_proxy.settings')
->set(ProxyManagerInterface::ROBOTS_HEADER_ENABLED, TRUE)
->save();
$event = $this->getResponseEvent();
$this->getSut()->onResponse($event);
$this->assertResponseEventHasHeader($event);
}

/**
* Tests that robots header is not present when no env variable is set.
*/
public function testNoEnvVariable() : void {
$event = $this->getResponseEvent();
$this->getSut()->onResponse($event);
$this->assertResponseEventNoHeader($event);

// Make sure setting null values to robots env variable does not
// enable the feature.
foreach ([0, NULL, ''] as $value) {
putenv(RobotsResponseSubscriber::X_ROBOTS_TAG_HEADER_NAME . '=' . $value);
$event = $this->getResponseEvent();
$this->getSut()->onResponse($event);
$this->assertResponseEventNoHeader($event);
}
}

/**
* Tests that robots header is added when env variable is present.
*/
public function testEnvVariable() : void {
putenv(RobotsResponseSubscriber::X_ROBOTS_TAG_HEADER_NAME . '=1');
$event = $this->getResponseEvent();
$this->getSut()->onResponse($event);
$this->assertResponseEventHasHeader($event);
}

/**
* Tests robots path handler.
*/
Expand Down

0 comments on commit a9a4e0c

Please sign in to comment.