larryi      01/08/07 05:08:26

  Modified:    src/share/org/apache/tomcat/modules/mappers
                        SimpleMapper1.java
  Log:
  Added support for Context virtual host aliases.  Added a warning to log
  if '/' is prepended to the path for a mapping.
  
  Committed a modified version of a patch to add wildcard support for virtual
  host names that begin with '*'. The beginning '*' should only be used with
  virtual host aliases.
  
  Submitted by: Chris Bryden
  
  Revision  Changes    Path
  1.9       +74 -12    
jakarta-tomcat/src/share/org/apache/tomcat/modules/mappers/SimpleMapper1.java
  
  Index: SimpleMapper1.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/modules/mappers/SimpleMapper1.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- SimpleMapper1.java        2001/08/03 02:50:20     1.8
  +++ SimpleMapper1.java        2001/08/07 12:08:26     1.9
  @@ -148,6 +148,7 @@
        throws TomcatException
       {
        map.addMapping( ctx.getHost(), ctx.getPath(), ctx.getContainer());
  +        map.addMappings( ctx.getHostAliases(), ctx.getPath(), ctx.getContainer());
       }
   
       /** Called when a context is removed from a CM - we must ask the mapper to
  @@ -158,6 +159,11 @@
       {
        if(debug>0) log( "Removed from maps ");
        map.removeAllMappings( ctx.getHost(), ctx);
  +
  +        Enumeration vhostAliases=ctx.getHostAliases();
  +        while( vhostAliases.hasMoreElements() )
  +            map.removeAllMappings( (String)vhostAliases.nextElement(), ctx );
  +
        // extension mappings are local to ctx, no need to do something
        // about that
       }
  @@ -179,6 +185,7 @@
       {
        Context ctx=ct.getContext();
        String vhost=ctx.getHost();
  +        Enumeration vhostAliases=ctx.getHostAliases();
        String path=ct.getPath();
        String ctxP=ctx.getPath();
   
  @@ -195,9 +202,15 @@
        case Container.PREFIX_MAP:
            // cut /* ( no need to do a string concat for every match )
            // workaround for frequent bug in web.xml ( backw. compat )
  -         if( ! path.startsWith( "/" ) ) path="/" + path;
  -         map.addMapping( vhost,
  -                         ctxP + path.substring( 0, path.length()-2 ), ct);
  +            if( ! path.startsWith( "/" ) ) {
  +                log("WARNING: Correcting error in web.xml for context \"" + ctxP +
  +                        "\". Mapping for path \"" + path + "\" is missing a leading 
'/'.");
  +                path="/" + path;
  +            }
  +            String prefixPath=ctxP + path.substring( 0, path.length()-2 );
  +         map.addMapping( vhost, prefixPath, ct);
  +         map.addMappings( vhostAliases, prefixPath, ct);
  +
            if( debug>0 )
                log("SM: prefix map " + vhost + ":" +  ctxP +
                    path + " -> " + ct + " " );
  @@ -236,8 +249,13 @@
            break;
        case Container.PATH_MAP:
            // workaround for frequent bug in web.xml
  -         if( ! path.startsWith( "/" ) ) path="/" + path;
  +            if( ! path.startsWith( "/" ) ) {
  +                log("WARNING: Correcting error in web.xml for context \"" + ctxP +
  +                        "\". Mapping for path \"" + path + "\" is missing a leading 
'/'.");
  +                path="/" + path;
  +            }
            map.addExactMapping( vhost, ctxP + path, ct);
  +         map.addExactMappings( vhostAliases, ctxP + path, ct);
            if( debug>0 )
                log("SM: exact map " + vhost + ":" + ctxP +
                    path + " -> " + ct + " " );
  @@ -261,7 +279,7 @@
       /* -------------------- Request mapping -------------------- */
   
   
  -    /** First step of request porcessing is finding the Context.
  +    /** First step of request processing is finding the Context.
        */
       public int contextMap( Request req ) {
        MessageBytes pathMB = req.requestURI();
  @@ -460,7 +478,9 @@
       // host -> PrefixMapper for virtual hosts
       // hosts are stored in lower case ( the "common" case )
       SimpleHashtable vhostMaps=new SimpleHashtable();
  -
  +    // host -> PrefixMapper for virtual hosts with leading '*'
  +    // host key has '*' removed
  +    SimpleHashtable vhostMapsWC=new SimpleHashtable();
   
       SimpleHashtable prefixMappedServlets;
       SimpleHashtable exactMappedServlets;
  @@ -503,7 +523,10 @@
        PrefixMapper vmap=this;
        if( host!=null ) {
            host=host.toLowerCase();
  -         vmap=(PrefixMapper)vhostMaps.get(host);
  +            if( host.startsWith( "*" ) )
  +                vmap=(PrefixMapper)vhostMapsWC.get(host.substring( 1 ));
  +            else
  +                vmap=(PrefixMapper)vhostMaps.get(host);
        }
        
        // remove all paths starting with path
  @@ -555,11 +578,18 @@
                prefixMappedServlets.put( path, target);
        } else {
            host=host.toLowerCase();
  -         PrefixMapper vmap=(PrefixMapper)vhostMaps.get( host );
  +            SimpleHashtable maps;
  +            if( host.startsWith( "*" ) ) {
  +                maps=vhostMapsWC;
  +                host=host.substring( 1 );
  +            } else {
  +                maps=vhostMaps;
  +            }
  +         PrefixMapper vmap=(PrefixMapper)maps.get( host );
            if( vmap == null ) {
                vmap=new PrefixMapper();
                vmap.setIgnoreCase( ignoreCase );
  -                 vhostMaps.put( host, vmap );
  +                maps.put( host, vmap );
                vmap.setMapCache( mapCacheEnabled );
            }
            if( ignoreCase ) 
  @@ -571,6 +601,13 @@
   
       /**
        */
  +    public void addMappings( Enumeration hostAliases, String path, Object target ) {
  +        while ( hostAliases.hasMoreElements() )
  +            addMapping( (String)hostAliases.nextElement(), path, target );
  +    }
  +
  +    /**
  +     */
       public void addExactMapping( String host, String path, Object target ) {
           if( host==null ) {
               if ( ignoreCase )
  @@ -579,10 +616,17 @@
                   exactMappedServlets.put( path, target);
           } else {
            host=host.toLowerCase();
  -         PrefixMapper vmap=(PrefixMapper)vhostMaps.get( host );
  +            SimpleHashtable maps;
  +            if( host.startsWith( "*" ) ) {
  +                maps = vhostMapsWC;
  +                host=host.substring( 1 );
  +            } else {
  +                maps = vhostMaps;
  +            }
  +         PrefixMapper vmap=(PrefixMapper)maps.get( host );
            if( vmap == null ) {
                vmap=new PrefixMapper();
  -             vhostMaps.put( host, vmap );
  +             maps.put( host, vmap );
            }
            if( ignoreCase ) 
                vmap.addExactMapping( path.toLowerCase(), target );
  @@ -590,7 +634,14 @@
                vmap.addExactMapping( path, target );
        }
       }
  -    
  +
  +    /**
  +     */
  +    public void addExactMappings( Enumeration hostAliases, String path, Object 
target ) {
  +        while ( hostAliases.hasMoreElements() )
  +            addExactMapping( (String)hostAliases.nextElement(), path, target );
  +    }
  +   
       
       // -------------------- Implementation --------------------
   
  @@ -610,6 +661,17 @@
            if( myMap==null ) {
                myMap=(PrefixMapper)vhostMaps.get( host.toLowerCase() );
            }
  +        }
  +        if( myMap==null ) {
  +            // Check host against virtual hosts that began with '*'
  +            Enumeration vhosts = vhostMapsWC.keys();
  +            while(vhosts.hasMoreElements()) {
  +                String vhostName = (String)vhosts.nextElement();
  +                if(host.endsWith(vhostName)) {
  +                    myMap = (PrefixMapper)vhostMapsWC.get(vhostName);
  +                    break;
  +                }
  +            }
        }
        
        if( myMap==null ) myMap = this; // default server
  
  
  

Reply via email to