[core] avoid spurious trace and error abort
[lighttpd.git] / src / mod_skeleton.c
blob9ad6c630c56971dee82c6ce264812de2d08e0cf8
1 #include "first.h"
3 #include "base.h"
4 #include "log.h"
5 #include "buffer.h"
7 #include "plugin.h"
9 #include <ctype.h>
10 #include <stdlib.h>
11 #include <string.h>
13 /**
14 * this is a skeleton for a lighttpd plugin
16 * just replaces every occurrence of 'skeleton' by your plugin name
18 * e.g. in vim:
20 * :%s/skeleton/myhandler/
26 /* plugin config for all request/connections */
28 typedef struct {
29 array *match;
30 } plugin_config;
32 typedef struct {
33 PLUGIN_DATA;
35 buffer *match_buf;
37 plugin_config **config_storage;
39 plugin_config conf;
40 } plugin_data;
42 typedef struct {
43 size_t foo;
44 } handler_ctx;
46 static handler_ctx * handler_ctx_init() {
47 handler_ctx * hctx;
49 hctx = calloc(1, sizeof(*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));
65 p->match_buf = buffer_init();
67 return p;
70 /* destroy the plugin data */
71 FREE_FUNC(mod_skeleton_free) {
72 plugin_data *p = p_d;
74 UNUSED(srv);
76 if (!p) return HANDLER_GO_ON;
78 if (p->config_storage) {
79 size_t i;
81 for (i = 0; i < srv->config_context->used; i++) {
82 plugin_config *s = p->config_storage[i];
84 if (NULL == s) continue;
86 array_free(s->match);
88 free(s);
90 free(p->config_storage);
93 buffer_free(p->match_buf);
95 free(p);
97 return HANDLER_GO_ON;
100 /* handle plugin config and check values */
102 SETDEFAULTS_FUNC(mod_skeleton_set_defaults) {
103 plugin_data *p = p_d;
104 size_t i = 0;
106 config_values_t cv[] = {
107 { "skeleton.array", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
108 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
111 if (!p) return HANDLER_ERROR;
113 p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
115 for (i = 0; i < srv->config_context->used; i++) {
116 data_config const* config = (data_config const*)srv->config_context->data[i];
117 plugin_config *s;
119 s = calloc(1, sizeof(plugin_config));
120 s->match = array_init();
122 cv[0].destination = s->match;
124 p->config_storage[i] = s;
126 if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
127 return HANDLER_ERROR;
131 return HANDLER_GO_ON;
134 #define PATCH(x) \
135 p->conf.x = s->x;
136 static int mod_skeleton_patch_connection(server *srv, connection *con, plugin_data *p) {
137 size_t i, j;
138 plugin_config *s = p->config_storage[0];
140 PATCH(match);
142 /* skip the first, the global context */
143 for (i = 1; i < srv->config_context->used; i++) {
144 data_config *dc = (data_config *)srv->config_context->data[i];
145 s = p->config_storage[i];
147 /* condition didn't match */
148 if (!config_check_cond(srv, con, dc)) continue;
150 /* merge config */
151 for (j = 0; j < dc->value->used; j++) {
152 data_unset *du = dc->value->data[j];
154 if (buffer_is_equal_string(du->key, CONST_STR_LEN("skeleton.array"))) {
155 PATCH(match);
160 return 0;
162 #undef PATCH
164 URIHANDLER_FUNC(mod_skeleton_uri_handler) {
165 plugin_data *p = p_d;
166 size_t s_len;
167 size_t k;
169 UNUSED(srv);
171 if (con->mode != DIRECT) return HANDLER_GO_ON;
173 s_len = buffer_string_length(con->uri.path);
174 if (0 == s_len) return HANDLER_GO_ON;
176 mod_skeleton_patch_connection(srv, con, p);
178 for (k = 0; k < p->conf.match->used; k++) {
179 data_string *ds = (data_string *)p->conf.match->data[k];
180 size_t ct_len = buffer_string_length(ds->value);
182 if (ct_len > s_len) continue;
183 if (ct_len == 0) continue;
185 if (0 == strncmp(con->uri.path->ptr + s_len - ct_len, ds->value->ptr, ct_len)) {
186 con->http_status = 403;
188 return HANDLER_FINISHED;
192 /* not found */
193 return HANDLER_GO_ON;
196 /* this function is called at dlopen() time and inits the callbacks */
198 int mod_skeleton_plugin_init(plugin *p) {
199 p->version = LIGHTTPD_VERSION_ID;
200 p->name = buffer_init_string("skeleton");
202 p->init = mod_skeleton_init;
203 p->handle_uri_clean = mod_skeleton_uri_handler;
204 p->set_defaults = mod_skeleton_set_defaults;
205 p->cleanup = mod_skeleton_free;
207 p->data = NULL;
209 return 0;