fastly.module in Fastly 7.2
Same filename and directory in other branches
Fastly module.
File
fastly.moduleView source
<?php
/**
* @file
* Fastly module.
*/
/**
* Implements hook_init().
*
* 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_init() {
// 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', TRUE);
// 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/webhook'] = array(
'title' => 'Webhook',
'description' => 'Webhook configuration',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'fastly_webhook_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,
);
$items['admin/config/services/fastly/vcl'] = array(
'title' => 'Vcl upload',
'description' => 'VCL upload',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'fastly_vcl_upload_form',
),
'access arguments' => array(
'administer fastly',
),
'file' => 'fastly.admin.inc',
'type' => MENU_LOCAL_TASK,
);
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 Webhook object.
*/
function fastly_get_webhook() {
return new Webhook();
}
/**
* 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);
}
/**
* Returns the API object.
*
* The key and service id can be overriden for validation reasons.
*/
function fastly_get_vcl($api_key = '', $service_id = '') {
$vcl_dir = $vcl_dir = drupal_get_path('module', 'fastly') . '/vcl_snippets';
$data = [
'vcl' => [
[
'vcl_dir' => $vcl_dir,
'type' => 'recv',
],
[
'vcl_dir' => $vcl_dir,
'type' => 'deliver',
],
[
'vcl_dir' => $vcl_dir,
'type' => 'error',
],
[
'vcl_dir' => $vcl_dir,
'type' => 'fetch',
],
],
'condition' => [
[
'name' => 'drupalmodule_request',
'statement' => 'req.http.x-pass == "1"',
'type' => 'REQUEST',
'priority' => 90,
],
],
'setting' => [
[
'name' => 'drupalmodule_setting',
'action' => 'pass',
'request_condition' => 'drupalmodule_request',
],
],
];
if (empty($api_key)) {
$api_key = variable_get('fastly_api_key', '');
}
if (empty($service_id)) {
$service_id = variable_get('fastly_service_id', '');
}
return new Vcl($data, $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
->purgeQuery($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().
if ($paths = array_keys($wildcards, TRUE)) {
$api
->purgeKeys($paths);
}
}
Functions
Name![]() |
Description |
---|---|
fastly_expire_cache | Implements hook_expire_cache(). |
fastly_get_api | Returns the API object. |
fastly_get_vcl | Returns the API object. |
fastly_get_webhook | Returns the Webhook object. |
fastly_init | Implements hook_init(). |
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. |