[tests] skip mod-secdownload HMAC-SHA1,HMAC-SHA256
[lighttpd.git] / src / mod_indexfile.c
bloba06a4bfd4513ce895675286273e5587b2685d123
1 #include "first.h"
3 #include "base.h"
4 #include "log.h"
5 #include "buffer.h"
6 #include "http_header.h"
8 #include "plugin.h"
10 #include "stat_cache.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(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;
104 if (!array_is_vlist(s->indexfiles)) {
105 log_error_write(srv, __FILE__, __LINE__, "s",
106 "unexpected value for index-file.names; expected list of \"file\"");
107 return HANDLER_ERROR;
111 return HANDLER_GO_ON;
114 #define PATCH(x) \
115 p->conf.x = s->x;
116 static int mod_indexfile_patch_connection(server *srv, connection *con, plugin_data *p) {
117 size_t i, j;
118 plugin_config *s = p->config_storage[0];
120 PATCH(indexfiles);
122 /* skip the first, the global context */
123 for (i = 1; i < srv->config_context->used; i++) {
124 data_config *dc = (data_config *)srv->config_context->data[i];
125 s = p->config_storage[i];
127 /* condition didn't match */
128 if (!config_check_cond(srv, con, dc)) continue;
130 /* merge config */
131 for (j = 0; j < dc->value->used; j++) {
132 data_unset *du = dc->value->data[j];
134 if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.indexfiles"))) {
135 PATCH(indexfiles);
136 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("index-file.names"))) {
137 PATCH(indexfiles);
142 return 0;
144 #undef PATCH
146 URIHANDLER_FUNC(mod_indexfile_subrequest) {
147 plugin_data *p = p_d;
148 size_t k;
149 stat_cache_entry *sce = NULL;
151 if (con->mode != DIRECT) return HANDLER_GO_ON;
153 if (buffer_is_empty(con->uri.path)) return HANDLER_GO_ON;
154 if (con->uri.path->ptr[buffer_string_length(con->uri.path) - 1] != '/') return HANDLER_GO_ON;
156 mod_indexfile_patch_connection(srv, con, p);
158 if (con->conf.log_request_handling) {
159 log_error_write(srv, __FILE__, __LINE__, "s", "-- handling the request as Indexfile");
160 log_error_write(srv, __FILE__, __LINE__, "sb", "URI :", con->uri.path);
163 /* indexfile */
164 for (k = 0; k < p->conf.indexfiles->used; k++) {
165 data_string *ds = (data_string *)p->conf.indexfiles->data[k];
167 if (ds->value && ds->value->ptr[0] == '/') {
168 /* if the index-file starts with a prefix as use this file as
169 * index-generator */
170 buffer_copy_buffer(p->tmp_buf, con->physical.doc_root);
171 } else {
172 buffer_copy_buffer(p->tmp_buf, con->physical.path);
174 buffer_append_string_buffer(p->tmp_buf, ds->value);
176 if (HANDLER_ERROR == stat_cache_get_entry(srv, con, p->tmp_buf, &sce)) {
177 if (errno == EACCES) {
178 con->http_status = 403;
179 buffer_reset(con->physical.path);
181 return HANDLER_FINISHED;
184 if (errno != ENOENT &&
185 errno != ENOTDIR) {
186 /* we have no idea what happend. let's tell the user so. */
188 con->http_status = 500;
190 log_error_write(srv, __FILE__, __LINE__, "ssbsb",
191 "file not found ... or so: ", strerror(errno),
192 con->uri.path,
193 "->", con->physical.path);
195 buffer_reset(con->physical.path);
197 return HANDLER_FINISHED;
199 continue;
202 if (ds->value && ds->value->ptr[0] == '/') {
203 /* replace uri.path */
204 buffer_copy_buffer(con->uri.path, ds->value);
205 http_header_env_set(con, CONST_STR_LEN("PATH_TRANSLATED_DIRINDEX"), CONST_BUF_LEN(con->physical.path));
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;