📄 jk_urimap.c
字号:
mapEnv = ctxEnv->exactMatch->valueAt(env, ctxEnv->exactMatch, k); newEnv = appEnv->exactMatch->get(env, appEnv->exactMatch, mapEnv->uri); /* Create the new exact match uri */ if (!newEnv) { newEnv = jk2_uriMap_duplicateUri(env, uriMap, uriEnv, mapEnv); appEnv->exactMatch->put(env, appEnv->exactMatch, newEnv->name, newEnv, NULL); } } /* Fix the prefix matches */ l = ctxEnv->prefixMatch->size(env, ctxEnv->prefixMatch); for (k = 0; k < l; k++) { jk_uriEnv_t *mapEnv; jk_uriEnv_t *newEnv; mapEnv = ctxEnv->prefixMatch->valueAt(env, ctxEnv->prefixMatch, k); newEnv = appEnv->prefixMatch->get(env, appEnv->prefixMatch, mapEnv->uri); /* Create the new prefix match uri */ if (!newEnv) { newEnv = jk2_uriMap_duplicateUri(env, uriMap, uriEnv, mapEnv); appEnv->prefixMatch->put(env, appEnv->prefixMatch, newEnv->name, newEnv, NULL); } } /* Fix the suffix matches */ l = ctxEnv->suffixMatch->size(env, ctxEnv->suffixMatch); for (k = 0; k < l; k++) { jk_uriEnv_t *mapEnv; jk_uriEnv_t *newEnv; mapEnv = ctxEnv->suffixMatch->valueAt(env, ctxEnv->suffixMatch, k); newEnv = appEnv->suffixMatch->get(env, appEnv->suffixMatch, mapEnv->uri); /* Create the new suffix match uri */ if (!newEnv) { newEnv = jk2_uriMap_duplicateUri(env, uriMap, uriEnv, mapEnv); appEnv->suffixMatch->put(env, appEnv->prefixMatch, newEnv->name, newEnv, NULL); } } uriEnv->webapps->put(env, uriEnv->webapps, appEnv->contextPath, appEnv, NULL); } } } return JK_OK;}static int jk2_uriMap_init(jk_env_t *env, jk_uriMap_t *uriMap){ int rc = JK_OK; jk_workerEnv_t *workerEnv = uriMap->workerEnv; jk_bean_t *mbean = env->getBean2(env, "uri", "*"); /* create the default server */ if (mbean == NULL) { mbean = env->createBean2(env, workerEnv->pool,"uri", "*"); if (mbean == NULL || mbean->object == NULL) { env->l->jkLog(env, env->l, JK_LOG_ERROR, "uriMap.factory() Fail to create default host\n"); return JK_ERR; } } /* Create virtual hosts and initialize them */ jk2_uriMap_createHosts(env, uriMap); /* Create webapps and initialize them */ jk2_uriMap_createWebapps(env, uriMap); /* All other mappings are added in the right context leaf. */ if ((rc = jk2_uriMap_createMappings(env, uriMap)) != JK_OK) return rc; /* Fix the global mappings for virtual hosts */ rc = jk2_uriMap_createGlobals(env, uriMap); return rc;}static void jk2_uriMap_destroy(jk_env_t *env, jk_uriMap_t *uriMap){ if( uriMap->mbean->debug > 0 ) env->l->jkLog(env, env->l, JK_LOG_DEBUG, "uriMap.destroy()\n"); /* this can't be null ( or a NPE would have been generated */ uriMap->pool->close(env, uriMap->pool);}/* returns the index of the last occurrence of the 'ch' character if ch=='\0' returns the length of the string str */static INLINE int jk2_last_index_of(const char *str,char ch){ const char *str_minus_one=str-1; const char *s=str+strlen(str); while(s!=str_minus_one && ch!=*s) { --s; } return (s-str);}/* find the suffix - only once. We also make sure we check only the last component, as required by servlet spec*/static INLINE const char *jk2_findExtension(jk_env_t *env, const char *uri) { int suffix_start; const char *suffix; for(suffix_start = strlen(uri) - 1 ; suffix_start > 0; suffix_start--) { if( '.' == uri[suffix_start] || '/' == uri[suffix_start] ) break; } if( '.' != uri[suffix_start] ) { suffix_start=-1; suffix = NULL; } else { suffix_start++; suffix = uri + suffix_start; } return suffix;}#define SAFE_URI_SIZE 8192static jk_uriEnv_t *jk2_uriMap_getHostCache(jk_env_t *env, jk_uriMap_t *uriMap, const char *vhost, int port){ char key[1024]; if (!vhost && !port) return uriMap->vhosts->get(env, uriMap->vhosts, "*"); if (!vhost) vhost = "*"; sprintf(key, "%s:%d", vhost, port); return uriMap->vhcache->get(env, uriMap->vhcache, key);}static void jk2_uriMap_addHostCache(jk_env_t *env, jk_uriMap_t *uriMap, const char *vhost, int port, jk_uriEnv_t *hostEnv){ char *key; if (!vhost) vhost = "*"; key = uriMap->pool->calloc(env, uriMap->pool, strlen(vhost) + 8); sprintf(key, "%s:%d", vhost, port); uriMap->vhcache->add(env, uriMap->vhcache, key, hostEnv);}static jk_uriEnv_t *jk2_uriMap_mapUri(jk_env_t *env, jk_uriMap_t *uriMap, const char *vhost, int port, const char *uri){ int best_match = -1; int longest_match = 0; char *clean_uri = NULL; char *url_rewrite = NULL; const char *suffix; int uriLen; jk_uriEnv_t *hostEnv; jk_uriEnv_t *ctxEnv; jk_uriEnv_t *match; /* Ugly hack to avoid using non-thread safe code. Modify the uri in place for uri session encoding, then restore it to the original. That works since the processing happens in a single thred. A better solution is to allocate the jk_ws_service and it's pool and pass it as param */ char origChar = '\0'; /* XXX - need to make sure prefix match take precedence over extension match ( now it doesn't ) */ if (uriMap == NULL || uri==NULL) return NULL; if (uriMap->mbean->debug > 1) env->l->jkLog(env, env->l, JK_LOG_DEBUG, "uriMap.mapUri() hostname %s port %d uri %s\n", vhost, port, uri); if (uri[0] != '/') { env->l->jkLog(env, env->l, JK_LOG_ERROR, "uriMap.mapUri() uri must start with /\n"); return NULL; } hostEnv = jk2_uriMap_getHostCache(env, uriMap, vhost, port); if (!hostEnv) { hostEnv = jk2_uriMap_hostMap(env, uriMap, vhost, port); if (!hostEnv) { env->l->jkLog(env, env->l, JK_LOG_INFO, "uriMap.mapUri() cannot find host %s/\n", vhost); return NULL; } if (uriMap->mbean->debug > 1) env->l->jkLog(env, env->l, JK_LOG_DEBUG, "uriMap.mapUri() caching host %s\n", hostEnv->virtual); jk2_uriMap_addHostCache(env, uriMap, vhost, port, hostEnv); } else if (uriMap->mbean->debug > 1) env->l->jkLog(env, env->l, JK_LOG_DEBUG, "uriMap.mapUri() found host %s\n", hostEnv->virtual); url_rewrite = strstr(uri, JK_PATH_SESSION_IDENTIFIER); if (url_rewrite) { origChar = *url_rewrite; *url_rewrite = '\0'; if (uriMap->mbean->debug > 0) env->l->jkLog(env, env->l, JK_LOG_DEBUG, "uriMap.mapUri() rewrote uri %s \n",uri); } uriLen = strlen(uri); /* Map the context */ ctxEnv = jk2_uriMap_prefixMap(env, uriMap, hostEnv->webapps, uri, uriLen); if (ctxEnv == NULL) { if (url_rewrite) *url_rewrite = origChar; env->l->jkLog(env, env->l, JK_LOG_INFO, "uriMap.mapUri() no context %s\n", uri); return NULL; } if (uriMap->mbean->debug > 1) env->l->jkLog(env, env->l, JK_LOG_DEBUG, "uriMap.mapUri() found ctx %s\n", ctxEnv->uri); match = jk2_uriMap_regexpMap(env, uriMap, ctxEnv->regexpMatch, uri); 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() regexp match %s %s\n", uri, match->workerName); return match; } /* As per Servlet spec, do exact match first */ match = jk2_uriMap_exactMap(env, uriMap, ctxEnv->exactMatch, 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() exact match %s %s\n", uri, match->workerName); return match; } /* Then prefix match */ match = jk2_uriMap_prefixMap(env, uriMap, ctxEnv->prefixMatch, uri, uriLen); if (match != NULL) { char c = uri[match->prefix_len - 1]; /* XXX Filter prefix matches to allow only exact matches with an optional path_info or query string at end. Fixes Bugzilla#12141, needs review.. */ if ((uriLen > match->prefix_len && (c=='/' || c=='?')) || uriLen == match->prefix_len) { /* restore */ if (url_rewrite) *url_rewrite = origChar; if (uriMap->mbean->debug > 0) env->l->jkLog(env, env->l, JK_LOG_DEBUG, "uriMap.mapUri() prefix match %s %s\n", uri, match->workerName); 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); if (suffix != NULL) { match = jk2_uriMap_suffixMap(env, uriMap, ctxEnv->suffixMatch, suffix, strlen(suffix)); 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() extension match %s %s\n", uri, match->workerName); return match; } } /* restore */ if (url_rewrite) *url_rewrite = origChar; if (uriMap->mbean->debug > 1) env->l->jkLog(env, env->l, JK_LOG_DEBUG, "uriMap.mapUri() no match found\n"); return NULL;}int JK_METHOD jk2_uriMap_factory(jk_env_t *env, jk_pool_t *pool, jk_bean_t *result, const char *type, const char *name){ jk_uriMap_t *uriMap; uriMap = (jk_uriMap_t *)pool->calloc(env, pool, sizeof(jk_uriMap_t)); if (!uriMap) { env->l->jkLog(env, env->l, JK_LOG_ERROR, "uriMap.factory() OutOfMemoryError\n"); return JK_ERR; } uriMap->pool = pool; jk2_map_default_create(env, &uriMap->maps, pool); jk2_map_default_create(env, &uriMap->vhosts, pool); jk2_map_default_create(env, &uriMap->vhcache, pool); uriMap->init = jk2_uriMap_init; uriMap->destroy = jk2_uriMap_destroy; uriMap->addUriEnv = jk2_uriMap_addUriEnv; uriMap->checkUri = jk2_uriMap_checkUri; uriMap->mapUri = jk2_uriMap_mapUri; result->object = uriMap; result->setAttribute = jk2_uriMap_setProperty; uriMap->mbean = result; return JK_OK;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -