20 /* init the plugin data */
21 INIT_FUNC(mod_cml_init
) {
24 p
= calloc(1, sizeof(*p
));
26 p
->basedir
= buffer_init();
27 p
->baseurl
= buffer_init();
28 p
->trigger_handler
= buffer_init();
33 /* detroy the plugin data */
34 FREE_FUNC(mod_cml_free
) {
39 if (!p
) return HANDLER_GO_ON
;
41 if (p
->config_storage
) {
43 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
44 plugin_config
*s
= p
->config_storage
[i
];
46 if (NULL
== s
) continue;
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
);
60 free(p
->config_storage
);
63 buffer_free(p
->trigger_handler
);
64 buffer_free(p
->basedir
);
65 buffer_free(p
->baseurl
);
72 /* handle plugin config and check values */
74 SETDEFAULTS_FUNC(mod_cml_set_defaults
) {
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
];
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)
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();
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:",
140 buffer_free(option_string
);
142 if (NULL
== s
->memc
) return HANDLER_ERROR
;
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
;
151 return HANDLER_GO_ON
;
156 static int mod_cml_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
158 plugin_config
*s
= p
->config_storage
[0];
161 #if defined(USE_MEMCACHED)
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;
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"))) {
181 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("cml.memcache-hosts"))) {
182 #if defined(USE_MEMCACHED)
185 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("cml.memcache-namespace"))) {
187 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("cml.power-magnet"))) {
197 static int cache_call_lua(server
*srv
, connection
*con
, plugin_data
*p
, buffer
*cml_file
) {
201 /* cleanup basedir */
203 buffer_copy_buffer(b
, con
->uri
.path
);
204 for (c
= b
->ptr
+ buffer_string_length(b
); c
> b
->ptr
&& *c
!= '/'; c
--);
207 buffer_string_set_length(b
, c
- b
->ptr
+ 1);
211 buffer_copy_buffer(b
, con
->physical
.path
);
212 for (c
= b
->ptr
+ buffer_string_length(b
); c
> b
->ptr
&& *c
!= '/'; c
--);
215 buffer_string_set_length(b
, c
- b
->ptr
+ 1);
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
;
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.
245 * if file_exists("/maintainance.html") {
246 * output_include = ( "/maintainance.html" )
250 * as we only want to rewrite HTML like requests we should cover it in a conditional
254 switch(cache_call_lua(srv
, con
, p
, p
->conf
.power_magnet
)) {
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
;
263 if (con
->conf
.log_request_handling
) {
264 log_error_write(srv
, __FILE__
, __LINE__
, "s", "cache-hit");
267 buffer_reset(con
->physical
.path
);
268 return HANDLER_FINISHED
;
271 return HANDLER_GO_ON
;
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
)) {
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
;
304 if (con
->conf
.log_request_handling
) {
305 log_error_write(srv
, __FILE__
, __LINE__
, "s", "cache-hit");
308 buffer_reset(con
->physical
.path
);
309 return HANDLER_FINISHED
;
311 if (con
->conf
.log_request_handling
) {
312 log_error_write(srv
, __FILE__
, __LINE__
, "s", "cache-miss");
315 return HANDLER_COMEBACK
;
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
;