2 * mod_domaintree.c - Apache2
6 * Copyright 2005 Michael Wallner <mike@iworks.at
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
22 * DomainTreeEnabled On
23 * DomainTreeMaxdepth 25
24 * DomainTreeStripWWW On
25 * DomainTreePrefix /sites
26 * DomainTreeSuffix /html
38 * | | +- /organisation
48 #define MODULE "mod_domaintree"
49 #define AUTHOR "mike@php.net"
55 #include "http_config.h"
58 #include "http_core.h"
62 #include "http_protocol.h"
63 #include "http_request.h"
68 #include "apr_strings.h"
70 #define APR_WANT_STRFUNC
74 /* {{{ domaintree_module */
76 module AP_MODULE_DECLARE_DATA domaintree_module
;
79 /* {{{ Macros & Types */
81 #define MOD_DT_CNF domaintree_conf
82 #define MOD_DT_PTR (&domaintree_module)
85 #define EMPTY(str) ((str == NULL) || (*(str) == NUL))
98 struct domaintree_entry
{
100 APR_RING_ENTRY(domaintree_entry
) link
;
102 APR_RING_HEAD(domaintree
, domaintree_entry
);
107 static APR_INLINE
char *domaintree_host(apr_pool_t
*pool
, MOD_DT_CNF
*DT
, const char *name
)
110 ap_log_error(APLOG_MARK
, APLOG_WARNING
, APR_SUCCESS
, DT
->server
, "DomainTree: no host/server name");
114 char *port
, *ptr
, *host
;
116 ptr
= host
= apr_pstrdup(pool
, name
);
117 ap_log_error(APLOG_MARK
, APLOG_DEBUG
, APR_SUCCESS
, DT
->server
, "DomainTree: host name = %s", host
);
119 /* check for :NN port */
120 if ((port
= strchr(ptr
, ':'))) {
126 /* strip leading and trailing dots */
127 while (ptr
[len
- 1] == '.') {
130 while (*ptr
== '.') {
136 /* terminate & lowercase */
142 ap_log_error(APLOG_MARK
, APLOG_DEBUG
, APR_SUCCESS
, DT
->server
, "DomainTree: sane host = %s", host
);
147 static APR_INLINE
const char *domaintree_append(apr_pool_t
*pool
, struct domaintree
*tree
, const char *name
, size_t length
)
149 struct domaintree_entry
*elem
= apr_palloc(pool
, sizeof(struct domaintree_entry
));
151 APR_RING_ELEM_INIT(elem
, link
);
152 APR_RING_INSERT_HEAD(tree
, elem
, domaintree_entry
, link
);
154 return elem
->name
= apr_pstrndup(pool
, name
, length
);
157 static APR_INLINE
struct domaintree
*domaintree_tree(apr_pool_t
*pool
, MOD_DT_CNF
*DT
, char *host
)
160 char *host_ptr
= host
;
161 struct domaintree
*tree
= apr_palloc(pool
, sizeof(struct domaintree
));
163 APR_RING_INIT(tree
, domaintree_entry
, link
);
165 while ((host_ptr
= strchr(host
, '.'))) {
167 /* check max depth */
168 if (++depth
> DT
->maxdepth
) {
169 ap_log_error(APLOG_MARK
, APLOG_ERR
, APR_SUCCESS
, DT
->server
, "DomainTree: maxdepth exceeded = %s", host
);
174 if (host_ptr
- host
) {
177 if (DT
->stripwww
&& (depth
== 1) && (!strncmp(host
, "www.", sizeof("www")))) {
178 ap_log_error(APLOG_MARK
, APLOG_DEBUG
, APR_SUCCESS
, DT
->server
, "DomainTree: stripping www.");
180 ap_log_error(APLOG_MARK
, APLOG_DEBUG
, APR_SUCCESS
, DT
->server
, "DomainTree: host part (%d) = %s",
181 depth
- 1, domaintree_append(pool
, tree
, host
, host_ptr
- host
)
189 /* append last part */
191 ap_log_error(APLOG_MARK
, APLOG_DEBUG
, APR_SUCCESS
, DT
->server
, "DomainTree: host part (%d) = %s",
192 depth
, domaintree_append(pool
, tree
, host
, strlen(host
))
199 static APR_INLINE
char *domaintree_path(apr_pool_t
*pool
, MOD_DT_CNF
*DT
, struct domaintree
*tree
)
201 struct domaintree_entry
*elem
;
202 char *path
= apr_pstrdup(pool
, DT
->prefix
);
203 APR_RING_FOREACH(elem
, tree
, domaintree_entry
, link
) {
204 path
= apr_pstrcat(pool
, path
, "/", elem
->name
, NULL
);
206 return path
= apr_pstrcat(pool
, path
, "/", DT
->suffix
, NULL
);
212 static STATUS
domaintree_hook_post_config(apr_pool_t
*pconf
, apr_pool_t
*plog
, apr_pool_t
*ptemp
, server_rec
*s
)
214 ap_add_version_component(pconf
, MODULE
"/" VERSION
);
218 static STATUS
domaintree_hook_translate_name(request_rec
*r
)
220 MOD_DT_CNF
*DT
= NULL
;
221 struct domaintree
*tree
;
224 DT
= ap_get_module_config(r
->server
->module_config
, MOD_DT_PTR
);
225 if ((!DT
) || (!DT
->enabled
)) {
229 /* get a usable host name */
230 if (!(host
= domaintree_host(r
->pool
, DT
, ap_get_server_name(r
)))) {
234 /* build domain tree */
235 if (!(tree
= domaintree_tree(r
->pool
, DT
, host
))) {
240 if (!(path
= domaintree_path(r
->pool
, DT
, tree
))) {
245 r
->canonical_filename
= "";
246 r
->filename
= apr_pstrcat(r
->pool
, path
, r
->uri
, NULL
);
247 ap_log_error(APLOG_MARK
, APLOG_DEBUG
, APR_SUCCESS
, DT
->server
, "DomainTree: final path = %s", r
->filename
);
252 static void domaintree_hooks(apr_pool_t
*pool
)
254 ap_hook_post_config(domaintree_hook_post_config
, NULL
, NULL
, APR_HOOK_MIDDLE
);
255 ap_hook_translate_name(domaintree_hook_translate_name
, NULL
, NULL
, APR_HOOK_MIDDLE
);
259 /* {{{ Configuration */
261 static void *domaintree_create_srv(apr_pool_t
*p
, server_rec
*s
)
265 DT
= (MOD_DT_CNF
*) apr_palloc(p
, sizeof(MOD_DT_CNF
));
272 DT
->prefix
= "/var/www";
273 DT
->suffix
= "public_html";
278 static const char *domaintree_enable(cmd_parms
*cmd
, void *conf
, int flag
)
282 DT
= ap_get_module_config(cmd
->server
->module_config
, MOD_DT_PTR
);
288 static const char *domaintree_stripwww(cmd_parms
*cmd
, void *conf
, int flag
)
292 DT
= ap_get_module_config(cmd
->server
->module_config
, MOD_DT_PTR
);
298 static const char *domaintree_prefix(cmd_parms
*cmd
, void *conf
, const char *prefix
)
302 DT
= ap_get_module_config(cmd
->server
->module_config
, MOD_DT_PTR
);
303 DT
->prefix
= EMPTY(prefix
) ? "/" : apr_pstrdup(cmd
->pool
, prefix
);
308 static const char *domaintree_suffix(cmd_parms
*cmd
, void *conf
, const char *suffix
)
312 DT
= ap_get_module_config(cmd
->server
->module_config
, MOD_DT_PTR
);
313 DT
->suffix
= EMPTY(suffix
) ? "/" : apr_pstrdup(cmd
->pool
, suffix
);
318 static const char *domaintree_maxdepth(cmd_parms
*cmd
, void *conf
, const char *max_depth
)
322 if ((depth
= atoi(max_depth
))) {
325 DT
= ap_get_module_config(cmd
->server
->module_config
, MOD_DT_PTR
);
326 DT
->maxdepth
= (size_t) depth
;
328 return "Maximum DomainTree depth cannot be negative.";
338 static command_rec domaintree_commands
[] = {
340 "DomainTreeEnabled", domaintree_enable
, NULL
, RSRC_CONF
,
341 "Turn the module on or off."
345 "DomainTreeStripWWW", domaintree_stripwww
, NULL
, RSRC_CONF
,
346 "Strip leading www from host. (default On)"
350 "DomainTreePrefix", domaintree_prefix
, NULL
, RSRC_CONF
,
351 "DomainTree path prefix. (default /var/www)"
355 "DomainTreeSuffix", domaintree_suffix
, NULL
, RSRC_CONF
,
356 "DomainTree path suffix. (default public_html)"
360 "DomainTreeMaxdepth", domaintree_maxdepth
, NULL
, RSRC_CONF
,
361 "DomainTree max path depth. (default 20)"
368 /* {{{ Module Administrativa */
370 module AP_MODULE_DECLARE_DATA domaintree_module
= {
371 STANDARD20_MODULE_STUFF
,
372 NULL
, /* create per-dir */
373 NULL
, /* merge per-dir */
374 domaintree_create_srv
, /* create per-server */
375 NULL
, /* merge per-server */
376 domaintree_commands
, /* apr_table_t commands */
377 domaintree_hooks
/* register hooks */
387 * vim600: noet sw=4 ts=4 fdm=marker
388 * vim<600: noet sw=4 ts=4