13 * this is a skeleton for a lighttpd plugin
15 * just replaces every occurrence of 'skeleton' by your plugin name
19 * :%s/skeleton/myhandler/
25 /* plugin config for all request/connections */
36 plugin_config
**config_storage
;
45 static handler_ctx
* handler_ctx_init() {
48 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
));
66 p
->match_buf
= buffer_init();
71 /* destroy the plugin data */
72 FREE_FUNC(mod_skeleton_free
) {
77 if (!p
) return HANDLER_GO_ON
;
79 if (p
->config_storage
) {
82 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
83 plugin_config
*s
= p
->config_storage
[i
];
85 if (NULL
== s
) continue;
91 free(p
->config_storage
);
94 buffer_free(p
->match_buf
);
101 /* handle plugin config and check values */
103 SETDEFAULTS_FUNC(mod_skeleton_set_defaults
) {
104 plugin_data
*p
= p_d
;
107 config_values_t cv
[] = {
108 { "skeleton.array", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 0 */
109 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
112 if (!p
) return HANDLER_ERROR
;
114 p
->config_storage
= calloc(1, srv
->config_context
->used
* sizeof(plugin_config
*));
115 force_assert(p
->config_storage
);
117 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
118 data_config
const* config
= (data_config
const*)srv
->config_context
->data
[i
];
119 plugin_config
*s
= calloc(1, sizeof(plugin_config
));
121 s
->match
= array_init();
123 cv
[0].destination
= s
->match
;
125 p
->config_storage
[i
] = s
;
127 if (0 != config_insert_values_global(srv
, config
->value
, cv
, i
== 0 ? T_CONFIG_SCOPE_SERVER
: T_CONFIG_SCOPE_CONNECTION
)) {
128 return HANDLER_ERROR
;
131 if (!array_is_vlist(s
->match
)) {
132 log_error_write(srv
, __FILE__
, __LINE__
, "s",
133 "unexpected value for skeleton.array; expected list of \"urlpath\"");
134 return HANDLER_ERROR
;
138 return HANDLER_GO_ON
;
143 static int mod_skeleton_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
145 plugin_config
*s
= p
->config_storage
[0];
149 /* skip the first, the global context */
150 for (i
= 1; i
< srv
->config_context
->used
; i
++) {
151 data_config
*dc
= (data_config
*)srv
->config_context
->data
[i
];
152 s
= p
->config_storage
[i
];
154 /* condition didn't match */
155 if (!config_check_cond(srv
, con
, dc
)) continue;
158 for (j
= 0; j
< dc
->value
->used
; j
++) {
159 data_unset
*du
= dc
->value
->data
[j
];
161 if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("skeleton.array"))) {
171 URIHANDLER_FUNC(mod_skeleton_uri_handler
) {
172 plugin_data
*p
= p_d
;
178 if (con
->mode
!= DIRECT
) return HANDLER_GO_ON
;
180 s_len
= buffer_string_length(con
->uri
.path
);
181 if (0 == s_len
) return HANDLER_GO_ON
;
183 mod_skeleton_patch_connection(srv
, con
, p
);
185 for (k
= 0; k
< p
->conf
.match
->used
; k
++) {
186 data_string
*ds
= (data_string
*)p
->conf
.match
->data
[k
];
187 size_t ct_len
= buffer_string_length(ds
->value
);
189 if (ct_len
> s_len
) continue;
190 if (ct_len
== 0) continue;
192 if (0 == strncmp(con
->uri
.path
->ptr
+ s_len
- ct_len
, ds
->value
->ptr
, ct_len
)) {
193 con
->http_status
= 403;
195 return HANDLER_FINISHED
;
200 return HANDLER_GO_ON
;
203 /* this function is called at dlopen() time and inits the callbacks */
205 int mod_skeleton_plugin_init(plugin
*p
) {
206 p
->version
= LIGHTTPD_VERSION_ID
;
207 p
->name
= buffer_init_string("skeleton");
209 p
->init
= mod_skeleton_init
;
210 p
->handle_uri_clean
= mod_skeleton_uri_handler
;
211 p
->set_defaults
= mod_skeleton_set_defaults
;
212 p
->cleanup
= mod_skeleton_free
;