14 * this is a skeleton for a lighttpd plugin
16 * just replaces every occurrence of 'skeleton' by your plugin name
20 * :%s/skeleton/myhandler/
26 /* plugin config for all request/connections */
37 plugin_config
**config_storage
;
46 static handler_ctx
* handler_ctx_init() {
49 hctx
= calloc(1, sizeof(*hctx
));
54 static void handler_ctx_free(handler_ctx
*hctx
) {
59 /* init the plugin data */
60 INIT_FUNC(mod_skeleton_init
) {
63 p
= calloc(1, sizeof(*p
));
65 p
->match_buf
= buffer_init();
70 /* destroy the plugin data */
71 FREE_FUNC(mod_skeleton_free
) {
76 if (!p
) return HANDLER_GO_ON
;
78 if (p
->config_storage
) {
81 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
82 plugin_config
*s
= p
->config_storage
[i
];
84 if (NULL
== s
) continue;
90 free(p
->config_storage
);
93 buffer_free(p
->match_buf
);
100 /* handle plugin config and check values */
102 SETDEFAULTS_FUNC(mod_skeleton_set_defaults
) {
103 plugin_data
*p
= p_d
;
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
];
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
;
136 static int mod_skeleton_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
138 plugin_config
*s
= p
->config_storage
[0];
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;
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"))) {
164 URIHANDLER_FUNC(mod_skeleton_uri_handler
) {
165 plugin_data
*p
= p_d
;
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
;
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
;