[core] avoid spurious trace and error abort
[lighttpd.git] / src / mod_evhost.c
blobda3284de423137f9ee2e82f1766ed592fa5852a3
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 void 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 s->path_pieces = realloc(s->path_pieces,(s->len+2) * sizeof(*s->path_pieces));
84 s->path_pieces[s->len] = buffer_init();
85 s->path_pieces[s->len+1] = buffer_init();
87 buffer_copy_string_len(s->path_pieces[s->len],pos,ptr-pos);
88 pos = ptr + 2;
90 buffer_copy_string_len(s->path_pieces[s->len+1],ptr++,2);
92 s->len += 2;
96 if(*pos != '\0') {
97 s->path_pieces = realloc(s->path_pieces,(s->len+1) * sizeof(*s->path_pieces));
98 s->path_pieces[s->len] = buffer_init();
100 buffer_copy_string_len(s->path_pieces[s->len],pos,ptr-pos);
102 s->len += 1;
106 SETDEFAULTS_FUNC(mod_evhost_set_defaults) {
107 plugin_data *p = p_d;
108 size_t i;
113 * # define a pattern for the host url finding
114 * # %% => % sign
115 * # %0 => domain name + tld
116 * # %1 => tld
117 * # %2 => domain name without tld
118 * # %3 => subdomain 1 name
119 * # %4 => subdomain 2 name
120 * # %_ => fqdn (without port info)
122 * evhost.path-pattern = "/home/ckruse/dev/www/%3/htdocs/"
126 config_values_t cv[] = {
127 { "evhost.path-pattern", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION },
128 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
131 if (!p) return HANDLER_ERROR;
133 p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
135 for (i = 0; i < srv->config_context->used; i++) {
136 data_config const* config = (data_config const*)srv->config_context->data[i];
137 plugin_config *s;
139 s = calloc(1, sizeof(plugin_config));
140 s->path_pieces_raw = buffer_init();
141 s->path_pieces = NULL;
142 s->len = 0;
144 cv[0].destination = s->path_pieces_raw;
146 p->config_storage[i] = s;
148 if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
149 return HANDLER_ERROR;
152 if (!buffer_string_is_empty(s->path_pieces_raw)) {
153 mod_evhost_parse_pattern(s);
157 return HANDLER_GO_ON;
161 * assign the different parts of the domain to array-indezes (sub2.sub1.domain.tld)
162 * - %0 - domain.tld
163 * - %1 - tld
164 * - %2 - domain
165 * - %3 - sub1
166 * - ...
169 static int mod_evhost_parse_host(connection *con,array *host) {
170 register char *ptr = con->uri.authority->ptr + buffer_string_length(con->uri.authority);
171 char *colon = ptr; /* needed to filter out the colon (if exists) */
172 int first = 1;
173 data_string *ds;
174 int i;
176 /* first, find the domain + tld */
177 for(;ptr > con->uri.authority->ptr;ptr--) {
178 if(*ptr == '.') {
179 if(first) first = 0;
180 else break;
181 } else if(*ptr == ':') {
182 colon = ptr;
183 first = 1;
187 ds = data_string_init();
188 buffer_copy_string_len(ds->key,CONST_STR_LEN("%0"));
190 /* if we stopped at a dot, skip the dot */
191 if (*ptr == '.') ptr++;
192 buffer_copy_string_len(ds->value, ptr, colon-ptr);
194 array_insert_unique(host,(data_unset *)ds);
196 /* if the : is not the start of the authority, go on parsing the hostname */
198 if (colon != con->uri.authority->ptr) {
199 for(ptr = colon - 1, i = 1; ptr > con->uri.authority->ptr; ptr--) {
200 if(*ptr == '.') {
201 if (ptr != colon - 1) {
202 /* is something between the dots */
203 ds = data_string_init();
204 buffer_copy_string_len(ds->key,CONST_STR_LEN("%"));
205 buffer_append_int(ds->key, i++);
206 buffer_copy_string_len(ds->value,ptr+1,colon-ptr-1);
208 array_insert_unique(host,(data_unset *)ds);
210 colon = ptr;
214 /* if the . is not the first charactor of the hostname */
215 if (colon != ptr) {
216 ds = data_string_init();
217 buffer_copy_string_len(ds->key,CONST_STR_LEN("%"));
218 buffer_append_int(ds->key, i /* ++ */);
219 buffer_copy_string_len(ds->value,ptr,colon-ptr);
221 array_insert_unique(host,(data_unset *)ds);
225 return 0;
228 #define PATCH(x) \
229 p->conf.x = s->x;
230 static int mod_evhost_patch_connection(server *srv, connection *con, plugin_data *p) {
231 size_t i, j;
232 plugin_config *s = p->config_storage[0];
234 PATCH(path_pieces);
235 PATCH(len);
237 /* skip the first, the global context */
238 for (i = 1; i < srv->config_context->used; i++) {
239 data_config *dc = (data_config *)srv->config_context->data[i];
240 s = p->config_storage[i];
242 /* condition didn't match */
243 if (!config_check_cond(srv, con, dc)) continue;
245 /* merge config */
246 for (j = 0; j < dc->value->used; j++) {
247 data_unset *du = dc->value->data[j];
249 if (buffer_is_equal_string(du->key, CONST_STR_LEN("evhost.path-pattern"))) {
250 PATCH(path_pieces);
251 PATCH(len);
256 return 0;
258 #undef PATCH
261 static handler_t mod_evhost_uri_handler(server *srv, connection *con, void *p_d) {
262 plugin_data *p = p_d;
263 size_t i;
264 array *parsed_host;
265 register char *ptr;
266 int not_good = 0;
267 stat_cache_entry *sce = NULL;
269 /* not authority set */
270 if (buffer_string_is_empty(con->uri.authority)) return HANDLER_GO_ON;
272 mod_evhost_patch_connection(srv, con, p);
274 /* missing even default(global) conf */
275 if (0 == p->conf.len) {
276 return HANDLER_GO_ON;
279 parsed_host = array_init();
281 mod_evhost_parse_host(con, parsed_host);
283 /* build document-root */
284 buffer_reset(p->tmp_buf);
286 for (i = 0; i < p->conf.len; i++) {
287 ptr = p->conf.path_pieces[i]->ptr;
288 if (*ptr == '%') {
289 data_string *ds;
291 if (*(ptr+1) == '%') {
292 /* %% */
293 buffer_append_string_len(p->tmp_buf,CONST_STR_LEN("%"));
294 } else if (*(ptr+1) == '_' ) {
295 /* %_ == full hostname */
296 char *colon = strchr(con->uri.authority->ptr, ':');
298 if(colon == NULL) {
299 buffer_append_string_buffer(p->tmp_buf, con->uri.authority); /* adds fqdn */
300 } else {
301 /* strip the port out of the authority-part of the URI scheme */
302 buffer_append_string_len(p->tmp_buf, con->uri.authority->ptr, colon - con->uri.authority->ptr); /* adds fqdn */
304 } else if (NULL != (ds = (data_string *)array_get_element(parsed_host,p->conf.path_pieces[i]->ptr))) {
305 buffer_append_string_buffer(p->tmp_buf,ds->value);
306 } else {
307 /* unhandled %-sequence */
309 } else {
310 buffer_append_string_buffer(p->tmp_buf,p->conf.path_pieces[i]);
314 buffer_append_slash(p->tmp_buf);
316 array_free(parsed_host);
318 if (HANDLER_ERROR == stat_cache_get_entry(srv, con, p->tmp_buf, &sce)) {
319 log_error_write(srv, __FILE__, __LINE__, "sb", strerror(errno), p->tmp_buf);
320 not_good = 1;
321 } else if(!S_ISDIR(sce->st.st_mode)) {
322 log_error_write(srv, __FILE__, __LINE__, "sb", "not a directory:", p->tmp_buf);
323 not_good = 1;
326 if (!not_good) {
327 buffer_copy_buffer(con->physical.doc_root, p->tmp_buf);
330 return HANDLER_GO_ON;
333 int mod_evhost_plugin_init(plugin *p);
334 int mod_evhost_plugin_init(plugin *p) {
335 p->version = LIGHTTPD_VERSION_ID;
336 p->name = buffer_init_string("evhost");
337 p->init = mod_evhost_init;
338 p->set_defaults = mod_evhost_set_defaults;
339 p->handle_docroot = mod_evhost_uri_handler;
340 p->cleanup = mod_evhost_free;
342 p->data = NULL;
344 return 0;
347 /* eof */