------------------------------------------------------------------------
--- mod_disk_cache.c.orig 2006-01-18 13:44:55.000000000 -0500
+++ mod_disk_cache.c 2006-01-20 09:48:23.000000000 -0500
@@ -40,15 +40,15 @@
* Format #1:
* apr_uint32_t format;
* apr_time_t expire;
- * apr_array_t vary_headers (delimited by CRLF)
+ * apr_array_t vary_headers (delimited by '\0')
*
* Format #2:
* disk_cache_info_t (first sizeof(apr_uint32_t) bytes is the format)
* entity name (dobj->name) [length is in disk_cache_info_t->name_len]
- * r->headers_out (delimited by CRLF)
- * CRLF
- * r->headers_in (delimited by CRLF)
- * CRLF
+ * length of r->headers_out
+ * r->headers_out (delimited by '\0')
+ * length r->headers_in
+ * r->headers_in (delimited by '\0')
*/
module AP_MODULE_DECLARE_DATA disk_cache_module;
@@ -60,8 +60,12 @@
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);
+ meta_file_t *file);
+static apr_status_t meta_file_close(void *data);
+static apr_status_t meta_file_open(meta_file_t **m, const char *file,
apr_pool_t *pool);
+static apr_status_t meta_file_read(meta_file_t *m, void **buf, apr_size_t
*nbytes);
+static apr_status_t meta_file_seek(meta_file_t *h, apr_off_t offset);
/*
* Local static functions
*/
@@ -195,46 +199,43 @@
* file for an ap_cache_el, this state information will be read
* and written transparent to clients of this module
*/
-static int file_cache_recall_mydata(apr_file_t *fd, cache_info *info,
+static int file_cache_recall_mydata(meta_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;
+ disk_cache_info_t *disk_info;
apr_size_t len;
/* read the data from the cache file */
len = sizeof(disk_cache_info_t);
- rv = apr_file_read_full(fd, &disk_info, len, &len);
+ rv = meta_file_read(fd, (void **)&disk_info, &len);
if (rv != APR_SUCCESS) {
+ /* Log error? */
return rv;
}
/* Store it away so we can get it later. */
- dobj->disk_info = disk_info;
+ 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;
-
- /* Note that we could optimize this by conditionally doing the palloc
- * depending upon the size. */
- 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';
+ 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;
+
+ len = disk_info->name_len;
+
+ rv = meta_file_read(fd, (void **)&urlbuff, &len);
/* check that we have the same URL */
- /* Would strncmp be correct? */
- if (strcmp(urlbuff, dobj->name) != 0) {
+ if (strcmp(dobj->name, urlbuff)) {
+ ap_log_error(APLOG_MARK, APLOG_INFO, 0, r->server,
+ "disk_cache: name mismatch: %s != %s",
+ urlbuff, dobj->name);
return APR_EGENERAL;
}
-
+
return APR_SUCCESS;
}
@@ -347,7 +348,7 @@
static int open_entity(cache_handle_t *h, request_rec *r, const char *key)
{
- apr_uint32_t format;
+ apr_uint32_t format, *tf;
apr_size_t len;
const char *nkey;
apr_status_t rc;
@@ -387,28 +388,32 @@
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);
+ rc = meta_file_open(&dobj->mf, dobj->hdrsfile, r->pool);
if (rc != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
+ "disk_cache: meta_file_open");
return DECLINED;
}
/* read the format from the cache file */
len = sizeof(format);
- apr_file_read_full(dobj->hfd, &format, len, &len);
+ meta_file_read(dobj->mf, (void **)&tf, &len);
+ format = *tf;
if (format == VARY_FORMAT_VERSION) {
apr_array_header_t* varray;
- apr_time_t expire;
+ apr_time_t expire, *te;
len = sizeof(expire);
- apr_file_read_full(dobj->hfd, &expire, len, &len);
+ meta_file_read(dobj->mf, (void **)&tf, &len);
+ expire = *te;
if (expire < r->request_time) {
return DECLINED;
}
varray = apr_array_make(r->pool, 5, sizeof(char*));
- rc = read_array(r, varray, dobj->hfd);
+ rc = read_array(r, varray, dobj->mf);
if (rc != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rc, r->server,
"disk_cache: Cannot parse vary header file: %s",
@@ -424,7 +429,7 @@
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);
+ rc = meta_file_open(&dobj->mf, dobj->hdrsfile, r->pool);
if (rc != APR_SUCCESS) {
return DECLINED;
}
@@ -440,7 +445,7 @@
/* This wasn't a Vary Format file, so we must seek to the
* start of the file again, so that later reads work.
*/
- apr_file_seek(dobj->hfd, APR_SET, &offset);
+ meta_file_seek(dobj->mf, offset);
nkey = key;
}
@@ -457,6 +462,8 @@
#endif
rc = apr_file_open(&dobj->fd, dobj->datafile, flags, 0, r->pool);
if (rc != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
+ "disk_cache: apr_file_open");
/* XXX: Log message */
return DECLINED;
}
@@ -467,8 +474,10 @@
}
/* Read the bytes to setup the cache_info fields */
- rc = file_cache_recall_mydata(dobj->hfd, info, dobj, r);
+ rc = file_cache_recall_mydata(dobj->mf, info, dobj, r);
if (rc != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
+ "disk_cache: file_cache_recall_mydata");
/* XXX log message */
return DECLINED;
}
@@ -572,152 +581,145 @@
return OK;
}
-
-static apr_status_t read_array(request_rec *r, apr_array_header_t* arr,
- apr_file_t *file)
+static apr_status_t read_array(request_rec *r, apr_array_header_t* arr,
+ meta_file_t *m)
{
- 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;
- }
+ apr_size_t len, length;
+ apr_size_t *tl;
+ char *buffer;
+ char *ptr, *end, *stop;
+ apr_status_t rv;
+
+ /*read length first*/
+ len = sizeof(apr_size_t);
+
+ if((rv = meta_file_read(m, (void **)&tl, &len)) != APR_SUCCESS) {
+ ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+ "Premature end of vary array length.");
+ return rv;
+ }
+
+ length = *tl;
- 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';
- }
- }
+ len = length;
+
+ if((rv = meta_file_read(m, (void **)&buffer, &len)) != APR_SUCCESS) {
+ ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+ "Premature end of vary array data: %"APR_SIZE_T_FMT,
+ len);
+ return rv;
+ }
- /* If we've finished reading the array, break out of the loop. */
- if (w[0] == '\0') {
- break;
+ ptr = buffer;
+ end = ptr + length;
+
+ while (ptr < end) {
+ if((stop = memchr(ptr, '\0', end - ptr)) != NULL) {
+ *((const char **) apr_array_push(arr)) = ptr;
+ ptr = stop + 1;
+ } else {
+ ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+ "Vary parse error.");
+ return APR_EOF;
}
-
- *((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)
+static apr_status_t store_array(request_rec *r, apr_file_t *fd,
apr_array_header_t* arr)
{
- int i;
+ int i, j;
apr_status_t rv;
- struct iovec iov[2];
- apr_size_t amt;
+ struct iovec *iov;
+ apr_size_t amt = 0;
const char **elts;
-
+ apr_size_t length;
+
elts = (const char **) arr->elts;
+ j = 1;
+ length = 0;
+ iov = apr_pcalloc(r->pool, sizeof(struct iovec) * (arr->nelts + 1));
+
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[j].iov_base = (char*) elts[i];
+ /*include '\0'*/
+ iov[j].iov_len = strlen(elts[i]) + 1 ;
+
+ length += iov[j].iov_len;
+ j++;
}
- iov[0].iov_base = CRLF;
- iov[0].iov_len = sizeof(CRLF) - 1;
- return apr_file_writev(fd, (const struct iovec *) &iov, 1,
+
+ iov[0].iov_base = &length;
+ iov[0].iov_len = sizeof(apr_size_t);;
+
+ rv = apr_file_writev(fd, iov, j,
&amt);
+
+ return rv;
}
-static apr_status_t read_table(cache_handle_t *handle, request_rec *r,
- apr_table_t *table, apr_file_t *file)
+static apr_status_t read_table(request_rec *r, apr_table_t* t,
+ meta_file_t *m)
{
- char w[MAX_STRING_LEN];
- char *l;
- int p;
+ char *buffer;
+ char *ptr, *end;;
apr_status_t rv;
-
- while (1) {
-
- /* ### What about APR_EOF? */
- 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;
- }
-
- /* Delete terminal (CR?)LF */
-
- p = strlen(w);
- /* Indeed, the host's '\n':
- '\012' for UNIX; '\015' for MacOS; '\025' for OS/390
- -- whatever the script generates.
- */
- if (p > 0 && w[p - 1] == '\n') {
- if (p > 1 && w[p - 2] == CR) {
- w[p - 2] = '\0';
- }
- else {
- w[p - 1] = '\0';
- }
- }
-
- /* If we've finished reading the headers, break out of the loop. */
- if (w[0] == '\0') {
- break;
- }
-
-#if APR_CHARSET_EBCDIC
- /* Chances are that we received an ASCII header text instead of
- * the expected EBCDIC header lines. Try to auto-detect:
- */
- 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);
+ apr_size_t len, length, *tl;
+ char *key, *val, *stop;
+ int state;
+#define KEY 0
+#define VAL 1
+
+ /*read length first*/
+ len = sizeof(apr_size_t);
+
+ if((rv = meta_file_read(m, (void **)&tl, &len)) != APR_SUCCESS) {
+ ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+ "Premature end of table.");
+ return rv;
+ }
+
+ length = (apr_size_t)*tl;
+
+ len = length;
+
+ if((rv = meta_file_read(m, (void **)&buffer, &len)) != APR_SUCCESS) {
+ ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+ "Premature end of table data: %"APR_SIZE_T_FMT", %"APR_SIZE_T_FMT,
+ len, length);
+ return rv;
+ }
+
+ ptr = buffer;
+ end = ptr + len;
+
+ key = ptr;
+ state = KEY;
+ while (ptr < end) {
+ if((stop = memchr(ptr, '\0', end - ptr)) != NULL) {
+
+ if(state == VAL) {
+ val = ptr;
+ apr_table_addn(t, key, val);
+ state = KEY;
+ } else {
+ key = ptr;
+ state = VAL;
}
+ ptr = stop + 1;
+ } else {
+ ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+ "Error parsing headers.");
+ return APR_EOF;
}
-#endif /*APR_CHARSET_EBCDIC*/
-
- /* if we see a bogus header don't ignore it. Shout and scream */
- if (!(l = strchr(w, ':'))) {
- return APR_EGENERAL;
- }
-
- *l++ = '\0';
- while (*l && apr_isspace(*l)) {
- ++l;
- }
-
- apr_table_add(table, w, l);
}
-
+
return APR_SUCCESS;
}
@@ -733,7 +735,7 @@
disk_cache_object_t *dobj = (disk_cache_object_t *) h->cache_obj->vobj;
/* This case should not happen... */
- if (!dobj->hfd) {
+ if (!dobj->mf) {
/* XXX log message */
return APR_NOTFOUND;
}
@@ -742,10 +744,10 @@
h->resp_hdrs = apr_table_make(r->pool, 20);
/* Call routine to read the header lines/status line */
- read_table(h, r, h->resp_hdrs, dobj->hfd);
- read_table(h, r, h->req_hdrs, dobj->hfd);
+ read_table(r, h->resp_hdrs, dobj->mf);
+ read_table(r, h->req_hdrs, dobj->mf);
- apr_file_close(dobj->hfd);
+ meta_file_close(dobj->mf);
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"disk_cache: Recalled headers for URL %s", dobj->name);
@@ -765,38 +767,44 @@
return APR_SUCCESS;
}
-
-static apr_status_t store_table(apr_file_t *fd, apr_table_t *table)
+static apr_status_t store_table(request_rec *r, apr_file_t *fd, apr_table_t* t)
{
- int i;
+ int i, j;
apr_status_t rv;
- struct iovec iov[4];
- apr_size_t amt;
+ struct iovec *iov;
+ apr_size_t amt = 0;
+ apr_size_t length;
apr_table_entry_t *elts;
+
+ j = 1;
+ length = 0;
+
+ iov = apr_pcalloc(r->pool, sizeof(struct iovec) * (apr_table_elts(t)->nelts + 1) * 2);
+ elts = (apr_table_entry_t *) apr_table_elts(t)->elts;
+
+ for (i = 0; i < apr_table_elts(t)->nelts; ++i) {
+ if (elts[i].key != NULL) {
+ iov[j].iov_base = (char*) elts[i].key;
+ /*include /\0'*/
+ iov[j].iov_len = strlen(elts[i].key) + 1;
+ length += iov[j].iov_len;
+ j++;
+
+ iov[j].iov_base = (char*) elts[i].val;
+ /*include /\0'*/
+ iov[j].iov_len = strlen(elts[i].val) + 1;
+ length += iov[j].iov_len;
+ j++;
+ }
+
+ }
- 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;
+ iov[0].iov_base = &length;
+ iov[0].iov_len = sizeof(apr_size_t);
- 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,
+ rv = apr_file_writev(fd, iov, j,
&amt);
+
return rv;
}
@@ -842,7 +850,7 @@
varray = apr_array_make(r->pool, 6, sizeof(char*));
tokens_to_array(r->pool, tmp, varray);
- store_array(dobj->tfd, varray);
+ store_array(r, dobj->tfd, varray);
apr_file_close(dobj->tfd);
@@ -885,13 +893,19 @@
disk_info.response_time = info->response_time;
disk_info.status = info->status;
- disk_info.name_len = strlen(dobj->name);
+ disk_info.name_len = strlen(dobj->name) + 1;
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;
+ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
+ "disk_cache: store_headers: name = %s, len = %d,
%"APR_TIME_T_FMT,
+ dobj->name, disk_info.name_len, disk_info.request_time
+ );
+
+
rv = apr_file_writev(dobj->hfd, (const struct iovec *) &iov, 2, &amt);
if (rv != APR_SUCCESS) {
return rv;
@@ -911,7 +925,7 @@
headers_out = apr_table_overlay(r->pool, headers_out,
r->err_headers_out);
- rv = store_table(dobj->hfd, headers_out);
+ rv = store_table(r, dobj->hfd, headers_out);
if (rv != APR_SUCCESS) {
return rv;
}
@@ -924,7 +938,7 @@
headers_in = ap_cache_cacheable_hdrs_out(r->pool, r->headers_in,
r->server);
- rv = store_table(dobj->hfd, headers_in);
+ rv = store_table(r, dobj->hfd, headers_in);
if (rv != APR_SUCCESS) {
return rv;
}
@@ -1164,3 +1178,86 @@
disk_cache_cmds, /* command apr_table_t */
disk_cache_register_hook /* register hooks */
};
+
+static apr_status_t meta_file_close(void *data) {
+ return APR_SUCCESS;
+}
+
+static apr_status_t meta_file_open(meta_file_t **m, const char *file,
apr_pool_t *pool) {
+ apr_finfo_t finfo;
+ apr_status_t rv;
+ apr_size_t len;
+ meta_file_t *new = NULL;
+ apr_file_t *fd;
+
+ if((rv = apr_file_open(&fd, file, APR_READ|APR_BINARY, 0, pool)) != APR_SUCCESS) {
+ return rv;
+ }
+
+ if((rv = apr_file_info_get(&finfo, APR_FINFO_SIZE, fd)) != APR_SUCCESS) {
+ return rv;
+ }
+
+ new = apr_pcalloc(pool, sizeof(meta_file_t));
+
+ new->pool = pool;
+ new->name = (char *)file;
+ new->bufflen = finfo.size;
+
+ new->buffer = apr_pcalloc(new->pool, new->bufflen);
+
+ len = new->bufflen;
+
+ if((rv = apr_file_read_full(fd, new->buffer, len, &len)) != APR_SUCCESS) {
+ return rv;
+ }
+
+ apr_file_close(fd);
+
+ new->curr = new->buffer;
+
+ apr_pool_cleanup_register(new->pool, (void *)new,
+ meta_file_close,
+ meta_file_close);
+
+
+ *m = new;
+
+ return APR_SUCCESS;
+}
+
+static apr_status_t meta_file_read(meta_file_t *m, void **buf, apr_size_t
*nbytes) {
+ apr_size_t len;
+
+ if(*nbytes < 0) {
+ return APR_EGENERAL;
+ }
+
+ len = (m->buffer + m->bufflen) - m->curr;
+
+ /*quick check are we EOF?*/
+ if(len <= 0) {
+ return APR_EOF;
+ }
+
+ *buf = m->curr;
+
+ if(*nbytes > len) {
+ m->curr += len;
+ *nbytes = len;
+ return APR_EOF;
+ }
+
+ m->curr += *nbytes;
+
+ return APR_SUCCESS;
+}
+
+static apr_status_t meta_file_seek(meta_file_t *m, apr_off_t offset) {
+ if(offset > m->bufflen) {
+ return APR_EOF;
+ } else {
+ m->curr = m->buffer + offset;
+ }
+ return APR_SUCCESS;
+}
--- mod_disk_cache.h.orig 2006-01-18 13:48:13.000000000 -0500
+++ mod_disk_cache.h 2006-01-20 09:44:53.000000000 -0500
@@ -22,7 +22,7 @@
*/
#define VARY_FORMAT_VERSION 3
-#define DISK_FORMAT_VERSION 4
+#define DISK_FORMAT_VERSION 5
#define CACHE_HEADER_SUFFIX ".header"
#define CACHE_DATA_SUFFIX ".data"
@@ -36,6 +36,15 @@
#define AP_TEMPFILE AP_TEMPFILE_PREFIX AP_TEMPFILE_BASE AP_TEMPFILE_SUFFIX
typedef struct {
+ apr_pool_t *pool;
+ apr_file_t *fd;
+ char *name; /*name of file we opened*/
+ char *buffer; /*buffer that holds disk_info*/
+ char *curr; /*current pointer in buffer*/
+ apr_size_t bufflen;
+} meta_file_t;
+
+typedef struct {
/* Indicates the format of the header struct stored on-disk. */
apr_uint32_t format;
/* The HTTP status code returned for this response. */
@@ -68,6 +77,7 @@
apr_file_t *fd; /* data file */
apr_file_t *hfd; /* headers file */
apr_file_t *tfd; /* temporary file for data */
+ meta_file_t *mf;
apr_off_t file_size; /* File size of the cached data file */
disk_cache_info_t disk_info; /* Header information. */
} disk_cache_object_t;