[mod_evhost] fix an incorrect error trace
[lighttpd.git] / src / mod_evhost.c
blobeabee5ca1c6ae1f2c81de1edc8550d0cf0b254e2
1 #include "first.h"
3 #include "plugin.h"
4 #include "log.h"
5 #include "response.h"
6 #include "stat_cache.h"
8 #include <string.h>
9 #include <errno.h>
10 #include <ctype.h>
12 typedef struct {
13 /* unparsed pieces */
14 buffer *path_pieces_raw;
16 /* pieces for path creation */
17 size_t len;
18 buffer **path_pieces;
19 } plugin_config;
21 typedef struct {
22 PLUGIN_DATA;
23 buffer *tmp_buf;
25 plugin_config **config_storage;
26 plugin_config conf;
27 } plugin_data;
29 INIT_FUNC(mod_evhost_init) {
30 plugin_data *p;
32 p = calloc(1, sizeof(*p));
34 p->tmp_buf = buffer_init();
36 return p;
39 FREE_FUNC(mod_evhost_free) {
40 plugin_data *p = p_d;
42 UNUSED(srv);
44 if (!p) return HANDLER_GO_ON;
46 if (p->config_storage) {
47 size_t i;
48 for (i = 0; i < srv->config_context->used; i++) {
49 plugin_config *s = p->config_storage[i];
51 if (NULL == s) continue;
53 if(s->path_pieces) {
54 size_t j;
55 for (j = 0; j < s->len; j++) {
56 buffer_free(s->path_pieces[j]);
59 free(s->path_pieces);
62 buffer_free(s->path_pieces_raw);
64 free(s);
66 free(p->config_storage);
69 buffer_free(p->tmp_buf);
71 free(p);
73 return HANDLER_GO_ON;
76 static int mod_evhost_parse_pattern(plugin_config *s) {
77 char *ptr = s->path_pieces_raw->ptr,*pos;
79 s->path_pieces = NULL;
81 for(pos=ptr;*ptr;ptr++) {
82 if(*ptr == '%') {
83 size_t len;
84 s->path_pieces = realloc(s->path_pieces,(s->len+2) * sizeof(*s->path_pieces));
85 s->path_pieces[s->len] = buffer_init();
86 s->path_pieces[s->len+1] = buffer_init();
88 /* "%%" "%_" "%x" "%{x.y}" where x and y are *single digit* 0 - 9 */
89 if (ptr[1] == '%' || ptr[1] == '_' || light_isdigit(ptr[1])) {
90 len = 2;
91 } else if (ptr[1] == '{') {
92 if (!light_isdigit(ptr[2])) return -1;
93 if (ptr[3] == '.') {
94 if (!light_isdigit(ptr[4])) return -1;
95 if (ptr[5] != '}') return -1;
96 len = 6;
97 } else if (ptr[3] == '}') {
98 len = 4;
99 } else {
100 return -1;
102 } else {
103 return -1;
106 buffer_copy_string_len(s->path_pieces[s->len],pos,ptr-pos);
107 pos = ptr + len;
109 buffer_copy_string_len(s->path_pieces[s->len+1],ptr,len);
110 ptr += len - 1; /*(ptr++ in for() loop)*/
112 s->len += 2;
116 if(*pos != '\0') {
117 s->path_pieces = realloc(s->path_pieces,(s->len+1) * sizeof(*s->path_pieces));
118 s->path_pieces[s->len] = buffer_init();
120 buffer_copy_string_len(s->path_pieces[s->len],pos,ptr-pos);
122 s->len += 1;
125 return 0;
128 SETDEFAULTS_FUNC(mod_evhost_set_defaults) {
129 plugin_data *p = p_d;
130 size_t i;
135 * # define a pattern for the host url finding
136 * # %% => % sign
137 * # %0 => domain name + tld
138 * # %1 => tld
139 * # %2 => domain name without tld
140 * # %3 => subdomain 1 name
141 * # %4 => subdomain 2 name
142 * # %_ => fqdn (without port info)
144 * evhost.path-pattern = "/home/ckruse/dev/www/%3/htdocs/"
148 config_values_t cv[] = {
149 { "evhost.path-pattern", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION },
150 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
153 if (!p) return HANDLER_ERROR;
155 p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
157 for (i = 0; i < srv->config_context->used; i++) {
158 data_config const* config = (data_config const*)srv->config_context->data[i];
159 plugin_config *s;
161 s = calloc(1, sizeof(plugin_config));
162 s->path_pieces_raw = buffer_init();
163 s->path_pieces = NULL;
164 s->len = 0;
166 cv[0].destination = s->path_pieces_raw;
168 p->config_storage[i] = s;
170 if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
171 return HANDLER_ERROR;
174 if (!buffer_string_is_empty(s->path_pieces_raw)) {
175 if (0 != mod_evhost_parse_pattern(s)) {
176 log_error_write(srv, __FILE__, __LINE__, "sb", "invalid evhost.path-pattern:", s->path_pieces_raw);
177 return HANDLER_ERROR;
182 return HANDLER_GO_ON;
186 * assign the different parts of the domain to array-indezes (sub2.sub1.domain.tld)
187 * - %0 - domain.tld
188 * - %1 - tld
189 * - %2 - domain
190 * - %3 - sub1
191 * - ...
194 static int mod_evhost_parse_host(connection *con,array *host) {
195 register char *ptr = con->uri.authority->ptr + buffer_string_length(con->uri.authority);
196 char *colon = ptr; /* needed to filter out the colon (if exists) */
197 int first = 1;
198 data_string *ds;
199 int i;
201 /* first, find the domain + tld */
202 for(;ptr > con->uri.authority->ptr;ptr--) {
203 if(*ptr == '.') {
204 if(first) first = 0;
205 else break;
206 } else if(*ptr == ':') {
207 colon = ptr;
208 first = 1;
212 ds = data_string_init();
213 buffer_copy_string_len(ds->key,CONST_STR_LEN("%0"));
215 /* if we stopped at a dot, skip the dot */
216 if (*ptr == '.') ptr++;
217 buffer_copy_string_len(ds->value, ptr, colon-ptr);
219 array_insert_unique(host,(data_unset *)ds);
221 /* if the : is not the start of the authority, go on parsing the hostname */
223 if (colon != con->uri.authority->ptr) {
224 for(ptr = colon - 1, i = 1; ptr > con->uri.authority->ptr; ptr--) {
225 if(*ptr == '.') {
226 if (ptr != colon - 1) {
227 /* is something between the dots */
228 ds = data_string_init();
229 buffer_copy_string_len(ds->key,CONST_STR_LEN("%"));
230 buffer_append_int(ds->key, i++);
231 buffer_copy_string_len(ds->value,ptr+1,colon-ptr-1);
233 array_insert_unique(host,(data_unset *)ds);
235 colon = ptr;
239 /* if the . is not the first charactor of the hostname */
240 if (colon != ptr) {
241 ds = data_string_init();
242 buffer_copy_string_len(ds->key,CONST_STR_LEN("%"));
243 buffer_append_int(ds->key, i /* ++ */);
244 buffer_copy_string_len(ds->value,ptr,colon-ptr);
246 array_insert_unique(host,(data_unset *)ds);
250 return 0;
253 #define PATCH(x) \
254 p->conf.x = s->x;
255 static int mod_evhost_patch_connection(server *srv, connection *con, plugin_data *p) {
256 size_t i, j;
257 plugin_config *s = p->config_storage[0];
259 PATCH(path_pieces);
260 PATCH(len);
262 /* skip the first, the global context */
263 for (i = 1; i < srv->config_context->used; i++) {
264 data_config *dc = (data_config *)srv->config_context->data[i];
265 s = p->config_storage[i];
267 /* condition didn't match */
268 if (!config_check_cond(srv, con, dc)) continue;
270 /* merge config */
271 for (j = 0; j < dc->value->used; j++) {
272 data_unset *du = dc->value->data[j];
274 if (buffer_is_equal_string(du->key, CONST_STR_LEN("evhost.path-pattern"))) {
275 PATCH(path_pieces);
276 PATCH(len);
281 return 0;
283 #undef PATCH
286 static handler_t mod_evhost_uri_handler(server *srv, connection *con, void *p_d) {
287 plugin_data *p = p_d;
288 size_t i;
289 array *parsed_host;
290 register char *ptr;
291 int not_good = 0;
292 stat_cache_entry *sce = NULL;
294 /* not authority set */
295 if (buffer_string_is_empty(con->uri.authority)) return HANDLER_GO_ON;
297 mod_evhost_patch_connection(srv, con, p);
299 /* missing even default(global) conf */
300 if (0 == p->conf.len) {
301 return HANDLER_GO_ON;
304 parsed_host = array_init();
306 mod_evhost_parse_host(con, parsed_host);
308 /* build document-root */
309 buffer_reset(p->tmp_buf);
311 for (i = 0; i < p->conf.len; i++) {
312 ptr = p->conf.path_pieces[i]->ptr;
313 if (*ptr == '%') {
314 data_string *ds;
316 if (*(ptr+1) == '%') {
317 /* %% */
318 buffer_append_string_len(p->tmp_buf,CONST_STR_LEN("%"));
319 } else if (*(ptr+1) == '_' ) {
320 /* %_ == full hostname */
321 char *colon = strchr(con->uri.authority->ptr, ':');
323 if(colon == NULL) {
324 buffer_append_string_buffer(p->tmp_buf, con->uri.authority); /* adds fqdn */
325 } else {
326 /* strip the port out of the authority-part of the URI scheme */
327 buffer_append_string_len(p->tmp_buf, con->uri.authority->ptr, colon - con->uri.authority->ptr); /* adds fqdn */
329 } else if (ptr[1] == '{' ) {
330 char s[3] = "% ";
331 s[1] = ptr[2]; /*(assumes single digit before '.', and, optionally, '.' and single digit after '.')*/
332 if (NULL != (ds = (data_string *)array_get_element(parsed_host, s))) {
333 if (ptr[3] != '.' || ptr[4] == '0') {
334 buffer_append_string_buffer(p->tmp_buf, ds->value);
335 } else {
336 if ((size_t)(ptr[4]-'0') <= buffer_string_length(ds->value)) {
337 buffer_append_string_len(p->tmp_buf, ds->value->ptr+(ptr[4]-'0')-1, 1);
340 } else {
341 /* unhandled %-sequence */
343 } else if (NULL != (ds = (data_string *)array_get_element(parsed_host,p->conf.path_pieces[i]->ptr))) {
344 buffer_append_string_buffer(p->tmp_buf,ds->value);
345 } else {
346 /* unhandled %-sequence */
348 } else {
349 buffer_append_string_buffer(p->tmp_buf,p->conf.path_pieces[i]);
353 buffer_append_slash(p->tmp_buf);
355 array_free(parsed_host);
357 if (HANDLER_ERROR == stat_cache_get_entry(srv, con, p->tmp_buf, &sce)) {
358 log_error_write(srv, __FILE__, __LINE__, "sb", strerror(errno), p->tmp_buf);
359 not_good = 1;
360 } else if(!S_ISDIR(sce->st.st_mode)) {
361 log_error_write(srv, __FILE__, __LINE__, "sb", "not a directory:", p->tmp_buf);
362 not_good = 1;
365 if (!not_good) {
366 buffer_copy_buffer(con->physical.doc_root, p->tmp_buf);
369 return HANDLER_GO_ON;
372 int mod_evhost_plugin_init(plugin *p);
373 int mod_evhost_plugin_init(plugin *p) {
374 p->version = LIGHTTPD_VERSION_ID;
375 p->name = buffer_init_string("evhost");
376 p->init = mod_evhost_init;
377 p->set_defaults = mod_evhost_set_defaults;
378 p->handle_docroot = mod_evhost_uri_handler;
379 p->cleanup = mod_evhost_free;
381 p->data = NULL;
383 return 0;
386 /* eof */