From e21543024f50e28e789dc47a6bd7d00f72a7d67b Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Fri, 10 Jun 2005 20:16:21 +0000 Subject: [PATCH] - added DomainTree aliases --- mod_domaintree.c | 226 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 170 insertions(+), 56 deletions(-) diff --git a/mod_domaintree.c b/mod_domaintree.c index 8bfad38..a67698e 100644 --- a/mod_domaintree.c +++ b/mod_domaintree.c @@ -17,7 +17,7 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * + * *
  * DomainTreeEnabled On
  * DomainTreeMaxdepth 25
@@ -47,7 +47,7 @@
 
 #define MODULE	"mod_domaintree"
 #define AUTHOR	"mike@php.net"
-#define VERSION "1.0"
+#define VERSION "1.1"
 
 /* {{{ Includes */
 
@@ -65,6 +65,7 @@
 #include "apr.h"
 #include "apr_lib.h"
 #include "apr_ring.h"
+#include "apr_hash.h"
 #include "apr_strings.h"
 
 #define APR_WANT_STRFUNC
@@ -86,6 +87,11 @@ module AP_MODULE_DECLARE_DATA domaintree_module;
 
 typedef int STATUS;
 
+typedef struct {
+	apr_hash_t	*hashtable;
+	size_t		recursion;
+} aliases_t;
+
 typedef struct {
 	server_rec	*server;
 	int			enabled;
@@ -93,6 +99,7 @@ typedef struct {
 	size_t		maxdepth;
 	char		*prefix;
 	char		*suffix;
+	aliases_t	aliases;
 } domaintree_conf;
 
 struct domaintree_entry {
@@ -104,6 +111,33 @@ APR_RING_HEAD(domaintree, domaintree_entry);
 /* }}} */
 /* {{{ Helpers */
 
+static APR_INLINE char *strtr(char *string, char from, char to)
+{
+	char *ptr = string;
+	
+	if (from != to) {
+		while ((ptr = strchr(ptr, from))) {
+			*ptr = to;
+		}
+	}
+	return string;
+}
+
+static APR_INLINE char *trim(char *string, size_t length, char what, int l, int r)
+{
+	if (r) {
+		while (length-- && (string[length] == what)) {
+			string[length] = NUL;
+		}
+	}
+	if (l) {
+		while (*string == what) {
+			++string;
+		}
+	}
+	return string;
+}
+
 static APR_INLINE char *domaintree_host(apr_pool_t *pool, MOD_DT_CNF *DT, const char *name)
 {
 	if (EMPTY(name)) {
@@ -112,45 +146,37 @@ static APR_INLINE char *domaintree_host(apr_pool_t *pool, MOD_DT_CNF *DT, const
 	} else {
 		size_t len;
 		char *port, *ptr, *host;
-
+		
 		ptr = host = apr_pstrdup(pool, name);
 		ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, DT->server, "DomainTree: host name = %s", host);
-
+		
 		/* check for :NN port */
 		if ((port = strchr(ptr, ':'))) {
 			len = port - ptr;
 		} else {
 			len = strlen(ptr);
 		}
-
-		/* strip leading and trailing dots */
-		while (ptr[len - 1] == '.') {
-			--len;
-		}
-		while (*ptr == '.') {
-			++ptr;
-			--len;
-		}
-		host = ptr;
-
-		/* terminate & lowercase */
-		ptr[len] = NUL;
+		
+		/* strip leading & trailing dots */
+		ptr = host = trim(ptr, len, '.', 1, 1);
+		/* lowercase */
 		while (*ptr) {
 			apr_tolower(*ptr++);
 		}
-
+		
 		ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, DT->server, "DomainTree: sane host = %s", host);
+		
 		return host;
 	}
 }
 
-static APR_INLINE const char *domaintree_append(apr_pool_t *pool, struct domaintree *tree, const char *name, size_t length)
+static APR_INLINE const char *domaintree_elem(apr_pool_t *pool, struct domaintree *tree, const char *name, size_t length)
 {
 	struct domaintree_entry *elem = apr_palloc(pool, sizeof(struct domaintree_entry));
-
+	
 	APR_RING_ELEM_INIT(elem, link);
 	APR_RING_INSERT_HEAD(tree, elem, domaintree_entry, link);
-
+	
 	return elem->name = apr_pstrndup(pool, name, length);
 }
 
@@ -159,40 +185,40 @@ static APR_INLINE struct domaintree *domaintree_tree(apr_pool_t *pool, MOD_DT_CN
 	size_t depth = 0;
 	char *host_ptr = host;
 	struct domaintree *tree = apr_palloc(pool, sizeof(struct domaintree));
-
+	
 	APR_RING_INIT(tree, domaintree_entry, link);
-
+	
 	while ((host_ptr = strchr(host, '.'))) {
-
+		
 		/* check max depth */
 		if (++depth > DT->maxdepth) {
 			ap_log_error(APLOG_MARK, APLOG_ERR, APR_SUCCESS, DT->server, "DomainTree: maxdepth exceeded = %s", host);
 			return NULL;
 		}
-
+		
 		/* append part */
 		if (host_ptr - host) {
-
+			
 			/* strip WWW */
 			if (DT->stripwww && (depth == 1) && (!strncmp(host, "www.", sizeof("www")))) {
 				ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, DT->server, "DomainTree: stripping www.");
 			} else {
-				ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, DT->server, "DomainTree: host part (%d) = %s",
-					depth - 1, domaintree_append(pool, tree, host, host_ptr - host)
+				ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, DT->server, "DomainTree: host part (%d) = %s", 
+					depth - 1, domaintree_elem(pool, tree, host, host_ptr - host)
 				);
 			}
 		}
-
+		
 		host = host_ptr + 1;
 	}
