-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastly.module
179 lines (157 loc) · 4.88 KB
/
fastly.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?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);
}
}
}