Hi,

I have a small query on the way tomcat chooses a virtual host from those 
specified using Host tags in server.xml.

We currently have a system where we can access web content at 
subdomain.ourdomain.com as <anything>.subdomain.ourdomain.com .

The host part of the domain name is then used by our application to modify 
it's behaviour depending on how it was accessed...


I have set up
<anything>.subdomain.ourdomain.com as an alias for subdomain.ourdomain.com
in dns using a wildcard CNAME record, and in apache using a wildcard in the 
ServerAlias directive inside the virtual host container:

ServerName subdomain.ourdomain.com
ServerAlias subdomain.ourdomain.com *.subdomain.ourdomain.com

The problem is setting up tomcat to map a single context for all the 
<anything>.subdomain.ourdomain.com hosts without having to put individual 
Host tags in for each one.

I don't want to just set this up as the default context and let the 
requests fall through as we have a few systems that work like this, and it 
would nice to give them all their own context within a single tomcat instance.

It would be nice to be able to specify wildcards in the Host tag, and have 
tomcat match them up. i.e.

<Host name="*.subdomain.ourdomain.com">
        ...stuff
</Host>

would match <anything>.subdomain.ourdomain.com

We are using tomcat 3.2.3 where this matching seems to happen 
in  PrefixMapper.java so I added a hack to make it work.

This behavior is still present in 3.3beta i got from cvs to have a look at, 
although the code for matching the virtual host names seems to have moved 
to SimpleMapper1.java...

I really *needed* to get this to work in a hurry, and last night was the 
first time i've ever looked at the tomcat code, so apologies that this is a 
pretty nasty hack, but i've attached the patch really just so you can see 
what i'm talking about....

basically if no matching virtual host is found, it loops through the 
defined virtual hosts looking for ones that begin '*.', if any do it will 
match that virtual host to a domain name that ends the same - yeah, i know, 
i did say it was a nasty hack....

I was wondering, will tomcat support some way around this problem in the 
future..?

Looking at the code there seems to be an unimplemented alias attribute for 
Hosts, which would proably be a better place for this kind of thing?

or is there a much better way around this?

thoughts appreciated....


C
















Index: SimpleMapper1.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-tomcat/src/share/org/apache/tomcat/modules/mappers/SimpleMapper1.java,v
retrieving revision 1.7
diff -u -r1.7 SimpleMapper1.java
--- SimpleMapper1.java  2001/07/17 14:02:33     1.7
+++ SimpleMapper1.java  2001/07/25 16:05:44
@@ -606,6 +606,19 @@
            if( myMap==null ) {
                myMap=(PrefixMapper)vhostMaps.get( host.toLowerCase() );
            }
+        if( myMap==null ) {
+        //look for defined hosts that begin with *. then check if the requested host 
+matched
+            Enumeration vhosts = vhostMaps.keys();
+            while(vhosts.hasMoreElements()) {
+                String vhostName = (String)vhosts.nextElement();
+                if(vhostName.startsWith("*.")) {
+                    if(host.endsWith(vhostName.substring(vhostName.indexOf("*.")+2))) 
+{
+                             myMap = (PrefixMapper)vhostMaps.get(vhostName);
+                          break;
+                    }
+                }
+             }
+        }
        }
        
        if( myMap==null ) myMap = this; // default server

Reply via email to