tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject cvs commit: jakarta-tomcat-connectors/jk/native2/common jk_uriMap.c
Date Wed, 02 Oct 2002 09:49:17 GMT
mturk       2002/10/02 02:49:17

  Modified:    jk/native2/common jk_uriMap.c
  Log:
  1. Correct webapps for standalone context statements inside any uri
  2. Add the contextMap (not sure if that is the correct name)
  
  The contextMap purpose is to map the uri with the prefix without the prefixes
  trailing slash, doing exact match. This allows mappings of /example if we
  only have /example/* directive, but sill alowing /examle/ as a true prefix
  match, not returning error for if we have /exam/* application too.
  
  Revision  Changes    Path
  1.46      +190 -91   jakarta-tomcat-connectors/jk/native2/common/jk_uriMap.c
  
  Index: jk_uriMap.c
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-connectors/jk/native2/common/jk_uriMap.c,v
  retrieving revision 1.45
  retrieving revision 1.46
  diff -u -r1.45 -r1.46
  --- jk_uriMap.c	26 Sep 2002 12:29:17 -0000	1.45
  +++ jk_uriMap.c	2 Oct 2002 09:49:16 -0000	1.46
  @@ -171,14 +171,32 @@
           if (uriLen < uwr->prefix_len)
               continue;
           if (strncmp(uri, uwr->prefix, uwr->prefix_len) == 0) {
  -            if (uwr->prefix_len > best_match) {
  +            if (uwr->prefix_len > best_match) {
                   best_match=uwr->prefix_len;
  -                match=uwr;
  +                match=uwr;
               }
           }
       }
       return match;
   }
  +
  +static jk_uriEnv_t *jk2_uriMap_contextMap(jk_env_t *env, jk_uriMap_t *uriMap,
  +                                          jk_map_t *mapTable, const char *uri, 
  +                                          int uriLen)
  +{
  +    int i;    
  +    int sz = mapTable->size(env, mapTable);
  +
  +    for (i = 0; i < sz; i++) {
  +        jk_uriEnv_t *uwr = mapTable->valueAt(env, mapTable, i);
  +        if (uriLen != uwr->prefix_len - 1)
  +            continue;
  +        if (strncmp(uri, uwr->prefix, uriLen) == 0) {
  +            return uwr;
  +        }
  +    }
  +    return NULL;
  +}
   
   static jk_uriEnv_t *jk2_uriMap_exactMap(jk_env_t *env, jk_uriMap_t *uriMap,
                                           jk_map_t *mapTable, const char *uri, 
  @@ -276,6 +294,151 @@
       }
       return uriMap->vhosts->get(env, uriMap->vhosts, "*");
   }
  +
  +static void jk2_uriMap_createHosts(jk_env_t *env, jk_uriMap_t *uriMap)
  +{
  +    int i;
  +
  +    for (i = 0; i < uriMap->maps->size(env, uriMap->maps); i++) {
  +        jk_uriEnv_t *uriEnv = uriMap->maps->valueAt(env, uriMap->maps, i);
  +        if (uriEnv == NULL) 
  +            continue;
  +        if (uriEnv->virtual != NULL && strlen(uriEnv->virtual)) {
  +            if (uriEnv->match_type == MATCH_TYPE_HOST) {
  +                jk2_map_default_create(env, &uriEnv->webapps, uriMap->pool);
  +                uriMap->vhosts->put(env, uriMap->vhosts,
  +                                    uriEnv->virtual, uriEnv, NULL);
  +            } 
  +            else { /* Create the missing vhosts */
  +                if (!uriMap->vhosts->get(env, uriMap->vhosts,
  +                                         uriEnv->virtual)) {
  +                    jk2_map_default_create(env, &uriEnv->webapps, uriMap->pool);
  +                    uriMap->vhosts->put(env, uriMap->vhosts,
  +                                        uriEnv->virtual, uriEnv, NULL);
  +
  +                    env->l->jkLog(env, env->l, JK_LOG_DEBUG,
  +                                  "uriMap.init() Fixing Host %s\n", 
  +                                  uriEnv->virtual);
  +                }
  +            }
  +        }
  +    }
  +
  +    /** Make sure each vhost has a default context
  +     */
  +    for(i = 0; i < uriMap->vhosts->size(env, uriMap->vhosts); i++) {
  +        jk_uriEnv_t *hostEnv = uriMap->vhosts->valueAt(env, uriMap->vhosts, i);
  +        jk_uriEnv_t *rootCtx;
  +        char *uriPath;
  +        
  +        if (hostEnv->virtual != NULL) {
  +            uriPath=env->tmpPool->calloc(env, env->tmpPool,
  +                                         strlen(hostEnv->virtual) + 3);
  +            strcpy(uriPath, hostEnv->virtual);
  +            strcat(uriPath, "/");
  +        } else {
  +            uriPath = "/";
  +        }
  +        
  +        rootCtx = env->getByName2(env, "uri", uriPath);
  +        if (rootCtx == NULL) {
  +            env->createBean2(env, uriMap->mbean->pool, "uri", uriPath);
  +            rootCtx = env->getByName2(env, "uri", uriPath);
  +            if (uriMap->mbean->debug > 0) 
  +                env->l->jkLog(env, env->l, JK_LOG_DEBUG,
  +                              "uriMap.init() Create default context %s\n", uriPath);
  +            rootCtx->mbean->setAttribute(env, rootCtx->mbean, "context", "/");
  +        }
  +    }
  +}
  +
  +static void jk2_uriMap_createWebapps(jk_env_t *env, jk_uriMap_t *uriMap)
  +{
  +    int i;
  +
  +    /* Init all contexts */
  +    /* For each context, init the local uri maps */
  +    for (i = 0; i < uriMap->maps->size(env, uriMap->maps); i++) {
  +        jk_uriEnv_t *uriEnv = uriMap->maps->valueAt(env, uriMap->maps, i);
  +        char *uri;
  +        char *context;
  +        if (uriEnv == NULL) {
  +            env->l->jkLog(env, env->l, JK_LOG_INFO,
  +                          "uriMap.init() NPE\n");
  +        }
  +        uri = uriEnv->uri;
  +        context = uriEnv->contextPath;
  +
  +        if (uri != NULL && context != NULL && strcmp(uri, context) == 0)
{
  +            char *vhost = uriEnv->virtual;
  +            int  port = uriEnv->port;
  +            jk_uriEnv_t *hostEnv = jk2_uriMap_hostMap(env, uriMap, vhost, port);
  +            if (!hostEnv)
  +                continue;
  +            if (  uriMap->mbean->debug > 5) 
  +                env->l->jkLog(env, env->l, JK_LOG_DEBUG,
  +                              "uriMap.init() loaded context %s %s %#lx %#lx %#lx\n",
  +                              uriEnv->virtual, context, hostEnv, hostEnv->webapps,
  +                              uriMap->pool); 
  +            uriEnv->match_type = MATCH_TYPE_CONTEXT;
  +
  +            uriEnv->prefix = context;
  +            uriEnv->prefix_len = strlen(context);
  +            hostEnv->webapps->put(env, hostEnv->webapps, context, uriEnv, NULL);
  +            jk2_map_default_create(env, &uriEnv->exactMatch, uriMap->pool);
  +            jk2_map_default_create(env, &uriEnv->prefixMatch, uriMap->pool);
  +            jk2_map_default_create(env, &uriEnv->suffixMatch, uriMap->pool);
  +        }
  +    }
  +    
  +    /* Now, fix the webapps finding context from each uri 
  +     * and create one if not found.
  +     */
  +    for (i = 0; i < uriMap->maps->size(env, uriMap->maps ); i++) {
  +        jk_uriEnv_t *uriEnv = uriMap->maps->valueAt(env, uriMap->maps, i);   
    
  +        char *vhost= uriEnv->virtual;
  +        int  port = uriEnv->port;
  +        char *context= uriEnv->contextPath;
  +        jk_uriEnv_t *hostEnv = jk2_uriMap_hostMap(env, uriMap, vhost, port);
  +        jk_uriEnv_t *ctxEnv;
  +        
  +        env->l->jkLog(env, env->l, JK_LOG_DEBUG,
  +                              "uriMap: fix uri %s context %s\n", uriEnv->uri, uriEnv->contextPath
);
  +        if (context == NULL) {
  +            if (  uriMap->mbean->debug > 5) 
  +                env->l->jkLog(env, env->l, JK_LOG_DEBUG,
  +                              "uriMap: no context %s\n", uriEnv->uri );
  +            continue;
  +        }
  +        
  +        ctxEnv = jk2_uriMap_exactMap(env, uriMap, hostEnv->webapps, context,
  +                                      strlen(context));
  +        /* if not alredy created, create it */
  +        if (ctxEnv == NULL) {
  +           jk_bean_t *mbean;
  + 
  +            env->l->jkLog(env, env->l, JK_LOG_INFO,
  +                          "uriMap: creating context %s\n", context);
  +            mbean = env->getBean2(env, "uri", context);
  +            if (mbean == NULL)
  +                mbean = env->createBean2(env, uriMap->pool,"uri", context);
  +            if (mbean == NULL || mbean->object == NULL) {
  +                env->l->jkLog(env, env->l, JK_LOG_ERROR,
  +                              "uriMap: can't create context object %s\n",context);
  +                continue;
  +            }
  +            ctxEnv = mbean->object;
  +            ctxEnv->match_type = MATCH_TYPE_CONTEXT;
  +            ctxEnv->prefix = context;
  +            ctxEnv->prefix_len = strlen(context);
  +            hostEnv->webapps->put(env, hostEnv->webapps, context, ctxEnv, NULL);
  +            jk2_map_default_create(env, &ctxEnv->exactMatch, uriMap->pool);
  +            jk2_map_default_create(env, &ctxEnv->prefixMatch, uriMap->pool);
  +            jk2_map_default_create(env, &ctxEnv->suffixMatch, uriMap->pool);
  +        }
  +    } 
  +
  +}
   
   static int jk2_uriMap_init(jk_env_t *env, jk_uriMap_t *uriMap)
   {
  @@ -294,92 +457,11 @@
           }
       }
   
  -    /* Initialize the context table */
  -    for (i = 0; i < uriMap->maps->size(env, uriMap->maps); i++) {
  -        jk_uriEnv_t *uriEnv = uriMap->maps->valueAt(env, uriMap->maps, i);
  -        if (uriEnv == NULL) 
  -            continue;
  -        if (uriEnv->virtual != NULL && strlen(uriEnv->virtual)) {
  -            if (uriEnv->match_type == MATCH_TYPE_HOST) {
  -                jk2_map_default_create(env, &uriEnv->webapps, uriMap->pool);
  -                uriMap->vhosts->put(env, uriMap->vhosts,
  -                                    uriEnv->virtual, uriEnv, NULL);
  -            } 
  -            else { /* Create the missing vhosts */
  -                if (!uriMap->vhosts->get(env, uriMap->vhosts,
  -                                         uriEnv->virtual)) {
  -                    jk2_map_default_create(env, &uriEnv->webapps, uriMap->pool);
  -                    uriMap->vhosts->put(env, uriMap->vhosts,
  -                                        uriEnv->virtual, uriEnv, NULL);
  -
  -                    env->l->jkLog(env, env->l, JK_LOG_DEBUG,
  -                                  "uriMap.init() Fixing Host %s\n", 
  -                                  uriEnv->virtual);
  -                }
  -            }
  -        }
  -    }
  -
  -    /** Make sure each vhost has a default context
  -     */
  -    for(i = 0; i < uriMap->vhosts->size(env, uriMap->vhosts); i++) {
  -        jk_uriEnv_t *hostEnv = uriMap->vhosts->valueAt(env, uriMap->vhosts, i);
  -        jk_uriEnv_t *rootCtx;
  -        char *uriPath;
  -        
  -        if (hostEnv->virtual != NULL) {
  -            uriPath=env->tmpPool->calloc(env, env->tmpPool,
  -                                         strlen(hostEnv->virtual) + 3);
  -            strcpy(uriPath, hostEnv->virtual);
  -            strcat(uriPath, "/");
  -        } else {
  -            uriPath = "/";
  -        }
  -        
  -        rootCtx = env->getByName2(env, "uri", uriPath);
  -        if (rootCtx == NULL) {
  -            env->createBean2(env, uriMap->mbean->pool, "uri", uriPath);
  -            rootCtx = env->getByName2(env, "uri", uriPath);
  -            if (uriMap->mbean->debug > 0) 
  -                env->l->jkLog(env, env->l, JK_LOG_DEBUG,
  -                              "uriMap.init() Create default context %s\n", uriPath);
  -            rootCtx->mbean->setAttribute(env, rootCtx->mbean, "context", "/");
  -        }
  -    }
  -    /* Init all contexts */
  -    /* For each context, init the local uri maps */
  -    for (i = 0; i < uriMap->maps->size(env, uriMap->maps); i++) {
  -        jk_uriEnv_t *uriEnv = uriMap->maps->valueAt(env, uriMap->maps, i);
  -        char *uri;
  -        char *context;
  -        if (uriEnv == NULL) {
  -            env->l->jkLog(env, env->l, JK_LOG_INFO,
  -                          "uriMap.init() NPE\n");
  -        }
  -        uri = uriEnv->uri;
  -        context = uriEnv->contextPath;
  -
  -        if (uri != NULL && context != NULL && strcmp(uri, context) == 0)
{
  -            char *vhost = uriEnv->virtual;
  -            int  port = uriEnv->port;
  -            jk_uriEnv_t *hostEnv = jk2_uriMap_hostMap(env, uriMap, vhost, port);
  -            if (!hostEnv)
  -                continue;
  -            if (  uriMap->mbean->debug > 5) 
  -                env->l->jkLog(env, env->l, JK_LOG_DEBUG,
  -                              "uriMap.init() loaded context %s %s %#lx %#lx %#lx\n",
  -                              uriEnv->virtual, context, hostEnv, hostEnv->webapps,
  -                              uriMap->pool); 
  -            uriEnv->match_type = MATCH_TYPE_CONTEXT;
  -
  -            uriEnv->prefix = context;
  -            uriEnv->prefix_len = strlen(context);
  -            hostEnv->webapps->put(env, hostEnv->webapps, context, uriEnv, NULL);
  -            jk2_map_default_create(env, &uriEnv->exactMatch, uriMap->pool);
  -            jk2_map_default_create(env, &uriEnv->prefixMatch, uriMap->pool);
  -            jk2_map_default_create(env, &uriEnv->suffixMatch, uriMap->pool);
  -        }
  -    }
  +    /* Create virtual hosts and initialize them */
  +    jk2_uriMap_createHosts(env, uriMap);
  +
  +    /* Create webapps and initialize them */
  +    jk2_uriMap_createWebapps(env, uriMap);
   
       if (uriMap->mbean->debug > 5) 
           env->l->jkLog(env, env->l, JK_LOG_DEBUG,
  @@ -418,7 +500,11 @@
                              "uriMap.init() no context for %s\n", uri); 
               return JK_ERR;
           }
  -        
  +        uriEnv->contextPath = ctxEnv->prefix;
  +        uriEnv->ctxt_len = ctxEnv->prefix_len;
  +        env->l->jkLog(env, env->l, JK_LOG_INFO, 
  +                           "uriMap.init() adding context %s for %s\n", ctxEnv->prefix,
uri); 
  +
           switch (uriEnv->match_type) {
               case MATCH_TYPE_EXACT:
                   ctxEnv->exactMatch->add(env, ctxEnv->exactMatch, uri, uriEnv);
  @@ -588,7 +674,20 @@
               return match;
           }
       }
  -
  +    
  +    /* Try to find exact match of /uri and prefix /uri/* */
  +    match = jk2_uriMap_contextMap(env, uriMap, ctxEnv->prefixMatch, uri, uriLen);
  +    if (match != NULL) {
  +        /* restore */
  +        if (url_rewrite)
  +            *url_rewrite = origChar;
  +        if (uriMap->mbean->debug > 0)
  +            env->l->jkLog(env, env->l, JK_LOG_DEBUG,
  +                          "uriMap.mapUri() context match %s %s\n",
  +                          uri, match->workerName); 
  +        return match;
  +    }
  +
       /* And extension match at the end */
       /* Only once, no need to compute it for each extension match */
       suffix = jk2_findExtension(env, uri);
  @@ -606,7 +705,7 @@
               return match;
           }
       }
  -        
  +
       /* restore */
       if (url_rewrite)
           *url_rewrite = origChar;
  
  
  

--
To unsubscribe, e-mail:   <mailto:tomcat-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:tomcat-dev-help@jakarta.apache.org>


Mime
View raw message