-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod_cachebusting.c
More file actions
381 lines (322 loc) · 12.6 KB
/
mod_cachebusting.c
File metadata and controls
381 lines (322 loc) · 12.6 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include <httpd.h>
#include <http_protocol.h>
#include <http_config.h>
#include <http_log.h>
#include <http_core.h>
#include <http_request.h>
#include <ap_config.h>
#include <ap_regex.h>
#include <apr_strings.h>
#include <apr_buckets.h>
#include <apr_hash.h>
#include <apr_lib.h>
#include <util_filter.h>
#define DISABLED 2
#define ENABLED 1
#define CB_UPDATE_HASH (1<<1)
#define CB_ADD_HEADER (1<<2)
#define CB_HAPPENING_NAME "happening"
#define CB_MUTEX_ID "cb_mutex"
#define CACHEBUSTING_PATTERN "img(?: )+src=['\"](.*?)['\"]"
static ap_filter_rec_t *cachebusting_add_header_filter;
static ap_filter_rec_t *cachebusting_add_hash_filter;
static ap_filter_rec_t *cachebusting_rewrite_html_filter;
module AP_MODULE_DECLARE_DATA cachebusting_module;
/* {{{ Structure to hold the config */
typedef struct _cachebusting_server_conf {
int state; /* State of the module */
unsigned int lifetime; /* Lifetime for cachebusting caches, default 15724800 */
char* prefix; /* Prefix for cachebusting assets, default cb */
} cachebusting_server_conf;
/* }}} */
/* {{{ Thread-shared structure */
typedef struct _cachebusting_struct {
ap_regex_t* compiled; /* Regex to rewrite HTML */
apr_pool_t* pool; /* Pool to register the values in */
apr_hash_t* hash; /* The key/value pairs for cachebusting hashes */
} cachebusting_struct;
/* }}} */
static cachebusting_struct *cb = NULL;
static void (*cb_set)(const char* key, const char* value);
static const char* (*cb_get)(const char* key);
static void cb_hash_set(const char* key, const char* value) {
apr_hash_set(cb->hash, key, APR_HASH_KEY_STRING, value);
}
static const char* cb_hash_get(const char* key) {
char *hash = apr_hash_get(cb->hash, key, APR_HASH_KEY_STRING);
return hash;
}
/* {{{ Cachebusting initialisation */
static void cachebusting_init(apr_pool_t *child, server_rec *s)
{
/* No error handling neccessary - if it fails it becomes an unrecoverable state anyways */
cb = apr_pcalloc(s->process->pool, sizeof(cachebusting_struct));
apr_pool_create(&cb->pool, s->process->pool);
cb->compiled = ap_pregcomp(cb->pool, CACHEBUSTING_PATTERN, AP_REG_EXTENDED);
cb->hash = apr_hash_make(s->process->pool);
}
/* }}} */
/* {{{ Create the cachebusting server config */
static void *create_cachebusting_server_conf(apr_pool_t *p, server_rec *s)
{
cachebusting_server_conf *sconf = apr_pcalloc(s->process->pool, sizeof(cachebusting_server_conf));
sconf->state = DISABLED;
sconf->prefix = apr_pstrndup(s->process->pool, "cb", 2);
sconf->lifetime = 15724800;
cb_set = cb_hash_set;
cb_get = cb_hash_get;
return sconf;
}
/* }}} */
/* {{{ Command to enable/disable cachebusting */
static const char* cmd_cachebusting(cmd_parms *cmd, void *in_dconf, int flag)
{
cachebusting_server_conf *sconf;
sconf = ap_get_module_config(cmd->server->module_config, &cachebusting_module);
sconf->state = (flag ? ENABLED : DISABLED);
return NULL;
}
/* }}} */
/* {{{ Set the prefix for cachebusting elements, default 'cb' */
static const char* cmd_cachebusting_prefix(cmd_parms *cmd, void *in_dconf, char* prefix)
{
cachebusting_server_conf *sconf;
sconf = ap_get_module_config(cmd->server->module_config, &cachebusting_module);
if (sconf->state) sconf->prefix = prefix;
return NULL;
}
/* }}} */
/* {{{ Set the lifetime for cachebusting caches, default '15724800' */
static const char* cmd_cachebusting_lifetime(cmd_parms *cmd, void *in_dconf, char* lifetime)
{
cachebusting_server_conf *sconf;
sconf = ap_get_module_config(cmd->server->module_config, &cachebusting_module);
if (sconf->state) sconf->lifetime = atoi(lifetime);
return NULL;
}
/* }}} */
/* {{{ Defined commands */
static const command_rec cachebusting_cmds[] = {
AP_INIT_FLAG("Cachebusting", cmd_cachebusting, NULL, RSRC_CONF,
"Whether to enable or disable cachebusting"),
AP_INIT_TAKE1("CachebustingPrefix", cmd_cachebusting_prefix, NULL, RSRC_CONF,
"Prefix for cachebusting elements, default 'cb'"),
AP_INIT_TAKE1("CachebustingLifetime", cmd_cachebusting_lifetime, NULL, RSRC_CONF,
"Lifetime for cachebusting caches, default '15724800'"),
{NULL}
};
/* }}} */
/* {{{ Set HTTP Metadata cover sheet for cachebusting */
static apr_status_t cachebusting_header_filter(ap_filter_t* f, apr_bucket_brigade* bb)
{
char* timestr;
apr_time_t expires;
cachebusting_server_conf *sconf;
sconf = ap_get_module_config(f->r->server->module_config, &cachebusting_module);
apr_table_t *headers_out = f->r->headers_out;
/* Maybe we need to add public here too */
apr_table_mergen(headers_out, "Cache-Control",
apr_psprintf(f->r->pool, "max-age=%" APR_TIME_T_FMT, sconf->lifetime));
timestr = apr_palloc(f->r->pool, APR_RFC822_DATE_LEN);
/* Calculate correct formatted expires string */
expires = f->r->request_time+apr_time_from_sec(sconf->lifetime);
apr_rfc822_date(timestr, expires);
apr_table_setn(headers_out, "Expires", timestr);
/* Remove filter and go to the next one in the pipe */
ap_remove_output_filter(f);
return ap_pass_brigade(f->next, bb) ;
}
/* }}} */
/* {{{ Add hash of delivered image to table */
static apr_status_t cachebusting_hash_filter(ap_filter_t* f, apr_bucket_brigade* bb)
{
cachebusting_server_conf *sconf;
sconf = ap_get_module_config(f->r->server->module_config, &cachebusting_module);
apr_status_t rv;
/* Add to r->notes instead of this hash for cluster capability
* Needs to be sent on the logging phase after the response
* due to the network overhead */
char* found = ap_strstr_c(f->r->unparsed_uri, sconf->prefix);
char* hash = NULL;
int mtime = 0;
if (found) {
hash = apr_palloc(f->r->pool, strlen(found) - strlen(sconf->prefix));
strncpy(hash, found + strlen(sconf->prefix), strlen(found) - strlen(sconf->prefix));
hash[strlen(found) - strlen(sconf->prefix)] = 0;
mtime = apr_time_sec(f->r->finfo.mtime);
}
/* Only write the hash if it has changed */
if (!found || atoi(hash) != mtime) {
cb_set(apr_pstrdup(cb->pool, f->r->uri), apr_itoa(cb->pool, apr_time_sec(f->r->finfo.mtime)));
}
/* Remove filter and go to the next one in the pipe */
ap_remove_output_filter(f);
return ap_pass_brigade(f->next, bb) ;
}
/* }}} */
/* {{{ Rewrite HTML content */
static apr_status_t cachebusting_html_filter(ap_filter_t* f, apr_bucket_brigade* bb)
{
cachebusting_server_conf *sconf;
apr_bucket *bucket, *out;
apr_status_t rv;
ap_regmatch_t regm[AP_MAX_REG_MATCH];
sconf = ap_get_module_config(f->r->server->module_config, &cachebusting_module);
for (bucket = APR_BRIGADE_FIRST(bb);
bucket != APR_BRIGADE_SENTINEL(bb);
bucket = APR_BUCKET_NEXT(bucket)) {
char* buf;
size_t bytes;
const char* data;
if (APR_BUCKET_IS_EOS(bucket)) {
/* Ignore */
} else if (APR_BUCKET_IS_METADATA(bucket)) {
/* Ignore */
} else if (apr_bucket_read(bucket, &data, &bytes, APR_BLOCK_READ) == APR_SUCCESS) {
char *filename;
int length = 0;
while (!ap_regexec(cb->compiled, data, AP_MAX_REG_MATCH, regm, 0)) {
/* Split off the bucket till the first char of the match */
length = regm[1].rm_so;
rv = apr_bucket_split(bucket, length);
if (rv == APR_SUCCESS) {
/* and jump to the next one */
bucket = APR_BUCKET_NEXT(bucket);
}
/* Filename to look for */
char *tmp = apr_pstrndup(f->r->pool,
&data[regm[1].rm_so],
regm[1].rm_eo - regm[1].rm_so);
if (*tmp != '/') {
tmp = apr_pstrcat(f->r->pool, "/", tmp, NULL);
}
char *hash = cb_get(tmp);
if (hash) {
filename = apr_pstrcat(f->r->pool, tmp, ";", sconf->prefix, hash, NULL);
/* Create a new bucket */
out = apr_bucket_pool_create(filename,
strlen(filename), f->r->pool,
f->r->connection->bucket_alloc);
APR_BUCKET_INSERT_BEFORE(bucket, out);
/* Remove current name */
int length = regm[1].rm_eo - regm[1].rm_so;
rv = apr_bucket_split(bucket, length);
if (rv == APR_SUCCESS) {
APR_BUCKET_REMOVE(bucket);
bucket = APR_BUCKET_NEXT(bucket);
}
}
/* Read bucket again or break the loop */
if (apr_bucket_read(bucket, &data, &bytes, APR_BLOCK_READ) != APR_SUCCESS) {
break;
}
}
}
}
/* Pass module brigade */
return ap_pass_brigade(f->next, bb) ;
}
/* }}} */
/* {{{ Strip ;prefixHash from the request path and resolve to
* local file */
static apr_status_t resolve_cachebusting_name(request_rec *r)
{
/* Skip if request doesn't start with / and first character isn't NULL */
if (r->uri[0] != '/' && r->uri[0] != '\0') {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r, APLOGNO(03461) "Wrong request type");
return DECLINED;
}
cachebusting_server_conf *sconf;
sconf = ap_get_module_config(r->server->module_config, &cachebusting_module);
/* Skip if not enabled */
if (!sconf || sconf->state == DISABLED) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r, APLOGNO(03462) "Disabled");
return DECLINED;
}
char *prefix, *found;
int happening = 0, res;
prefix = apr_pstrcat(r->pool, ";", sconf->prefix, NULL);
happening |= CB_UPDATE_HASH;
/* Skip if prefix not found */
if ((found = ap_strstr_c(r->uri, prefix)) == NULL) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r, APLOGNO(03463) "Prefix not found");
apr_table_setn(r->notes, CB_HAPPENING_NAME, apr_itoa(r->pool, happening));
return DECLINED;
}
happening |= CB_ADD_HEADER;
/* Use the core translator after modifying the uri from the request */
char* new_filename = apr_palloc(r->pool, found - r->uri + 1);
strncpy(new_filename, r->uri, found - r->uri);
new_filename[found - r->uri] = 0;
/* Check if mod_rewrite and mod_alias still work as expected */
r->uri = new_filename;
apr_table_setn(r->notes, CB_HAPPENING_NAME, apr_itoa(r->pool, happening));
res = ap_core_translate(r);
return res;
}
/* }}} */
/* {{{ Add filters */
static void cachebusting_insert_filter(request_rec* r)
{
/* Don't add Expires headers to errors */
if (ap_is_HTTP_ERROR(r->status)) {
return;
}
cachebusting_server_conf *sconf;
sconf = ap_get_module_config(r->server->module_config, &cachebusting_module);
/* Skip if not enabled */
if (!sconf || sconf->state == DISABLED) {
return;
}
char *prefix, *found;
int happening = NULL;
prefix = apr_pstrcat(r->pool, ";", sconf->prefix, NULL);
found = strstr(r->uri, prefix);
happening = atoi(apr_table_get(r->notes, CB_HAPPENING_NAME));
/* Check if content type is an image and headers should be appended */
if (r->content_type && !strncmp(r->content_type, "image", 5) && (happening & CB_ADD_HEADER)) {
ap_add_output_filter_handle(cachebusting_add_header_filter, NULL, r, r->connection);
}
/* Check if content type is an image and hash should be updated */
if (r->content_type && !strncmp(r->content_type, "image", 5) && (happening & CB_UPDATE_HASH)) {
ap_add_output_filter_handle(cachebusting_add_hash_filter, NULL, r, r->connection);
return;
}
if (r->content_type && !strncmp(r->content_type, "text/html", 9)) {
ap_add_output_filter_handle(cachebusting_rewrite_html_filter, NULL, r, r->connection);
return;
}
}
/* }}} */
/* {{{ Register hooks into runtime,
* Executed: once at process start */
static void cachebusting_hooks(apr_pool_t *pool)
{
/* TODO: Let mod_alias and mod_rewrite run before mod_cachebusting
* to ensure the functionality will stack
*
* Alias /foo.png /bar.png
*
* Should deliver /bar.png if request looks like host/foo.png;cb123456 */
static const char * const aszPre[] = { "http_core.c", "mod_mime.c", NULL };
/* Create filter handles */
cachebusting_add_header_filter =
ap_register_output_filter("MOD_CACHEBUSTING_HEADER", cachebusting_header_filter, NULL, AP_FTYPE_RESOURCE);
cachebusting_add_hash_filter =
ap_register_output_filter("MOD_CACHEBUSTING_HASH", cachebusting_hash_filter, NULL, AP_FTYPE_RESOURCE);
cachebusting_rewrite_html_filter =
ap_register_output_filter("MOD_CACHEBUSTING_HTML", cachebusting_html_filter, NULL, AP_FTYPE_RESOURCE);
ap_hook_child_init(cachebusting_init, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_translate_name(resolve_cachebusting_name, NULL, aszPre, APR_HOOK_MIDDLE);
ap_hook_insert_filter(cachebusting_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
}
/* }}} */
module AP_MODULE_DECLARE_DATA cachebusting_module = {
STANDARD20_MODULE_STUFF,
NULL, /* create per server config structure */
NULL, /* merge per dir config structure */
create_cachebusting_server_conf, /* create per server config structure */
NULL, /* merge per server config structure */
cachebusting_cmds, /* table of config file commands */
cachebusting_hooks /* register hooks */
} ;