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 */
92 #define MDT_CNF domaintree_conf
93 #define MDT_PTR (&domaintree_module)
95 #define GET_MDT_CNF(srv) ((MDT_CNF *) ap_get_module_config((srv)->module_config, MDT_PTR))
97 #define IF_SET_ELSE(a, b) (a != -1) ? (a) : (b)
100 #define EMPTY(str) ((str == NULL) || (*(str) == NUL))
102 # define local static
104 # define local static APR_INLINE
107 #define DT_LOG_ERR ap_log_error(APLOG_MARK, APLOG_ERR, APR_SUCCESS, DT->server, "DomainTree: "
108 #define DT_LOG_WRN ap_log_error(APLOG_MARK, APLOG_WARNING, APR_SUCCESS, DT->server, "DomainTree: "
109 #define DT_LOG_DBG ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, DT->server, "DomainTree: "
110 #define DT_LOG_END );
116 apr_table_t
*faketable
;
130 apr_global_mutex_t
*lock
;
148 local
char *strtr(char *string
, char from
, char to
)
153 while ((ptr
= strchr(ptr
, from
))) {
162 #define TRIM_BOTH (TRIM_LEFT|TRIM_RIGHT)
163 local
char *trim(char *string
, size_t length
, char what
, int where
)
165 if (where
& TRIM_RIGHT
) {
166 while (length
-- && (string
[length
] == what
)) {
167 string
[length
] = NUL
;
170 if (where
& TRIM_LEFT
) {
171 while (*string
== what
) {
178 local
int strmatch(const char *match
, const char *string
, const char **begin
, const char **end
)
180 *begin
= *end
= NULL
;
187 while (*match
== '*' || *match
== '?') {
196 *end
= string
+ strlen(string
);
200 if (!(string
= strchr(string
, *match
))) {
215 if (*match
== *string
) {
235 local
char *struniqchr(char *string
, char uniq
)
240 if (ptr
[0] == uniq
&& ptr
[1] == uniq
) {
243 for (; pos
[1] == uniq
; ++pos
);
244 for (; pos
[0]; ++pos
) {
254 local
char *domaintree_host(apr_pool_t
*pool
, MDT_CNF
*DT
, const char *host_name
)
259 if (EMPTY(host_name
)) {
261 "no host/server name"
267 host
= apr_pstrdup(pool
, host_name
);
269 /* check for :NN port */
270 if ((port
= strchr(host
, ':'))) {
277 /* strip leading & trailing dots */
278 host
= trim(host
, len
, '.', TRIM_BOTH
);
281 "host name = %s for %s", host
, host_name
287 local
char *domaintree_path(apr_pool_t
*pool
, MDT_CNF
*DT
, const char *host_name
)
289 long depth
= 0, maxdepth
= IF_SET_ELSE(DT
->maxdepth
, 20);
290 const char *host
= host_name
;
291 char *path
= NULL
, *host_ptr
;
293 while ((host_ptr
= strchr(host
, '.'))) {
295 /* check max depth */
296 if (++depth
> maxdepth
) {
298 "maxdepth exceeded (%ld)", maxdepth
304 if (host_ptr
- host
) {
307 if ((DT
->stripwww
> 0) && (depth
== 1) && (!strncmp(host
, "www.", sizeof("www")))) {
312 path
= apr_pstrcat(pool
, apr_pstrndup(pool
, host
, host_ptr
- host
), "/", path
, NULL
);
319 /* append last part if any and duplicate full path */
321 path
= apr_pstrcat(pool
, host
, "/", path
, NULL
);
325 "path name = %s for %s", path
, host_name
331 local
void domaintree_fake(apr_pool_t
*pool
, MDT_CNF
*DT
, char **path
)
335 apr_pool_t
*local_pool
;
336 const apr_array_header_t
*header
= apr_table_elts(DT
->aliases
.faketable
);
337 apr_table_entry_t
*array
= (apr_table_entry_t
*) header
->elts
;
339 if (APR_SUCCESS
!= apr_pool_create(&local_pool
, pool
)) {
346 if (recurlevel
++ > DT
->aliases
.recursion
) {
348 "maximum alias recursion level (%ld) exceeded! "
349 "Check if you have recursive definitions of DomainTreeAlias directives.",
350 DT
->aliases
.recursion
355 for (i
= 0; i
< header
->nelts
; ++i
) {
356 const char *begin
, *end
;
359 "fake test = %s on %s", array
[i
].key
, *path
362 if (strmatch(array
[i
].key
, *path
, &begin
, &end
)) {
364 *path
= apr_pstrcat(local_pool
, "/", apr_pstrndup(local_pool
, *path
, begin
- *path
), "/", array
[i
].val
, "/", end
, NULL
);
367 "fake done = %s (%s <> %s)", *path
, array
[i
].key
, array
[i
].val
371 } while (more
&& (DT
->aliases
.recursion
> 0));
373 *path
= apr_pstrdup(pool
, struniqchr(*path
, '/'));
375 apr_pool_destroy(local_pool
);
378 local
char *domaintree_cache_get(MDT_CNF
*DT
, apr_time_t atime
, const char *host
)
381 dircache_entry_t
*cache_entry
;
383 apr_global_mutex_lock(DT
->dircache
.lock
);
385 if ((cache_entry
= apr_hash_get(DT
->dircache
.hmap
, host
, APR_HASH_KEY_STRING
))) {
386 cache_entry
->lacc
= atime
;
387 path
= cache_entry
->path
;
390 apr_global_mutex_unlock(DT
->dircache
.lock
);
394 "cache hit = %s for %s", path
, host
401 local
void domaintree_cache_set(MDT_CNF
*DT
, apr_time_t atime
, const char *host
, const char *path
)
404 dircache_entry_t
*cache_entry
;
406 apr_pool_create(&pool
, DT
->dircache
.pool
);
407 cache_entry
= apr_palloc(pool
, sizeof(dircache_entry_t
));
409 cache_entry
->pool
= pool
;
410 cache_entry
->lacc
= atime
;
411 cache_entry
->host
= apr_pstrdup(pool
, host
);
412 cache_entry
->path
= apr_pstrdup(pool
, path
);
414 apr_global_mutex_lock(DT
->dircache
.lock
);
416 if (apr_hash_count(DT
->dircache
.hmap
) >= DT
->dircache
.clim
) {
417 apr_hash_index_t
*idx
;
418 dircache_entry_t
*purge_this
= NULL
;
421 "reached cache limit (%ld)", DT
->dircache
.clim
424 for (idx
= apr_hash_first(DT
->dircache
.pool
, DT
->dircache
.hmap
); idx
; idx
= apr_hash_next(idx
)) {
425 dircache_entry_t
*current
;
427 apr_hash_this(idx
, NULL
, NULL
, (void *) ¤t
);
428 if ((!purge_this
) || (purge_this
->lacc
> current
->lacc
)) {
429 purge_this
= current
;
435 "cache del = %s", purge_this
->host
437 apr_hash_set(DT
->dircache
.hmap
, purge_this
->host
, APR_HASH_KEY_STRING
, NULL
);
438 apr_pool_destroy(purge_this
->pool
);
441 apr_hash_set(DT
->dircache
.hmap
, cache_entry
->host
, APR_HASH_KEY_STRING
, cache_entry
);
443 apr_global_mutex_unlock(DT
->dircache
.lock
);
446 "cache set = %s for %s", path
, host
453 static STATUS
domaintree_hook_post_config(apr_pool_t
*pconf
, apr_pool_t
*plog
, apr_pool_t
*ptemp
, server_rec
*s
)
455 ap_add_version_component(pconf
, "MDT/" VERSION
);
459 static STATUS
domaintree_hook_translate_name(request_rec
*r
)
462 char *host
, *path
, *docroot
;
464 if ((!(DT
= GET_MDT_CNF(r
->server
))) || (DT
->enabled
< 1)) {
470 "processid = %d", (int) getpid()
474 /* get a usable host name */
475 if (!(host
= domaintree_host(r
->pool
, DT
, ap_get_server_name(r
)))) {
480 if ((DT
->dircache
.clim
< 1) || (!(path
= domaintree_cache_get(DT
, r
->request_time
, host
)))) {
482 if (!(path
= domaintree_path(r
->pool
, DT
, host
))) {
486 /* apply any aliases */
487 if (apr_table_elts(DT
->aliases
.faketable
)->nelts
) {
488 domaintree_fake(r
->pool
, DT
, &path
);
492 if (DT
->dircache
.clim
> 0) {
493 domaintree_cache_set(DT
, r
->request_time
, host
, path
);
497 /* compose virtual docroot */
498 docroot
= struniqchr(apr_pstrcat(r
->pool
, DT
->prefix
, "/", path
, "/", DT
->suffix
, "/", NULL
), '/');
501 if (DT
->statroot
> 0) {
504 switch (apr_stat(&sb
, docroot
, APR_FINFO_MIN
, r
->pool
))
509 "stat path = %s (success)", docroot
515 "stat path = %s (failure)", docroot
522 /* set virtual docroot */
523 apr_table_set(r
->subprocess_env
, "VIRTUAL_DOCUMENT_ROOT", docroot
);
526 r
->canonical_filename
= "";
527 r
->filename
= apr_pstrcat(r
->pool
, docroot
, EMPTY(r
->uri
) ? NULL
: ('/' == *r
->uri
? r
->uri
+ 1 : r
->uri
), NULL
);
530 "path done = %s", r
->filename
536 static void domaintree_hooks(apr_pool_t
*pool
)
538 static const char * const pre
[] = {"mod_alias.c", "mod_userdir.c", NULL
};
540 ap_hook_post_config(domaintree_hook_post_config
, NULL
, NULL
, APR_HOOK_MIDDLE
);
541 ap_hook_translate_name(domaintree_hook_translate_name
, pre
, NULL
, APR_HOOK_FIRST
);
545 /* {{{ Configuration */
547 static void *domaintree_create_srv(apr_pool_t
*p
, server_rec
*s
)
549 MDT_CNF
*DT
= (MDT_CNF
*) apr_palloc(p
, sizeof(MDT_CNF
));
557 DT
->prefix
= "/var/www";
558 DT
->suffix
= "public_html";
560 DT
->aliases
.recursion
= -1;
561 DT
->aliases
.faketable
= apr_table_make(p
, 0);
563 DT
->dircache
.clim
= -1;
564 DT
->dircache
.hmap
= apr_hash_make(p
);
565 apr_pool_create(&DT
->dircache
.pool
, p
);
566 apr_global_mutex_create(&DT
->dircache
.lock
, __FILE__
, APR_LOCK_DEFAULT
, p
);
568 fprintf(stderr
, "MDT: cfg create %p\n", DT
);
573 static void *domaintree_merge_srv(apr_pool_t
*p
, void *old_cfg_ptr
, void *new_cfg_ptr
)
575 MDT_CNF
*old_cfg
= (MDT_CNF
*) old_cfg_ptr
, *new_cfg
= (MDT_CNF
*) new_cfg_ptr
;
576 MDT_CNF
*DT
= (MDT_CNF
*) apr_palloc(p
, sizeof(MDT_CNF
));
578 DT
->server
= new_cfg
->server
;
579 DT
->enabled
= IF_SET_ELSE(new_cfg
->enabled
, old_cfg
->enabled
);
580 DT
->stripwww
= IF_SET_ELSE(new_cfg
->stripwww
, old_cfg
->stripwww
);
581 DT
->statroot
= IF_SET_ELSE(new_cfg
->statroot
, old_cfg
->statroot
);
582 DT
->maxdepth
= IF_SET_ELSE(new_cfg
->maxdepth
, old_cfg
->maxdepth
);
584 DT
->prefix
= EMPTY(new_cfg
->prefix
) ? EMPTY(old_cfg
->prefix
) ? "/var/www" : old_cfg
->prefix
: new_cfg
->prefix
;
585 DT
->suffix
= EMPTY(new_cfg
->suffix
) ? EMPTY(old_cfg
->suffix
) ? "public_html" : old_cfg
->suffix
: new_cfg
->suffix
;
587 DT
->aliases
.recursion
= IF_SET_ELSE(new_cfg
->aliases
.recursion
, old_cfg
->aliases
.recursion
);
588 DT
->aliases
.faketable
= apr_table_overlay(p
, new_cfg
->aliases
.faketable
, old_cfg
->aliases
.faketable
);
590 DT
->dircache
.clim
= IF_SET_ELSE(new_cfg
->dircache
.clim
, old_cfg
->dircache
.clim
);
591 DT
->dircache
.hmap
= apr_hash_overlay(p
, new_cfg
->dircache
.hmap
, old_cfg
->dircache
.hmap
);
592 apr_global_mutex_create(&new_cfg
->dircache
.lock
, __FILE__
, APR_LOCK_DEFAULT
, p
);
594 fprintf(stderr
, "MDT: cfg merge %p + %p = %p\n", old_cfg
, new_cfg
, DT
);
599 static const char *domaintree_enable(cmd_parms
*cmd
, void *conf
, int flag
)
601 GET_MDT_CNF(cmd
->server
)->enabled
= flag
;
605 static const char *domaintree_stripwww(cmd_parms
*cmd
, void *conf
, int flag
)
607 GET_MDT_CNF(cmd
->server
)->stripwww
= flag
;
611 static const char *domaintree_statroot(cmd_parms
*cmd
, void *conf
, int flag
)
613 GET_MDT_CNF(cmd
->server
)->statroot
= flag
;
617 static const char *domaintree_prefix(cmd_parms
*cmd
, void *conf
, const char *prefix
)
619 GET_MDT_CNF(cmd
->server
)->prefix
= EMPTY(prefix
) ? "/" : trim(apr_pstrdup(cmd
->pool
, prefix
), strlen(prefix
), '/', TRIM_RIGHT
);
623 static const char *domaintree_suffix(cmd_parms
*cmd
, void *conf
, const char *suffix
)
625 GET_MDT_CNF(cmd
->server
)->suffix
= EMPTY(suffix
) ? "" : trim(apr_pstrdup(cmd
->pool
, suffix
), strlen(suffix
), '/', TRIM_BOTH
);
629 static const char *domaintree_maxdepth(cmd_parms
*cmd
, void *conf
, const char *max_depth
)
633 if ((depth
= atol(max_depth
))) {
635 GET_MDT_CNF(cmd
->server
)->maxdepth
= depth
;
637 return "Maximum DomainTree depth cannot be negative.";
644 static const char *domaintree_aliasrecursion(cmd_parms
*cmd
, void *conf
, const char *alias_recursion
)
648 if ((recursion
= atol(alias_recursion
))) {
649 if (recursion
>= 0L) {
650 GET_MDT_CNF(cmd
->server
)->aliases
.recursion
= recursion
;
652 return "DomainTree alias recursion cannot be negative.";
659 static const char *domaintree_alias(cmd_parms
*cmd
, void *conf
, const char *fake
, const char *real
)
661 char *f
= strtr(apr_pstrdup(cmd
->pool
, fake
), '.', '/'), *r
= strtr(apr_pstrdup(cmd
->pool
, real
), '.', '/');
663 apr_table_set(GET_MDT_CNF(cmd
->server
)->aliases
.faketable
, trim(f
, strlen(f
), '/', TRIM_BOTH
), trim(r
, strlen(r
), '/', TRIM_BOTH
));
668 static const char *domaintree_cache(cmd_parms
*cmd
, void *conf
, const char *cache
)
672 if ((limit
= atol(cache
))) {
674 GET_MDT_CNF(cmd
->server
)->dircache
.clim
= limit
;
676 return "DomainTree cache limit cannot be negative.";
686 static command_rec domaintree_commands
[] = {
688 "DomainTreeEnabled", domaintree_enable
, NULL
, RSRC_CONF
,
689 "Turn the module on or off."
693 "DomainTreeStripWWW", domaintree_stripwww
, NULL
, RSRC_CONF
,
694 "Strip leading www from host. (default On)"
698 "DomainTreeStatRoot", domaintree_statroot
, NULL
, RSRC_CONF
,
699 "Wheter to check for the evaluated virtual document root with a stat call. (default Off)"
703 "DomainTreePrefix", domaintree_prefix
, NULL
, RSRC_CONF
,
704 "DomainTree path prefix. (default /var/www) Do not forget the leading slash!"
708 "DomainTreeSuffix", domaintree_suffix
, NULL
, RSRC_CONF
,
709 "DomainTree path suffix. (default public_html)"
713 "DomainTreeMaxdepth", domaintree_maxdepth
, NULL
, RSRC_CONF
,
714 "DomainTree max path depth. (default 20)"
718 "DomainTreeAliasRecursion", domaintree_aliasrecursion
, NULL
, RSRC_CONF
,
719 "Whether (and how often at the maximum) DomainTree should walk recursively "
720 "through the aliases list as long as matching aliases are found. (default: 0 = turned off)"
724 "DomainTreeAlias", domaintree_alias
, NULL
, RSRC_CONF
,
725 "DomainTree aliases; e.g. DomainTreeAlias com/example/tickets com/example/support (dots or slashes equal)"
729 "DomainTreeCache", domaintree_cache
, NULL
, RSRC_CONF
,
730 "DomainTree server-wide host to directory cache; specify how many cache entries to allow (default: 0 = turned off)"
737 /* {{{ Module Administrativa */
739 module AP_MODULE_DECLARE_DATA domaintree_module
= {
740 STANDARD20_MODULE_STUFF
,
741 NULL
, /* create per-dir */
742 NULL
, /* merge per-dir */
743 domaintree_create_srv
, /* create per-server */
744 domaintree_merge_srv
, /* merge per-server */
745 domaintree_commands
, /* config commands */
746 domaintree_hooks
/* hooks */
756 * vim600: noet sw=4 ts=4 fdm=marker
757 * vim<600: noet sw=4 ts=4