Annotation of embedaddon/lighttpd/src/mod_skeleton.c, revision 1.1.1.3
1.1.1.3 ! misho 1: #include "first.h"
! 2:
1.1 misho 3: #include "base.h"
4: #include "log.h"
5: #include "buffer.h"
6:
7: #include "plugin.h"
8:
9: #include <ctype.h>
10: #include <stdlib.h>
11: #include <string.h>
12:
13: /**
14: * this is a skeleton for a lighttpd plugin
15: *
1.1.1.3 ! misho 16: * just replaces every occurrence of 'skeleton' by your plugin name
1.1 misho 17: *
18: * e.g. in vim:
19: *
20: * :%s/skeleton/myhandler/
21: *
22: */
23:
24:
25:
26: /* plugin config for all request/connections */
27:
28: typedef struct {
29: array *match;
30: } plugin_config;
31:
32: typedef struct {
33: PLUGIN_DATA;
34:
35: buffer *match_buf;
36:
37: plugin_config **config_storage;
38:
39: plugin_config conf;
40: } plugin_data;
41:
42: typedef struct {
43: size_t foo;
44: } handler_ctx;
45:
46: static handler_ctx * handler_ctx_init() {
47: handler_ctx * hctx;
48:
49: hctx = calloc(1, sizeof(*hctx));
50:
51: return hctx;
52: }
53:
54: static void handler_ctx_free(handler_ctx *hctx) {
55:
56: free(hctx);
57: }
58:
59: /* init the plugin data */
60: INIT_FUNC(mod_skeleton_init) {
61: plugin_data *p;
62:
63: p = calloc(1, sizeof(*p));
64:
65: p->match_buf = buffer_init();
66:
67: return p;
68: }
69:
1.1.1.3 ! misho 70: /* destroy the plugin data */
1.1 misho 71: FREE_FUNC(mod_skeleton_free) {
72: plugin_data *p = p_d;
73:
74: UNUSED(srv);
75:
76: if (!p) return HANDLER_GO_ON;
77:
78: if (p->config_storage) {
79: size_t i;
80:
81: for (i = 0; i < srv->config_context->used; i++) {
82: plugin_config *s = p->config_storage[i];
83:
1.1.1.3 ! misho 84: if (NULL == s) continue;
1.1 misho 85:
86: array_free(s->match);
87:
88: free(s);
89: }
90: free(p->config_storage);
91: }
92:
93: buffer_free(p->match_buf);
94:
95: free(p);
96:
97: return HANDLER_GO_ON;
98: }
99:
100: /* handle plugin config and check values */
101:
102: SETDEFAULTS_FUNC(mod_skeleton_set_defaults) {
103: plugin_data *p = p_d;
104: size_t i = 0;
105:
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 }
109: };
110:
111: if (!p) return HANDLER_ERROR;
112:
1.1.1.2 misho 113: p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
1.1 misho 114:
115: for (i = 0; i < srv->config_context->used; i++) {
1.1.1.3 ! misho 116: data_config const* config = (data_config const*)srv->config_context->data[i];
1.1 misho 117: plugin_config *s;
118:
119: s = calloc(1, sizeof(plugin_config));
120: s->match = array_init();
121:
122: cv[0].destination = s->match;
123:
124: p->config_storage[i] = s;
125:
1.1.1.3 ! misho 126: if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
1.1 misho 127: return HANDLER_ERROR;
128: }
129: }
130:
131: return HANDLER_GO_ON;
132: }
133:
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];
139:
140: PATCH(match);
141:
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];
146:
147: /* condition didn't match */
148: if (!config_check_cond(srv, con, dc)) continue;
149:
150: /* merge config */
151: for (j = 0; j < dc->value->used; j++) {
152: data_unset *du = dc->value->data[j];
153:
154: if (buffer_is_equal_string(du->key, CONST_STR_LEN("skeleton.array"))) {
155: PATCH(match);
156: }
157: }
158: }
159:
160: return 0;
161: }
162: #undef PATCH
163:
164: URIHANDLER_FUNC(mod_skeleton_uri_handler) {
165: plugin_data *p = p_d;
1.1.1.3 ! misho 166: size_t s_len;
! 167: size_t k;
1.1 misho 168:
169: UNUSED(srv);
170:
171: if (con->mode != DIRECT) return HANDLER_GO_ON;
172:
1.1.1.3 ! misho 173: s_len = buffer_string_length(con->uri.path);
! 174: if (0 == s_len) return HANDLER_GO_ON;
1.1 misho 175:
176: mod_skeleton_patch_connection(srv, con, p);
177:
178: for (k = 0; k < p->conf.match->used; k++) {
179: data_string *ds = (data_string *)p->conf.match->data[k];
1.1.1.3 ! misho 180: size_t ct_len = buffer_string_length(ds->value);
1.1 misho 181:
182: if (ct_len > s_len) continue;
1.1.1.3 ! misho 183: if (ct_len == 0) continue;
1.1 misho 184:
185: if (0 == strncmp(con->uri.path->ptr + s_len - ct_len, ds->value->ptr, ct_len)) {
186: con->http_status = 403;
187:
188: return HANDLER_FINISHED;
189: }
190: }
191:
192: /* not found */
193: return HANDLER_GO_ON;
194: }
195:
196: /* this function is called at dlopen() time and inits the callbacks */
197:
198: int mod_skeleton_plugin_init(plugin *p) {
199: p->version = LIGHTTPD_VERSION_ID;
200: p->name = buffer_init_string("skeleton");
201:
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;
206:
207: p->data = NULL;
208:
209: return 0;
210: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>