[core] calloc plugin_config for consistent init
[lighttpd.git] / src / mod_skeleton.c
blobbc182da242fa9a1e6429a153b6f422969e7578bb
1 #include "first.h"
3 #include "base.h"
4 #include "log.h"
5 #include "buffer.h"
7 #include "plugin.h"
9 #include <stdlib.h>
10 #include <string.h>
12 /**
13 * this is a skeleton for a lighttpd plugin
15 * just replaces every occurrence of 'skeleton' by your plugin name
17 * e.g. in vim:
19 * :%s/skeleton/myhandler/
25 /* plugin config for all request/connections */
27 typedef struct {
28 array *match;
29 } plugin_config;
31 typedef struct {
32 PLUGIN_DATA;
34 buffer *match_buf;
36 plugin_config **config_storage;
38 plugin_config conf;
39 } plugin_data;
41 typedef struct {
42 size_t foo;
43 } handler_ctx;
45 static handler_ctx * handler_ctx_init() {
46 handler_ctx * hctx;
48 hctx = calloc(1, sizeof(*hctx));
49 force_assert(hctx);
51 return hctx;
54 static void handler_ctx_free(handler_ctx *hctx) {
56 free(hctx);
59 /* init the plugin data */
60 INIT_FUNC(mod_skeleton_init) {
61 plugin_data *p;
63 p = calloc(1, sizeof(*p));
64 force_assert(p);
66 p->match_buf = buffer_init();
68 return p;
71 /* destroy the plugin data */
72 FREE_FUNC(mod_skeleton_free) {
73 plugin_data *p = p_d;
75 UNUSED(srv);
77 if (!p) return HANDLER_GO_ON;
79 if (p->config_storage) {
80 size_t i;
82 for (i = 0; i < srv->config_context->used; i++) {
83 plugin_config *s = p->config_storage[i];
85 if (NULL == s) continue;
87 array_free(s->match);
89 free(s);
91 free(p->config_storage);
94 buffer_free(p->match_buf);
96 free(p);
98 return HANDLER_GO_ON;
101 /* handle plugin config and check values */
103 SETDEFAULTS_FUNC(mod_skeleton_set_defaults) {
104 plugin_data *p = p_d;
105 size_t i = 0;
107 config_values_t cv[] = {
108 { "skeleton.array", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
109 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
112 if (!p) return HANDLER_ERROR;
114 p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
115 force_assert(p->config_storage);
117 for (i = 0; i < srv->config_context->used; i++) {
118 data_config const* config = (data_config const*)srv->config_context->data[i];
119 plugin_config *s = calloc(1, sizeof(plugin_config));
120 force_assert(s);
121 s->match = array_init();
123 cv[0].destination = s->match;
125 p->config_storage[i] = s;
127 if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
128 return HANDLER_ERROR;
131 if (!array_is_vlist(s->match)) {
132 log_error_write(srv, __FILE__, __LINE__, "s",
133 "unexpected value for skeleton.array; expected list of \"urlpath\"");
134 return HANDLER_ERROR;
138 return HANDLER_GO_ON;
141 #define PATCH(x) \
142 p->conf.x = s->x;
143 static int mod_skeleton_patch_connection(server *srv, connection *con, plugin_data *p) {
144 size_t i, j;
145 plugin_config *s = p->config_storage[0];
147 PATCH(match);
149 /* skip the first, the global context */
150 for (i = 1; i < srv->config_context->used; i++) {
151 data_config *dc = (data_config *)srv->config_context->data[i];
152 s = p->config_storage[i];
154 /* condition didn't match */
155 if (!config_check_cond(srv, con, dc)) continue;
157 /* merge config */
158 for (j = 0; j < dc->value->used; j++) {
159 data_unset *du = dc->value->data[j];
161 if (buffer_is_equal_string(du->key, CONST_STR_LEN("skeleton.array"))) {
162 PATCH(match);
167 return 0;
169 #undef PATCH
171 URIHANDLER_FUNC(mod_skeleton_uri_handler) {
172 plugin_data *p = p_d;
173 size_t s_len;
174 size_t k;
176 UNUSED(srv);
178 if (con->mode != DIRECT) return HANDLER_GO_ON;
180 s_len = buffer_string_length(con->uri.path);
181 if (0 == s_len) return HANDLER_GO_ON;
183 mod_skeleton_patch_connection(srv, con, p);
185 for (k = 0; k < p->conf.match->used; k++) {
186 data_string *ds = (data_string *)p->conf.match->data[k];
187 size_t ct_len = buffer_string_length(ds->value);
189 if (ct_len > s_len) continue;
190 if (ct_len == 0) continue;
192 if (0 == strncmp(con->uri.path->ptr + s_len - ct_len, ds->value->ptr, ct_len)) {
193 con->http_status = 403;
195 return HANDLER_FINISHED;
199 /* not found */
200 return HANDLER_GO_ON;
203 /* this function is called at dlopen() time and inits the callbacks */
205 int mod_skeleton_plugin_init(plugin *p) {
206 p->version = LIGHTTPD_VERSION_ID;
207 p->name = buffer_init_string("skeleton");
209 p->init = mod_skeleton_init;
210 p->handle_uri_clean = mod_skeleton_uri_handler;
211 p->set_defaults = mod_skeleton_set_defaults;
212 p->cleanup = mod_skeleton_free;
214 p->data = NULL;
216 return 0;