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"
69 #include "apr_strings.h"
71 #define APR_WANT_STRFUNC
75 /* {{{ domaintree_module */
77 module AP_MODULE_DECLARE_DATA domaintree_module
;
80 /* {{{ Macros & Types */
82 #define MOD_DT_CNF domaintree_conf
83 #define MOD_DT_PTR (&domaintree_module)
86 #define EMPTY(str) ((str == NULL) || (*(str) == NUL))
91 apr_hash_t
*hashtable
;
105 struct domaintree_entry
{
107 APR_RING_ENTRY(domaintree_entry
) link
;
109 APR_RING_HEAD(domaintree
, domaintree_entry
);
114 static APR_INLINE
char *strtr(char *string
, char from
, char to
)
119 while ((ptr
= strchr(ptr
, from
))) {
126 static APR_INLINE
char *trim(char *string
, size_t length
, char what
, int l
, int r
)
129 while (length
-- && (string
[length
] == what
)) {
130 string
[length
] = NUL
;
134 while (*string
== what
) {
141 static APR_INLINE
char *domaintree_host(apr_pool_t
*pool
, MOD_DT_CNF
*DT
, const char *name
)
144 ap_log_error(APLOG_MARK
, APLOG_WARNING
, APR_SUCCESS
, DT
->server
, "DomainTree: no host/server name");
148 char *port
, *ptr
, *host
;
150 ptr
= host
= apr_pstrdup(pool
, name
);
151 ap_log_error(APLOG_MARK
, APLOG_DEBUG
, APR_SUCCESS
, DT
->server
, "DomainTree: host name = %s", host
);
153 /* check for :NN port */
154 if ((port
= strchr(ptr
, ':'))) {
160 /* strip leading & trailing dots */
161 ptr
= host
= trim(ptr
, len
, '.', 1, 1);
167 ap_log_error(APLOG_MARK
, APLOG_DEBUG
, APR_SUCCESS
, DT
->server
, "DomainTree: sane host = %s", host
);
173 static APR_INLINE
const char *domaintree_elem(apr_pool_t
*pool
, struct domaintree
*tree
, const char *name
, size_t length
)
175 struct domaintree_entry
*elem
= apr_palloc(pool
, sizeof(struct domaintree_entry
));
177 APR_RING_ELEM_INIT(elem
, link
);
178 APR_RING_INSERT_HEAD(tree
, elem
, domaintree_entry
, link
);
180 return elem
->name
= apr_pstrndup(pool
, name
, length
);
183 static APR_INLINE
struct domaintree
*domaintree_tree(apr_pool_t
*pool
, MOD_DT_CNF
*DT
, char *host
)
186 char *host_ptr
= host
;
187 struct domaintree
*tree
= apr_palloc(pool
, sizeof(struct domaintree
));
189 APR_RING_INIT(tree
, domaintree_entry
, link
);
191 while ((host_ptr
= strchr(host
, '.'))) {
193 /* check max depth */
194 if (++depth
> DT
->maxdepth
) {
195 ap_log_error(APLOG_MARK
, APLOG_ERR
, APR_SUCCESS
, DT
->server
, "DomainTree: maxdepth exceeded = %s", host
);
200 if (host_ptr
- host
) {
203 if (DT
->stripwww
&& (depth
== 1) && (!strncmp(host
, "www.", sizeof("www")))) {
204 ap_log_error(APLOG_MARK
, APLOG_DEBUG
, APR_SUCCESS
, DT
->server
, "DomainTree: stripping www.");
206 ap_log_error(APLOG_MARK
, APLOG_DEBUG
, APR_SUCCESS
, DT
->server
, "DomainTree: host part (%d) = %s",
207 depth
- 1, domaintree_elem(pool
, tree
, host
, host_ptr
- host
)
215 /* append last part */
217 ap_log_error(APLOG_MARK
, APLOG_DEBUG
, APR_SUCCESS
, DT
->server
, "DomainTree: host part (%d) = %s",
218 depth
, domaintree_elem(pool
, tree
, host
, strlen(host
))
225 static APR_INLINE
char *domaintree_path(apr_pool_t
*pool
, MOD_DT_CNF
*DT
, struct domaintree
*tree
)
227 struct domaintree_entry
*elem
;
228 char *path
= apr_pstrdup(pool
, DT
->prefix
);
230 APR_RING_FOREACH(elem
, tree
, domaintree_entry
, link
) {
231 path
= apr_pstrcat(pool
, path
, "/", elem
->name
, NULL
);
234 return path
= apr_pstrcat(pool
, path
, "/", DT
->suffix
, NULL
);
237 static APR_INLINE
void domaintree_fake(apr_pool_t
*pool
, MOD_DT_CNF
*DT
, char **path
)
240 apr_hash_index_t
*idx
;
241 size_t recurlevel
= 0, plen
= strlen(DT
->prefix
) + 1;
246 if (recurlevel
++ > DT
->aliases
.recursion
) {
247 ap_log_error(APLOG_MARK
, APLOG_ERR
, APR_SUCCESS
, DT
->server
, "DomainTree: maximum alias recursion level (%d) exceeded!"
248 " Check if you have recursive definitions of DomainTreeAlias directives.", DT
->aliases
.recursion
);
252 for (idx
= apr_hash_first(pool
, DT
->aliases
.hashtable
); idx
; idx
= apr_hash_next(idx
)) {
254 char *fake
, *real
, *poff
;
257 apr_hash_this(idx
, (const void **) &fake
, &flen
, (void **) &real
);
259 ap_log_error(APLOG_MARK
, APLOG_DEBUG
, APR_SUCCESS
, DT
->server
, "DomainTree: fake test %s in %s", fake
, poff
);
261 if ((!strncasecmp(poff
, fake
, flen
)) && ((!poff
[flen
]) || (poff
[flen
] == '/'))) {
265 padd
= apr_pstrndup(pool
, poff
+ flen
+ 1, strlen(poff
) - flen
- 1);
266 *path
= apr_pstrcat(pool
, DT
->prefix
, "/", real
, "/", padd
, NULL
);
268 ap_log_error(APLOG_MARK
, APLOG_DEBUG
, APR_SUCCESS
, DT
->server
, "DomainTree: fake done %s<>%s = %s", fake
, real
, *path
);
272 if (more
&& DT
->aliases
.recursion
) {
280 static STATUS
domaintree_hook_post_config(apr_pool_t
*pconf
, apr_pool_t
*plog
, apr_pool_t
*ptemp
, server_rec
*s
)
282 ap_add_version_component(pconf
, MODULE
"/" VERSION
);
286 static STATUS
domaintree_hook_translate_name(request_rec
*r
)
288 MOD_DT_CNF
*DT
= NULL
;
289 struct domaintree
*tree
;
292 DT
= ap_get_module_config(r
->server
->module_config
, MOD_DT_PTR
);
293 if ((!DT
) || (!DT
->enabled
)) {
297 /* get a usable host name */
298 if (!(host
= domaintree_host(r
->pool
, DT
, ap_get_server_name(r
)))) {
302 /* build domain tree */
303 if (!(tree
= domaintree_tree(r
->pool
, DT
, host
))) {
308 if (!(path
= domaintree_path(r
->pool
, DT
, tree
))) {
312 /* apply any aliases.hashtable */
313 domaintree_fake(r
->pool
, DT
, &path
);
316 r
->canonical_filename
= "";
317 r
->filename
= apr_pstrcat(r
->pool
, path
, r
->uri
, NULL
);
318 ap_log_error(APLOG_MARK
, APLOG_DEBUG
, APR_SUCCESS
, DT
->server
, "DomainTree: path done = %s", r
->filename
);
323 static void domaintree_hooks(apr_pool_t
*pool
)
325 ap_hook_post_config(domaintree_hook_post_config
, NULL
, NULL
, APR_HOOK_MIDDLE
);
326 ap_hook_translate_name(domaintree_hook_translate_name
, NULL
, NULL
, APR_HOOK_MIDDLE
);
330 /* {{{ Configuration */
332 static void *domaintree_create_srv(apr_pool_t
*p
, server_rec
*s
)
336 DT
= (MOD_DT_CNF
*) apr_palloc(p
, sizeof(MOD_DT_CNF
));
343 DT
->prefix
= "/var/www";
344 DT
->suffix
= "public_html";
346 DT
->aliases
.hashtable
= apr_hash_make(p
);
347 DT
->aliases
.recursion
= 0;
352 static const char *domaintree_enable(cmd_parms
*cmd
, void *conf
, int flag
)
356 DT
= ap_get_module_config(cmd
->server
->module_config
, MOD_DT_PTR
);
362 static const char *domaintree_stripwww(cmd_parms
*cmd
, void *conf
, int flag
)
366 DT
= ap_get_module_config(cmd
->server
->module_config
, MOD_DT_PTR
);
372 static const char *domaintree_prefix(cmd_parms
*cmd
, void *conf
, const char *prefix
)
376 DT
= ap_get_module_config(cmd
->server
->module_config
, MOD_DT_PTR
);
377 DT
->prefix
= EMPTY(prefix
) ? "/" : trim(apr_pstrdup(cmd
->pool
, prefix
), strlen(prefix
), '/', 0, 1);
382 static const char *domaintree_suffix(cmd_parms
*cmd
, void *conf
, const char *suffix
)
386 DT
= ap_get_module_config(cmd
->server
->module_config
, MOD_DT_PTR
);
387 DT
->suffix
= EMPTY(suffix
) ? "" : trim(apr_pstrdup(cmd
->pool
, suffix
), strlen(suffix
), '/', 1, 1);
392 static const char *domaintree_maxdepth(cmd_parms
*cmd
, void *conf
, const char *max_depth
)
396 if ((depth
= atoi(max_depth
))) {
399 DT
= ap_get_module_config(cmd
->server
->module_config
, MOD_DT_PTR
);
400 DT
->maxdepth
= (size_t) depth
;
402 return "Maximum DomainTree depth cannot be negative.";
409 static const char *domaintree_aliasrecursion(cmd_parms
*cmd
, void *conf
, const char *alias_recursion
)
413 if ((recursion
= atoi(alias_recursion
))) {
416 DT
= ap_get_module_config(cmd
->server
->module_config
, MOD_DT_PTR
);
417 DT
->aliases
.recursion
= (size_t) recursion
;
419 return "DomainTree alias recursion cannot be negative.";
426 static const char *domaintree_alias(cmd_parms
*cmd
, void *conf
, const char *fake
, const char *real
)
429 char *f
= strtr(apr_pstrdup(cmd
->pool
, fake
), '.', '/'), *r
= strtr(apr_pstrdup(cmd
->pool
, real
), '.', '/');
431 DT
= ap_get_module_config(cmd
->server
->module_config
, MOD_DT_PTR
);
432 apr_hash_set(DT
->aliases
.hashtable
, trim(f
, strlen(f
), '/', 1, 1), APR_HASH_KEY_STRING
, trim(r
, strlen(r
), '/', 1, 1));
441 static command_rec domaintree_commands
[] = {
443 "DomainTreeEnabled", domaintree_enable
, NULL
, RSRC_CONF
,
444 "Turn the module on or off."
448 "DomainTreeStripWWW", domaintree_stripwww
, NULL
, RSRC_CONF
,
449 "Strip leading www from host. (default On)"
453 "DomainTreePrefix", domaintree_prefix
, NULL
, RSRC_CONF
,
454 "DomainTree path prefix. (default /var/www) Do not forget the leading slash!"
458 "DomainTreeSuffix", domaintree_suffix
, NULL
, RSRC_CONF
,
459 "DomainTree path suffix. (default public_html)"
463 "DomainTreeMaxdepth", domaintree_maxdepth
, NULL
, RSRC_CONF
,
464 "DomainTree max path depth. (default 20)"
468 "DomainTreeAliasRecursion", domaintree_aliasrecursion
, NULL
, RSRC_CONF
,
469 "Whether (and how often at the maximum) DomainTree should walk recursively "
470 "through the aliases.hashtable list as long as matching aliases.hashtable are found. (Default: 0 = turned off)"
474 "DomainTreeAlias", domaintree_alias
, NULL
, RSRC_CONF
,
475 "DomainTree aliases.hashtable; e.g. DomainTreeAlias com/example/tickets com/example/support (dots or slashes equal)"
482 /* {{{ Module Administrativa */
484 module AP_MODULE_DECLARE_DATA domaintree_module
= {
485 STANDARD20_MODULE_STUFF
,
486 NULL
, /* create per-dir */
487 NULL
, /* merge per-dir */
488 domaintree_create_srv
, /* create per-server */
489 NULL
, /* merge per-server */
490 domaintree_commands
, /* apr_table_t commands */
491 domaintree_hooks
/* register hooks */
501 * vim600: noet sw=4 ts=4 fdm=marker
502 * vim<600: noet sw=4 ts=4