Merge branch '2.5-AssetDispatcher-404' into 2.5

Return a 404 much earlier when handling missing theme/plugin assets.

Fixes #2750
This commit is contained in:
mark_story 2014-04-07 21:43:37 -04:00
commit b05ab740d6
2 changed files with 26 additions and 4 deletions

View file

@ -1,7 +1,5 @@
<?php
/**
*
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
@ -38,7 +36,8 @@ class AssetDispatcher extends DispatcherFilter {
* Checks if a requested asset exists and sends it to the browser
*
* @param CakeEvent $event containing the request and response object
* @return CakeResponse if the client is requesting a recognized asset, null otherwise
* @return mixed The resulting response.
* @throws NotFoundException When asset not found
*/
public function beforeDispatch(CakeEvent $event) {
$url = urldecode($event->data['request']->url);
@ -52,13 +51,19 @@ class AssetDispatcher extends DispatcherFilter {
}
$assetFile = $this->_getAssetFile($url);
if ($assetFile === null || !file_exists($assetFile)) {
if ($assetFile === null) {
return null;
}
$response = $event->data['response'];
$event->stopPropagation();
if (!file_exists($assetFile)) {
$response->statusCode(404);
$response->send();
return $response;
}
$response->modified(filemtime($assetFile));
if ($response->checkNotModified($event->data['request'])) {
return $response;

View file

@ -129,6 +129,23 @@ class AssetDispatcherTest extends CakeTestCase {
$this->assertEquals($time->format('D, j M Y H:i:s') . ' GMT', $response->modified());
}
/**
* Test 404 status code is set on missing asset.
*
* @return void
*/
public function test404OnMissingFile() {
$filter = new AssetDispatcher();
$response = $this->getMock('CakeResponse', array('_sendHeader'));
$request = new CakeRequest('/theme/test_theme/img/nope.gif');
$event = new CakeEvent('Dispatcher.beforeRequest', $this, compact('request', 'response'));
$response = $filter->beforeDispatch($event);
$this->assertTrue($event->isStopped());
$this->assertEquals(404, $response->statusCode());
}
/**
* Test that no exceptions are thrown for //index.php type URLs.
*