Commit 48922a93 authored by Denis Rudoi's avatar Denis Rudoi Committed by Stephen Mustgrave
Browse files

Issue #3396634 by dewalt: CORS headers aren't applied to the module response

parent 6c1e046c
Loading
Loading
Loading
Loading
Loading
+7 −9
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ namespace Drupal\stage_file_proxy\EventSubscriber;

use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\Core\StreamWrapper\StreamWrapperManager;
use Drupal\Core\Url;
use Drupal\stage_file_proxy\DownloadManagerInterface;
@@ -11,6 +12,7 @@ use Drupal\stage_file_proxy\EventDispatcher\AlterExcludedPathsEvent;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
@@ -192,12 +194,13 @@ class StageFileProxySubscriber implements EventSubscriberInterface {
      ];

      if ($config->get('hotlink')) {

        $location = Url::fromUri("$server/$remote_file_dir/$relative_path", [
          'query' => $query_parameters,
          'absolute' => TRUE,
        ])->toString();

        $response = new TrustedRedirectResponse($location);
        $response->addCacheableDependency($config);
        $event->setResponse($response);
      }
      elseif ($this->manager->fetch($server, $remote_file_dir, $fetch_path, $options)) {
        // Refresh this request & let the web server work out mime type, etc.
@@ -205,13 +208,8 @@ class StageFileProxySubscriber implements EventSubscriberInterface {
          'query' => $query_parameters,
          'absolute' => TRUE,
        ])->toString();
        // Avoid redirection caching in upstream proxies.
        header("Cache-Control: must-revalidate, no-cache, post-check=0, pre-check=0, private");
      }

      if (isset($location)) {
        header("Location: $location");
        exit;
        // Use default cache control: must-revalidate, no-cache, private.
        $event->setResponse(new RedirectResponse($location));
      }
    }
  }