-
+	
 	/* append last part */
 	if (!EMPTY(host)) {
-		ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, DT->server, "DomainTree: host part (%d) = %s",
-			depth, domaintree_append(pool, tree, host, strlen(host))
+		ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, DT->server, "DomainTree: host part (%d) = %s", 
+			depth, domaintree_elem(pool, tree, host, strlen(host))
 		);
 	}
-
+	
 	return tree;
 }
 
@@ -200,12 +226,54 @@ static APR_INLINE char *domaintree_path(apr_pool_t *pool, MOD_DT_CNF *DT, struct
 {
 	struct domaintree_entry *elem;
 	char *path = apr_pstrdup(pool, DT->prefix);
+	
 	APR_RING_FOREACH(elem, tree, domaintree_entry, link) {
 		path = apr_pstrcat(pool, path, "/", elem->name, NULL);
 	}
+	
 	return path = apr_pstrcat(pool, path, "/", DT->suffix, NULL);
 }
 
+static APR_INLINE void domaintree_fake(apr_pool_t *pool, MOD_DT_CNF *DT, char **path)
+{
+	int more;
+	apr_hash_index_t *idx;
+	size_t recurlevel = 0, plen = strlen(DT->prefix) + 1;
+	
+begin:
+	more = 0;
+	
+	if (recurlevel++ > DT->aliases.recursion) {
+		ap_log_error(APLOG_MARK, APLOG_ERR, APR_SUCCESS, DT->server, "DomainTree: maximum alias recursion level (%d) exceeded!"
+			" Check if you have recursive definitions of DomainTreeAlias directives.", DT->aliases.recursion);
+		return;
+	}
+	
+	for (idx = apr_hash_first(pool, DT->aliases.hashtable); idx; idx = apr_hash_next(idx)) {
+		apr_ssize_t flen;
+		char *fake, *real, *poff;
+		
+		poff = *path + plen;
+		apr_hash_this(idx, (const void **) &fake, &flen, (void **) &real);
+		
+		ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, DT->server, "DomainTree: fake test %s in %s", fake, poff);
+				
+		if ((!strncasecmp(poff, fake, flen)) && ((!poff[flen]) || (poff[flen] == '/'))) {
+			char *padd;
+			
+			more = 1;
+			padd = apr_pstrndup(pool, poff + flen + 1, strlen(poff) - flen - 1);
+			*path = apr_pstrcat(pool, DT->prefix, "/", real, "/", padd, NULL);
+			
+			ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, DT->server, "DomainTree: fake done %s<>%s = %s", fake, real, *path);
+		}
+	}
+	
+	if (more && DT->aliases.recursion) {
+		goto begin;
+	}
+}
+
 /* }}} */
 /* {{{ Hooks */
 
@@ -220,32 +288,35 @@ static STATUS domaintree_hook_translate_name(request_rec *r)
 	MOD_DT_CNF *DT = NULL;
 	struct domaintree *tree;
 	char *path, *host;
-
+	
 	DT = ap_get_module_config(r->server->module_config, MOD_DT_PTR);
 	if ((!DT) || (!DT->enabled)) {
 		return DECLINED;
 	}
-
+	
 	/* get a usable host name */
 	if (!(host = domaintree_host(r->pool, DT, ap_get_server_name(r)))) {
 		return DECLINED;
 	}
-
+	
 	/* build domain tree */
 	if (!(tree = domaintree_tree(r->pool, DT, host))) {
 		return DECLINED;
 	}
-
+	
 	/* build path */
 	if (!(path = domaintree_path(r->pool, DT, tree))) {
 		return DECLINED;
 	}
-
+	
+	/* apply any aliases.hashtable */
+	domaintree_fake(r->pool, DT, &path);
+	
 	/* done */
 	r->canonical_filename = "";
 	r->filename = apr_pstrcat(r->pool, path, r->uri, NULL);
-	ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, DT->server, "DomainTree: final path = %s", r->filename);
-
+	ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, DT->server, "DomainTree: path done = %s", r->filename);
+	
 	return OK;
 }
 
