14 * this is a skeleton for a lighttpd plugin
16 * just replaces every occurrence of 'skeleton' by your plugin name
20 * :%s/skeleton/myhandler/
25 /* plugin config for all request/connections */
33 plugin_config
**config_storage
;
38 #if 0 /* (needed if module keeps state for request) */
44 static handler_ctx
* handler_ctx_init() {
45 handler_ctx
* hctx
= calloc(1, sizeof(*hctx
));
50 static void handler_ctx_free(handler_ctx
*hctx
) {
57 /* init the plugin data */
58 INIT_FUNC(mod_skeleton_init
) {
59 return calloc(1, sizeof(plugin_data
));
62 /* destroy the plugin data */
63 FREE_FUNC(mod_skeleton_free
) {
66 if (!p
) return HANDLER_GO_ON
;
67 if (p
->config_storage
) {
68 for (size_t i
= 0; i
< srv
->config_context
->used
; i
++) {
69 plugin_config
*s
= p
->config_storage
[i
];
70 if (NULL
== s
) continue;
74 free(p
->config_storage
);
80 /* handle plugin config and check values */
81 SETDEFAULTS_FUNC(mod_skeleton_set_defaults
) {
85 config_values_t cv
[] = {
86 { "skeleton.array", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 0 */
87 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
90 if (!p
) return HANDLER_ERROR
;
92 p
->config_storage
= calloc(srv
->config_context
->used
, sizeof(plugin_config
*));
93 force_assert(p
->config_storage
);
95 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
96 data_config
const* config
= (data_config
const*)srv
->config_context
->data
[i
];
97 plugin_config
*s
= calloc(1, sizeof(plugin_config
));
99 s
->match
= array_init();
101 cv
[0].destination
= s
->match
;
103 p
->config_storage
[i
] = s
;
105 if (0 != config_insert_values_global(srv
, config
->value
, cv
, i
== 0 ? T_CONFIG_SCOPE_SERVER
: T_CONFIG_SCOPE_CONNECTION
)) {
106 return HANDLER_ERROR
;
109 if (!array_is_vlist(s
->match
)) {
110 log_error_write(srv
, __FILE__
, __LINE__
, "s",
111 "unexpected value for skeleton.array; expected list of \"urlpath\"");
112 return HANDLER_ERROR
;
116 return HANDLER_GO_ON
;
121 static int mod_skeleton_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
122 plugin_config
*s
= p
->config_storage
[0];
126 /* skip the first, the global context */
127 for (size_t i
= 1; i
< srv
->config_context
->used
; ++i
) {
128 data_config
*dc
= (data_config
*)srv
->config_context
->data
[i
];
129 s
= p
->config_storage
[i
];
131 /* condition didn't match */
132 if (!config_check_cond(srv
, con
, dc
)) continue;
135 for (size_t j
= 0; j
< dc
->value
->used
; ++j
) {
136 data_unset
*du
= dc
->value
->data
[j
];
138 if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("skeleton.array"))) {
148 URIHANDLER_FUNC(mod_skeleton_uri_handler
) {
149 plugin_data
*p
= p_d
;
152 /* determine whether or not module participates in request */
154 if (con
->mode
!= DIRECT
) return HANDLER_GO_ON
;
155 if (buffer_string_is_empty(con
->uri
.path
)) return HANDLER_GO_ON
;
157 /* get module config for request */
158 mod_skeleton_patch_connection(srv
, con
, p
);
160 if (NULL
== array_match_value_suffix(p
->conf
.match
, con
->uri
.path
)) {
161 return HANDLER_GO_ON
;
164 /* module participates in request; business logic here */
166 con
->http_status
= 403; /* example: reject request with 403 Forbidden */
167 return HANDLER_FINISHED
;
170 /* this function is called at dlopen() time and inits the callbacks */
171 int mod_skeleton_plugin_init(plugin
*p
);
172 int mod_skeleton_plugin_init(plugin
*p
) {
173 p
->version
= LIGHTTPD_VERSION_ID
;
174 p
->name
= buffer_init_string("skeleton");
176 p
->init
= mod_skeleton_init
;
177 p
->cleanup
= mod_skeleton_free
;
178 p
->set_defaults
= mod_skeleton_set_defaults
;
180 p
->handle_uri_clean
= mod_skeleton_uri_handler
;