22 /* init the plugin data */
23 INIT_FUNC(mod_cml_init
) {
26 p
= calloc(1, sizeof(*p
));
28 p
->basedir
= buffer_init();
29 p
->baseurl
= buffer_init();
30 p
->trigger_handler
= buffer_init();
35 /* detroy the plugin data */
36 FREE_FUNC(mod_cml_free
) {
41 if (!p
) return HANDLER_GO_ON
;
43 if (p
->config_storage
) {
45 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
46 plugin_config
*s
= p
->config_storage
[i
];
48 if (NULL
== s
) continue;
52 buffer_free(s
->mc_namespace
);
53 buffer_free(s
->power_magnet
);
54 array_free(s
->mc_hosts
);
56 #if defined(USE_MEMCACHED)
57 if (s
->memc
) memcached_free(s
->memc
);
62 free(p
->config_storage
);
65 buffer_free(p
->trigger_handler
);
66 buffer_free(p
->basedir
);
67 buffer_free(p
->baseurl
);
74 /* handle plugin config and check values */
76 SETDEFAULTS_FUNC(mod_cml_set_defaults
) {
80 config_values_t cv
[] = {
81 { "cml.extension", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 0 */
82 { "cml.memcache-hosts", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 1 */
83 { "cml.memcache-namespace", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 2 */
84 { "cml.power-magnet", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 3 */
85 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
88 if (!p
) return HANDLER_ERROR
;
90 p
->config_storage
= calloc(1, srv
->config_context
->used
* sizeof(plugin_config
*));
92 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
93 data_config
const* config
= (data_config
const*)srv
->config_context
->data
[i
];
96 s
= malloc(sizeof(plugin_config
));
97 s
->ext
= buffer_init();
98 s
->mc_hosts
= array_init();
99 s
->mc_namespace
= buffer_init();
100 s
->power_magnet
= buffer_init();
101 #if defined(USE_MEMCACHED)
105 cv
[0].destination
= s
->ext
;
106 cv
[1].destination
= s
->mc_hosts
;
107 cv
[2].destination
= s
->mc_namespace
;
108 cv
[3].destination
= s
->power_magnet
;
110 p
->config_storage
[i
] = s
;
112 if (0 != config_insert_values_global(srv
, config
->value
, cv
, i
== 0 ? T_CONFIG_SCOPE_SERVER
: T_CONFIG_SCOPE_CONNECTION
)) {
113 return HANDLER_ERROR
;
116 if (s
->mc_hosts
->used
) {
117 #if defined(USE_MEMCACHED)
118 buffer
*option_string
= buffer_init();
122 data_string
*ds
= (data_string
*)s
->mc_hosts
->data
[0];
124 buffer_append_string_len(option_string
, CONST_STR_LEN("--SERVER="));
125 buffer_append_string_buffer(option_string
, ds
->value
);
128 for (k
= 1; k
< s
->mc_hosts
->used
; k
++) {
129 data_string
*ds
= (data_string
*)s
->mc_hosts
->data
[k
];
131 buffer_append_string_len(option_string
, CONST_STR_LEN(" --SERVER="));
132 buffer_append_string_buffer(option_string
, ds
->value
);
135 s
->memc
= memcached(CONST_BUF_LEN(option_string
));
137 if (NULL
== s
->memc
) {
138 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
139 "configuring memcached failed for option string:",
142 buffer_free(option_string
);
144 if (NULL
== s
->memc
) return HANDLER_ERROR
;
146 log_error_write(srv
, __FILE__
, __LINE__
, "s",
147 "memcache support is not compiled in but cml.memcache-hosts is set, aborting");
148 return HANDLER_ERROR
;
153 return HANDLER_GO_ON
;
158 static int mod_cml_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
160 plugin_config
*s
= p
->config_storage
[0];
163 #if defined(USE_MEMCACHED)
169 /* skip the first, the global context */
170 for (i
= 1; i
< srv
->config_context
->used
; i
++) {
171 data_config
*dc
= (data_config
*)srv
->config_context
->data
[i
];
172 s
= p
->config_storage
[i
];
174 /* condition didn't match */
175 if (!config_check_cond(srv
, con
, dc
)) continue;
178 for (j
= 0; j
< dc
->value
->used
; j
++) {
179 data_unset
*du
= dc
->value
->data
[j
];
181 if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("cml.extension"))) {
183 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("cml.memcache-hosts"))) {
184 #if defined(USE_MEMCACHED)
187 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("cml.memcache-namespace"))) {
189 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("cml.power-magnet"))) {
199 static int cache_call_lua(server
*srv
, connection
*con
, plugin_data
*p
, buffer
*cml_file
) {
203 /* cleanup basedir */
205 buffer_copy_buffer(b
, con
->uri
.path
);
206 for (c
= b
->ptr
+ buffer_string_length(b
); c
> b
->ptr
&& *c
!= '/'; c
--);
209 buffer_string_set_length(b
, c
- b
->ptr
+ 1);
213 buffer_copy_buffer(b
, con
->physical
.path
);
214 for (c
= b
->ptr
+ buffer_string_length(b
); c
> b
->ptr
&& *c
!= '/'; c
--);
217 buffer_string_set_length(b
, c
- b
->ptr
+ 1);
225 return cache_parse_lua(srv
, con
, p
, cml_file
);
228 URIHANDLER_FUNC(mod_cml_power_magnet
) {
229 plugin_data
*p
= p_d
;
231 mod_cml_patch_connection(srv
, con
, p
);
233 buffer_reset(p
->basedir
);
234 buffer_reset(p
->baseurl
);
235 buffer_reset(p
->trigger_handler
);
237 if (buffer_string_is_empty(p
->conf
.power_magnet
)) return HANDLER_GO_ON
;
241 * cml.power-magnet = server.docroot + "/rewrite.cml"
243 * is called on EACH request, take the original REQUEST_URI and modifies the
244 * request header as neccesary.
247 * if file_exists("/maintainance.html") {
248 * output_include = ( "/maintainance.html" )
252 * as we only want to rewrite HTML like requests we should cover it in a conditional
256 switch(cache_call_lua(srv
, con
, p
, p
->conf
.power_magnet
)) {
259 if (con
->conf
.log_request_handling
) {
260 log_error_write(srv
, __FILE__
, __LINE__
, "s", "cache-error");
262 con
->http_status
= 500;
263 return HANDLER_COMEBACK
;
265 if (con
->conf
.log_request_handling
) {
266 log_error_write(srv
, __FILE__
, __LINE__
, "s", "cache-hit");
269 buffer_reset(con
->physical
.path
);
270 return HANDLER_FINISHED
;
273 return HANDLER_GO_ON
;
275 con
->http_status
= 500;
276 return HANDLER_COMEBACK
;
280 URIHANDLER_FUNC(mod_cml_is_handled
) {
281 plugin_data
*p
= p_d
;
283 if (buffer_string_is_empty(con
->physical
.path
)) return HANDLER_ERROR
;
285 mod_cml_patch_connection(srv
, con
, p
);
287 buffer_reset(p
->basedir
);
288 buffer_reset(p
->baseurl
);
289 buffer_reset(p
->trigger_handler
);
291 if (buffer_string_is_empty(p
->conf
.ext
)) return HANDLER_GO_ON
;
293 if (!buffer_is_equal_right_len(con
->physical
.path
, p
->conf
.ext
, buffer_string_length(p
->conf
.ext
))) {
294 return HANDLER_GO_ON
;
297 switch(cache_call_lua(srv
, con
, p
, con
->physical
.path
)) {
300 if (con
->conf
.log_request_handling
) {
301 log_error_write(srv
, __FILE__
, __LINE__
, "s", "cache-error");
303 con
->http_status
= 500;
304 return HANDLER_COMEBACK
;
306 if (con
->conf
.log_request_handling
) {
307 log_error_write(srv
, __FILE__
, __LINE__
, "s", "cache-hit");
310 buffer_reset(con
->physical
.path
);
311 return HANDLER_FINISHED
;
313 if (con
->conf
.log_request_handling
) {
314 log_error_write(srv
, __FILE__
, __LINE__
, "s", "cache-miss");
317 return HANDLER_COMEBACK
;
319 con
->http_status
= 500;
320 return HANDLER_COMEBACK
;
324 int mod_cml_plugin_init(plugin
*p
);
325 int mod_cml_plugin_init(plugin
*p
) {
326 p
->version
= LIGHTTPD_VERSION_ID
;
327 p
->name
= buffer_init_string("cache");
329 p
->init
= mod_cml_init
;
330 p
->cleanup
= mod_cml_free
;
331 p
->set_defaults
= mod_cml_set_defaults
;
333 p
->handle_subrequest_start
= mod_cml_is_handled
;
334 p
->handle_physical
= mod_cml_power_magnet
;