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
28 * DomainTreeAliasRecursion Off
29 * DomainTreeAlias /??/exmaple /com/exmaple
30 * DomainTreeAlias /???/example /com/example
31 * DomainTreeAlias /*one/ /anyone/
55 #define MODULE "mod_domaintree"
56 #define AUTHOR "<mike@iworks.at>"
64 #include "apr_tables.h"
65 #include "apr_strings.h"
71 #define APR_WANT_MEMFUNC
72 #define APR_WANT_STRFUNC
76 #include "http_config.h"
77 #include "http_core.h"
79 #include "http_protocol.h"
80 #include "http_request.h"
83 /* {{{ domaintree_module */
85 module AP_MODULE_DECLARE_DATA domaintree_module
;
88 /* {{{ Macros & Types */
90 #define MDT_CNF domaintree_conf
91 #define MDT_PTR (&domaintree_module)
93 #define GET_MDT_CNF(srv) ((MDT_CNF *) ap_get_module_config((srv)->module_config, MDT_PTR))
95 #define IF_SET_ELSE(a, b) (a != -1) ? (a) : (b)
98 #define EMPTY(str) ((str == NULL) || (*(str) == NUL))
99 #define local static APR_INLINE
101 #define DT_LOG_ERR ap_log_error(APLOG_MARK, APLOG_ERR, APR_SUCCESS, DT->server,
102 #define DT_LOG_WRN ap_log_error(APLOG_MARK, APLOG_WARNING, APR_SUCCESS, DT->server,
103 #define DT_LOG_DBG ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, DT->server,
104 #define DT_LOG_END );
110 apr_table_t
*faketable
;
124 apr_global_mutex_t
*lock
;
142 local
char *strtr(char *string
, char from
, char to
)
147 while ((ptr
= strchr(ptr
, from
))) {
156 #define TRIM_BOTH (TRIM_LEFT|TRIM_RIGHT)
157 local
char *trim(char *string
, size_t length
, char what
, int where
)
159 if (where
& TRIM_RIGHT
) {
160 while (length
-- && (string
[length
] == what
)) {
161 string
[length
] = NUL
;
164 if (where
& TRIM_LEFT
) {
165 while (*string
== what
) {
172 local
int strmatch(const char *match
, const char *string
, const char **begin
, const char **end
)
174 *begin
= *end
= NULL
;
181 while (*match
== '*' || *match
== '?') {
190 *end
= string
+ strlen(string
);
194 if (!(string
= strchr(string
, *match
))) {
209 if (*match
== *string
) {
229 local
char *struniqchr(char *string
, char uniq
)
234 if (*ptr
== uniq
&& *(ptr
+ 1) == uniq
) {
237 while (*(pos
+ 1) == uniq
) {
241 memmove(ptr
, pos
, strlen(pos
) + 1);
249 local
char *domaintree_host(apr_pool_t
*pool
, MDT_CNF
*DT
, const char *host_name
)
254 if (EMPTY(host_name
)) {
256 "DomainTree: no host/server name"
262 host
= apr_pstrdup(pool
, host_name
);
264 /* check for :NN port */
265 if ((port
= strchr(host
, ':'))) {
272 /* strip leading & trailing dots */
273 host
= trim(host
, len
, '.', TRIM_BOTH
);
276 "DomainTree: host name = %s for %s", host
, host_name
282 local
char *domaintree_path(apr_pool_t
*pool
, MDT_CNF
*DT
, const char *host_name
)
284 long depth
= 0, maxdepth
= IF_SET_ELSE(DT
->maxdepth
, 20);
285 const char *host
= host_name
;
286 char *path
= NULL
, *host_ptr
;
288 while ((host_ptr
= strchr(host
, '.'))) {
290 /* check max depth */
291 if (++depth
> maxdepth
) {
293 "DomainTree: maxdepth exceeded (%ld)", maxdepth
299 if (host_ptr
- host
) {
302 if ((DT
->stripwww
> 0) && (depth
== 1) && (!strncmp(host
, "www.", sizeof("www")))) {
304 "DomainTree: strip www"
307 path
= apr_pstrcat(pool
, apr_pstrndup(pool
, host
, host_ptr
- host
), "/", path
, NULL
);
314 /* append last part if any and duplicate full path */
316 path
= apr_pstrcat(pool
, host
, "/", path
, NULL
);
320 "DomainTree: path name = %s for %s", path
, host_name
326 local
void domaintree_fake(apr_pool_t
*pool
, MDT_CNF
*DT
, char **path
)
330 apr_pool_t
*local_pool
;
331 const apr_array_header_t
*header
= apr_table_elts(DT
->aliases
.faketable
);
332 apr_table_entry_t
*array
= (apr_table_entry_t
*) header
->elts
;
334 if (APR_SUCCESS
!= apr_pool_create(&local_pool
, pool
)) {
341 if (recurlevel
++ > DT
->aliases
.recursion
) {
343 "DomainTree: maximum alias recursion level (%ld) exceeded! "
344 "Check if you have recursive definitions of DomainTreeAlias directives.",
345 DT
->aliases
.recursion
350 for (i
= 0; i
< header
->nelts
; ++i
) {
351 const char *begin
, *end
;
354 "DomainTree: fake test = %s on %s", array
[i
].key
, *path
357 if (strmatch(array
[i
].key
, *path
, &begin
, &end
)) {
359 *path
= apr_pstrcat(local_pool
, "/", apr_pstrndup(local_pool
, *path
, begin
- *path
), "/", array
[i
].val
, "/", end
, NULL
);
362 "DomainTree: fake done = %s (%s <> %s)", *path
, array
[i
].key
, array
[i
].val
366 } while (more
&& (DT
->aliases
.recursion
> 0));
368 *path
= apr_pstrdup(pool
, struniqchr(*path
, '/'));
370 apr_pool_destroy(local_pool
);
373 local
char *domaintree_cache_get(MDT_CNF
*DT
, apr_time_t atime
, const char *host
)
376 dircache_entry_t
*cache_entry
;
378 apr_global_mutex_lock(DT
->dircache
.lock
);
380 if ((cache_entry
= apr_hash_get(DT
->dircache
.hmap
, host
, APR_HASH_KEY_STRING
))) {
381 cache_entry
->lacc
= atime
;
382 path
= cache_entry
->path
;
385 apr_global_mutex_unlock(DT
->dircache
.lock
);
389 "DomainTree: cache hit = %s for %s", path
, host
396 local
void domaintree_cache_set(MDT_CNF
*DT
, apr_time_t atime
, const char *host
, const char *path
)
399 dircache_entry_t
*cache_entry
;
401 apr_pool_create(&pool
, DT
->dircache
.pool
);
402 cache_entry
= apr_palloc(pool
, sizeof(dircache_entry_t
));
404 cache_entry
->pool
= pool
;
405 cache_entry
->lacc
= atime
;
406 cache_entry
->host
= apr_pstrdup(pool
, host
);
407 cache_entry
->path
= apr_pstrdup(pool
, path
);
409 apr_global_mutex_lock(DT
->dircache
.lock
);
411 if (apr_hash_count(DT
->dircache
.hmap
) >= DT
->dircache
.clim
) {
412 apr_hash_index_t
*idx
;
413 dircache_entry_t
*purge_this
= NULL
;
416 "DomainTree: reached cache limit (%ld)", DT
->dircache
.clim
419 for (idx
= apr_hash_first(DT
->dircache
.pool
, DT
->dircache
.hmap
); idx
; idx
= apr_hash_next(idx
)) {
420 dircache_entry_t
*current
;
422 apr_hash_this(idx
, NULL
, NULL
, (void *) ¤t
);
423 if ((!purge_this
) || (purge_this
->lacc
> current
->lacc
)) {
424 purge_this
= current
;
430 "DomainTree: cache del = %s", purge_this
->host
432 apr_hash_set(DT
->dircache
.hmap
, purge_this
->host
, APR_HASH_KEY_STRING
, NULL
);
433 apr_pool_destroy(purge_this
->pool
);
436 apr_hash_set(DT
->dircache
.hmap
, cache_entry
->host
, APR_HASH_KEY_STRING
, cache_entry
);
438 apr_global_mutex_unlock(DT
->dircache
.lock
);
441 "DomainTree: cache set = %s for %s", path
, host
448 static STATUS
domaintree_hook_post_config(apr_pool_t
*pconf
, apr_pool_t
*plog
, apr_pool_t
*ptemp
, server_rec
*s
)
450 ap_add_version_component(pconf
, "MDT/" VERSION
);
454 static STATUS
domaintree_hook_translate_name(request_rec
*r
)
457 char *host
, *path
, *docroot
;
459 if ((!(DT
= GET_MDT_CNF(r
->server
))) || (DT
->enabled
< 1)) {
464 "DomainTree: processid = %d", (int) getpid()
467 /* get a usable host name */
468 if (!(host
= domaintree_host(r
->pool
, DT
, ap_get_server_name(r
)))) {
473 if ((DT
->dircache
.clim
< 1) || (!(path
= domaintree_cache_get(DT
, r
->request_time
, host
)))) {
475 if (!(path
= domaintree_path(r
->pool
, DT
, host
))) {
479 /* apply any aliases */
480 if (apr_table_elts(DT
->aliases
.faketable
)->nelts
) {
481 domaintree_fake(r
->pool
, DT
, &path
);
485 if (DT
->dircache
.clim
) {
486 domaintree_cache_set(DT
, r
->request_time
, host
, path
);
490 /* compose virtual docroot */
491 docroot
= struniqchr(apr_pstrcat(r
->pool
, DT
->prefix
, "/", path
, "/", DT
->suffix
, "/", NULL
), '/');
494 if (DT
->statroot
> 0) {
497 switch (apr_stat(&sb
, docroot
, APR_FINFO_MIN
, r
->pool
))
502 "DomainTree: stat path = %s (success)", docroot
508 "DomainTree: stat path = %s (failure)", docroot
515 /* set virtual docroot */
516 apr_table_set(r
->subprocess_env
, "VIRTUAL_DOCUMENT_ROOT", docroot
);
519 r
->canonical_filename
= "";
520 r
->filename
= apr_pstrcat(r
->pool
, docroot
, EMPTY(r
->uri
) ? NULL
: ('/' == *r
->uri
? r
->uri
+ 1 : r
->uri
), NULL
);
523 "DomainTree: path done = %s", r
->filename
529 static void domaintree_hooks(apr_pool_t
*pool
)
531 static const char * const pre
[] = {"mod_alias.c", "mod_userdir.c", NULL
};
533 ap_hook_post_config(domaintree_hook_post_config
, NULL
, NULL
, APR_HOOK_MIDDLE
);
534 ap_hook_translate_name(domaintree_hook_translate_name
, pre
, NULL
, APR_HOOK_FIRST
);
538 /* {{{ Configuration */
540 static void *domaintree_create_srv(apr_pool_t
*p
, server_rec
*s
)
542 MDT_CNF
*DT
= (MDT_CNF
*) apr_palloc(p
, sizeof(MDT_CNF
));
550 DT
->prefix
= "/var/www";
551 DT
->suffix
= "public_html";
553 DT
->aliases
.recursion
= -1;
554 DT
->aliases
.faketable
= apr_table_make(p
, 0);
556 DT
->dircache
.clim
= -1;
557 DT
->dircache
.hmap
= apr_hash_make(p
);
558 apr_pool_create(&DT
->dircache
.pool
, p
);
559 apr_global_mutex_create(&DT
->dircache
.lock
, __FILE__
, APR_LOCK_DEFAULT
, p
);
564 static void *domaintree_merge_srv(apr_pool_t
*p
, void *old_cfg_ptr
, void *new_cfg_ptr
)
566 MDT_CNF
*old_cfg
= (MDT_CNF
*) old_cfg_ptr
;
567 MDT_CNF
*new_cfg
= (MDT_CNF
*) new_cfg_ptr
;
568 MDT_CNF
*mrg_cfg
= (MDT_CNF
*) domaintree_create_srv(p
, new_cfg
->server
);
570 mrg_cfg
->enabled
= IF_SET_ELSE(new_cfg
->enabled
, old_cfg
->enabled
);
571 mrg_cfg
->stripwww
= IF_SET_ELSE(new_cfg
->stripwww
, old_cfg
->stripwww
);
572 mrg_cfg
->statroot
= IF_SET_ELSE(new_cfg
->statroot
, old_cfg
->statroot
);
573 mrg_cfg
->maxdepth
= IF_SET_ELSE(new_cfg
->maxdepth
, old_cfg
->maxdepth
);
575 mrg_cfg
->prefix
= EMPTY(new_cfg
->prefix
) ? EMPTY(old_cfg
->prefix
) ? "/var/www" : old_cfg
->prefix
: new_cfg
->prefix
;
576 mrg_cfg
->suffix
= EMPTY(new_cfg
->suffix
) ? EMPTY(old_cfg
->suffix
) ? "public_html" : old_cfg
->suffix
: new_cfg
->suffix
;
578 mrg_cfg
->aliases
.recursion
= IF_SET_ELSE(new_cfg
->aliases
.recursion
, old_cfg
->aliases
.recursion
);
579 mrg_cfg
->dircache
.clim
= IF_SET_ELSE(new_cfg
->dircache
.clim
, old_cfg
->dircache
.clim
);
584 static const char *domaintree_enable(cmd_parms
*cmd
, void *conf
, int flag
)
586 GET_MDT_CNF(cmd
->server
)->enabled
= flag
;
590 static const char *domaintree_stripwww(cmd_parms
*cmd
, void *conf
, int flag
)
592 GET_MDT_CNF(cmd
->server
)->stripwww
= flag
;
596 static const char *domaintree_statroot(cmd_parms
*cmd
, void *conf
, int flag
)
598 GET_MDT_CNF(cmd
->server
)->statroot
= flag
;
602 static const char *domaintree_prefix(cmd_parms
*cmd
, void *conf
, const char *prefix
)
604 GET_MDT_CNF(cmd
->server
)->prefix
= EMPTY(prefix
) ? "/" : trim(apr_pstrdup(cmd
->pool
, prefix
), strlen(prefix
), '/', TRIM_RIGHT
);
608 static const char *domaintree_suffix(cmd_parms
*cmd
, void *conf
, const char *suffix
)
610 GET_MDT_CNF(cmd
->server
)->suffix
= EMPTY(suffix
) ? "" : trim(apr_pstrdup(cmd
->pool
, suffix
), strlen(suffix
), '/', TRIM_BOTH
);
614 static const char *domaintree_maxdepth(cmd_parms
*cmd
, void *conf
, const char *max_depth
)
618 if ((depth
= atol(max_depth
))) {
620 GET_MDT_CNF(cmd
->server
)->maxdepth
= depth
;
622 return "Maximum DomainTree depth cannot be negative.";
629 static const char *domaintree_aliasrecursion(cmd_parms
*cmd
, void *conf
, const char *alias_recursion
)
633 if ((recursion
= atol(alias_recursion
))) {
634 if (recursion
> 0L) {
635 GET_MDT_CNF(cmd
->server
)->aliases
.recursion
= recursion
;
637 return "DomainTree alias recursion cannot be negative.";
644 static const char *domaintree_alias(cmd_parms
*cmd
, void *conf
, const char *fake
, const char *real
)
646 char *f
= strtr(apr_pstrdup(cmd
->pool
, fake
), '.', '/'), *r
= strtr(apr_pstrdup(cmd
->pool
, real
), '.', '/');
648 apr_table_set(GET_MDT_CNF(cmd
->server
)->aliases
.faketable
, trim(f
, strlen(f
), '/', TRIM_BOTH
), trim(r
, strlen(r
), '/', TRIM_BOTH
));
653 static const char *domaintree_cache(cmd_parms
*cmd
, void *conf
, const char *cache
)
657 if ((limit
= atol(cache
))) {
659 GET_MDT_CNF(cmd
->server
)->dircache
.clim
= limit
;
661 return "DomainTree cache limit cannot be negative.";
671 static command_rec domaintree_commands
[] = {
673 "DomainTreeEnabled", domaintree_enable
, NULL
, RSRC_CONF
,
674 "Turn the module on or off."
678 "DomainTreeStripWWW", domaintree_stripwww
, NULL
, RSRC_CONF
,
679 "Strip leading www from host. (default On)"
683 "DomainTreeStatRoot", domaintree_statroot
, NULL
, RSRC_CONF
,
684 "Wheter to check for the evaluated virtual document root with a stat call. (default Off)"
688 "DomainTreePrefix", domaintree_prefix
, NULL
, RSRC_CONF
,
689 "DomainTree path prefix. (default /var/www) Do not forget the leading slash!"
693 "DomainTreeSuffix", domaintree_suffix
, NULL
, RSRC_CONF
,
694 "DomainTree path suffix. (default public_html)"
698 "DomainTreeMaxdepth", domaintree_maxdepth
, NULL
, RSRC_CONF
,
699 "DomainTree max path depth. (default 20)"
703 "DomainTreeAliasRecursion", domaintree_aliasrecursion
, NULL
, RSRC_CONF
,
704 "Whether (and how often at the maximum) DomainTree should walk recursively "
705 "through the aliases list as long as matching aliases are found. (default: 0 = turned off)"
709 "DomainTreeAlias", domaintree_alias
, NULL
, RSRC_CONF
,
710 "DomainTree aliases; e.g. DomainTreeAlias com/example/tickets com/example/support (dots or slashes equal)"
714 "DomainTreeCache", domaintree_cache
, NULL
, RSRC_CONF
,
715 "DomainTree server-wide host to directory cache; specify how many cache entries to allow (default: 0 = turned off)"
722 /* {{{ Module Administrativa */
724 module AP_MODULE_DECLARE_DATA domaintree_module
= {
725 STANDARD20_MODULE_STUFF
,
726 NULL
, /* create per-dir */
727 NULL
, /* merge per-dir */
728 domaintree_create_srv
, /* create per-server */
729 domaintree_merge_srv
, /* merge per-server */
730 domaintree_commands
, /* config commands */
731 domaintree_hooks
/* hooks */
741 * vim600: noet sw=4 ts=4 fdm=marker
742 * vim<600: noet sw=4 ts=4