- next is 1.4.56
[lighttpd.git] / src / mod_evhost.c
blob311c2cc02e6025a0f78c73c0ea013db574d70956
1 #include "first.h"
3 #include "base.h"
4 #include "plugin.h"
5 #include "log.h"
6 #include "stat_cache.h"
8 #include <stdlib.h>
9 #include <string.h>
10 #include <errno.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(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 void mod_evhost_parse_host(buffer *key, array *host, buffer *authority) {
195 char *ptr = authority->ptr + buffer_string_length(authority);
196 char *colon = ptr; /* needed to filter out the colon (if exists) */
197 int first = 1;
198 int i;
200 /*if (ptr == authority->ptr) return;*//*(no authority checked earlier)*/
202 if (*authority->ptr == '[') { /* authority is IPv6 literal address */
203 colon = ptr;
204 if (ptr[-1] != ']') {
205 do { --ptr; } while (ptr > authority->ptr && ptr[-1] != ']');
206 if (*ptr != ':') return; /*(should not happen for valid authority)*/
207 colon = ptr;
209 ptr = authority->ptr;
210 array_insert_key_value(host,CONST_STR_LEN("%0"),ptr,colon-ptr);
211 return;
214 /* first, find the domain + tld */
215 for(; ptr > authority->ptr; --ptr) {
216 if(*ptr == '.') {
217 if(first) first = 0;
218 else break;
219 } else if(*ptr == ':') {
220 colon = ptr;
221 first = 1;
225 /* if we stopped at a dot, skip the dot */
226 if (*ptr == '.') ptr++;
227 array_insert_key_value(host, CONST_STR_LEN("%0"), ptr, colon-ptr);
229 /* if the : is not the start of the authority, go on parsing the hostname */
231 if (colon != authority->ptr) {
232 for(ptr = colon - 1, i = 1; ptr > authority->ptr; --ptr) {
233 if(*ptr == '.') {
234 if (ptr != colon - 1) {
235 /* is something between the dots */
236 buffer_copy_string_len(key, CONST_STR_LEN("%"));
237 buffer_append_int(key, i++);
238 array_insert_key_value(host, CONST_BUF_LEN(key), ptr+1, colon-ptr-1);
240 colon = ptr;
244 /* if the . is not the first charactor of the hostname */
245 if (colon != ptr) {
246 buffer_copy_string_len(key, CONST_STR_LEN("%"));
247 buffer_append_int(key, i /* ++ */);
248 array_insert_key_value(host, CONST_BUF_LEN(key), ptr, colon-ptr);
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 void mod_evhost_build_doc_root_path(buffer *b, array *parsed_host, buffer *authority, buffer **path_pieces, const size_t npieces) {
287 array_reset_data_strings(parsed_host);
288 mod_evhost_parse_host(b, parsed_host, authority);
289 buffer_clear(b);
291 for (size_t i = 0; i < npieces; ++i) {
292 const char *ptr = path_pieces[i]->ptr;
293 if (*ptr == '%') {
294 data_string *ds;
296 if (*(ptr+1) == '%') {
297 /* %% */
298 buffer_append_string_len(b, CONST_STR_LEN("%"));
299 } else if (*(ptr+1) == '_' ) {
300 /* %_ == full hostname */
301 char *colon = strchr(authority->ptr, ':');
303 if(colon == NULL) {
304 buffer_append_string_buffer(b, authority); /* adds fqdn */
305 } else {
306 /* strip the port out of the authority-part of the URI scheme */
307 buffer_append_string_len(b, authority->ptr, colon - authority->ptr); /* adds fqdn */
309 } else if (ptr[1] == '{' ) {
310 char s[3] = "% ";
311 s[1] = ptr[2]; /*(assumes single digit before '.', and, optionally, '.' and single digit after '.')*/
312 if (NULL != (ds = (data_string *)array_get_element_klen(parsed_host, s, 2))) {
313 if (ptr[3] != '.' || ptr[4] == '0') {
314 buffer_append_string_buffer(b, ds->value);
315 } else {
316 if ((size_t)(ptr[4]-'0') <= buffer_string_length(ds->value)) {
317 buffer_append_string_len(b, ds->value->ptr+(ptr[4]-'0')-1, 1);
320 } else {
321 /* unhandled %-sequence */
323 } else if (NULL != (ds = (data_string *)array_get_element_klen(parsed_host, CONST_BUF_LEN(path_pieces[i])))) {
324 buffer_append_string_buffer(b, ds->value);
325 } else {
326 /* unhandled %-sequence */
328 } else {
329 buffer_append_string_buffer(b, path_pieces[i]);
333 buffer_append_slash(b);
336 static handler_t mod_evhost_uri_handler(server *srv, connection *con, void *p_d) {
337 plugin_data *p = p_d;
338 stat_cache_entry *sce = NULL;
340 /* not authority set */
341 if (buffer_string_is_empty(con->uri.authority)) return HANDLER_GO_ON;
343 mod_evhost_patch_connection(srv, con, p);
345 /* missing even default(global) conf */
346 if (0 == p->conf.len) {
347 return HANDLER_GO_ON;
350 mod_evhost_build_doc_root_path(p->tmp_buf, srv->split_vals, con->uri.authority, p->conf.path_pieces, p->conf.len);
352 if (HANDLER_ERROR == stat_cache_get_entry(srv, con, p->tmp_buf, &sce)) {
353 log_error_write(srv, __FILE__, __LINE__, "sb", strerror(errno), p->tmp_buf);
354 } else if(!S_ISDIR(sce->st.st_mode)) {
355 log_error_write(srv, __FILE__, __LINE__, "sb", "not a directory:", p->tmp_buf);
356 } else {
357 buffer_copy_buffer(con->physical.doc_root, p->tmp_buf);
360 return HANDLER_GO_ON;
363 int mod_evhost_plugin_init(plugin *p);
364 int mod_evhost_plugin_init(plugin *p) {
365 p->version = LIGHTTPD_VERSION_ID;
366 p->name = buffer_init_string("evhost");
367 p->init = mod_evhost_init;
368 p->set_defaults = mod_evhost_set_defaults;
369 p->handle_docroot = mod_evhost_uri_handler;
370 p->cleanup = mod_evhost_free;
372 p->data = NULL;
374 return 0;
377 /* eof */