6 #include "stat_cache.h"
14 buffer
*path_pieces_raw
;
16 /* pieces for path creation */
25 plugin_config
**config_storage
;
29 INIT_FUNC(mod_evhost_init
) {
32 p
= calloc(1, sizeof(*p
));
34 p
->tmp_buf
= buffer_init();
39 FREE_FUNC(mod_evhost_free
) {
44 if (!p
) return HANDLER_GO_ON
;
46 if (p
->config_storage
) {
48 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
49 plugin_config
*s
= p
->config_storage
[i
];
51 if (NULL
== s
) continue;
55 for (j
= 0; j
< s
->len
; j
++) {
56 buffer_free(s
->path_pieces
[j
]);
62 buffer_free(s
->path_pieces_raw
);
66 free(p
->config_storage
);
69 buffer_free(p
->tmp_buf
);
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
++) {
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])) {
91 } else if (ptr
[1] == '{') {
92 if (!light_isdigit(ptr
[2])) return -1;
94 if (!light_isdigit(ptr
[4])) return -1;
95 if (ptr
[5] != '}') return -1;
97 } else if (ptr
[3] == '}') {
106 buffer_copy_string_len(s
->path_pieces
[s
->len
],pos
,ptr
-pos
);
109 buffer_copy_string_len(s
->path_pieces
[s
->len
+1],ptr
,len
);
110 ptr
+= len
- 1; /*(ptr++ in for() loop)*/
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
);
128 SETDEFAULTS_FUNC(mod_evhost_set_defaults
) {
129 plugin_data
*p
= p_d
;
135 * # define a pattern for the host url finding
137 * # %0 => domain name + 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
];
161 s
= calloc(1, sizeof(plugin_config
));
162 s
->path_pieces_raw
= buffer_init();
163 s
->path_pieces
= NULL
;
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)
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) */
201 /* first, find the domain + tld */
202 for(;ptr
> con
->uri
.authority
->ptr
;ptr
--) {
206 } else if(*ptr
== ':') {
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
--) {
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
);
239 /* if the . is not the first charactor of the hostname */
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
);
255 static int mod_evhost_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
257 plugin_config
*s
= p
->config_storage
[0];
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;
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"))) {
286 static handler_t
mod_evhost_uri_handler(server
*srv
, connection
*con
, void *p_d
) {
287 plugin_data
*p
= p_d
;
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
;
316 if (*(ptr
+1) == '%') {
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
, ':');
324 buffer_append_string_buffer(p
->tmp_buf
, con
->uri
.authority
); /* adds fqdn */
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] == '{' ) {
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
);
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);
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
);
346 /* unhandled %-sequence */
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
);
360 } else if(!S_ISDIR(sce
->st
.st_mode
)) {
361 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "not a directory:", p
->tmp_buf
);
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
;