jk_uri_worker_map.c

来自「Tomcat 4.1与WebServer集成组件的源代码包.」· C语言 代码 · 共 604 行 · 第 1/2 页

C
604
字号
		} else if ('\0' != asterisk[2]) {		    /* general suffix rule */		    asterisk[1] = '\0';		    uwr->worker_name = worker;		    uwr->context = uri;		    uwr->suffix  = asterisk + 2;		    uwr->match_type = MATCH_TYPE_GENERAL_SUFFIX;		    jk_log(l, JK_LOG_DEBUG,			   "Into jk_uri_worker_map_t::uri_worker_map_open, "			   "general suffix rule %s*%s=%s was added\n",			   uri, asterisk + 2, worker);		} else {		    /* context based */		    asterisk[1]      = '\0';		    uwr->worker_name = worker;		    uwr->context     = uri;		    uwr->suffix      = NULL;		    uwr->match_type  = MATCH_TYPE_CONTEXT;		    jk_log(l, JK_LOG_DEBUG,			   "Into jk_uri_worker_map_t::uri_worker_map_open, "			   "match rule %s=%s was added\n",			   uri, worker);                }            } else {                /* Something like : JkMount /servlets/exampl* ajp13 */                uwr->uri         = uri;                uwr->worker_name = worker;                uwr->context     = uri;                uwr->suffix      = NULL;                uwr->match_type  = MATCH_TYPE_EXACT;                jk_log(l, JK_LOG_DEBUG,                       "Into jk_uri_worker_map_t::uri_worker_map_open, exact rule %s=%s was added\n",                        uri, worker);            }        } else {            /* Something like:  JkMount /login/j_security_check ajp13 */            uwr->uri         = uri;            uwr->worker_name = worker;            uwr->context     = uri;            uwr->suffix      = NULL;            uwr->match_type  = MATCH_TYPE_EXACT;            jk_log(l, JK_LOG_DEBUG,                   "Into jk_uri_worker_map_t::uri_worker_map_open, exact rule %s=%s was added\n",                    uri, worker);        }        uwr->ctxt_len = strlen(uwr->context);    } else {        /*         * JFC: please check...         * Not sure what to do, but I try to prevent problems.         * I have fixed jk_mount_context() in apaches/mod_jk.c so we should         * not arrive here when using Apache.         */        jk_log(l, JK_LOG_ERROR,               "jk_uri_worker_map_t::uri_worker_map_add, invalid context %s\n",               uri);        return JK_FALSE;    }    uw_map->maps[uw_map->size] = uwr;    uw_map->size++;    return JK_TRUE;}int uri_worker_map_open(jk_uri_worker_map_t *uw_map,                        jk_map_t *init_data,                        jk_logger_t *l){    int rc = JK_TRUE;    jk_log(l, JK_LOG_DEBUG, "Into jk_uri_worker_map_t::uri_worker_map_open\n");    uw_map->size     = 0;    uw_map->capacity = 0;    if (uw_map) {        int sz;        rc = JK_TRUE;        jk_open_pool(&uw_map->p,                      uw_map->buf,                      sizeof(jk_pool_atom_t) * SMALL_POOL_SIZE);        jk_open_pool(&uw_map->tp,                     uw_map->tbuf,                     sizeof(jk_pool_atom_t) * SMALL_POOL_SIZE);        uw_map->size = 0;        uw_map->maps = NULL;                sz = map_size(init_data);        jk_log(l, JK_LOG_DEBUG, "jk_uri_worker_map_t::uri_worker_map_open, rule map size is %d\n", sz);        if (sz > 0) {            int i;            for(i = 0; i < sz ; i++) {                if (uri_worker_map_add(uw_map, map_name_at(init_data, i), map_value_at(init_data, i), l) == JK_FALSE) {                    rc = JK_FALSE;                    break;                }            }            if (i == sz) {                jk_log(l, JK_LOG_DEBUG, "Into jk_uri_worker_map_t::uri_worker_map_open, there are %d rules\n", uw_map->size);                } else {                jk_log(l, JK_LOG_ERROR, "jk_uri_worker_map_t::uri_worker_map_open, There was a parsing error\n");                rc = JK_FALSE;            }        }               if (rc == JK_FALSE) {            jk_log(l, JK_LOG_ERROR, "jk_uri_worker_map_t::uri_worker_map_open, there was an error, freing buf\n");            jk_close_pool(&uw_map->p);            jk_close_pool(&uw_map->tp);        }    }        jk_log(l, JK_LOG_DEBUG, "jk_uri_worker_map_t::uri_worker_map_open, done\n");    return rc;}/* returns the index of the last occurrence of the 'ch' character   if ch=='\0' returns the length of the string str  */int 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);}int uri_worker_map_close(jk_uri_worker_map_t *uw_map,                         jk_logger_t *l){    jk_log(l, JK_LOG_DEBUG,            "Into jk_uri_worker_map_t::uri_worker_map_close\n");     if(uw_map) {        jk_close_pool(&uw_map->p);        jk_close_pool(&uw_map->tp);	return JK_TRUE;    }    jk_log(l, JK_LOG_ERROR,            "jk_uri_worker_map_t::uri_worker_map_close, NULL parameter\n");     return JK_FALSE;}char *map_uri_to_worker(jk_uri_worker_map_t *uw_map,                        const char *uri,                        jk_logger_t *l){    jk_log(l, JK_LOG_DEBUG,            "Into jk_uri_worker_map_t::map_uri_to_worker\n");        if(uw_map && uri && '/' == uri[0]) {        unsigned i;        unsigned best_match = -1;        unsigned longest_match = 0;        char * clean_uri = NULL;        char *url_rewrite = strstr(uri, JK_PATH_SESSION_IDENTIFIER);                if(url_rewrite) {            clean_uri = jk_pool_strdup(&uw_map->tp,uri);            url_rewrite = strstr(clean_uri, JK_PATH_SESSION_IDENTIFIER);            *url_rewrite = '\0';            uri = clean_uri;        }		jk_log(l, JK_LOG_DEBUG, "Attempting to map URI '%s'\n", uri);        for(i = 0 ; i < uw_map->size ; i++) {            uri_worker_record_t *uwr = uw_map->maps[i];            if(uwr->ctxt_len < longest_match) {                continue; /* can not be a best match anyway */            }            if(0 == strncmp(uwr->context,                             uri,                             uwr->ctxt_len)) {                if(MATCH_TYPE_EXACT == uwr->match_type) {                    if(strlen(uri) == uwr->ctxt_len) {			jk_log(l,			       JK_LOG_DEBUG,			       "jk_uri_worker_map_t::map_uri_to_worker, "			       "Found an exact match %s -> %s\n",			       uwr->worker_name,			       uwr->context );                        jk_reset_pool(&uw_map->tp);                        return uwr->worker_name;                    }                } else if(MATCH_TYPE_CONTEXT == uwr->match_type) {                    if(uwr->ctxt_len > longest_match) {			jk_log(l,			       JK_LOG_DEBUG,			       "jk_uri_worker_map_t::map_uri_to_worker, "			       "Found a context match %s -> %s\n",			       uwr->worker_name,			       uwr->context );                        longest_match = uwr->ctxt_len;                        best_match = i;                    }		} else if(MATCH_TYPE_GENERAL_SUFFIX == uwr->match_type) {                    int suffix_start=last_index_of(uri,uwr->suffix[0]);                    if (suffix_start>=0 && 0==strcmp(uri+suffix_start,uwr->suffix)) {			if(uwr->ctxt_len >= longest_match) {			    jk_log(l,				   JK_LOG_DEBUG,				   "jk_uri_worker_map_t::map_uri_to_worker, "				   "Found a general suffix match %s -> *%s\n",				   uwr->worker_name,				   uwr->suffix );			    longest_match = uwr->ctxt_len;			    best_match = i;			}                    }                } else if(MATCH_TYPE_CONTEXT_PATH == uwr->match_type) {                    char *suffix_path = NULL;                    if (strlen(uri) > 1 && (suffix_path = strchr(uri+1,'/')) != NULL) {                        if (0 == strncmp(suffix_path,uwr->suffix,strlen(uwr->suffix))) {                            if(uwr->ctxt_len >= longest_match) {                                jk_log(l,                                       JK_LOG_DEBUG,                                       "jk_uri_worker_map_t::map_uri_to_worker, "                                       "Found a general context path match %s -> *%s\n",                                       uwr->worker_name,                                       uwr->suffix );                                   longest_match = uwr->ctxt_len;                                best_match = i;                            }                        }                    }                } else /* suffix match */ {                    int suffix_start;                                        for(suffix_start = strlen(uri) - 1 ;                         suffix_start > 0 && '.' != uri[suffix_start];                         suffix_start--)                         ;                    if('.' == uri[suffix_start]) {                        const char *suffix = uri + suffix_start + 1;                        /* for WinXX, fix the JsP != jsp problems */#ifdef WIN32                                                if(0 == strcasecmp(suffix, uwr->suffix))  {#else                        if(0 == strcmp(suffix, uwr->suffix)) {#endif                            if(uwr->ctxt_len >= longest_match) {				jk_log(l,				       JK_LOG_DEBUG,				       "jk_uri_worker_map_t::map_uri_to_worker, Found a suffix match %s -> *.%s\n",				       uwr->worker_name,				       uwr->suffix );                                longest_match = uwr->ctxt_len;                                best_match = i;                            }                        }                    }                                                       }            }        }        if(-1 != best_match) {            jk_reset_pool(&uw_map->tp);            return uw_map->maps[best_match]->worker_name;        } else {            /*             * We are now in a security nightmare, it maybe that somebody sent              * us a uri that looks like /top-secret.jsp. and the web server will              * fumble and return the jsp content.              *             * To solve that we will check for path info following the suffix, we              * will also check that the end of the uri is not .suffix.             */            int fraud = check_security_fraud(uw_map, uri, l);            if(fraud >= 0) {                jk_log(l, JK_LOG_EMERG,                        "In jk_uri_worker_map_t::map_uri_to_worker, found a security fraud in '%s'\n",                       uri);                    jk_reset_pool(&uw_map->tp);                return uw_map->maps[fraud]->worker_name;            }            jk_reset_pool(&uw_map->tp);       }            } else {        jk_log(l, JK_LOG_ERROR,                "In jk_uri_worker_map_t::map_uri_to_worker, wrong parameters\n");        }    jk_log(l, JK_LOG_DEBUG,            "jk_uri_worker_map_t::map_uri_to_worker, done without a match\n");     return NULL;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?