#include "apr_file_io.h"
#include "apr_strings.h"
#include "mod_cache.h"
#include "mod_disk_cache.h"
#include "ap_provider.h"
#include "util_filter.h"
#include "util_script.h"
#include "util_charset.h"
module AP_MODULE_DECLARE_DATA disk_cache_module;
static int remove_entity(cache_handle_t *h);
static apr_status_t store_headers(cache_handle_t *h, request_rec *r, cache_info *i);
static apr_status_t store_body(cache_handle_t *h, request_rec *r, apr_bucket_brigade *b);
static apr_status_t recall_headers(cache_handle_t *h, request_rec *r);
static apr_status_t recall_body(cache_handle_t *h, apr_pool_t *p, apr_bucket_brigade *bb);
static apr_status_t read_array(request_rec *r, apr_array_header_t* arr,
apr_file_t *file);
static char *header_file(apr_pool_t *p, disk_cache_conf *conf,
disk_cache_object_t *dobj, const char *name)
{
if (!dobj->hashfile) {
dobj->hashfile = ap_cache_generate_name(p, conf->dirlevels,
conf->dirlength, name);
}
if (dobj->prefix) {
return apr_pstrcat(p, dobj->prefix, CACHE_VDIR_SUFFIX, "/",
dobj->hashfile, CACHE_HEADER_SUFFIX, NULL);
}
else {
return apr_pstrcat(p, conf->cache_root, "/", dobj->hashfile,
CACHE_HEADER_SUFFIX, NULL);
}
}
static char *data_file(apr_pool_t *p, disk_cache_conf *conf,
disk_cache_object_t *dobj, const char *name)
{
if (!dobj->hashfile) {
dobj->hashfile = ap_cache_generate_name(p, conf->dirlevels,
conf->dirlength, name);
}
if (dobj->prefix) {
return apr_pstrcat(p, dobj->prefix, CACHE_VDIR_SUFFIX, "/",
dobj->hashfile, CACHE_DATA_SUFFIX, NULL);
}
else {
return apr_pstrcat(p, conf->cache_root, "/", dobj->hashfile,
CACHE_DATA_SUFFIX, NULL);
}
}
static void mkdir_structure(disk_cache_conf *conf, const char *file, apr_pool_t *pool)
{
apr_status_t rv;
char *p;
for (p = (char*)file + conf->cache_root_len + 1;;) {
p = strchr(p, '/');
if (!p)
break;
*p = '\0';
rv = apr_dir_make(file,
APR_UREAD|APR_UWRITE|APR_UEXECUTE, pool);
if (rv != APR_SUCCESS && !APR_STATUS_IS_EEXIST(rv)) {
}
*p = '/';
++p;
}
}
static apr_status_t safe_file_rename(disk_cache_conf *conf,
const char *src, const char *dest,
apr_pool_t *pool)
{
apr_status_t rv;
rv = apr_file_rename(src, dest, pool);
if (rv != APR_SUCCESS) {
int i;
for (i = 0; i < 2 && rv != APR_SUCCESS; i++) {
apr_sleep(1000);
mkdir_structure(conf, dest, pool);
rv = apr_file_rename(src, dest, pool);
}
}
return rv;
}
static apr_status_t file_cache_el_final(disk_cache_object_t *dobj,
request_rec *r)
{
if (dobj->tfd) {
apr_status_t rv;
apr_file_close(dobj->tfd);
rv = apr_file_rename(dobj->tempfile, dobj->datafile, r->pool);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_WARNING, rv, r->server,
"disk_cache: rename tempfile to datafile failed:"
" %s -> %s", dobj->tempfile, dobj->datafile);
apr_file_remove(dobj->tempfile, r->pool);
}
dobj->tfd = NULL;
}
return APR_SUCCESS;
}
static apr_status_t file_cache_errorcleanup(disk_cache_object_t *dobj, request_rec *r)
{
apr_file_remove(dobj->hdrsfile, r->pool);
apr_file_remove(dobj->datafile, r->pool);
if (dobj->tfd) {
apr_file_close(dobj->tfd);
apr_file_remove(dobj->tempfile, r->pool);
dobj->tfd = NULL;
}
return APR_SUCCESS;
}
static int file_cache_recall_mydata(apr_file_t *fd, cache_info *info,
disk_cache_object_t *dobj, request_rec *r)
{
apr_status_t rv;
char *urlbuff;
disk_cache_info_t disk_info;
apr_size_t len;
len = sizeof(disk_cache_info_t);
rv = apr_file_read_full(fd, &disk_info, len, &len);
if (rv != APR_SUCCESS) {
return rv;
}
dobj->disk_info = disk_info;
info->status = disk_info.status;
info->date = disk_info.date;
info->expire = disk_info.expire;
info->request_time = disk_info.request_time;
info->response_time = disk_info.response_time;
urlbuff = apr_palloc(r->pool, disk_info.name_len + 1);
len = disk_info.name_len;
rv = apr_file_read_full(fd, urlbuff, len, &len);
if (rv != APR_SUCCESS) {
return rv;
}
urlbuff[disk_info.name_len] = '\0';
if (strcmp(urlbuff, dobj->name) != 0) {
return APR_EGENERAL;
}
return APR_SUCCESS;
}
static const char* regen_key(apr_pool_t *p, apr_table_t *headers,
apr_array_header_t *varray, const char *oldkey)
{
struct iovec *iov;
int i, k;
int nvec;
const char *header;
const char **elts;
nvec = (varray->nelts * 2) + 1;
iov = apr_palloc(p, sizeof(struct iovec) * nvec);
elts = (const char **) varray->elts;
for(i=0, k=0; i < varray->nelts; i++) {
header = apr_table_get(headers, elts[i]);
if (!header) {
header = "";
}
iov[k].iov_base = (char*) elts[i];
iov[k].iov_len = strlen(elts[i]);
k++;
iov[k].iov_base = (char*) header;
iov[k].iov_len = strlen(header);
k++;
}
iov[k].iov_base = (char*) oldkey;
iov[k].iov_len = strlen(oldkey);
k++;
return apr_pstrcatv(p, iov, k, NULL);
}
static int array_alphasort(const void *fn1, const void *fn2)
{
return strcmp(*(char**)fn1, *(char**)fn2);
}
static void tokens_to_array(apr_pool_t *p, const char *data,
apr_array_header_t *arr)
{
char *token;
while ((token = ap_get_list_item(p, &data)) != NULL) {
*((const char **) apr_array_push(arr)) = token;
}
qsort((void *) arr->elts, arr->nelts,
sizeof(char *), array_alphasort);
}
static int create_entity(cache_handle_t *h, request_rec *r, const char *key, apr_off_t len)
{
disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
&disk_cache_module);
cache_object_t *obj;
disk_cache_object_t *dobj;
if (conf->cache_root == NULL) {
return DECLINED;
}
h->cache_obj = obj = apr_pcalloc(r->pool, sizeof(*obj));
obj->vobj = dobj = apr_pcalloc(r->pool, sizeof(*dobj));
obj->key = apr_pstrdup(r->pool, key);
dobj->name = obj->key;
dobj->prefix = NULL;
dobj->root = apr_pstrndup(r->pool, conf->cache_root, conf->cache_root_len);
dobj->root_len = conf->cache_root_len;
dobj->datafile = data_file(r->pool, conf, dobj, key);
dobj->hdrsfile = header_file(r->pool, conf, dobj, key);
dobj->tempfile = apr_pstrcat(r->pool, conf->cache_root, AP_TEMPFILE, NULL);
return OK;
}
static int open_entity(cache_handle_t *h, request_rec *r, const char *key)
{
apr_uint32_t format;
apr_size_t len;
const char *nkey;
apr_status_t rc;
static int error_logged = 0;
disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
&disk_cache_module);
#ifdef APR_SENDFILE_ENABLED
core_dir_config *coreconf = ap_get_module_config(r->per_dir_config,
&core_module);
#endif
apr_finfo_t finfo;
cache_object_t *obj;
cache_info *info;
disk_cache_object_t *dobj;
int flags;
h->cache_obj = NULL;
if (conf->cache_root == NULL) {
if (!error_logged) {
error_logged = 1;
ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
"disk_cache: Cannot cache files to disk without a CacheRoot specified.");
}
return DECLINED;
}
h->cache_obj = obj = apr_pcalloc(r->pool, sizeof(cache_object_t));
obj->vobj = dobj = apr_pcalloc(r->pool, sizeof(disk_cache_object_t));
info = &(obj->info);
dobj->prefix = NULL;
dobj->root = apr_pstrndup(r->pool, conf->cache_root, conf->cache_root_len);
dobj->root_len = conf->cache_root_len;
dobj->hdrsfile = header_file(r->pool, conf, dobj, key);
flags = APR_READ|APR_BINARY|APR_BUFFERED;
rc = apr_file_open(&dobj->hfd, dobj->hdrsfile, flags, 0, r->pool);
if (rc != APR_SUCCESS) {
return DECLINED;
}
len = sizeof(format);
apr_file_read_full(dobj->hfd, &format, len, &len);
if (format == VARY_FORMAT_VERSION) {
apr_array_header_t* varray;
apr_time_t expire;
len = sizeof(expire);
apr_file_read_full(dobj->hfd, &expire, len, &len);
varray = apr_array_make(r->pool, 5, sizeof(char*));
rc = read_array(r, varray, dobj->hfd);
if (rc != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rc, r->server,
"disk_cache: Cannot parse vary header file: %s",
dobj->hdrsfile);
return DECLINED;
}
apr_file_close(dobj->hfd);
nkey = regen_key(r->pool, r->headers_in, varray, key);
dobj->hashfile = NULL;
dobj->prefix = dobj->hdrsfile;
dobj->hdrsfile = header_file(r->pool, conf, dobj, nkey);
flags = APR_READ|APR_BINARY|APR_BUFFERED;
rc = apr_file_open(&dobj->hfd, dobj->hdrsfile, flags, 0, r->pool);
if (rc != APR_SUCCESS) {
return DECLINED;
}
}
else if (format != DISK_FORMAT_VERSION) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
"cache_disk: File '%s' has a version mismatch. File had version: %d.",
dobj->hdrsfile, format);
return DECLINED;
}
else {
apr_off_t offset = 0;
apr_file_seek(dobj->hfd, APR_SET, &offset);
nkey = key;
}
obj->key = nkey;
dobj->key = nkey;
dobj->name = key;
dobj->datafile = data_file(r->pool, conf, dobj, nkey);
dobj->tempfile = apr_pstrcat(r->pool, conf->cache_root, AP_TEMPFILE, NULL);
flags = APR_READ|APR_BINARY;
#ifdef APR_SENDFILE_ENABLED
flags |= ((coreconf->enable_sendfile == ENABLE_SENDFILE_OFF)
? 0 : APR_SENDFILE_ENABLED);
#endif
rc = apr_file_open(&dobj->fd, dobj->datafile, flags, 0, r->pool);
if (rc != APR_SUCCESS) {
return DECLINED;
}
rc = apr_file_info_get(&finfo, APR_FINFO_SIZE, dobj->fd);
if (rc == APR_SUCCESS) {
dobj->file_size = finfo.size;
}
rc = file_cache_recall_mydata(dobj->hfd, info, dobj, r);
if (rc != APR_SUCCESS) {
return DECLINED;
}
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"disk_cache: Recalled cached URL info header %s", dobj->name);
return OK;
}
static int remove_entity(cache_handle_t *h)
{
h->cache_obj = NULL;
return OK;
}
static int remove_url(cache_handle_t *h, apr_pool_t *p)
{
apr_status_t rc;
disk_cache_object_t *dobj;
dobj = (disk_cache_object_t *) h->cache_obj->vobj;
if (!dobj) {
return DECLINED;
}
if (dobj->hdrsfile) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
"disk_cache: Deleting %s from cache.", dobj->hdrsfile);
rc = apr_file_remove(dobj->hdrsfile, p);
if ((rc != APR_SUCCESS) && !APR_STATUS_IS_ENOENT(rc)) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, rc, NULL,
"disk_cache: Failed to delete headers file %s from cache.",
dobj->hdrsfile);
return DECLINED;
}
}
if (dobj->datafile) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
"disk_cache: Deleting %s from cache.", dobj->datafile);
rc = apr_file_remove(dobj->datafile, p);
if ((rc != APR_SUCCESS) && !APR_STATUS_IS_ENOENT(rc)) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, rc, NULL,
"disk_cache: Failed to delete data file %s from cache.",
dobj->datafile);
return DECLINED;
}
}
if (dobj->root) {
const char *str_to_copy;
str_to_copy = dobj->hdrsfile ? dobj->hdrsfile : dobj->datafile;
if (str_to_copy) {
char *dir, *slash, *q;
dir = apr_pstrdup(p, str_to_copy);
slash = strrchr(dir, '/');
*slash = '\0';
for (q = dir + dobj->root_len; *q ; ) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
"disk_cache: Deleting directory %s from cache",
dir);
rc = apr_dir_remove(dir, p);
if (rc != APR_SUCCESS && !APR_STATUS_IS_ENOENT(rc)) {
break;
}
slash = strrchr(q, '/');
*slash = '\0';
}
}
}
return OK;
}
static apr_status_t read_array(request_rec *r, apr_array_header_t* arr,
apr_file_t *file)
{
char w[MAX_STRING_LEN];
int p;
apr_status_t rv;
while (1) {
rv = apr_file_gets(w, MAX_STRING_LEN - 1, file);
if (rv != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"Premature end of vary array.");
return rv;
}
p = strlen(w);
if (p > 0 && w[p - 1] == '\n') {
if (p > 1 && w[p - 2] == CR) {
w[p - 2] = '\0';
}
else {
w[p - 1] = '\0';
}
}
if (w[0] == '\0') {
break;
}
*((const char **) apr_array_push(arr)) = apr_pstrdup(r->pool, w);
}
return APR_SUCCESS;
}
static apr_status_t store_array(apr_file_t *fd, apr_array_header_t* arr)
{
int i;
apr_status_t rv;
struct iovec iov[2];
apr_size_t amt;
const char **elts;
elts = (const char **) arr->elts;
for (i = 0; i < arr->nelts; i++) {
iov[0].iov_base = (char*) elts[i];
iov[0].iov_len = strlen(elts[i]);
iov[1].iov_base = CRLF;
iov[1].iov_len = sizeof(CRLF) - 1;
rv = apr_file_writev(fd, (const struct iovec *) &iov, 2,
&amt);
if (rv != APR_SUCCESS) {
return rv;
}
}
iov[0].iov_base = CRLF;
iov[0].iov_len = sizeof(CRLF) - 1;
return apr_file_writev(fd, (const struct iovec *) &iov, 1,
&amt);
}
static apr_status_t read_table(cache_handle_t *handle, request_rec *r,
apr_table_t *table, apr_file_t *file)
{
char w[MAX_STRING_LEN];
char *l;
int p;
apr_status_t rv;
while (1) {
rv = apr_file_gets(w, MAX_STRING_LEN - 1, file);
if (rv != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"Premature end of cache headers.");
return rv;
}
p = strlen(w);
if (p > 0 && w[p - 1] == '\n') {
if (p > 1 && w[p - 2] == CR) {
w[p - 2] = '\0';
}
else {
w[p - 1] = '\0';
}
}
if (w[0] == '\0') {
break;
}
#if APR_CHARSET_EBCDIC
if (!(l = strchr(w, ':'))) {
int maybeASCII = 0, maybeEBCDIC = 0;
unsigned char *cp, native;
apr_size_t inbytes_left, outbytes_left;
for (cp = w; *cp != '\0'; ++cp) {
native = apr_xlate_conv_byte(ap_hdrs_from_ascii, *cp);
if (apr_isprint(*cp) && !apr_isprint(native))
++maybeEBCDIC;
if (!apr_isprint(*cp) && apr_isprint(native))
++maybeASCII;
}
if (maybeASCII > maybeEBCDIC) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
"CGI Interface Error: Script headers apparently ASCII: (CGI = %s)",
r->filename);
inbytes_left = outbytes_left = cp - w;
apr_xlate_conv_buffer(ap_hdrs_from_ascii,
w, &inbytes_left, w, &outbytes_left);
}
}
#endif
if (!(l = strchr(w, ':'))) {
return APR_EGENERAL;
}
*l++ = '\0';
while (*l && apr_isspace(*l)) {
++l;
}
apr_table_add(table, w, l);
}
return APR_SUCCESS;
}
static apr_status_t recall_headers(cache_handle_t *h, request_rec *r)
{
disk_cache_object_t *dobj = (disk_cache_object_t *) h->cache_obj->vobj;
if (!dobj->hfd) {
return APR_NOTFOUND;
}
h->req_hdrs = apr_table_make(r->pool, 20);
h->resp_hdrs = apr_table_make(r->pool, 20);
read_table(h, r, h->resp_hdrs, dobj->hfd);
read_table(h, r, h->req_hdrs, dobj->hfd);
apr_file_close(dobj->hfd);
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"disk_cache: Recalled headers for URL %s", dobj->name);
return APR_SUCCESS;
}
static apr_status_t recall_body(cache_handle_t *h, apr_pool_t *p, apr_bucket_brigade *bb)
{
apr_bucket *e;
disk_cache_object_t *dobj = (disk_cache_object_t*) h->cache_obj->vobj;
e = apr_bucket_file_create(dobj->fd, 0, (apr_size_t) dobj->file_size, p,
bb->bucket_alloc);
APR_BRIGADE_INSERT_HEAD(bb, e);
e = apr_bucket_eos_create(bb->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(bb, e);
return APR_SUCCESS;
}
static apr_status_t store_table(apr_file_t *fd, apr_table_t *table)
{
int i;
apr_status_t rv;
struct iovec iov[4];
apr_size_t amt;
apr_table_entry_t *elts;
elts = (apr_table_entry_t *) apr_table_elts(table)->elts;
for (i = 0; i < apr_table_elts(table)->nelts; ++i) {
if (elts[i].key != NULL) {
iov[0].iov_base = elts[i].key;
iov[0].iov_len = strlen(elts[i].key);
iov[1].iov_base = ": ";
iov[1].iov_len = sizeof(": ") - 1;
iov[2].iov_base = elts[i].val;
iov[2].iov_len = strlen(elts[i].val);
iov[3].iov_base = CRLF;
iov[3].iov_len = sizeof(CRLF) - 1;
rv = apr_file_writev(fd, (const struct iovec *) &iov, 4,
&amt);
if (rv != APR_SUCCESS) {
return rv;
}
}
}
iov[0].iov_base = CRLF;
iov[0].iov_len = sizeof(CRLF) - 1;
rv = apr_file_writev(fd, (const struct iovec *) &iov, 1,
&amt);
return rv;
}
static apr_status_t store_headers(cache_handle_t *h, request_rec *r, cache_info *info)
{
disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
&disk_cache_module);
apr_status_t rv;
apr_size_t amt;
disk_cache_object_t *dobj = (disk_cache_object_t*) h->cache_obj->vobj;
disk_cache_info_t disk_info;
struct iovec iov[2];
h->cache_obj->info = *info;
if (r->headers_out) {
const char *tmp;
tmp = apr_table_get(r->headers_out, "Vary");
if (tmp) {
apr_array_header_t* varray;
apr_uint32_t format = VARY_FORMAT_VERSION;
if (dobj->prefix) {
dobj->hdrsfile = dobj->prefix;
dobj->prefix = NULL;
}
mkdir_structure(conf, dobj->hdrsfile, r->pool);
rv = apr_file_mktemp(&dobj->tfd, dobj->tempfile,
APR_CREATE | APR_WRITE | APR_BINARY | APR_EXCL,
r->pool);
if (rv != APR_SUCCESS) {
return rv;
}
amt = sizeof(format);
apr_file_write(dobj->tfd, &format, &amt);
amt = sizeof(info->expire);
apr_file_write(dobj->tfd, &info->expire, &amt);
varray = apr_array_make(r->pool, 6, sizeof(char*));
tokens_to_array(r->pool, tmp, varray);
store_array(dobj->tfd, varray);
apr_file_close(dobj->tfd);
dobj->tfd = NULL;
rv = safe_file_rename(conf, dobj->tempfile, dobj->hdrsfile,
r->pool);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_WARNING, rv, r->server,
"disk_cache: rename tempfile to varyfile failed: %s -> %s",
dobj->tempfile, dobj->hdrsfile);
apr_file_remove(dobj->tempfile, r->pool);
return rv;
}
dobj->tempfile = apr_pstrcat(r->pool, conf->cache_root, AP_TEMPFILE, NULL);
tmp = regen_key(r->pool, r->headers_in, varray, dobj->name);
dobj->prefix = dobj->hdrsfile;
dobj->hashfile = NULL;
dobj->datafile = data_file(r->pool, conf, dobj, tmp);
dobj->hdrsfile = header_file(r->pool, conf, dobj, tmp);
}
}
rv = apr_file_mktemp(&dobj->hfd, dobj->tempfile,
APR_CREATE | APR_WRITE | APR_BINARY |
APR_BUFFERED | APR_EXCL, r->pool);
if (rv != APR_SUCCESS) {
return rv;
}
disk_info.format = DISK_FORMAT_VERSION;
disk_info.date = info->date;
disk_info.expire = info->expire;
disk_info.entity_version = dobj->disk_info.entity_version++;
disk_info.request_time = info->request_time;
disk_info.response_time = info->response_time;
disk_info.status = info->status;
disk_info.name_len = strlen(dobj->name);
iov[0].iov_base = (void*)&disk_info;
iov[0].iov_len = sizeof(disk_cache_info_t);
iov[1].iov_base = (void*)dobj->name;
iov[1].iov_len = disk_info.name_len;
rv = apr_file_writev(dobj->hfd, (const struct iovec *) &iov, 2, &amt);
if (rv != APR_SUCCESS) {
return rv;
}
if (r->headers_out) {
apr_table_t *headers_out;
headers_out = apr_table_overlay(r->pool, r->headers_out,
r->err_headers_out);
headers_out = ap_cache_cacheable_hdrs_out(r->pool, headers_out,
r->server);
if (!apr_table_get(headers_out, "Content-Type")
&& r->content_type) {
apr_table_setn(headers_out, "Content-Type",
ap_make_content_type(r, r->content_type));
}
if (!apr_table_get(headers_out, "Content-Encoding")
&& r->content_encoding) {
apr_table_setn(headers_out, "Content-Encoding",
r->content_encoding);
}
rv = store_table(dobj->hfd, headers_out);
if (rv != APR_SUCCESS) {
return rv;
}
}
if (r->headers_in) {
apr_table_t *headers_in;
headers_in = ap_cache_cacheable_hdrs_out(r->pool, r->headers_in,
r->server);
rv = store_table(dobj->hfd, headers_in);
if (rv != APR_SUCCESS) {
return rv;
}
}
apr_file_close(dobj->hfd);
rv = apr_file_remove(dobj->hdrsfile, r->pool);
if (rv != APR_SUCCESS) {
mkdir_structure(conf, dobj->hdrsfile, r->pool);
}
rv = safe_file_rename(conf, dobj->tempfile, dobj->hdrsfile, r->pool);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_WARNING, rv, r->server,
"disk_cache: rename tempfile to hdrsfile failed: %s -> %s",
dobj->tempfile, dobj->hdrsfile);
apr_file_remove(dobj->tempfile, r->pool);
return rv;
}
dobj->tempfile = apr_pstrcat(r->pool, conf->cache_root, AP_TEMPFILE, NULL);
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"disk_cache: Stored headers for URL %s", dobj->name);
return APR_SUCCESS;
}
static apr_status_t store_body(cache_handle_t *h, request_rec *r,
apr_bucket_brigade *bb)
{
apr_bucket *e;
apr_status_t rv;
disk_cache_object_t *dobj = (disk_cache_object_t *) h->cache_obj->vobj;
disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
&disk_cache_module);
if (!dobj->tfd) {
rv = apr_file_mktemp(&dobj->tfd, dobj->tempfile,
APR_CREATE | APR_WRITE | APR_BINARY |
APR_BUFFERED | APR_EXCL, r->pool);
if (rv != APR_SUCCESS) {
return rv;
}
dobj->file_size = 0;
}
for (e = APR_BRIGADE_FIRST(bb);
e != APR_BRIGADE_SENTINEL(bb);
e = APR_BUCKET_NEXT(e))
{
const char *str;
apr_size_t length, written;
rv = apr_bucket_read(e, &str, &length, APR_BLOCK_READ);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
"cache_disk: Error when reading bucket for URL %s",
h->cache_obj->key);
file_cache_errorcleanup(dobj, r);
return rv;
}
rv = apr_file_write_full(dobj->tfd, str, length, &written);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
"cache_disk: Error when writing cache file for URL %s",
h->cache_obj->key);
file_cache_errorcleanup(dobj, r);
return rv;
}
dobj->file_size += written;
if (dobj->file_size > conf->maxfs) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"cache_disk: URL %s failed the size check "
"(%" APR_OFF_T_FMT " > %" APR_OFF_T_FMT ")",
h->cache_obj->key, dobj->file_size, conf->maxfs);
file_cache_errorcleanup(dobj, r);
return APR_EGENERAL;
}
}
if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(bb))) {
const char *cl_header = apr_table_get(r->headers_out, "Content-Length");
if (r->connection->aborted || r->no_cache) {
ap_log_error(APLOG_MARK, APLOG_INFO, 0, r->server,
"disk_cache: Discarding body for URL %s "
"because connection has been aborted.",
h->cache_obj->key);
file_cache_errorcleanup(dobj, r);
return APR_EGENERAL;
}
if (dobj->file_size < conf->minfs) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"cache_disk: URL %s failed the size check "
"(%" APR_OFF_T_FMT " < %" APR_OFF_T_FMT ")",
h->cache_obj->key, dobj->file_size, conf->minfs);
file_cache_errorcleanup(dobj, r);
return APR_EGENERAL;
}
if (cl_header) {
apr_int64_t cl = apr_atoi64(cl_header);
if ((errno == 0) && (dobj->file_size != cl)) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"disk_cache: URL %s didn't receive complete response, not caching",
h->cache_obj->key);
file_cache_errorcleanup(dobj, r);
return APR_EGENERAL;
}
}
file_cache_el_final(dobj, r);
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"disk_cache: Body for URL %s cached.", dobj->name);
}
return APR_SUCCESS;
}
static void *create_config(apr_pool_t *p, server_rec *s)
{
disk_cache_conf *conf = apr_pcalloc(p, sizeof(disk_cache_conf));
conf->dirlevels = DEFAULT_DIRLEVELS;
conf->dirlength = DEFAULT_DIRLENGTH;
conf->maxfs = DEFAULT_MAX_FILE_SIZE;
conf->minfs = DEFAULT_MIN_FILE_SIZE;
conf->cache_root = NULL;
conf->cache_root_len = 0;
return conf;
}
static const char
*set_cache_root(cmd_parms *parms, void *in_struct_ptr, const char *arg)
{
disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,
&disk_cache_module);
conf->cache_root = arg;
conf->cache_root_len = strlen(arg);
return NULL;
}
static const char
*set_cache_dirlevels(cmd_parms *parms, void *in_struct_ptr, const char *arg)
{
disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,
&disk_cache_module);
int val = atoi(arg);
if (val < 1)
return "CacheDirLevels value must be an integer greater than 0";
if (val * conf->dirlength > CACHEFILE_LEN)
return "CacheDirLevels*CacheDirLength value must not be higher than 20";
conf->dirlevels = val;
return NULL;
}
static const char
*set_cache_dirlength(cmd_parms *parms, void *in_struct_ptr, const char *arg)
{
disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,
&disk_cache_module);
int val = atoi(arg);
if (val < 1)
return "CacheDirLength value must be an integer greater than 0";
if (val * conf->dirlevels > CACHEFILE_LEN)
return "CacheDirLevels*CacheDirLength value must not be higher than 20";
conf->dirlength = val;
return NULL;
}
static const char
*set_cache_minfs(cmd_parms *parms, void *in_struct_ptr, const char *arg)
{
disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,
&disk_cache_module);
if (apr_strtoff(&conf->minfs, arg, NULL, 0) != APR_SUCCESS ||
conf->minfs < 0)
{
return "CacheMinFileSize argument must be a non-negative integer representing the min size of a file to cache in bytes.";
}
return NULL;
}
static const char
*set_cache_maxfs(cmd_parms *parms, void *in_struct_ptr, const char *arg)
{
disk_cache_conf *conf = ap_get_module_config(parms->server->module_config,
&disk_cache_module);
if (apr_strtoff(&conf->maxfs, arg, NULL, 0) != APR_SUCCESS ||
conf->maxfs < 0)
{
return "CacheMaxFileSize argument must be a non-negative integer representing the max size of a file to cache in bytes.";
}
return NULL;
}
static const command_rec disk_cache_cmds[] =
{
AP_INIT_TAKE1("CacheRoot", set_cache_root, NULL, RSRC_CONF,
"The directory to store cache files"),
AP_INIT_TAKE1("CacheDirLevels", set_cache_dirlevels, NULL, RSRC_CONF,
"The number of levels of subdirectories in the cache"),
AP_INIT_TAKE1("CacheDirLength", set_cache_dirlength, NULL, RSRC_CONF,
"The number of characters in subdirectory names"),
AP_INIT_TAKE1("CacheMinFileSize", set_cache_minfs, NULL, RSRC_CONF,
"The minimum file size to cache a document"),
AP_INIT_TAKE1("CacheMaxFileSize", set_cache_maxfs, NULL, RSRC_CONF,
"The maximum file size to cache a document"),
{NULL}
};
static const cache_provider cache_disk_provider =
{
&remove_entity,
&store_headers,
&store_body,
&recall_headers,
&recall_body,
&create_entity,
&open_entity,
&remove_url,
};
static void disk_cache_register_hook(apr_pool_t *p)
{
ap_register_provider(p, CACHE_PROVIDER_GROUP, "disk", "0",
&cache_disk_provider);
}
module AP_MODULE_DECLARE_DATA disk_cache_module = {
STANDARD20_MODULE_STUFF,
NULL,
NULL,
create_config,
NULL,
disk_cache_cmds,
disk_cache_register_hook
};