[core] http_response_send_file() shared code (#2017)
[lighttpd.git] / src / mod_skeleton.c
blob77fd53cfbeef37390041b168bf07ef2b2df44084
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 occurance 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 int s_len;
167 size_t k, i;
169 UNUSED(srv);
171 if (con->mode != DIRECT) return HANDLER_GO_ON;
173 if (con->uri.path->used == 0) return HANDLER_GO_ON;
175 mod_skeleton_patch_connection(srv, con, p);
177 s_len = con->uri.path->used - 1;
179 for (k = 0; k < p->conf.match->used; k++) {
180 data_string *ds = (data_string *)p->conf.match->data[k];
181 int ct_len = ds->value->used - 1;
183 if (ct_len > s_len) continue;
184 if (ds->value->used == 0) continue;
186 if (0 == strncmp(con->uri.path->ptr + s_len - ct_len, ds->value->ptr, ct_len)) {
187 con->http_status = 403;
189 return HANDLER_FINISHED;
193 /* not found */
194 return HANDLER_GO_ON;
197 /* this function is called at dlopen() time and inits the callbacks */
199 int mod_skeleton_plugin_init(plugin *p) {
200 p->version = LIGHTTPD_VERSION_ID;
201 p->name = buffer_init_string("skeleton");
203 p->init = mod_skeleton_init;
204 p->handle_uri_clean = mod_skeleton_uri_handler;
205 p->set_defaults = mod_skeleton_set_defaults;
206 p->cleanup = mod_skeleton_free;
208 p->data = NULL;
210 return 0;