Skip to content

Commit

Permalink
Implement a 404 handler so that if the ESI URL is somehow incorrect, …
Browse files Browse the repository at this point in the history
…full Drupal 404 pages do not get embedded within another page.
  • Loading branch information
manarth committed Mar 3, 2012
1 parent d4f37f8 commit d222527
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
6 changes: 5 additions & 1 deletion esi.module
Expand Up @@ -106,7 +106,11 @@ function esi_admin_menu_output_alter(&$admin_menu) {
*/
function esi_component_load($component) {
$components = esi_get_components();
return array_key_exists($component, $components) ? $components[$component] : FALSE;
// Do not return FALSE: even if the requested component is not valid, allow
// the menu handler to return control to esi_handle_component() in order to
// return a custom ESI 404. Having an entire theme Drupal 404 page embedded
// within another Drupal page would not be desirable.
return array_key_exists($component, $components) ? $components[$component] : NULL;
}

/**
Expand Down
29 changes: 29 additions & 0 deletions esi.pages.inc
Expand Up @@ -6,8 +6,16 @@

/**
* Menu callback to handle an ESI component.
*
* @see esi_component_load().
*/
function esi_handle_component($component) {
// The menu wildcard loader will return NULL for invalid components, so that
// the menu-handler will delegate 404 delivery here.
if (empty($component)) {
esi_fast_404();
}

// Remove the component from the arguments.
$args = array_slice(func_get_args(),1);

Expand Down Expand Up @@ -63,3 +71,24 @@ function esi_deliver_esi_component($esi_rendered_component) {
// activity to commit.
drupal_page_footer();
}

/**
* Generate a fast 404 (consisting of a single HTML comment).
* Full delivery is not desired, because it would typically result in embedding
* a complete, themed Drupal 404 page *within* another page.
*
* @see drupal_fast_404()
*/
function esi_fast_404() {
drupal_add_http_header('Status', '404 Not Found');
drupal_add_http_header('X-ESI', 'Component not recognised');
watchdog('page not found', check_plain($_GET['q']), NULL, WATCHDOG_WARNING);

// Unlike drupal_fast_404(), the HTML furniture (html tag, head tag, etc) are
// not delivered
echo "<!-- esi_fast_404 -->\n";

// Unlike normal page-views, invoking hook_exit() is unneccessary overhead.
// This use of exit matches the behaviour of drupal_fast_404().
exit;
}

0 comments on commit d222527

Please sign in to comment.