[mod_openssl] remove erroneous SSL_set_shutdown()
[lighttpd.git] / src / mod_cml.c
blob16abd5895f4cb599a043d6bbea3965635c0c02ad
1 #include "first.h"
3 #include "buffer.h"
4 #include "server.h"
5 #include "log.h"
6 #include "plugin.h"
7 #include "response.h"
9 #include "mod_cml.h"
11 #include <sys/stat.h>
12 #include <time.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <errno.h>
18 /* init the plugin data */
19 INIT_FUNC(mod_cml_init) {
20 plugin_data *p;
22 p = calloc(1, sizeof(*p));
24 p->basedir = buffer_init();
25 p->baseurl = buffer_init();
26 p->trigger_handler = buffer_init();
28 return p;
31 /* detroy the plugin data */
32 FREE_FUNC(mod_cml_free) {
33 plugin_data *p = p_d;
35 UNUSED(srv);
37 if (!p) return HANDLER_GO_ON;
39 if (p->config_storage) {
40 size_t i;
41 for (i = 0; i < srv->config_context->used; i++) {
42 plugin_config *s = p->config_storage[i];
44 if (NULL == s) continue;
46 buffer_free(s->ext);
48 buffer_free(s->mc_namespace);
49 buffer_free(s->power_magnet);
50 array_free(s->mc_hosts);
52 #if defined(USE_MEMCACHED)
53 if (s->memc) memcached_free(s->memc);
54 #endif
56 free(s);
58 free(p->config_storage);
61 buffer_free(p->trigger_handler);
62 buffer_free(p->basedir);
63 buffer_free(p->baseurl);
65 free(p);
67 return HANDLER_GO_ON;
70 /* handle plugin config and check values */
72 SETDEFAULTS_FUNC(mod_cml_set_defaults) {
73 plugin_data *p = p_d;
74 size_t i = 0;
76 config_values_t cv[] = {
77 { "cml.extension", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
78 { "cml.memcache-hosts", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
79 { "cml.memcache-namespace", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
80 { "cml.power-magnet", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 3 */
81 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
84 if (!p) return HANDLER_ERROR;
86 p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
88 for (i = 0; i < srv->config_context->used; i++) {
89 data_config const* config = (data_config const*)srv->config_context->data[i];
90 plugin_config *s;
92 s = calloc(1, sizeof(plugin_config));
93 s->ext = buffer_init();
94 s->mc_hosts = array_init();
95 s->mc_namespace = buffer_init();
96 s->power_magnet = buffer_init();
97 #if defined(USE_MEMCACHED)
98 s->memc = NULL;
99 #endif
101 cv[0].destination = s->ext;
102 cv[1].destination = s->mc_hosts;
103 cv[2].destination = s->mc_namespace;
104 cv[3].destination = s->power_magnet;
106 p->config_storage[i] = s;
108 if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
109 return HANDLER_ERROR;
112 if (!array_is_vlist(s->mc_hosts)) {
113 log_error_write(srv, __FILE__, __LINE__, "s",
114 "unexpected value for cml.memcache-hosts; expected list of \"host\"");
115 return HANDLER_ERROR;
118 if (s->mc_hosts->used) {
119 #if defined(USE_MEMCACHED)
120 buffer *option_string = buffer_init();
121 size_t k;
124 data_string *ds = (data_string *)s->mc_hosts->data[0];
126 buffer_append_string_len(option_string, CONST_STR_LEN("--SERVER="));
127 buffer_append_string_buffer(option_string, ds->value);
130 for (k = 1; k < s->mc_hosts->used; k++) {
131 data_string *ds = (data_string *)s->mc_hosts->data[k];
133 buffer_append_string_len(option_string, CONST_STR_LEN(" --SERVER="));
134 buffer_append_string_buffer(option_string, ds->value);
137 s->memc = memcached(CONST_BUF_LEN(option_string));
139 if (NULL == s->memc) {
140 log_error_write(srv, __FILE__, __LINE__, "sb",
141 "configuring memcached failed for option string:",
142 option_string);
144 buffer_free(option_string);
146 if (NULL == s->memc) return HANDLER_ERROR;
147 #else
148 log_error_write(srv, __FILE__, __LINE__, "s",
149 "memcache support is not compiled in but cml.memcache-hosts is set, aborting");
150 return HANDLER_ERROR;
151 #endif
155 return HANDLER_GO_ON;
158 #define PATCH(x) \
159 p->conf.x = s->x;
160 static int mod_cml_patch_connection(server *srv, connection *con, plugin_data *p) {
161 size_t i, j;
162 plugin_config *s = p->config_storage[0];
164 PATCH(ext);
165 #if defined(USE_MEMCACHED)
166 PATCH(memc);
167 #endif
168 PATCH(mc_namespace);
169 PATCH(power_magnet);
171 /* skip the first, the global context */
172 for (i = 1; i < srv->config_context->used; i++) {
173 data_config *dc = (data_config *)srv->config_context->data[i];
174 s = p->config_storage[i];
176 /* condition didn't match */
177 if (!config_check_cond(srv, con, dc)) continue;
179 /* merge config */
180 for (j = 0; j < dc->value->used; j++) {
181 data_unset *du = dc->value->data[j];
183 if (buffer_is_equal_string(du->key, CONST_STR_LEN("cml.extension"))) {
184 PATCH(ext);
185 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("cml.memcache-hosts"))) {
186 #if defined(USE_MEMCACHED)
187 PATCH(memc);
188 #endif
189 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("cml.memcache-namespace"))) {
190 PATCH(mc_namespace);
191 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("cml.power-magnet"))) {
192 PATCH(power_magnet);
197 return 0;
199 #undef PATCH
201 static int cache_call_lua(server *srv, connection *con, plugin_data *p, buffer *cml_file) {
202 buffer *b;
203 char *c;
205 /* cleanup basedir */
206 b = p->baseurl;
207 buffer_copy_buffer(b, con->uri.path);
208 for (c = b->ptr + buffer_string_length(b); c > b->ptr && *c != '/'; c--);
210 if (*c == '/') {
211 buffer_string_set_length(b, c - b->ptr + 1);
214 b = p->basedir;
215 buffer_copy_buffer(b, con->physical.path);
216 for (c = b->ptr + buffer_string_length(b); c > b->ptr && *c != '/'; c--);
218 if (*c == '/') {
219 buffer_string_set_length(b, c - b->ptr + 1);
223 /* prepare variables
224 * - cookie-based
225 * - get-param-based
227 return cache_parse_lua(srv, con, p, cml_file);
230 URIHANDLER_FUNC(mod_cml_power_magnet) {
231 plugin_data *p = p_d;
233 mod_cml_patch_connection(srv, con, p);
235 buffer_reset(p->basedir);
236 buffer_reset(p->baseurl);
237 buffer_reset(p->trigger_handler);
239 if (buffer_string_is_empty(p->conf.power_magnet)) return HANDLER_GO_ON;
242 * power-magnet:
243 * cml.power-magnet = server.docroot + "/rewrite.cml"
245 * is called on EACH request, take the original REQUEST_URI and modifies the
246 * request header as neccesary.
248 * First use:
249 * if file_exists("/maintainance.html") {
250 * output_include = ( "/maintainance.html" )
251 * return CACHE_HIT
254 * as we only want to rewrite HTML like requests we should cover it in a conditional
256 * */
258 switch(cache_call_lua(srv, con, p, p->conf.power_magnet)) {
259 case -1:
260 /* error */
261 if (con->conf.log_request_handling) {
262 log_error_write(srv, __FILE__, __LINE__, "s", "cache-error");
264 con->http_status = 500;
265 return HANDLER_COMEBACK;
266 case 0:
267 if (con->conf.log_request_handling) {
268 log_error_write(srv, __FILE__, __LINE__, "s", "cache-hit");
270 /* cache-hit */
271 buffer_reset(con->physical.path);
272 return HANDLER_FINISHED;
273 case 1:
274 /* cache miss */
275 return HANDLER_GO_ON;
276 default:
277 con->http_status = 500;
278 return HANDLER_COMEBACK;
282 URIHANDLER_FUNC(mod_cml_is_handled) {
283 plugin_data *p = p_d;
285 if (buffer_string_is_empty(con->physical.path)) return HANDLER_ERROR;
287 mod_cml_patch_connection(srv, con, p);
289 buffer_reset(p->basedir);
290 buffer_reset(p->baseurl);
291 buffer_reset(p->trigger_handler);
293 if (buffer_string_is_empty(p->conf.ext)) return HANDLER_GO_ON;
295 if (!buffer_is_equal_right_len(con->physical.path, p->conf.ext, buffer_string_length(p->conf.ext))) {
296 return HANDLER_GO_ON;
299 switch(cache_call_lua(srv, con, p, con->physical.path)) {
300 case -1:
301 /* error */
302 if (con->conf.log_request_handling) {
303 log_error_write(srv, __FILE__, __LINE__, "s", "cache-error");
305 con->http_status = 500;
306 return HANDLER_COMEBACK;
307 case 0:
308 if (con->conf.log_request_handling) {
309 log_error_write(srv, __FILE__, __LINE__, "s", "cache-hit");
311 /* cache-hit */
312 buffer_reset(con->physical.path);
313 return HANDLER_FINISHED;
314 case 1:
315 if (con->conf.log_request_handling) {
316 log_error_write(srv, __FILE__, __LINE__, "s", "cache-miss");
318 /* cache miss */
319 return HANDLER_COMEBACK;
320 default:
321 con->http_status = 500;
322 return HANDLER_COMEBACK;
326 int mod_cml_plugin_init(plugin *p);
327 int mod_cml_plugin_init(plugin *p) {
328 p->version = LIGHTTPD_VERSION_ID;
329 p->name = buffer_init_string("cache");
331 p->init = mod_cml_init;
332 p->cleanup = mod_cml_free;
333 p->set_defaults = mod_cml_set_defaults;
335 p->handle_subrequest_start = mod_cml_is_handled;
336 p->handle_physical = mod_cml_power_magnet;
338 p->data = NULL;
340 return 0;