Blame modules/cache/mod_socache_dc.c

Packit 90a5c9
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 90a5c9
 * contributor license agreements.  See the NOTICE file distributed with
Packit 90a5c9
 * this work for additional information regarding copyright ownership.
Packit 90a5c9
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 90a5c9
 * (the "License"); you may not use this file except in compliance with
Packit 90a5c9
 * the License.  You may obtain a copy of the License at
Packit 90a5c9
 *
Packit 90a5c9
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 90a5c9
 *
Packit 90a5c9
 * Unless required by applicable law or agreed to in writing, software
Packit 90a5c9
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 90a5c9
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 90a5c9
 * See the License for the specific language governing permissions and
Packit 90a5c9
 * limitations under the License.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
#include "httpd.h"
Packit 90a5c9
#include "http_log.h"
Packit 90a5c9
#include "http_request.h"
Packit 90a5c9
#include "http_config.h"
Packit 90a5c9
#include "http_protocol.h"
Packit 90a5c9
#include "mod_status.h"
Packit 90a5c9
Packit 90a5c9
#include "apr_strings.h"
Packit 90a5c9
#include "apr_time.h"
Packit 90a5c9
Packit 90a5c9
#include "ap_socache.h"
Packit 90a5c9
Packit 90a5c9
#include "distcache/dc_client.h"
Packit 90a5c9
Packit 90a5c9
#if !defined(DISTCACHE_CLIENT_API) || (DISTCACHE_CLIENT_API < 0x0001)
Packit 90a5c9
#error "You must compile with a more recent version of the distcache-base package"
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
struct ap_socache_instance_t {
Packit 90a5c9
    /* Configured target server: */
Packit 90a5c9
    const char *target;
Packit 90a5c9
    /* distcache client context: */
Packit 90a5c9
    DC_CTX *dc;
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
static const char *socache_dc_create(ap_socache_instance_t **context,
Packit 90a5c9
                                     const char *arg,
Packit 90a5c9
                                     apr_pool_t *tmp, apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    struct ap_socache_instance_t *ctx;
Packit 90a5c9
Packit 90a5c9
    ctx = *context = apr_palloc(p, sizeof *ctx);
Packit 90a5c9
Packit 90a5c9
    ctx->target = apr_pstrdup(p, arg);
Packit 90a5c9
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static apr_status_t socache_dc_init(ap_socache_instance_t *ctx,
Packit 90a5c9
                                    const char *namespace,
Packit 90a5c9
                                    const struct ap_socache_hints *hints,
Packit 90a5c9
                                    server_rec *s, apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
#if 0
Packit 90a5c9
    /* If a "persistent connection" mode of operation is preferred, you *must*
Packit 90a5c9
     * also use the PIDCHECK flag to ensure fork()'d processes don't interlace
Packit 90a5c9
     * comms on the same connection as each other. */
Packit 90a5c9
#define SESSION_CTX_FLAGS        SESSION_CTX_FLAG_PERSISTENT | \
Packit 90a5c9
                                 SESSION_CTX_FLAG_PERSISTENT_PIDCHECK | \
Packit 90a5c9
                                 SESSION_CTX_FLAG_PERSISTENT_RETRY | \
Packit 90a5c9
                                 SESSION_CTX_FLAG_PERSISTENT_LATE
Packit 90a5c9
#else
Packit 90a5c9
    /* This mode of operation will open a temporary connection to the 'target'
Packit 90a5c9
     * for each cache operation - this makes it safe against fork()
Packit 90a5c9
     * automatically. This mode is preferred when running a local proxy (over
Packit 90a5c9
     * unix domain sockets) because overhead is negligable and it reduces the
Packit 90a5c9
     * performance/stability danger of file-descriptor bloatage. */
Packit 90a5c9
#define SESSION_CTX_FLAGS        0
Packit 90a5c9
#endif
Packit 90a5c9
    ctx->dc = DC_CTX_new(ctx->target, SESSION_CTX_FLAGS);
Packit 90a5c9
    if (!ctx->dc) {
Packit 90a5c9
        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00738) "distributed scache failed to obtain context");
Packit 90a5c9
        return APR_EGENERAL;
Packit 90a5c9
    }
Packit 90a5c9
    ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, APLOGNO(00739) "distributed scache context initialised");
