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/
51 #define MODULE "mod_domaintree"
52 #define AUTHOR "mike@php.net"
60 #include "apr_tables.h"
61 #include "apr_strings.h"
67 #define APR_WANT_MEMFUNC
68 #define APR_WANT_STRFUNC
72 #include "http_config.h"
73 #include "http_core.h"
75 #include "http_protocol.h"
76 #include "http_request.h"
79 /* {{{ domaintree_module */
81 module AP_MODULE_DECLARE_DATA domaintree_module
;
84 /* {{{ Macros & Types */
86 #define MOD_DT_CNF domaintree_conf
87 #define MOD_DT_PTR (&domaintree_module)
89 #define GET_MOD_DT_CNF(srv) ((MOD_DT_CNF *) ap_get_module_config((srv)->module_config, MOD_DT_PTR))
92 #define EMPTY(str) ((str == NULL) || (*(str) == NUL))
93 #define local static APR_INLINE
95 #define DT_LOG_ERR ap_log_error(APLOG_MARK, APLOG_ERR, APR_SUCCESS, DT->server,
96 #define DT_LOG_WRN ap_log_error(APLOG_MARK, APLOG_WARNING, APR_SUCCESS, DT->server,
97 #define DT_LOG_DBG ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, DT->server,
104 apr_table_t
*faketable
;
107 struct dircache_entry
{
118 apr_global_mutex_t
*lock
;
136 local
char *strtr(char *string
, char from
, char to
)
141 while ((ptr
= strchr(ptr
, from
))) {
150 #define TRIM_BOTH (TRIM_LEFT|TRIM_RIGHT)
151 local
char *trim(char *string
, size_t length
, char what
, int where
)
153 if (where
& TRIM_RIGHT
) {
154 while (length
-- && (string
[length
] == what
)) {
155 string
[length
] = NUL
;
158 if (where
& TRIM_LEFT
) {
159 while (*string
== what
) {
166 local
int strmatch(const char *match
, const char *string
, const char **begin
, const char **end
)
168 *begin
= *end
= NULL
;
175 while (*match
== '*' || *match
== '?') {
184 *end
= string
+ strlen(string
);
188 if (!(string
= strchr(string
, *match
))) {
203 if (*match
== *string
) {
223 local
char *struniqchr(char *string
, char uniq
)
228 if (*ptr
== uniq
&& *(ptr
+ 1) == uniq
) {
231 while (*(pos
+ 1) == uniq
) {
235 memmove(ptr
, pos
, strlen(pos
) + 1);
243 local
char *domaintree_host(apr_pool_t
*pool
, MOD_DT_CNF
*DT
, const char *host_name
)
248 if (EMPTY(host_name
)) {
250 "DomainTree: no host/server name"
256 host
= apr_pstrdup(pool
, host_name
);
258 /* check for :NN port */
259 if ((port
= strchr(host
, ':'))) {
266 /* strip leading & trailing dots */
267 host
= trim(host
, len
, '.', TRIM_BOTH
);
270 "DomainTree: host name = %s for %s", host
, host_name
276 local
char *domaintree_path(apr_pool_t
*pool
, MOD_DT_CNF
*DT
, const char *host_name
)
279 const char *host
= host_name
;
280 char *path
= NULL
, *host_ptr
;
282 while ((host_ptr
= strchr(host
, '.'))) {
284 /* check max depth */
285 if (++depth
> DT
->maxdepth
) {
287 "DomainTree: maxdepth exceeded (%ld)", DT
->maxdepth
293 if (host_ptr
- host
) {
296 if (DT
->stripwww
&& (depth
== 1) && (!strncmp(host
, "www.", sizeof("www")))) {
298 "DomainTree: strip www"
301 path
= apr_pstrcat(pool
, apr_pstrndup(pool
, host
, host_ptr
- host
), "/", path
, NULL
);
308 /* append last part if any and duplicate full path */
310 path
= apr_pstrcat(pool
, host
, "/", path
, NULL
);
314 "DomainTree: path name = %s for %s", path
, host_name
320 local
void domaintree_fake(apr_pool_t
*pool
, MOD_DT_CNF
*DT
, char **path
)
324 apr_pool_t
*local_pool
;
325 const apr_array_header_t
*header
= apr_table_elts(DT
->aliases
.faketable
);
326 apr_table_entry_t
*array
= (apr_table_entry_t
*) header
->elts
;
328 if (APR_SUCCESS
!= apr_pool_create(&local_pool
, pool
)) {
335 if (recurlevel
++ > DT
->aliases
.recursion
) {
337 "DomainTree: maximum alias recursion level (%ld) exceeded! "
338 "Check if you have recursive definitions of DomainTreeAlias directives.",
339 DT
->aliases
.recursion
344 for (i
= 0; i
< header
->nelts
; ++i
) {
345 const char *begin
, *end
;
348 "DomainTree: fake test = %s on %s", array
[i
].key
, *path
351 if (strmatch(array
[i
].key
, *path
, &begin
, &end
)) {
353 *path
= apr_pstrcat(local_pool
, "/", apr_pstrndup(local_pool
, *path
, begin
- *path
), "/", array
[i
].val
, "/", end
, NULL
);
356 "DomainTree: fake done = %s (%s <> %s)", *path
, array
[i
].key
, array
[i
].val
360 } while (more
&& DT
->aliases
.recursion
);
362 *path
= apr_pstrdup(pool
, struniqchr(*path
, '/'));
364 apr_pool_destroy(local_pool
);
367 local
char *domaintree_cache_get(MOD_DT_CNF
*DT
, apr_time_t atime
, const char *host
)
370 struct dircache_entry
*cache_entry
;
372 apr_global_mutex_lock(DT
->dircache
.lock
);
374 if ((cache_entry
= apr_hash_get(DT
->dircache
.hmap
, host
, APR_HASH_KEY_STRING
))) {
375 cache_entry
->lacc
= atime
;
376 path
= cache_entry
->path
;
379 apr_global_mutex_unlock(DT
->dircache
.lock
);
383 "DomainTree: cache hit = %s for %s", path
, host
390 local
void domaintree_cache_set(MOD_DT_CNF
*DT
, apr_time_t atime
, const char *host
, const char *path
)
393 struct dircache_entry
*cache_entry
;
395 apr_pool_create(&pool
, DT
->dircache
.pool
);
396 cache_entry
= apr_palloc(pool
, sizeof(struct dircache_entry
));
398 cache_entry
->pool
= pool
;
399 cache_entry
->lacc
= atime
;
400 cache_entry
->host
= apr_pstrdup(pool
, host
);
401 cache_entry
->path
= apr_pstrdup(pool
, path
);
403 apr_global_mutex_lock(DT
->dircache
.lock
);
405 if (apr_hash_count(DT
->dircache
.hmap
) >= DT
->dircache
.clim
) {
406 apr_hash_index_t
*idx
;
407 struct dircache_entry
*purge_this
= NULL
;
410 "DomainTree: reached cache limit (%ld)", DT
->dircache
.clim
413 for (idx
= apr_hash_first(DT
->dircache
.pool
, DT
->dircache
.hmap
); idx
; idx
= apr_hash_next(idx
)) {
414 struct dircache_entry
*current
;
416 apr_hash_this(idx
, NULL
, NULL
, (void **) ¤t
);
417 if ((!purge_this
) || (purge_this
->lacc
> current
->lacc
)) {
418 purge_this
= current
;
424 "DomainTree: cache del = %s", purge_this
->host
426 apr_hash_set(DT
->dircache
.hmap
, purge_this
->host
, APR_HASH_KEY_STRING
, NULL
);
427 apr_pool_destroy(purge_this
->pool
);
430 apr_hash_set(DT
->dircache
.hmap
, cache_entry
->host
, APR_HASH_KEY_STRING
, cache_entry
);
432 apr_global_mutex_unlock(DT
->dircache
.lock
);
435 "DomainTree: cache set = %s for %s", path
, host
442 static STATUS
domaintree_hook_post_config(apr_pool_t
*pconf
, apr_pool_t
*plog
, apr_pool_t
*ptemp
, server_rec
*s
)
444 ap_add_version_component(pconf
, "MDT/" VERSION
);
448 static STATUS
domaintree_hook_translate_name(request_rec
*r
)
450 MOD_DT_CNF
*DT
= NULL
;
451 char *host
, *path
, *docroot
;
453 if ((!(DT
= GET_MOD_DT_CNF(r
->server
))) || (!DT
->enabled
)) {
458 "DomainTree: processid = %d", (int) getpid()
461 /* get a usable host name */
462 if (!(host
= domaintree_host(r
->pool
, DT
, ap_get_server_name(r
)))) {
467 if ((!DT
->dircache
.clim
) || (!(path
= domaintree_cache_get(DT
, r
->request_time
, host
)))) {
469 if (!(path
= domaintree_path(r
->pool
, DT
, host
))) {
473 /* apply any aliases */
474 if (apr_table_elts(DT
->aliases
.faketable
)->nelts
) {
475 domaintree_fake(r
->pool
, DT
, &path
);
479 if (DT
->dircache
.clim
) {
480 domaintree_cache_set(DT
, r
->request_time
, host
, path
);
484 /* compose virtual docroot */
485 docroot
= struniqchr(apr_pstrcat(r
->pool
, DT
->prefix
, "/", path
, "/", DT
->suffix
, "/", NULL
), '/');
491 switch (apr_stat(&sb
, docroot
, APR_FINFO_MIN
, r
->pool
))
496 "DomainTree: stat path = %s (success)", docroot
502 "DomainTree: stat path = %s (failure)", docroot
509 /* set virtual docroot */
510 apr_table_set(r
->subprocess_env
, "VIRTUAL_DOCUMENT_ROOT", docroot
);
513 r
->canonical_filename
= "";
514 r
->filename
= apr_pstrcat(r
->pool
, docroot
, EMPTY(r
->uri
) ? NULL
: ('/' == *r
->uri
? r
->uri
+ 1 : r
->uri
), NULL
);
517 "DomainTree: path done = %s", r
->filename
523 static void domaintree_hooks(apr_pool_t
*pool
)
525 static const char * const pre
[] = {"mod_alias.c", "mod_userdir.c", NULL
};
527 ap_hook_post_config(domaintree_hook_post_config
, NULL
, NULL
, APR_HOOK_MIDDLE
);
528 ap_hook_translate_name(domaintree_hook_translate_name
, pre
, NULL
, APR_HOOK_FIRST
);
532 /* {{{ Configuration */
534 static void *domaintree_create_srv(apr_pool_t
*p
, server_rec
*s
)
538 DT
= (MOD_DT_CNF
*) apr_palloc(p
, sizeof(MOD_DT_CNF
));
546 DT
->prefix
= "/var/www";
547 DT
->suffix
= "public_html";
549 DT
->aliases
.recursion
= 0;
550 DT
->aliases
.faketable
= apr_table_make(p
, 0);
552 DT
->dircache
.clim
= 0;
553 DT
->dircache
.hmap
= apr_hash_make(p
);
554 apr_pool_create(&DT
->dircache
.pool
, p
);
555 apr_global_mutex_create(&DT
->dircache
.lock
, __FILE__
, APR_LOCK_DEFAULT
, p
);
560 static const char *domaintree_enable(cmd_parms
*cmd
, void *conf
, int flag
)
562 GET_MOD_DT_CNF(cmd
->server
)->enabled
= flag
;
566 static const char *domaintree_stripwww(cmd_parms
*cmd
, void *conf
, int flag
)
568 GET_MOD_DT_CNF(cmd
->server
)->stripwww
= flag
;
572 static const char *domaintree_statroot(cmd_parms
*cmd
, void *conf
, int flag
)
574 GET_MOD_DT_CNF(cmd
->server
)->statroot
= flag
;
578 static const char *domaintree_prefix(cmd_parms
*cmd
, void *conf
, const char *prefix
)
580 GET_MOD_DT_CNF(cmd
->server
)->prefix
= EMPTY(prefix
) ? "/" : trim(apr_pstrdup(cmd
->pool
, prefix
), strlen(prefix
), '/', TRIM_RIGHT
);
584 static const char *domaintree_suffix(cmd_parms
*cmd
, void *conf
, const char *suffix
)
586 GET_MOD_DT_CNF(cmd
->server
)->suffix
= EMPTY(suffix
) ? "" : trim(apr_pstrdup(cmd
->pool
, suffix
), strlen(suffix
), '/', TRIM_BOTH
);
590 static const char *domaintree_maxdepth(cmd_parms
*cmd
, void *conf
, const char *max_depth
)
594 if ((depth
= atol(max_depth
))) {
596 GET_MOD_DT_CNF(cmd
->server
)->maxdepth
= depth
;
598 return "Maximum DomainTree depth cannot be negative.";
605 static const char *domaintree_aliasrecursion(cmd_parms
*cmd
, void *conf
, const char *alias_recursion
)
609 if ((recursion
= atol(alias_recursion
))) {
610 if (recursion
> 0L) {
611 GET_MOD_DT_CNF(cmd
->server
)->aliases
.recursion
= recursion
;
613 return "DomainTree alias recursion cannot be negative.";
620 static const char *domaintree_alias(cmd_parms
*cmd
, void *conf
, const char *fake
, const char *real
)
622 char *f
= strtr(apr_pstrdup(cmd
->pool
, fake
), '.', '/'), *r
= strtr(apr_pstrdup(cmd
->pool
, real
), '.', '/');
624 apr_table_set(GET_MOD_DT_CNF(cmd
->server
)->aliases
.faketable
, trim(f
, strlen(f
), '/', TRIM_BOTH
), trim(r
, strlen(r
), '/', TRIM_BOTH
));
629 static const char *domaintree_cache(cmd_parms
*cmd
, void *conf
, const char *cache
)
633 if ((limit
= atol(cache
))) {
635 GET_MOD_DT_CNF(cmd
->server
)->dircache
.clim
= limit
;
637 return "DomainTree cache limit cannot be negative.";
647 static command_rec domaintree_commands
[] = {
649 "DomainTreeEnabled", domaintree_enable
, NULL
, RSRC_CONF
,
650 "Turn the module on or off."
654 "DomainTreeStripWWW", domaintree_stripwww
, NULL
, RSRC_CONF
,
655 "Strip leading www from host. (default On)"
659 "DomainTreeStatRoot", domaintree_statroot
, NULL
, RSRC_CONF
,
660 "Wheter to check for the evaluated virtual document root with a stat call. (default Off)"
664 "DomainTreePrefix", domaintree_prefix
, NULL
, RSRC_CONF
,
665 "DomainTree path prefix. (default /var/www) Do not forget the leading slash!"
669 "DomainTreeSuffix", domaintree_suffix
, NULL
, RSRC_CONF
,
670 "DomainTree path suffix. (default public_html)"
674 "DomainTreeMaxdepth", domaintree_maxdepth
, NULL
, RSRC_CONF
,
675 "DomainTree max path depth. (default 20)"
679 "DomainTreeAliasRecursion", domaintree_aliasrecursion
, NULL
, RSRC_CONF
,
680 "Whether (and how often at the maximum) DomainTree should walk recursively "
681 "through the aliases list as long as matching aliases are found. (default: 0 = turned off)"
685 "DomainTreeAlias", domaintree_alias
, NULL
, RSRC_CONF
,
686 "DomainTree aliases; e.g. DomainTreeAlias com/example/tickets com/example/support (dots or slashes equal)"
690 "DomainTreeCache", domaintree_cache
, NULL
, RSRC_CONF
,
691 "DomainTree server-wide host to directory cache; specify how many cache entries to allow (default: 0 = turned off)"
698 /* {{{ Module Administrativa */
700 module AP_MODULE_DECLARE_DATA domaintree_module
= {
701 STANDARD20_MODULE_STUFF
,
702 NULL
, /* create per-dir */
703 NULL
, /* merge per-dir */
704 domaintree_create_srv
, /* create per-server */
705 NULL
, /* merge per-server */
706 domaintree_commands
, /* config commands */
707 domaintree_hooks
/* hooks */
717 * vim600: noet sw=4 ts=4 fdm=marker
718 * vim<600: noet sw=4 ts=4