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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "http_request.h"
20 #include "http_config.h"
21 #include "http_protocol.h"
23 #include "apr_strings.h"
26 #include "ap_socache.h"
28 #include "distcache/dc_client.h"
30 #if !defined(DISTCACHE_CLIENT_API) || (DISTCACHE_CLIENT_API < 0x0001)
31 #error "You must compile with a more recent version of the distcache-base package"
34 struct ap_socache_instance_t
{
35 /* Configured target server: */
37 /* distcache client context: */
41 static const char *socache_dc_create(ap_socache_instance_t
**context
,
43 apr_pool_t
*tmp
, apr_pool_t
*p
)
45 struct ap_socache_instance_t
*ctx
;
47 ctx
= *context
= apr_palloc(p
, sizeof *ctx
);
49 ctx
->target
= apr_pstrdup(p
, arg
);
54 static apr_status_t
socache_dc_init(ap_socache_instance_t
*ctx
,
55 const char *namespace,
56 const struct ap_socache_hints
*hints
,
57 server_rec
*s
, apr_pool_t
*p
)
60 /* If a "persistent connection" mode of operation is preferred, you *must*
61 * also use the PIDCHECK flag to ensure fork()'d processes don't interlace
62 * comms on the same connection as each other. */
63 #define SESSION_CTX_FLAGS SESSION_CTX_FLAG_PERSISTENT | \
64 SESSION_CTX_FLAG_PERSISTENT_PIDCHECK | \
65 SESSION_CTX_FLAG_PERSISTENT_RETRY | \
66 SESSION_CTX_FLAG_PERSISTENT_LATE
68 /* This mode of operation will open a temporary connection to the 'target'
69 * for each cache operation - this makes it safe against fork()
70 * automatically. This mode is preferred when running a local proxy (over
71 * unix domain sockets) because overhead is negligable and it reduces the
72 * performance/stability danger of file-descriptor bloatage. */
73 #define SESSION_CTX_FLAGS 0
75 ctx
->dc
= DC_CTX_new(ctx
->target
, SESSION_CTX_FLAGS
);
77 ap_log_error(APLOG_MARK
, APLOG_ERR
, 0, s
, "distributed scache failed to obtain context");
80 ap_log_error(APLOG_MARK
, APLOG_INFO
, 0, s
, "distributed scache context initialised");
85 static void socache_dc_kill(ap_socache_instance_t
*ctx
, server_rec
*s
)
93 static apr_status_t
socache_dc_store(ap_socache_instance_t
*ctx
, server_rec
*s
,
94 const unsigned char *id
, unsigned int idlen
,
96 unsigned char *der
, unsigned int der_len
,
99 /* !@#$%^ - why do we deal with *absolute* time anyway??? */
100 timeout
-= time(NULL
);
101 /* Send the serialised session to the distributed cache context */
102 if (!DC_CTX_add_session(ctx
->dc
, id
, idlen
, der
, der_len
,
103 (unsigned long)timeout
* 1000)) {
104 ap_log_error(APLOG_MARK
, APLOG_ERR
, 0, s
, "distributed scache 'add_session' failed");
107 ap_log_error(APLOG_MARK
, APLOG_DEBUG
, 0, s
, "distributed scache 'add_session' successful");
111 static apr_status_t
socache_dc_retrieve(ap_socache_instance_t
*ctx
, server_rec
*s
,
112 const unsigned char *id
, unsigned int idlen
,
113 unsigned char *dest
, unsigned int *destlen
,
116 unsigned int data_len
;
118 /* Retrieve any corresponding session from the distributed cache context */
119 if (!DC_CTX_get_session(ctx
->dc
, id
, idlen
, dest
, *destlen
, &data_len
)) {
120 ap_log_error(APLOG_MARK
, APLOG_DEBUG
, 0, s
, "distributed scache 'get_session' MISS");
123 if (data_len
> *destlen
) {
124 ap_log_error(APLOG_MARK
, APLOG_ERR
, 0, s
, "distributed scache 'get_session' OVERFLOW");
128 ap_log_error(APLOG_MARK
, APLOG_DEBUG
, 0, s
, "distributed scache 'get_session' HIT");
132 static apr_status_t
socache_dc_remove(ap_socache_instance_t
*ctx
,
133 server_rec
*s
, const unsigned char *id
,
134 unsigned int idlen
, apr_pool_t
*p
)
136 /* Remove any corresponding session from the distributed cache context */
137 if (!DC_CTX_remove_session(ctx
->dc
, id
, idlen
)) {
138 ap_log_error(APLOG_MARK
, APLOG_ERR
, 0, s
, "distributed scache 'remove_session' MISS");
141 ap_log_error(APLOG_MARK
, APLOG_ERR
, 0, s
, "distributed scache 'remove_session' HIT");
146 static void socache_dc_status(ap_socache_instance_t
*ctx
, request_rec
*r
, int flags
)
148 ap_log_rerror(APLOG_MARK
, APLOG_DEBUG
, 0, r
,
149 "distributed scache 'socache_dc_status'");
150 ap_rprintf(r
, "cache type: <b>DC (Distributed Cache)</b>, "
151 " target: <b>%s</b><br>", ctx
->target
);
154 static const ap_socache_provider_t socache_dc
= {
166 static void register_hooks(apr_pool_t
*p
)
168 ap_register_provider(p
, AP_SOCACHE_PROVIDER_GROUP
, "dc",
169 AP_SOCACHE_PROVIDER_VERSION
,
173 module AP_MODULE_DECLARE_DATA socache_dc_module
= {
174 STANDARD20_MODULE_STUFF
,
175 NULL
, NULL
, NULL
, NULL
, NULL
,