Packit 90a5c9
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void socache_dc_destroy(ap_socache_instance_t *ctx, server_rec *s)
Packit 90a5c9
{
Packit 90a5c9
    if (ctx && ctx->dc) {
Packit 90a5c9
        DC_CTX_free(ctx->dc);
Packit 90a5c9
        ctx->dc = NULL;
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static apr_status_t socache_dc_store(ap_socache_instance_t *ctx, server_rec *s,
Packit 90a5c9
                                     const unsigned char *id, unsigned int idlen,
Packit 90a5c9
                                     apr_time_t expiry,
Packit 90a5c9
                                     unsigned char *der, unsigned int der_len,
Packit 90a5c9
                                     apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    /* !@#$%^ - why do we deal with *absolute* time anyway???
Packit 90a5c9
     * Uhm - because most things expire things at a specific time?
Packit 90a5c9
     * Were the API were thought out expiry - r->request_time is a good approximation
Packit 90a5c9
     */
Packit 90a5c9
    expiry -= apr_time_now();
Packit 90a5c9
    /* Send the serialised session to the distributed cache context */
Packit 90a5c9
    if (!DC_CTX_add_session(ctx->dc, id, idlen, der, der_len,
Packit 90a5c9
                            apr_time_msec(expiry))) {
Packit 90a5c9
        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00740) "distributed scache 'store' failed");
Packit 90a5c9
        return APR_EGENERAL;
Packit 90a5c9
    }
Packit 90a5c9
    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00741) "distributed scache 'store' successful");
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static apr_status_t socache_dc_retrieve(ap_socache_instance_t *ctx, server_rec *s,
Packit 90a5c9
                                        const unsigned char *id, unsigned int idlen,
Packit 90a5c9
                                        unsigned char *dest, unsigned int *destlen,
Packit 90a5c9
                                        apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    unsigned int data_len;
Packit 90a5c9
Packit 90a5c9
    /* Retrieve any corresponding session from the distributed cache context */
Packit 90a5c9
    if (!DC_CTX_get_session(ctx->dc, id, idlen, dest, *destlen, &data_len)) {
Packit 90a5c9
        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00742) "distributed scache 'retrieve' MISS");
Packit 90a5c9
        return APR_NOTFOUND;
Packit 90a5c9
    }
Packit 90a5c9
    if (data_len > *destlen) {
Packit 90a5c9
        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00743) "distributed scache 'retrieve' OVERFLOW");
Packit 90a5c9
        return APR_ENOSPC;
Packit 90a5c9
    }
Packit 90a5c9
    *destlen = data_len;
Packit 90a5c9
    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00744) "distributed scache 'retrieve' HIT");
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static apr_status_t socache_dc_remove(ap_socache_instance_t *ctx,
Packit 90a5c9
                                      server_rec *s, const unsigned char *id,
Packit 90a5c9
                                      unsigned int idlen, apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    /* Remove any corresponding session from the distributed cache context */
Packit 90a5c9
    if (!DC_CTX_remove_session(ctx->dc, id, idlen)) {
Packit 90a5c9
        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00745) "distributed scache 'remove' MISS");
Packit 90a5c9
        return APR_NOTFOUND;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00746) "distributed scache 'remove' HIT");
Packit 90a5c9
        return APR_SUCCESS;
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void socache_dc_status(ap_socache_instance_t *ctx, request_rec *r, int flags)
Packit 90a5c9
{
Packit 90a5c9
    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00747)
Packit 90a5c9
                  "distributed scache 'socache_dc_status'");
Packit 90a5c9
    if (!(flags & AP_STATUS_SHORT)) {
Packit 90a5c9
        ap_rprintf(r, "cache type: DC (Distributed Cache), "
Packit 90a5c9
                   " target: %s
", ctx->target);
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        ap_rputs("CacheType: DC\n", r);
Packit 90a5c9
        ap_rvputs(r, "CacheTarget: ", ctx->target, "\n", NULL);
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static apr_status_t socache_dc_iterate(ap_socache_instance_t *instance,
Packit 90a5c9
                                       server_rec *s, void *userctx,
Packit 90a5c9
                                       ap_socache_iterator_t *iterator,
Packit 90a5c9
                                       apr_pool_t *pool)
Packit 90a5c9
{
Packit 90a5c9
    return APR_ENOTIMPL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const ap_socache_provider_t socache_dc = {
Packit 90a5c9
    "distcache",
Packit 90a5c9
    0,
Packit 90a5c9
    socache_dc_create,
Packit 90a5c9
    socache_dc_init,
Packit 90a5c9
    socache_dc_destroy,
Packit 90a5c9
    socache_dc_store,
Packit 90a5c9
    socache_dc_retrieve,
Packit 90a5c9
    socache_dc_remove,
Packit 90a5c9
    socache_dc_status,
Packit 90a5c9
    socache_dc_iterate
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
static void register_hooks(apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    ap_register_provider(p, AP_SOCACHE_PROVIDER_GROUP, "dc",
Packit 90a5c9
                         AP_SOCACHE_PROVIDER_VERSION,
Packit 90a5c9
                         &socache_dc);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE_MODULE(socache_dc) = {
Packit 90a5c9
    STANDARD20_MODULE_STUFF,
Packit 90a5c9
    NULL, NULL, NULL, NULL, NULL,
Packit 90a5c9
    register_hooks
Packit 90a5c9
};
Packit 90a5c9