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