Commit 0dc696df authored by alexpott's avatar alexpott

Issue #2218463 by znerol, damiankloip, rbayliss: Fixed Private file and image...

Issue #2218463 by znerol, damiankloip, rbayliss: Fixed Private file and image style downloads throw 500 error when page cache is active.
parent 36596e33
......@@ -11,8 +11,10 @@
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Language\LanguageManager;
use Drupal\Core\Site\Settings;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\HttpKernelInterface;
......@@ -93,8 +95,12 @@ public function onRespond(FilterResponseEvent $event) {
$this->setResponseNotCacheable($response, $request);
}
// Store the response in the internal page cache.
if ($is_cacheable && $this->config->get('cache.page.use_internal')) {
// Currently it is not possible to cache some types of responses. Therefore
// exclude binary file responses (generated files, e.g. images with image
// styles) and streamed responses (files directly read from the disk).
// see: https://github.com/symfony/symfony/issues/9128#issuecomment-25088678
if ($is_cacheable && $this->config->get('cache.page.use_internal') && !($response instanceof BinaryFileResponse) && !($response instanceof StreamedResponse)) {
// Store the response in the internal page cache.
drupal_page_set_cache($response, $request);
$response->headers->set('X-Drupal-Cache', 'MISS');
drupal_serve_page_from_cache($response, $request);
......
......@@ -53,7 +53,27 @@ function testPublicFileTransfer() {
/**
* Test the private file transfer system.
*/
function testPrivateFileTransfer() {
public function testPrivateFileTransferWithoutPageCache() {
$this->doPrivateFileTransferTest();
}
/**
* Test the private file transfer system with page cache.
*/
public function testPrivateFileTransferWithPageCache() {
// Turn on page caching and rerun the test.
$config = \Drupal::config('system.performance');
$config->set('cache.page.use_internal', 1);
$config->set('cache.page.max_age', 300);
$config->save();
$this->doPrivateFileTransferTest();
}
/**
* Test the private file transfer system.
*/
protected function doPrivateFileTransferTest() {
// Set file downloads to private so handler functions get called.
// Create a file.
......@@ -64,8 +84,8 @@ function testPrivateFileTransfer() {
// Set file_test access header to allow the download.
file_test_set_return('download', array('x-foo' => 'Bar'));
$this->drupalGet($url);
$headers = $this->drupalGetHeaders();
$this->assertEqual($headers['x-foo'], 'Bar', 'Found header set by file_test module on private download.');
$this->assertEqual($this->drupalGetHeader('x-foo'), 'Bar', 'Found header set by file_test module on private download.');
$this->assertFalse($this->drupalGetHeader('x-drupal-cache'), 'Page cache is disabled on private file download.');
$this->assertResponse(200, 'Correctly allowed access to a file when file_test provides headers.');
// Test that the file transferred correctly.
......
......@@ -143,6 +143,7 @@ public function deliver(Request $request, $scheme, ImageStyleInterface $image_st
}
if ($success) {
drupal_page_is_cacheable(FALSE);
$image = $this->imageFactory->get($derivative_uri);
$uri = $image->getSource();
$headers += array(
......
......@@ -84,6 +84,22 @@ function testImageStyleUrlAndPathPrivateUnclean() {
$this->doImageStyleUrlAndPathTests('private', FALSE);
}
/**
* Tests an image style URL using the "public://" scheme and page cache.
*/
public function testImageStyleUrlAndPathPublicWithPageCache() {
$this->enablePageCache();
$this->doImageStyleUrlAndPathTests('public');
}
/**
* Tests an image style URL using the "private://" scheme and page cache.
*/
public function testImageStyleUrlAndPathPrivateWithPageCache() {
$this->enablePageCache();
$this->doImageStyleUrlAndPathTests('private');
}
/**
* Tests an image style URL with a file URL that has an extra slash in it.
*/
......@@ -232,4 +248,15 @@ function doImageStyleUrlAndPathTests($scheme, $clean_url = TRUE, $extra_slash =
$this->assertResponse(200, 'Image was accessible at the URL with a missing token.');
}
/**
* Turn on page caching.
*/
protected function enablePageCache() {
// Turn on page caching and rerun the test.
$config = \Drupal::config('system.performance');
$config->set('cache.page.use_internal', 1);
$config->set('cache.page.max_age', 300);
$config->save();
}
}
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