6 #include "stat_cache.h"
13 buffer
*path_pieces_raw
;
15 /* pieces for path creation */
24 plugin_config
**config_storage
;
28 INIT_FUNC(mod_evhost_init
) {
31 p
= calloc(1, sizeof(*p
));
33 p
->tmp_buf
= buffer_init();
38 FREE_FUNC(mod_evhost_free
) {
43 if (!p
) return HANDLER_GO_ON
;
45 if (p
->config_storage
) {
47 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
48 plugin_config
*s
= p
->config_storage
[i
];
50 if (NULL
== s
) continue;
54 for (j
= 0; j
< s
->len
; j
++) {
55 buffer_free(s
->path_pieces
[j
]);
61 buffer_free(s
->path_pieces_raw
);
65 free(p
->config_storage
);
68 buffer_free(p
->tmp_buf
);
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
++) {
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])) {
90 } else if (ptr
[1] == '{') {
91 if (!light_isdigit(ptr
[2])) return -1;
93 if (!light_isdigit(ptr
[4])) return -1;
94 if (ptr
[5] != '}') return -1;
96 } else if (ptr
[3] == '}') {
105 buffer_copy_string_len(s
->path_pieces
[s
->len
],pos
,ptr
-pos
);
108 buffer_copy_string_len(s
->path_pieces
[s
->len
+1],ptr
,len
);
109 ptr
+= len
- 1; /*(ptr++ in for() loop)*/
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
);
127 SETDEFAULTS_FUNC(mod_evhost_set_defaults
) {
128 plugin_data
*p
= p_d
;
134 * # define a pattern for the host url finding
136 * # %0 => domain name + 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
];
160 s
= calloc(1, sizeof(plugin_config
));
161 s
->path_pieces_raw
= buffer_init();
162 s
->path_pieces
= NULL
;
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)
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) */
200 /* first, find the domain + tld */
201 for(;ptr
> con
->uri
.authority
->ptr
;ptr
--) {
205 } else if(*ptr
== ':') {
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
--) {
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
);
238 /* if the . is not the first charactor of the hostname */
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
);
254 static int mod_evhost_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
256 plugin_config
*s
= p
->config_storage
[0];
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;
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"))) {
285 static handler_t
mod_evhost_uri_handler(server
*srv
, connection
*con
, void *p_d
) {
286 plugin_data
*p
= p_d
;
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
;
315 if (*(ptr
+1) == '%') {
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
, ':');
323 buffer_append_string_buffer(p
->tmp_buf
, con
->uri
.authority
); /* adds fqdn */
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] == '{' ) {
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
);
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);
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
);
345 /* unhandled %-sequence */
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
);
359 } else if(!S_ISDIR(sce
->st
.st_mode
)) {
360 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "not a directory:", p
->tmp_buf
);
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
;