9 #include "stat_cache.h"
16 /* plugin config for all request/connections */
27 plugin_config
**config_storage
;
32 /* init the plugin data */
33 INIT_FUNC(mod_indexfile_init
) {
36 p
= calloc(1, sizeof(*p
));
38 p
->tmp_buf
= buffer_init();
43 /* detroy the plugin data */
44 FREE_FUNC(mod_indexfile_free
) {
49 if (!p
) return HANDLER_GO_ON
;
51 if (p
->config_storage
) {
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
);
62 free(p
->config_storage
);
65 buffer_free(p
->tmp_buf
);
72 /* handle plugin config and check values */
74 SETDEFAULTS_FUNC(mod_indexfile_set_defaults
) {
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
];
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
;
110 static int mod_indexfile_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
112 plugin_config
*s
= p
->config_storage
[0];
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;
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"))) {
130 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("index-file.names"))) {
140 URIHANDLER_FUNC(mod_indexfile_subrequest
) {
141 plugin_data
*p
= p_d
;
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
);
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
164 buffer_copy_buffer(p
->tmp_buf
, con
->physical
.doc_root
);
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
&&
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
),
187 "->", con
->physical
.path
);
189 buffer_reset(con
->physical
.path
);
191 return HANDLER_FINISHED
;
196 /* rewrite uri.path to the real path (/ -> /index.php) */
197 buffer_append_string_buffer(con
->uri
.path
, ds
->value
);
198 buffer_copy_buffer(con
->physical
.path
, p
->tmp_buf
);
200 /* fce is already set up a few lines above */
202 return HANDLER_GO_ON
;
206 return HANDLER_GO_ON
;
209 /* this function is called at dlopen() time and inits the callbacks */
211 int mod_indexfile_plugin_init(plugin
*p
);
212 int mod_indexfile_plugin_init(plugin
*p
) {
213 p
->version
= LIGHTTPD_VERSION_ID
;
214 p
->name
= buffer_init_string("indexfile");
216 p
->init
= mod_indexfile_init
;
217 p
->handle_subrequest_start
= mod_indexfile_subrequest
;
218 p
->set_defaults
= mod_indexfile_set_defaults
;
219 p
->cleanup
= mod_indexfile_free
;