@@ -261,64 +332,67 @@ static void domaintree_hooks(apr_pool_t *pool)
 static void *domaintree_create_srv(apr_pool_t *p, server_rec *s)
 {
 	MOD_DT_CNF *DT;
-
+	
 	DT = (MOD_DT_CNF *) apr_palloc(p, sizeof(MOD_DT_CNF));
-
+	
 	DT->server = s;
 	DT->enabled = 1;
 	DT->stripwww = 1;
 	DT->maxdepth = 20;
-
+	
 	DT->prefix = "/var/www";
 	DT->suffix = "public_html";
-
+	
+	DT->aliases.hashtable = apr_hash_make(p);
+	DT->aliases.recursion = 0;
+	
 	return DT;
 }
 
 static const char *domaintree_enable(cmd_parms *cmd, void *conf, int flag)
 {
 	MOD_DT_CNF *DT;
-
+	
 	DT = ap_get_module_config(cmd->server->module_config, MOD_DT_PTR);
 	DT->enabled = flag;
-
+	
 	return NULL;
 }
 
 static const char *domaintree_stripwww(cmd_parms *cmd, void *conf, int flag)
 {
 	MOD_DT_CNF *DT;
-
+	
 	DT = ap_get_module_config(cmd->server->module_config, MOD_DT_PTR);
 	DT->stripwww = flag;
-
+	
 	return NULL;
 }
 
 static const char *domaintree_prefix(cmd_parms *cmd, void *conf, const char *prefix)
 {
 	MOD_DT_CNF *DT;
-
+	
 	DT = ap_get_module_config(cmd->server->module_config, MOD_DT_PTR);
-	DT->prefix = EMPTY(prefix) ? "/" : apr_pstrdup(cmd->pool, prefix);
-
+	DT->prefix = EMPTY(prefix) ? "/" : trim(apr_pstrdup(cmd->pool, prefix), strlen(prefix), '/', 0, 1);
+	
 	return NULL;
 }
 
 static const char *domaintree_suffix(cmd_parms *cmd, void *conf, const char *suffix)
 {
 	MOD_DT_CNF *DT;
-
+	
 	DT = ap_get_module_config(cmd->server->module_config, MOD_DT_PTR);
-	DT->suffix = EMPTY(suffix) ? "/" : apr_pstrdup(cmd->pool, suffix);
-
+	DT->suffix = EMPTY(suffix) ? "" : trim(apr_pstrdup(cmd->pool, suffix), strlen(suffix), '/', 1, 1);
+	
 	return NULL;
 }
 
 static const char *domaintree_maxdepth(cmd_parms *cmd, void *conf, const char *max_depth)
 {
 	int depth;
-
+	
 	if ((depth = atoi(max_depth))) {
 		if (depth > 0) {
 			MOD_DT_CNF *DT;
@@ -328,10 +402,39 @@ static const char *domaintree_maxdepth(cmd_parms *cmd, void *conf, const char *m
 			return "Maximum DomainTree depth cannot be negative.";
 		}
 	}
+	
+	return NULL;
+}
 
+static const char *domaintree_aliasrecursion(cmd_parms *cmd, void *conf, const char *alias_recursion)
+{
+	int recursion;
+	
+	if ((recursion = atoi(alias_recursion))) {
+		if (recursion > 0) {
+			MOD_DT_CNF *DT;
+			DT = ap_get_module_config(cmd->server->module_config, MOD_DT_PTR);
+			DT->aliases.recursion = (size_t) recursion;
+		} else {
+			return "DomainTree alias recursion cannot be negative.";
+		}
+	}
+	
 	return NULL;
 }
 
+static const char *domaintree_alias(cmd_parms *cmd, void *conf, const char *fake, const char *real)
+{
+	MOD_DT_CNF *DT;
+	char *f = strtr(apr_pstrdup(cmd->pool, fake), '.', '/'), *r = strtr(apr_pstrdup(cmd->pool, real), '.', '/');
+	
+	DT = ap_get_module_config(cmd->server->module_config, MOD_DT_PTR);
+	apr_hash_set(DT->aliases.hashtable, trim(f, strlen(f), '/', 1, 1), APR_HASH_KEY_STRING, trim(r, strlen(r), '/', 1, 1));
+	
+	return NULL;
+}
+
+
 /* }}} */
 /* {{{ Commands */
 
@@ -348,7 +451,7 @@ static command_rec domaintree_commands[] = {
 
 	AP_INIT_TAKE1(
 		"DomainTreePrefix", domaintree_prefix, NULL, RSRC_CONF,
-		"DomainTree path prefix. (default /var/www)"
+		"DomainTree path prefix. (default /var/www) Do not forget the leading slash!"
 	),
 
 	AP_INIT_TAKE1(
@@ -361,6 +464,17 @@ static command_rec domaintree_commands[] = {
 		"DomainTree max path depth. (default 20)"
 	),
 
+	AP_INIT_TAKE1(
+		"DomainTreeAliasRecursion", domaintree_aliasrecursion, NULL, RSRC_CONF,
+		"Whether (and how often at the maximum) DomainTree should walk recursively "
+		"through the aliases.hashtable list as long as matching aliases.hashtable are found. (Default: 0 = turned off)"
+	),
+	
+	AP_INIT_TAKE2(
+		"DomainTreeAlias", domaintree_alias, NULL, RSRC_CONF,
+		"DomainTree aliases.hashtable; e.g. DomainTreeAlias com/example/tickets com/example/support (dots or slashes equal)"
+	),
+
 	{ NULL }
 };
 
-- 
2.30.2