6 #include "stat_cache.h"
17 buffer
*document_root
;
19 buffer
*docroot_cache_key
;
20 buffer
*docroot_cache_value
;
21 buffer
*docroot_cache_servername
;
31 plugin_config
**config_storage
;
35 INIT_FUNC(mod_simple_vhost_init
) {
38 p
= calloc(1, sizeof(*p
));
40 p
->doc_root
= buffer_init();
45 FREE_FUNC(mod_simple_vhost_free
) {
50 if (!p
) return HANDLER_GO_ON
;
52 if (p
->config_storage
) {
54 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
55 plugin_config
*s
= p
->config_storage
[i
];
56 if (NULL
== s
) continue;
58 buffer_free(s
->document_root
);
59 buffer_free(s
->default_host
);
60 buffer_free(s
->server_root
);
62 buffer_free(s
->docroot_cache_key
);
63 buffer_free(s
->docroot_cache_value
);
64 buffer_free(s
->docroot_cache_servername
);
69 free(p
->config_storage
);
72 buffer_free(p
->doc_root
);
79 SETDEFAULTS_FUNC(mod_simple_vhost_set_defaults
) {
83 config_values_t cv
[] = {
84 { "simple-vhost.server-root", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
},
85 { "simple-vhost.default-host", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
},
86 { "simple-vhost.document-root", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
},
87 { "simple-vhost.debug", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
},
88 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
91 if (!p
) return HANDLER_ERROR
;
93 p
->config_storage
= calloc(1, srv
->config_context
->used
* sizeof(plugin_config
*));
95 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
96 data_config
const* config
= (data_config
const*)srv
->config_context
->data
[i
];
99 s
= calloc(1, sizeof(plugin_config
));
101 s
->server_root
= buffer_init();
102 s
->default_host
= buffer_init();
103 s
->document_root
= buffer_init();
105 s
->docroot_cache_key
= buffer_init();
106 s
->docroot_cache_value
= buffer_init();
107 s
->docroot_cache_servername
= buffer_init();
111 cv
[0].destination
= s
->server_root
;
112 cv
[1].destination
= s
->default_host
;
113 cv
[2].destination
= s
->document_root
;
114 cv
[3].destination
= &(s
->debug
);
117 p
->config_storage
[i
] = s
;
119 if (0 != config_insert_values_global(srv
, config
->value
, cv
, i
== 0 ? T_CONFIG_SCOPE_SERVER
: T_CONFIG_SCOPE_CONNECTION
)) {
120 return HANDLER_ERROR
;
124 return HANDLER_GO_ON
;
127 static int build_doc_root(server
*srv
, connection
*con
, plugin_data
*p
, buffer
*out
, buffer
*host
) {
128 stat_cache_entry
*sce
= NULL
;
129 force_assert(!buffer_string_is_empty(p
->conf
.server_root
));
131 buffer_string_prepare_copy(out
, 127);
132 buffer_copy_buffer(out
, p
->conf
.server_root
);
134 if (!buffer_string_is_empty(host
)) {
135 /* a hostname has to start with a alpha-numerical character
136 * and must not contain a slash "/"
140 buffer_append_slash(out
);
142 if (NULL
== (dp
= strchr(host
->ptr
, ':'))) {
143 buffer_append_string_buffer(out
, host
);
145 buffer_append_string_len(out
, host
->ptr
, dp
- host
->ptr
);
148 buffer_append_slash(out
);
150 if (buffer_string_length(p
->conf
.document_root
) > 1 && p
->conf
.document_root
->ptr
[0] == '/') {
151 buffer_append_string_len(out
, p
->conf
.document_root
->ptr
+ 1, buffer_string_length(p
->conf
.document_root
) - 1);
153 buffer_append_string_buffer(out
, p
->conf
.document_root
);
154 buffer_append_slash(out
);
157 if (HANDLER_ERROR
== stat_cache_get_entry(srv
, con
, out
, &sce
)) {
159 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
160 strerror(errno
), out
);
163 } else if (!S_ISDIR(sce
->st
.st_mode
)) {
173 static int mod_simple_vhost_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
175 plugin_config
*s
= p
->config_storage
[0];
179 PATCH(document_root
);
181 PATCH(docroot_cache_key
);
182 PATCH(docroot_cache_value
);
183 PATCH(docroot_cache_servername
);
187 /* skip the first, the global context */
188 for (i
= 1; i
< srv
->config_context
->used
; i
++) {
189 data_config
*dc
= (data_config
*)srv
->config_context
->data
[i
];
190 s
= p
->config_storage
[i
];
192 /* condition didn't match */
193 if (!config_check_cond(srv
, con
, dc
)) continue;
196 for (j
= 0; j
< dc
->value
->used
; j
++) {
197 data_unset
*du
= dc
->value
->data
[j
];
199 if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("simple-vhost.server-root"))) {
201 PATCH(docroot_cache_key
);
202 PATCH(docroot_cache_value
);
203 PATCH(docroot_cache_servername
);
204 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("simple-vhost.default-host"))) {
206 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("simple-vhost.document-root"))) {
207 PATCH(document_root
);
208 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("simple-vhost.debug"))) {
218 static handler_t
mod_simple_vhost_docroot(server
*srv
, connection
*con
, void *p_data
) {
219 plugin_data
*p
= p_data
;
222 * cache the last successfull translation from hostname (authority) to docroot
223 * - this saves us a stat() call
227 mod_simple_vhost_patch_connection(srv
, con
, p
);
229 /* build_doc_root() requires a server_root; skip module if simple-vhost.server-root is not set
230 * or set to an empty string (especially don't cache any results!)
232 if (buffer_string_is_empty(p
->conf
.server_root
)) return HANDLER_GO_ON
;
234 if (!buffer_string_is_empty(p
->conf
.docroot_cache_key
) &&
235 !buffer_string_is_empty(con
->uri
.authority
) &&
236 buffer_is_equal(p
->conf
.docroot_cache_key
, con
->uri
.authority
)) {
238 buffer_copy_buffer(con
->server_name
, p
->conf
.docroot_cache_servername
);
239 buffer_copy_buffer(con
->physical
.doc_root
, p
->conf
.docroot_cache_value
);
241 /* build document-root */
242 if (buffer_string_is_empty(con
->uri
.authority
) ||
243 build_doc_root(srv
, con
, p
, p
->doc_root
, con
->uri
.authority
)) {
244 /* not found, fallback the default-host */
245 if (0 == build_doc_root(srv
, con
, p
,
247 p
->conf
.default_host
)) {
248 /* default host worked */
249 buffer_copy_buffer(con
->server_name
, p
->conf
.default_host
);
250 buffer_copy_buffer(con
->physical
.doc_root
, p
->doc_root
);
251 /* do not cache default host */
253 return HANDLER_GO_ON
;
257 buffer_copy_buffer(con
->server_name
, con
->uri
.authority
);
258 buffer_copy_buffer(con
->physical
.doc_root
, p
->doc_root
);
261 buffer_copy_buffer(p
->conf
.docroot_cache_key
, con
->uri
.authority
);
262 buffer_copy_buffer(p
->conf
.docroot_cache_value
, p
->doc_root
);
263 buffer_copy_buffer(p
->conf
.docroot_cache_servername
, con
->server_name
);
266 return HANDLER_GO_ON
;
270 int mod_simple_vhost_plugin_init(plugin
*p
);
271 int mod_simple_vhost_plugin_init(plugin
*p
) {
272 p
->version
= LIGHTTPD_VERSION_ID
;
273 p
->name
= buffer_init_string("simple_vhost");
275 p
->init
= mod_simple_vhost_init
;
276 p
->set_defaults
= mod_simple_vhost_set_defaults
;
277 p
->handle_docroot
= mod_simple_vhost_docroot
;
278 p
->cleanup
= mod_simple_vhost_free
;