[mod_accesslog] %{ratio}n logs compression ratio (fixes #2133)
[lighttpd.git] / src / mod_indexfile.c
blob90829d4316d1dcda796489cc9a21f42687a79490
1 #include "first.h"
3 #include "base.h"
4 #include "log.h"
5 #include "buffer.h"
7 #include "plugin.h"
9 #include "stat_cache.h"
11 #include <ctype.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <errno.h>
16 /* plugin config for all request/connections */
18 typedef struct {
19 array *indexfiles;
20 } plugin_config;
22 typedef struct {
23 PLUGIN_DATA;
25 buffer *tmp_buf;
27 plugin_config **config_storage;
29 plugin_config conf;
30 } plugin_data;
32 /* init the plugin data */
33 INIT_FUNC(mod_indexfile_init) {
34 plugin_data *p;
36 p = calloc(1, sizeof(*p));
38 p->tmp_buf = buffer_init();
40 return p;
43 /* detroy the plugin data */
44 FREE_FUNC(mod_indexfile_free) {
45 plugin_data *p = p_d;
47 UNUSED(srv);
49 if (!p) return HANDLER_GO_ON;
51 if (p->config_storage) {
52 size_t i;
53 for (i = 0; i < srv->config_context->used; i++) {
54 plugin_config *s = p->config_storage[i];
56 if (NULL == s) continue;
58 array_free(s->indexfiles);
60 free(s);
62 free(p->config_storage);
65 buffer_free(p->tmp_buf);
67 free(p);
69 return HANDLER_GO_ON;
72 /* handle plugin config and check values */
74 SETDEFAULTS_FUNC(mod_indexfile_set_defaults) {
75 plugin_data *p = p_d;
76 size_t i = 0;
78 config_values_t cv[] = {
79 { "index-file.names", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
80 { "server.indexfiles", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
81 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
84 if (!p) return HANDLER_ERROR;
86 p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
88 for (i = 0; i < srv->config_context->used; i++) {
89 data_config const* config = (data_config const*)srv->config_context->data[i];
90 plugin_config *s;
92 s = calloc(1, sizeof(plugin_config));
93 s->indexfiles = array_init();
95 cv[0].destination = s->indexfiles;
96 cv[1].destination = s->indexfiles; /* old name for [0] */
98 p->config_storage[i] = s;
100 if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
101 return HANDLER_ERROR;
105 return HANDLER_GO_ON;
108 #define PATCH(x) \
109 p->conf.x = s->x;
110 static int mod_indexfile_patch_connection(server *srv, connection *con, plugin_data *p) {
111 size_t i, j;
112 plugin_config *s = p->config_storage[0];
114 PATCH(indexfiles);
116 /* skip the first, the global context */
117 for (i = 1; i < srv->config_context->used; i++) {
118 data_config *dc = (data_config *)srv->config_context->data[i];
119 s = p->config_storage[i];
121 /* condition didn't match */
122 if (!config_check_cond(srv, con, dc)) continue;
124 /* merge config */
125 for (j = 0; j < dc->value->used; j++) {
126 data_unset *du = dc->value->data[j];
128 if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.indexfiles"))) {
129 PATCH(indexfiles);
130 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("index-file.names"))) {
131 PATCH(indexfiles);
136 return 0;
138 #undef PATCH
140 URIHANDLER_FUNC(mod_indexfile_subrequest) {
141 plugin_data *p = p_d;
142 size_t k;
143 stat_cache_entry *sce = NULL;
145 if (con->mode != DIRECT) return HANDLER_GO_ON;
147 if (buffer_is_empty(con->uri.path)) return HANDLER_GO_ON;
148 if (con->uri.path->ptr[buffer_string_length(con->uri.path) - 1] != '/') return HANDLER_GO_ON;
150 mod_indexfile_patch_connection(srv, con, p);
152 if (con->conf.log_request_handling) {
153 log_error_write(srv, __FILE__, __LINE__, "s", "-- handling the request as Indexfile");
154 log_error_write(srv, __FILE__, __LINE__, "sb", "URI :", con->uri.path);
157 /* indexfile */
158 for (k = 0; k < p->conf.indexfiles->used; k++) {
159 data_string *ds = (data_string *)p->conf.indexfiles->data[k];
161 if (ds->value && ds->value->ptr[0] == '/') {
162 /* if the index-file starts with a prefix as use this file as
163 * index-generator */
164 buffer_copy_buffer(p->tmp_buf, con->physical.doc_root);
165 } else {
166 buffer_copy_buffer(p->tmp_buf, con->physical.path);
168 buffer_append_string_buffer(p->tmp_buf, ds->value);
170 if (HANDLER_ERROR == stat_cache_get_entry(srv, con, p->tmp_buf, &sce)) {
171 if (errno == EACCES) {
172 con->http_status = 403;
173 buffer_reset(con->physical.path);
175 return HANDLER_FINISHED;
178 if (errno != ENOENT &&
179 errno != ENOTDIR) {
180 /* we have no idea what happend. let's tell the user so. */
182 con->http_status = 500;
184 log_error_write(srv, __FILE__, __LINE__, "ssbsb",
185 "file not found ... or so: ", strerror(errno),
186 con->uri.path,
187 "->", con->physical.path);
189 buffer_reset(con->physical.path);
191 return HANDLER_FINISHED;
193 continue;
196 if (ds->value && ds->value->ptr[0] == '/') {
197 /* replace uri.path */
198 buffer_copy_buffer(con->uri.path, ds->value);
200 if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
201 ds = data_string_init();
203 buffer_copy_string_len(ds->key, CONST_STR_LEN("PATH_TRANSLATED_DIRINDEX"));
204 buffer_copy_buffer(ds->value, con->physical.path);
205 array_insert_unique(con->environment, (data_unset *)ds);
206 } else {
207 /* append to uri.path the relative path to index file (/ -> /index.php) */
208 buffer_append_string_buffer(con->uri.path, ds->value);
211 buffer_copy_buffer(con->physical.path, p->tmp_buf);
213 return HANDLER_GO_ON;
216 /* not found */
217 return HANDLER_GO_ON;
220 /* this function is called at dlopen() time and inits the callbacks */
222 int mod_indexfile_plugin_init(plugin *p);
223 int mod_indexfile_plugin_init(plugin *p) {
224 p->version = LIGHTTPD_VERSION_ID;
225 p->name = buffer_init_string("indexfile");
227 p->init = mod_indexfile_init;
228 p->handle_subrequest_start = mod_indexfile_subrequest;
229 p->set_defaults = mod_indexfile_set_defaults;
230 p->cleanup = mod_indexfile_free;
232 p->data = NULL;
234 return 0;