13
#include "stat_cache.h"
15
/* plugin config for all request/connections */
26
plugin_config **config_storage;
31
/* init the plugin data */
32
INIT_FUNC(mod_indexfile_init) {
35
p = calloc(1, sizeof(*p));
37
p->tmp_buf = buffer_init();
42
/* detroy the plugin data */
43
FREE_FUNC(mod_indexfile_free) {
48
if (!p) return HANDLER_GO_ON;
50
if (p->config_storage) {
52
for (i = 0; i < srv->config_context->used; i++) {
53
plugin_config *s = p->config_storage[i];
57
array_free(s->indexfiles);
61
free(p->config_storage);
64
buffer_free(p->tmp_buf);
71
/* handle plugin config and check values */
73
SETDEFAULTS_FUNC(mod_indexfile_set_defaults) {
77
config_values_t cv[] = {
78
{ "index-file.names", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
79
{ "server.indexfiles", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
80
{ NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
83
if (!p) return HANDLER_ERROR;
85
p->config_storage = calloc(1, srv->config_context->used * sizeof(specific_config *));
87
for (i = 0; i < srv->config_context->used; i++) {
90
s = calloc(1, sizeof(plugin_config));
91
s->indexfiles = array_init();
93
cv[0].destination = s->indexfiles;
94
cv[1].destination = s->indexfiles; /* old name for [0] */
96
p->config_storage[i] = s;
98
if (0 != config_insert_values_global(srv, ((data_config *)srv->config_context->data[i])->value, cv)) {
103
return HANDLER_GO_ON;
108
static int mod_indexfile_patch_connection(server *srv, connection *con, plugin_data *p) {
110
plugin_config *s = p->config_storage[0];
114
/* skip the first, the global context */
115
for (i = 1; i < srv->config_context->used; i++) {
116
data_config *dc = (data_config *)srv->config_context->data[i];
117
s = p->config_storage[i];
119
/* condition didn't match */
120
if (!config_check_cond(srv, con, dc)) continue;
123
for (j = 0; j < dc->value->used; j++) {
124
data_unset *du = dc->value->data[j];
126
if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.indexfiles"))) {
128
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("index-file.names"))) {
138
URIHANDLER_FUNC(mod_indexfile_subrequest) {
139
plugin_data *p = p_d;
141
stat_cache_entry *sce = NULL;
143
if (con->uri.path->used == 0) return HANDLER_GO_ON;
144
if (con->uri.path->ptr[con->uri.path->used - 2] != '/') return HANDLER_GO_ON;
146
mod_indexfile_patch_connection(srv, con, p);
148
if (con->conf.log_request_handling) {
149
log_error_write(srv, __FILE__, __LINE__, "s", "-- handling the request as Indexfile");
150
log_error_write(srv, __FILE__, __LINE__, "sb", "URI :", con->uri.path);
154
for (k = 0; k < p->conf.indexfiles->used; k++) {
155
data_string *ds = (data_string *)p->conf.indexfiles->data[k];
157
if (ds->value && ds->value->ptr[0] == '/') {
158
/* if the index-file starts with a prefix as use this file as
160
buffer_copy_string_buffer(p->tmp_buf, con->physical.doc_root);
162
buffer_copy_string_buffer(p->tmp_buf, con->physical.path);
164
buffer_append_string_buffer(p->tmp_buf, ds->value);
166
if (HANDLER_ERROR == stat_cache_get_entry(srv, con, p->tmp_buf, &sce)) {
167
if (errno == EACCES) {
168
con->http_status = 403;
169
buffer_reset(con->physical.path);
171
return HANDLER_FINISHED;
174
if (errno != ENOENT &&
176
/* we have no idea what happend. let's tell the user so. */
178
con->http_status = 500;
180
log_error_write(srv, __FILE__, __LINE__, "ssbsb",
181
"file not found ... or so: ", strerror(errno),
183
"->", con->physical.path);
185
buffer_reset(con->physical.path);
187
return HANDLER_FINISHED;
192
/* rewrite uri.path to the real path (/ -> /index.php) */
193
buffer_append_string_buffer(con->uri.path, ds->value);
194
buffer_copy_string_buffer(con->physical.path, p->tmp_buf);
196
/* fce is already set up a few lines above */
198
return HANDLER_GO_ON;
202
return HANDLER_GO_ON;
205
/* this function is called at dlopen() time and inits the callbacks */
207
int mod_indexfile_plugin_init(plugin *p) {
208
p->version = LIGHTTPD_VERSION_ID;
209
p->name = buffer_init_string("indexfile");
211
p->init = mod_indexfile_init;
212
p->handle_subrequest_start = mod_indexfile_subrequest;
213
p->set_defaults = mod_indexfile_set_defaults;
214
p->cleanup = mod_indexfile_free;