Apache HTTPD
mod_socache_dc.c
Go to the documentation of this file.
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "httpd.h"
18#include "http_log.h"
19#include "http_request.h"
20#include "http_config.h"
21#include "http_protocol.h"
22#include "mod_status.h"
23
24#include "apr_strings.h"
25#include "apr_time.h"
26
27#include "ap_socache.h"
28
29#include "distcache/dc_client.h"
30
31#if !defined(DISTCACHE_CLIENT_API) || (DISTCACHE_CLIENT_API < 0x0001)
32#error "You must compile with a more recent version of the distcache-base package"
33#endif
34
36 /* Configured target server: */
37 const char *target;
38 /* distcache client context: */
40};
41
43 const char *arg,
44 apr_pool_t *tmp, apr_pool_t *p)
45{
47
48 ctx = *context = apr_palloc(p, sizeof *ctx);
49
50 ctx->target = apr_pstrdup(p, arg);
51
52 return NULL;
53}
54
56 const char *namespace,
57 const struct ap_socache_hints *hints,
59{
60#if 0
61 /* If a "persistent connection" mode of operation is preferred, you *must*
62 * also use the PIDCHECK flag to ensure fork()'d processes don't interlace
63 * comms on the same connection as each other. */
64#define SESSION_CTX_FLAGS SESSION_CTX_FLAG_PERSISTENT | \
65 SESSION_CTX_FLAG_PERSISTENT_PIDCHECK | \
66 SESSION_CTX_FLAG_PERSISTENT_RETRY | \
67 SESSION_CTX_FLAG_PERSISTENT_LATE
68#else
69 /* This mode of operation will open a temporary connection to the 'target'
70 * for each cache operation - this makes it safe against fork()
71 * automatically. This mode is preferred when running a local proxy (over
72 * unix domain sockets) because overhead is negligible and it reduces the
73 * performance/stability danger of file-descriptor bloatage. */
74#define SESSION_CTX_FLAGS 0
75#endif
76 ctx->dc = DC_CTX_new(ctx->target, SESSION_CTX_FLAGS);
77 if (!ctx->dc) {
78 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00738) "distributed scache failed to obtain context");
79 return APR_EGENERAL;
80 }
81 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, APLOGNO(00739) "distributed scache context initialised");
82
83 return APR_SUCCESS;
84}
85
87{
88 if (ctx && ctx->dc) {
89 DC_CTX_free(ctx->dc);
90 ctx->dc = NULL;
91 }
92}
93
95 const unsigned char *id, unsigned int idlen,
96 apr_time_t expiry,
97 unsigned char *der, unsigned int der_len,
99{
100 /* !@#$%^ - why do we deal with *absolute* time anyway???
101 * Uhm - because most things expire things at a specific time?
102 * Were the API were thought out expiry - r->request_time is a good approximation
103 */
104 expiry -= apr_time_now();
105 /* Send the serialised session to the distributed cache context */
106 if (!DC_CTX_add_session(ctx->dc, id, idlen, der, der_len,
107 apr_time_msec(expiry))) {
108 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00740) "distributed scache 'store' failed");
109 return APR_EGENERAL;
110 }
111 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00741) "distributed scache 'store' successful");
112 return APR_SUCCESS;
113}
114
116 const unsigned char *id, unsigned int idlen,
117 unsigned char *dest, unsigned int *destlen,
118 apr_pool_t *p)
119{
120 unsigned int data_len;
121
122 /* Retrieve any corresponding session from the distributed cache context */
123 if (!DC_CTX_get_session(ctx->dc, id, idlen, dest, *destlen, &data_len)) {
124 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00742) "distributed scache 'retrieve' MISS");
125 return APR_NOTFOUND;
126 }
127 if (data_len > *destlen) {
128 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00743) "distributed scache 'retrieve' OVERFLOW");
129 return APR_ENOSPC;
130 }
131 *destlen = data_len;
132 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00744) "distributed scache 'retrieve' HIT");
133 return APR_SUCCESS;
134}
135
137 server_rec *s, const unsigned char *id,
138 unsigned int idlen, apr_pool_t *p)
139{
140 /* Remove any corresponding session from the distributed cache context */
141 if (!DC_CTX_remove_session(ctx->dc, id, idlen)) {
142 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00745) "distributed scache 'remove' MISS");
143 return APR_NOTFOUND;
144 }
145 else {
146 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00746) "distributed scache 'remove' HIT");
147 return APR_SUCCESS;
148 }
149}
150
152{
154 "distributed scache 'socache_dc_status'");
155 if (!(flags & AP_STATUS_SHORT)) {
156 ap_rprintf(r, "cache type: <b>DC (Distributed Cache)</b>, "
157 " target: <b>%s</b><br>", ctx->target);
158 }
159 else {
160 ap_rputs("CacheType: DC\n", r);
161 ap_rvputs(r, "CacheTarget: ", ctx->target, "\n", NULL);
162 }
163}
164
172
185
192
195 NULL, NULL, NULL, NULL, NULL,
197};
198
Small object cache provider interface.
APR Strings library.
APR Time Library.
#define AP_DECLARE_MODULE(foo)
request_rec * r
#define APLOGNO(n)
Definition http_log.h:117
#define APLOG_INFO
Definition http_log.h:70
#define ap_log_rerror
Definition http_log.h:454
#define APLOG_ERR
Definition http_log.h:67
#define ap_log_error
Definition http_log.h:370
#define APLOG_MARK
Definition http_log.h:283
#define APLOG_DEBUG
Definition http_log.h:71
apr_md5_ctx_t * context
Definition util_md5.h:58
int ap_rvputs(request_rec *r,...)
Definition protocol.c:2220
int ap_rprintf(request_rec *r, const char *fmt,...) __attribute__((format(printf
static APR_INLINE int ap_rputs(const char *str, request_rec *r)
apr_status_t ap_register_provider(apr_pool_t *pool, const char *provider_group, const char *provider_name, const char *provider_version, const void *provider)
Definition provider.c:35
void const char * arg
Definition http_vhost.h:63
#define APR_EGENERAL
Definition apr_errno.h:313
#define APR_ENOSPC
Definition apr_errno.h:676
#define APR_ENOTIMPL
Definition apr_errno.h:476
#define APR_NOTFOUND
Definition apr_errno.h:463
apr_brigade_flush void * ctx
const char apr_ssize_t int flags
Definition apr_encode.h:168
const char const apr_size_t data_len
apr_status_t() ap_socache_iterator_t(ap_socache_instance_t *instance, server_rec *s, void *userctx, const unsigned char *id, unsigned int idlen, const unsigned char *data, unsigned int datalen, apr_pool_t *pool)
Definition ap_socache.h:77
#define AP_SOCACHE_PROVIDER_GROUP
Definition ap_socache.h:218
#define AP_SOCACHE_PROVIDER_VERSION
Definition ap_socache.h:220
#define AP_STATUS_SHORT
Definition mod_status.h:32
#define STANDARD20_MODULE_STUFF
apr_size_t size
const char int apr_pool_t * pool
Definition apr_cstr.h:84
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
const char * s
Definition apr_strings.h:95
#define apr_time_msec(time)
Definition apr_time.h:69
apr_int64_t apr_time_t
Definition apr_time.h:45
Apache Configuration.
Apache Logging library.
HTTP protocol handling.
Apache Request library.
HTTP Daemon routines.
apr_pool_t * p
Definition md_event.c:32
return NULL
Definition mod_so.c:359
static apr_status_t socache_dc_remove(ap_socache_instance_t *ctx, server_rec *s, const unsigned char *id, unsigned int idlen, apr_pool_t *p)
static apr_status_t socache_dc_iterate(ap_socache_instance_t *instance, server_rec *s, void *userctx, ap_socache_iterator_t *iterator, apr_pool_t *pool)
static const ap_socache_provider_t socache_dc
#define SESSION_CTX_FLAGS
static apr_status_t socache_dc_init(ap_socache_instance_t *ctx, const char *namespace, const struct ap_socache_hints *hints, server_rec *s, apr_pool_t *p)
static void socache_dc_status(ap_socache_instance_t *ctx, request_rec *r, int flags)
static void register_hooks(apr_pool_t *p)
static apr_status_t socache_dc_store(ap_socache_instance_t *ctx, server_rec *s, const unsigned char *id, unsigned int idlen, apr_time_t expiry, unsigned char *der, unsigned int der_len, apr_pool_t *p)
static apr_status_t socache_dc_retrieve(ap_socache_instance_t *ctx, server_rec *s, const unsigned char *id, unsigned int idlen, unsigned char *dest, unsigned int *destlen, apr_pool_t *p)
static void socache_dc_destroy(ap_socache_instance_t *ctx, server_rec *s)
static const char * socache_dc_create(ap_socache_instance_t **context, const char *arg, apr_pool_t *tmp, apr_pool_t *p)
Status Report Extension Module to Apache.
A structure that represents the current request.
Definition httpd.h:845
A structure to store information for each virtual server.
Definition httpd.h:1322