jk_uri_worker_map.c
来自「Tomcat 4.1与WebServer集成组件的源代码包.」· C语言 代码 · 共 604 行 · 第 1/2 页
C
604 行
/* ========================================================================= * * * * The Apache Software License, Version 1.1 * * * * Copyright (c) 1999-2001 The Apache Software Foundation. * * All rights reserved. * * * * ========================================================================= * * * * Redistribution and use in source and binary forms, with or without modi- * * fication, are permitted provided that the following conditions are met: * * * * 1. Redistributions of source code must retain the above copyright notice * * notice, this list of conditions and the following disclaimer. * * * * 2. Redistributions in binary form must reproduce the above copyright * * notice, this list of conditions and the following disclaimer in the * * documentation and/or other materials provided with the distribution. * * * * 3. The end-user documentation included with the redistribution, if any, * * must include the following acknowlegement: * * * * "This product includes software developed by the Apache Software * * Foundation <http://www.apache.org/>." * * * * Alternately, this acknowlegement may appear in the software itself, if * * and wherever such third-party acknowlegements normally appear. * * * * 4. The names "The Jakarta Project", "Jk", and "Apache Software * * Foundation" must not be used to endorse or promote products derived * * from this software without prior written permission. For written * * permission, please contact <apache@apache.org>. * * * * 5. Products derived from this software may not be called "Apache" nor may * * "Apache" appear in their names without prior written permission of the * * Apache Software Foundation. * * * * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES * * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * * THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY * * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * * POSSIBILITY OF SUCH DAMAGE. * * * * ========================================================================= * * * * This software consists of voluntary contributions made by many indivi- * * duals on behalf of the Apache Software Foundation. For more information * * on the Apache Software Foundation, please see <http://www.apache.org/>. * * * * ========================================================================= *//*************************************************************************** * Description: URI to worker map object. * * Maps can be * * * * Exact Context -> /exact/uri=worker e.g. /examples/do*=ajp12 * * Context Based -> /context/*=worker e.g. /examples/*=ajp12 * * Context and suffix ->/context/*.suffix=worker e.g. /examples/*.jsp=ajp12* * * * This lets us either partition the work among the web server and the * * servlet container. * * * * Author: Gal Shachor <shachor@il.ibm.com> * * Version: $Revision: 1.14 $ * ***************************************************************************/#include "jk_pool.h"#include "jk_util.h"#include "jk_uri_worker_map.h"#define MATCH_TYPE_EXACT (0)#define MATCH_TYPE_CONTEXT (1)#define MATCH_TYPE_SUFFIX (2)#define MATCH_TYPE_GENERAL_SUFFIX (3) /* match all URIs of the form *ext *//* match all context path URIs with a path component suffix */#define MATCH_TYPE_CONTEXT_PATH (4)struct uri_worker_record { /* Original uri for logging */ char *uri; /* Name of worker mapped */ char *worker_name; /* Suffix of uri */ char *suffix; /* Base context */ char *context; /* char length of the context */ unsigned ctxt_len; int match_type;};typedef struct uri_worker_record uri_worker_record_t;struct jk_uri_worker_map { /* Memory Pool */ jk_pool_t p; jk_pool_atom_t buf[SMALL_POOL_SIZE]; /* Temp Pool */ jk_pool_t tp; jk_pool_atom_t tbuf[SMALL_POOL_SIZE]; /* map URI->WORKER */ uri_worker_record_t **maps; /* Map Number */ unsigned size; /* Map Capacity */ unsigned capacity;};/* * 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.", * ".suffix/", or ".suffix ". */static int check_security_fraud(jk_uri_worker_map_t *uw_map, const char *uri, jk_logger_t *l){ unsigned i; for(i = 0 ; i < uw_map->size ; i++) { if(MATCH_TYPE_SUFFIX == uw_map->maps[i]->match_type) { char *suffix_start; for(suffix_start = strstr(uri, uw_map->maps[i]->suffix) ; suffix_start ; suffix_start = strstr(suffix_start + 1, uw_map->maps[i]->suffix)) { if('.' != *(suffix_start - 1)) { continue; } else { char *after_suffix = suffix_start + strlen(uw_map->maps[i]->suffix); if((('.' == *after_suffix) || ('/' == *after_suffix) || (' ' == *after_suffix)) && (0 == strncmp(uw_map->maps[i]->context, uri, uw_map->maps[i]->ctxt_len))) { /* * Security violation !!! * this is a fraud. */ return i; } } } } } return -1;}int uri_worker_map_alloc(jk_uri_worker_map_t **uw_map, jk_map_t *init_data, jk_logger_t *l){ jk_log(l, JK_LOG_DEBUG, "Into jk_uri_worker_map_t::uri_worker_map_alloc\n"); if(init_data && uw_map) { return uri_worker_map_open(*uw_map = (jk_uri_worker_map_t *)malloc(sizeof(jk_uri_worker_map_t)), init_data, l); } jk_log(l, JK_LOG_ERROR, "In jk_uri_worker_map_t::uri_worker_map_alloc, NULL parameters\n"); return JK_FALSE;}int uri_worker_map_free(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_free\n"); if(uw_map && *uw_map) { uri_worker_map_close(*uw_map, l); free(*uw_map); *uw_map = NULL; return JK_TRUE; } else jk_log(l, JK_LOG_ERROR, "In jk_uri_worker_map_t::uri_worker_map_free, NULL parameters\n"); return JK_FALSE;}/* * Ensure there will be memory in context info to store Context Bases */#define UW_INC_SIZE 4 /* 4 URI->WORKER STEP */static int uri_worker_map_realloc(jk_uri_worker_map_t *uw_map){ if (uw_map->size == uw_map->capacity) { uri_worker_record_t **uwr; int capacity = uw_map->capacity + UW_INC_SIZE; uwr = (uri_worker_record_t **)jk_pool_alloc(&uw_map->p, sizeof(uri_worker_record_t *) * capacity); if (! uwr) return JK_FALSE; if (uw_map->capacity && uw_map->maps) memcpy(uwr, uw_map->maps, sizeof(uri_worker_record_t *) * uw_map->capacity); uw_map->maps = uwr; uw_map->capacity = capacity; } return JK_TRUE;}int uri_worker_map_add(jk_uri_worker_map_t *uw_map, char *puri, char *pworker, jk_logger_t *l){ uri_worker_record_t *uwr; char *uri; char *worker; if (uri_worker_map_realloc(uw_map) == JK_FALSE) return JK_FALSE; uwr = (uri_worker_record_t *)jk_pool_alloc(&uw_map->p, sizeof(uri_worker_record_t)); if (! uwr) { jk_log(l, JK_LOG_ERROR, "jk_uri_worker_map_t::uri_worker_map_add, can't alloc map entry\n"); return JK_FALSE; } uri = jk_pool_strdup(&uw_map->p, puri); worker = jk_pool_strdup(&uw_map->p, pworker); if (!uri || ! worker) { jk_log(l, JK_LOG_ERROR, "jk_uri_worker_map_t::uri_worker_map_add, can't alloc uri/worker strings\n"); return JK_FALSE; } if ('/' == uri[0]) { char *asterisk = strchr(uri, '*'); if (asterisk) { uwr->uri = jk_pool_strdup(&uw_map->p, uri); if (!uwr->uri) { jk_log(l, JK_LOG_ERROR, "jk_uri_worker_map_t::uri_worker_map_add, can't alloc uri string\n"); return JK_FALSE; } /* * Now, lets check that the pattern is /context/*.suffix * or /context/* * we need to have a '/' then a '*' and the a '.' or a * '/' then a '*' */ asterisk--; if ('/' == asterisk[0]) { if ( 0 == strncmp("/*/",uri,3) ) { /* general context path */ asterisk[1] = '\0'; uwr->worker_name = worker; uwr->context = uri; uwr->suffix = asterisk + 2; uwr->match_type = MATCH_TYPE_CONTEXT_PATH; jk_log(l, JK_LOG_DEBUG, "Into jk_uri_worker_map_t::uri_worker_map_open, " "general context path rule %s*%s=%s was added\n", uri, asterisk + 2, worker); } else if ('.' == asterisk[2]) { /* suffix rule */ asterisk[1] = asterisk[2] = '\0'; uwr->worker_name = worker; uwr->context = uri; uwr->suffix = asterisk + 3; uwr->match_type = MATCH_TYPE_SUFFIX; jk_log(l, JK_LOG_DEBUG, "Into jk_uri_worker_map_t::uri_worker_map_open, " "suffix rule %s.%s=%s was added\n", uri, asterisk + 3, worker);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?