[mod_ssi] produce content in subrequest hook
[lighttpd.git] / src / mod_cml.c
blob0b264816b2c05cf728249d2dc3728a679fe65f73
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>
17 #include <unistd.h>
18 #include <stdio.h>
20 /* init the plugin data */
21 INIT_FUNC(mod_cml_init) {
22 plugin_data *p;
24 p = calloc(1, sizeof(*p));
26 p->basedir = buffer_init();
27 p->baseurl = buffer_init();
28 p->trigger_handler = buffer_init();
30 return p;
33 /* detroy the plugin data */
34 FREE_FUNC(mod_cml_free) {
35 plugin_data *p = p_d;
37 UNUSED(srv);
39 if (!p) return HANDLER_GO_ON;
41 if (p->config_storage) {
42 size_t i;
43 for (i = 0; i < srv->config_context->used; i++) {
44 plugin_config *s = p->config_storage[i];
46 if (NULL == s) continue;
48 buffer_free(s->ext);
50 buffer_free(s->mc_namespace);
51 buffer_free(s->power_magnet);
52 array_free(s->mc_hosts);
54 #if defined(USE_MEMCACHED)
55 if (s->memc) memcached_free(s->memc);
56 #endif
58 free(s);
60 free(p->config_storage);
63 buffer_free(p->trigger_handler);
64 buffer_free(p->basedir);
65 buffer_free(p->baseurl);
67 free(p);
69 return HANDLER_GO_ON;
72 /* handle plugin config and check values */
74 SETDEFAULTS_FUNC(mod_cml_set_defaults) {
75 plugin_data *p = p_d;
76 size_t i = 0;
78 config_values_t cv[] = {
79 { "cml.extension", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
80 { "cml.memcache-hosts", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
81 { "cml.memcache-namespace", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
82 { "cml.power-magnet", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 3 */
83 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
86 if (!p) return HANDLER_ERROR;
88 p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
90 for (i = 0; i < srv->config_context->used; i++) {
91 data_config const* config = (data_config const*)srv->config_context->data[i];
92 plugin_config *s;
94 s = malloc(sizeof(plugin_config));
95 s->ext = buffer_init();
96 s->mc_hosts = array_init();
97 s->mc_namespace = buffer_init();
98 s->power_magnet = buffer_init();
99 #if defined(USE_MEMCACHED)
100 s->memc = NULL;
101 #endif
103 cv[0].destination = s->ext;
104 cv[1].destination = s->mc_hosts;
105 cv[2].destination = s->mc_namespace;
106 cv[3].destination = s->power_magnet;
108 p->config_storage[i] = s;
110 if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
111 return HANDLER_ERROR;
114 if (s->mc_hosts->used) {
115 #if defined(USE_MEMCACHED)
116 buffer *option_string = buffer_init();
117 size_t k;
120 data_string *ds = (data_string *)s->mc_hosts->data[0];
122 buffer_append_string_len(option_string, CONST_STR_LEN("--SERVER="));
123 buffer_append_string_buffer(option_string, ds->value);
126 for (k = 1; k < s->mc_hosts->used; k++) {
127 data_string *ds = (data_string *)s->mc_hosts->data[k];
129 buffer_append_string_len(option_string, CONST_STR_LEN(" --SERVER="));
130 buffer_append_string_buffer(option_string, ds->value);
133 s->memc = memcached(CONST_BUF_LEN(option_string));
135 if (NULL == s->memc) {
136 log_error_write(srv, __FILE__, __LINE__, "sb",
137 "configuring memcached failed for option string:",
138 option_string);
140 buffer_free(option_string);
142 if (NULL == s->memc) return HANDLER_ERROR;
143 #else
144 log_error_write(srv, __FILE__, __LINE__, "s",
145 "memcache support is not compiled in but cml.memcache-hosts is set, aborting");
146 return HANDLER_ERROR;
147 #endif
151 return HANDLER_GO_ON;
154 #define PATCH(x) \
155 p->conf.x = s->x;
156 static int mod_cml_patch_connection(server *srv, connection *con, plugin_data *p) {
157 size_t i, j;
158 plugin_config *s = p->config_storage[0];
160 PATCH(ext);
161 #if defined(USE_MEMCACHED)
162 PATCH(memc);
163 #endif
164 PATCH(mc_namespace);
165 PATCH(power_magnet);
167 /* skip the first, the global context */
168 for (i = 1; i < srv->config_context->used; i++) {
169 data_config *dc = (data_config *)srv->config_context->data[i];
170 s = p->config_storage[i];
172 /* condition didn't match */
173 if (!config_check_cond(srv, con, dc)) continue;
175 /* merge config */
176 for (j = 0; j < dc->value->used; j++) {
177 data_unset *du = dc->value->data[j];
179 if (buffer_is_equal_string(du->key, CONST_STR_LEN("cml.extension"))) {
180 PATCH(ext);
181 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("cml.memcache-hosts"))) {
182 #if defined(USE_MEMCACHED)
183 PATCH(memc);
184 #endif
185 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("cml.memcache-namespace"))) {
186 PATCH(mc_namespace);
187 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("cml.power-magnet"))) {
188 PATCH(power_magnet);
193 return 0;
195 #undef PATCH
197 static int cache_call_lua(server *srv, connection *con, plugin_data *p, buffer *cml_file) {
198 buffer *b;
199 char *c;
201 /* cleanup basedir */
202 b = p->baseurl;
203 buffer_copy_buffer(b, con->uri.path);
204 for (c = b->ptr + buffer_string_length(b); c > b->ptr && *c != '/'; c--);
206 if (*c == '/') {
207 buffer_string_set_length(b, c - b->ptr + 1);
210 b = p->basedir;
211 buffer_copy_buffer(b, con->physical.path);
212 for (c = b->ptr + buffer_string_length(b); c > b->ptr && *c != '/'; c--);
214 if (*c == '/') {
215 buffer_string_set_length(b, c - b->ptr + 1);
219 /* prepare variables
220 * - cookie-based
221 * - get-param-based
223 return cache_parse_lua(srv, con, p, cml_file);
226 URIHANDLER_FUNC(mod_cml_power_magnet) {
227 plugin_data *p = p_d;
229 mod_cml_patch_connection(srv, con, p);
231 buffer_reset(p->basedir);
232 buffer_reset(p->baseurl);
233 buffer_reset(p->trigger_handler);
235 if (buffer_string_is_empty(p->conf.power_magnet)) return HANDLER_GO_ON;
238 * power-magnet:
239 * cml.power-magnet = server.docroot + "/rewrite.cml"
241 * is called on EACH request, take the original REQUEST_URI and modifies the
242 * request header as neccesary.
244 * First use:
245 * if file_exists("/maintainance.html") {
246 * output_include = ( "/maintainance.html" )
247 * return CACHE_HIT
250 * as we only want to rewrite HTML like requests we should cover it in a conditional
252 * */
254 switch(cache_call_lua(srv, con, p, p->conf.power_magnet)) {
255 case -1:
256 /* error */
257 if (con->conf.log_request_handling) {
258 log_error_write(srv, __FILE__, __LINE__, "s", "cache-error");
260 con->http_status = 500;
261 return HANDLER_COMEBACK;
262 case 0:
263 if (con->conf.log_request_handling) {
264 log_error_write(srv, __FILE__, __LINE__, "s", "cache-hit");
266 /* cache-hit */
267 buffer_reset(con->physical.path);
268 return HANDLER_FINISHED;
269 case 1:
270 /* cache miss */
271 return HANDLER_GO_ON;
272 default:
273 con->http_status = 500;
274 return HANDLER_COMEBACK;
278 URIHANDLER_FUNC(mod_cml_is_handled) {
279 plugin_data *p = p_d;
281 if (buffer_string_is_empty(con->physical.path)) return HANDLER_ERROR;
283 mod_cml_patch_connection(srv, con, p);
285 buffer_reset(p->basedir);
286 buffer_reset(p->baseurl);
287 buffer_reset(p->trigger_handler);
289 if (buffer_string_is_empty(p->conf.ext)) return HANDLER_GO_ON;
291 if (!buffer_is_equal_right_len(con->physical.path, p->conf.ext, buffer_string_length(p->conf.ext))) {
292 return HANDLER_GO_ON;
295 switch(cache_call_lua(srv, con, p, con->physical.path)) {
296 case -1:
297 /* error */
298 if (con->conf.log_request_handling) {
299 log_error_write(srv, __FILE__, __LINE__, "s", "cache-error");
301 con->http_status = 500;
302 return HANDLER_COMEBACK;
303 case 0:
304 if (con->conf.log_request_handling) {
305 log_error_write(srv, __FILE__, __LINE__, "s", "cache-hit");
307 /* cache-hit */
308 buffer_reset(con->physical.path);
309 return HANDLER_FINISHED;
310 case 1:
311 if (con->conf.log_request_handling) {
312 log_error_write(srv, __FILE__, __LINE__, "s", "cache-miss");
314 /* cache miss */
315 return HANDLER_COMEBACK;
316 default:
317 con->http_status = 500;
318 return HANDLER_COMEBACK;
322 int mod_cml_plugin_init(plugin *p);
323 int mod_cml_plugin_init(plugin *p) {
324 p->version = LIGHTTPD_VERSION_ID;
325 p->name = buffer_init_string("cache");
327 p->init = mod_cml_init;
328 p->cleanup = mod_cml_free;
329 p->set_defaults = mod_cml_set_defaults;
331 p->handle_subrequest_start = mod_cml_is_handled;
332 p->handle_physical = mod_cml_power_magnet;
334 p->data = NULL;
336 return 0;