www-modproxy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r..@covalent.net
Subject mod_cache.c
Date Wed, 15 Nov 2000 14:34:12 GMT

I am posting a copy of mod_cache.c, which is a VERY simple cache module
for Apache 2.0.  This basically implements a file cache filter and with
some work it can be abstracted to other cache types.  The reason I am not
committing it, is because this is horribly broken right now.  It never
reads from the cache to try to serve that page, and it always caches to
the same file.  :-)  The first problem is just a matter of writing a
handler.  The second is even easier, we need to rip the logic out of the
current file_cache directory to get a valid key for the file.

I have added comments for where things should be abstracted so that
different back-ends can be used.  I don't want this to fall on the floor,
and if this stays on my hard-drive, it will not see the light of day for
at least a month.  So, have at it and if anybody wants to pick this up,
let me know.  I want to help, but I am stretched too thin to do the cache
myself.  :-)


#include "apr_strings.h"
#include "ap_config.h"
#include "util_filter.h"
#include "httpd.h"
#include "http_config.h"
#include "http_request.h"
#include "http_core.h"
#include "http_protocol.h"
#include "http_log.h"
#include "http_main.h"
#include "util_script.h"
#include "http_core.h"                                                             

module MODULE_VAR_EXPORT cache_module;

typedef struct cache_struct {
    char *filename;
    apr_file_t *fd;
    int state;
} cache_struct;

static int cache_filter(ap_filter_t *f, ap_bucket_brigade *bb)
{
    cache_struct *ctx;
    ap_bucket *e;
    
    if (ctx == NULL) {
        f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
    }
    ctx->state = 1;
    if (ctx->state) {
        return ap_pass_brigade(f->next, bb);
    }
    if (ctx->filename == NULL) {
        apr_status_t rv;
        ctx->filename = ap_server_root_relative(f->r->pool, "logs/cachefile");
/* XXX Cache modules should abstract out the open call */
        if ((rv = apr_open(&ctx->fd, ctx->filename, 
                     APR_WRITE | APR_CREATE | APR_TRUNCATE | APR_BUFFERED,
                     APR_UREAD | APR_UWRITE, f->r->pool)) != APR_SUCCESS) {
            ap_log_error(APLOG_MARK, APlOG_ERROR, rv, f->r,
                         "Could not create cache file");
            return ap_pass_brigade(f->next, bb);
        }
    } 
    AP_BRIGADE_FOREACH(e, bb) {
        const char *str;
        apr_ssize_t length;

        ap_bucket_read(e, &str, &length, 0);
/* XXX Cache modules should abstract out the write call */
        apr_write(ctx->fd, str, length);
    }
    ap_pass_brigade(f->next, bb);
    return APR_SUCCESS;	
}

static void cache_register_hook(void)
{
    ap_register_output_filter("CACHE", cache_filter, AP_FTYPE_HTTP_HEADER);
}

module MODULE_VAR_EXPORT cache_module = {
    STANDARD20_MODULE_STUFF,
    NULL,			/* create per-directory config structure */
    NULL,        		/* merge per-directory config structures */
    NULL,			/* create per-server config structure */
    NULL,			/* merge per-server config structures */
    NULL,			/* command apr_table_t */
    NULL,          		/* handlers */
    cache_register_hook		/* register hooks */
};


_______________________________________________________________________________
Ryan Bloom                        	rbb@apache.org
406 29th St.
San Francisco, CA 94131
-------------------------------------------------------------------------------



Mime
View raw message