[autobuild] allow sendfile() in cross-compile (fixes #2836)
[lighttpd.git] / src / mod_evhost.c
blobe669b3cce2bd6ca37442e46c74551c7f3015a7e5
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>
11 typedef struct {
12 /* unparsed pieces */
13 buffer *path_pieces_raw;
15 /* pieces for path creation */
16 size_t len;
17 buffer **path_pieces;
18 } plugin_config;
20 typedef struct {
21 PLUGIN_DATA;
22 buffer *tmp_buf;
24 plugin_config **config_storage;
25 plugin_config conf;
26 } plugin_data;
28 INIT_FUNC(mod_evhost_init) {
29 plugin_data *p;
31 p = calloc(1, sizeof(*p));
33 p->tmp_buf = buffer_init();
35 return p;
38 FREE_FUNC(mod_evhost_free) {
39 plugin_data *p = p_d;
41 UNUSED(srv);
43 if (!p) return HANDLER_GO_ON;
45 if (p->config_storage) {
46 size_t i;
47 for (i = 0; i < srv->config_context->used; i++) {
48 plugin_config *s = p->config_storage[i];
50 if (NULL == s) continue;
52 if(s->path_pieces) {
53 size_t j;
54 for (j = 0; j < s->len; j++) {
55 buffer_free(s->path_pieces[j]);
58 free(s->path_pieces);
61 buffer_free(s->path_pieces_raw);
63 free(s);
65 free(p->config_storage);
68 buffer_free(p->tmp_buf);
70 free(p);
72 return HANDLER_GO_ON;
75 static int mod_evhost_parse_pattern(plugin_config *s) {
76 char *ptr = s->path_pieces_raw->ptr,*pos;
78 s->path_pieces = NULL;
80 for(pos=ptr;*ptr;ptr++) {
81 if(*ptr == '%') {
82 size_t len;
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 /* "%%" "%_" "%x" "%{x.y}" where x and y are *single digit* 0 - 9 */
88 if (ptr[1] == '%' || ptr[1] == '_' || light_isdigit(ptr[1])) {
89 len = 2;
90 } else if (ptr[1] == '{') {
91 if (!light_isdigit(ptr[2])) return -1;
92 if (ptr[3] == '.') {
93 if (!light_isdigit(ptr[4])) return -1;
94 if (ptr[5] != '}') return -1;
95 len = 6;
96 } else if (ptr[3] == '}') {
97 len = 4;
98 } else {
99 return -1;
101 } else {
102 return -1;
105 buffer_copy_string_len(s->path_pieces[s->len],pos,ptr-pos);
106 pos = ptr + len;
108 buffer_copy_string_len(s->path_pieces[s->len+1],ptr,len);
109 ptr += len - 1; /*(ptr++ in for() loop)*/
111 s->len += 2;
115 if(*pos != '\0') {
116 s->path_pieces = realloc(s->path_pieces,(s->len+1) * sizeof(*s->path_pieces));
117 s->path_pieces[s->len] = buffer_init();
119 buffer_copy_string_len(s->path_pieces[s->len],pos,ptr-pos);
121 s->len += 1;
124 return 0;
127 SETDEFAULTS_FUNC(mod_evhost_set_defaults) {
128 plugin_data *p = p_d;
129 size_t i;
134 * # define a pattern for the host url finding
135 * # %% => % sign
136 * # %0 => domain name + tld
137 * # %1 => tld
138 * # %2 => domain name without tld
139 * # %3 => subdomain 1 name
140 * # %4 => subdomain 2 name
141 * # %_ => fqdn (without port info)
143 * evhost.path-pattern = "/home/ckruse/dev/www/%3/htdocs/"
147 config_values_t cv[] = {
148 { "evhost.path-pattern", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION },
149 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
152 if (!p) return HANDLER_ERROR;
154 p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
156 for (i = 0; i < srv->config_context->used; i++) {
157 data_config const* config = (data_config const*)srv->config_context->data[i];
158 plugin_config *s;
160 s = calloc(1, sizeof(plugin_config));
161 s->path_pieces_raw = buffer_init();
162 s->path_pieces = NULL;
163 s->len = 0;
165 cv[0].destination = s->path_pieces_raw;
167 p->config_storage[i] = s;
169 if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
170 return HANDLER_ERROR;
173 if (!buffer_string_is_empty(s->path_pieces_raw)) {
174 if (0 != mod_evhost_parse_pattern(s)) {
175 log_error_write(srv, __FILE__, __LINE__, "sb", "invalid evhost.path-pattern:", s->path_pieces_raw);
176 return HANDLER_ERROR;
181 return HANDLER_GO_ON;
185 * assign the different parts of the domain to array-indezes (sub2.sub1.domain.tld)
186 * - %0 - domain.tld
187 * - %1 - tld
188 * - %2 - domain
189 * - %3 - sub1
190 * - ...
193 static int mod_evhost_parse_host(connection *con,array *host) {
194 register char *ptr = con->uri.authority->ptr + buffer_string_length(con->uri.authority);
195 char *colon = ptr; /* needed to filter out the colon (if exists) */
196 int first = 1;
197 data_string *ds;
198 int i;
200 /* first, find the domain + tld */
201 for(;ptr > con->uri.authority->ptr;ptr--) {
202 if(*ptr == '.') {
203 if(first) first = 0;
204 else break;
205 } else if(*ptr == ':') {
206 colon = ptr;
207 first = 1;
211 ds = data_string_init();
212 buffer_copy_string_len(ds->key,CONST_STR_LEN("%0"));
214 /* if we stopped at a dot, skip the dot */
215 if (*ptr == '.') ptr++;
216 buffer_copy_string_len(ds->value, ptr, colon-ptr);
218 array_insert_unique(host,(data_unset *)ds);
220 /* if the : is not the start of the authority, go on parsing the hostname */
222 if (colon != con->uri.authority->ptr) {
223 for(ptr = colon - 1, i = 1; ptr > con->uri.authority->ptr; ptr--) {
224 if(*ptr == '.') {
225 if (ptr != colon - 1) {
226 /* is something between the dots */
227 ds = data_string_init();
228 buffer_copy_string_len(ds->key,CONST_STR_LEN("%"));
229 buffer_append_int(ds->key, i++);
230 buffer_copy_string_len(ds->value,ptr+1,colon-ptr-1);
232 array_insert_unique(host,(data_unset *)ds);
234 colon = ptr;
238 /* if the . is not the first charactor of the hostname */
239 if (colon != ptr) {
240 ds = data_string_init();
241 buffer_copy_string_len(ds->key,CONST_STR_LEN("%"));
242 buffer_append_int(ds->key, i /* ++ */);
243 buffer_copy_string_len(ds->value,ptr,colon-ptr);
245 array_insert_unique(host,(data_unset *)ds);
249 return 0;
252 #define PATCH(x) \
253 p->conf.x = s->x;
254 static int mod_evhost_patch_connection(server *srv, connection *con, plugin_data *p) {
255 size_t i, j;
256 plugin_config *s = p->config_storage[0];
258 PATCH(path_pieces);
259 PATCH(len);
261 /* skip the first, the global context */
262 for (i = 1; i < srv->config_context->used; i++) {
263 data_config *dc = (data_config *)srv->config_context->data[i];
264 s = p->config_storage[i];
266 /* condition didn't match */
267 if (!config_check_cond(srv, con, dc)) continue;
269 /* merge config */
270 for (j = 0; j < dc->value->used; j++) {
271 data_unset *du = dc->value->data[j];
273 if (buffer_is_equal_string(du->key, CONST_STR_LEN("evhost.path-pattern"))) {
274 PATCH(path_pieces);
275 PATCH(len);
280 return 0;
282 #undef PATCH
285 static handler_t mod_evhost_uri_handler(server *srv, connection *con, void *p_d) {
286 plugin_data *p = p_d;
287 size_t i;
288 array *parsed_host;
289 register char *ptr;
290 int not_good = 0;
291 stat_cache_entry *sce = NULL;
293 /* not authority set */
294 if (buffer_string_is_empty(con->uri.authority)) return HANDLER_GO_ON;
296 mod_evhost_patch_connection(srv, con, p);
298 /* missing even default(global) conf */
299 if (0 == p->conf.len) {
300 return HANDLER_GO_ON;
303 parsed_host = array_init();
305 mod_evhost_parse_host(con, parsed_host);
307 /* build document-root */
308 buffer_reset(p->tmp_buf);
310 for (i = 0; i < p->conf.len; i++) {
311 ptr = p->conf.path_pieces[i]->ptr;
312 if (*ptr == '%') {
313 data_string *ds;
315 if (*(ptr+1) == '%') {
316 /* %% */
317 buffer_append_string_len(p->tmp_buf,CONST_STR_LEN("%"));
318 } else if (*(ptr+1) == '_' ) {
319 /* %_ == full hostname */
320 char *colon = strchr(con->uri.authority->ptr, ':');
322 if(colon == NULL) {
323 buffer_append_string_buffer(p->tmp_buf, con->uri.authority); /* adds fqdn */
324 } else {
325 /* strip the port out of the authority-part of the URI scheme */
326 buffer_append_string_len(p->tmp_buf, con->uri.authority->ptr, colon - con->uri.authority->ptr); /* adds fqdn */
328 } else if (ptr[1] == '{' ) {
329 char s[3] = "% ";
330 s[1] = ptr[2]; /*(assumes single digit before '.', and, optionally, '.' and single digit after '.')*/
331 if (NULL != (ds = (data_string *)array_get_element_klen(parsed_host, s, 2))) {
332 if (ptr[3] != '.' || ptr[4] == '0') {
333 buffer_append_string_buffer(p->tmp_buf, ds->value);
334 } else {
335 if ((size_t)(ptr[4]-'0') <= buffer_string_length(ds->value)) {
336 buffer_append_string_len(p->tmp_buf, ds->value->ptr+(ptr[4]-'0')-1, 1);
339 } else {
340 /* unhandled %-sequence */
342 } else if (NULL != (ds = (data_string *)array_get_element_klen(parsed_host, CONST_BUF_LEN(p->conf.path_pieces[i])))) {
343 buffer_append_string_buffer(p->tmp_buf,ds->value);
344 } else {
345 /* unhandled %-sequence */
347 } else {
348 buffer_append_string_buffer(p->tmp_buf,p->conf.path_pieces[i]);
352 buffer_append_slash(p->tmp_buf);
354 array_free(parsed_host);
356 if (HANDLER_ERROR == stat_cache_get_entry(srv, con, p->tmp_buf, &sce)) {
357 log_error_write(srv, __FILE__, __LINE__, "sb", strerror(errno), p->tmp_buf);
358 not_good = 1;
359 } else if(!S_ISDIR(sce->st.st_mode)) {
360 log_error_write(srv, __FILE__, __LINE__, "sb", "not a directory:", p->tmp_buf);
361 not_good = 1;
364 if (!not_good) {
365 buffer_copy_buffer(con->physical.doc_root, p->tmp_buf);
368 return HANDLER_GO_ON;
371 int mod_evhost_plugin_init(plugin *p);
372 int mod_evhost_plugin_init(plugin *p) {
373 p->version = LIGHTTPD_VERSION_ID;
374 p->name = buffer_init_string("evhost");
375 p->init = mod_evhost_init;
376 p->set_defaults = mod_evhost_set_defaults;
377 p->handle_docroot = mod_evhost_uri_handler;
378 p->cleanup = mod_evhost_free;
380 p->data = NULL;
382 return 0;
385 /* eof */