You are here

fastly.module in Fastly 7

Same filename and directory in other branches
  1. 8.3 fastly.module
  2. 7.2 fastly.module

Fastly module.

File

fastly.module
View source
<?php

/**
 * @file
 * Fastly module.
 */

/**
 * Implements hook_exit().
 *
 * Add cache-control headers to tell Fastly to cache content.
 * We mimic the cache-control headers sent by Drupal core, by avoiding sending
 * cache headers if the result is an error or if in maintenance mode.
 */
function fastly_exit() {

  // Avoid caching 404's and error pages.
  // To do this check the status header.
  $status = drupal_get_http_header("status");
  $is_error = preg_match('/^(4|5)/', $status);
  $path = drupal_strtolower(drupal_get_path_alias($_GET['q']));
  $no_store = drupal_strtolower(variable_get('fastly_non_cached', ''));
  if (drupal_page_is_cacheable() && !$is_error && !variable_get('maintenance_mode', 0) && !drupal_match_path($path, $no_store)) {
    drupal_add_http_header('Surrogate-Control', 'max-age=' . variable_get('fastly_ttl', '86400'));
  }
  else {
    drupal_add_http_header('Surrogate-Control', 'no-store');
  }
  drupal_add_http_header('Vary', 'Cookie,fastly-ssl');

  // Add Surrogate-Key headers based on path segments.
  // E.g. if the current path is product/some-category/product-name
  // we should end up with the following Surrogate-Keys:
  // product product/some-category product/some-category/product-name
  $path_segments = explode('/', drupal_get_path_alias());
  $surrogate_keys = array();
  $full_url = '';
  foreach ($path_segments as $segment) {
    if (empty($surrogate_keys)) {
      $full_url = $segment;
    }
    else {
      $full_url .= '/' . $segment;
    }
    $surrogate_keys[] = $full_url;
  }
  drupal_add_http_header('Surrogate-Key', implode(' ', $surrogate_keys));
}

/**
 * Return permissions for the Fastly module.
 */
function fastly_permission() {
  $perms = array(
    'administer fastly' => array(
      'title' => t('Administer Fastly'),
      'description' => t('Allows users to administer Fastly.'),
      'restrict access' => TRUE,
    ),
  );
  return $perms;
}

/**
 * Implements hook_menu().
 */
function fastly_menu() {
  $items = array();
  $items['admin/config/services/fastly'] = array(
    'title' => 'Fastly configuration',
    'description' => 'Fastly configuration',
    'page callback' => 'fastly_select_page',
    'access arguments' => array(
      'administer fastly',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/config/services/fastly/new'] = array(
    'title' => 'Create a service',
    'description' => 'Create a service',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'fastly_new_service_form',
    ),
    'access arguments' => array(
      'administer fastly',
    ),
    'file' => 'fastly.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/config/services/fastly/config'] = array(
    'title' => 'Configuration',
    'description' => 'Fastly configuration',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'fastly_setup_form',
    ),
    'access arguments' => array(
      'administer fastly',
    ),
    'file' => 'fastly.admin.inc',
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/config/services/fastly/register'] = array(
    'title' => 'Registration',
    'description' => 'Fastly registration',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'fastly_register_form',
    ),
    'access arguments' => array(
      'administer fastly',
    ),
    'file' => 'fastly.admin.inc',
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/config/services/fastly/purge'] = array(
    'title' => 'Purge cache',
    'description' => 'Fastly purge cache',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'fastly_purge_form',
    ),
    'access arguments' => array(
      'administer fastly',
    ),
    'file' => 'fastly.admin.inc',
    'type' => MENU_LOCAL_TASK,
    'weight' => -10,
  );
  return $items;
}

/**
 * Menu callback. Redirect the user to the right page.
 */
function fastly_select_page() {
  if (variable_get('fastly_api_key', FALSE)) {
    $path = 'admin/config/services/fastly/config';
  }
  else {
    $path = 'admin/config/services/fastly/register';
  }
  drupal_goto($path);
}

/**
 * Returns the API object.
 *
 * The key and service id can be overriden for validation reasons.
 */
function fastly_get_api($api_key = '', $service_id = '') {
  if (empty($api_key)) {
    $api_key = variable_get('fastly_api_key', '');
  }
  if (empty($service_id)) {
    $service_id = variable_get('fastly_service_id', '');
  }
  return new Fastly($api_key, $service_id);
}

/**
 * Implements hook_expire_cache().
 *
 * Provides integration with the Cache Expiration (expire) module.
 */
function fastly_expire_cache($urls, $wildcards, $object_type, $object) {
  $api = fastly_get_api();
  foreach ($urls as $url) {
    $api
      ->purgePath($url);
  }

  // For wildcards, we use the Surrogate-Key purging functionality.
  // Surrogate-Key headers are set in the response based on the
  // url path segments.
  // @See fastly_exit().
  foreach ($wildcards as $path => $wildcard) {
    if ($wildcard) {
      $api
        ->purgeKey($path);
    }
  }
}

Functions

Namesort descending Description
fastly_exit Implements hook_exit().
fastly_expire_cache Implements hook_expire_cache().
fastly_get_api Returns the API object.
fastly_menu Implements hook_menu().
fastly_permission Return permissions for the Fastly module.
fastly_select_page Menu callback. Redirect the user to the right page.