📄 config.c
字号:
/* * Copyright (c) 1999-2002 Caucho Technology. All rights reserved. * * Caucho Technology permits redistribution, modification and use * of this file in source and binary form ("the Software") under the * Caucho Developer Source License ("the License"). The following * conditions must be met: * * 1. Each copy or derived work of the Software must preserve the copyright * notice and this notice unmodified. * * 2. Redistributions of the Software in source or binary form must include * an unmodified copy of the License, normally in a plain ASCII text * * 3. The names "Resin" or "Caucho" are trademarks of Caucho Technology and * may not be used to endorse products derived from this software. * "Resin" or "Caucho" may not appear in the names of products derived * from this software. * * This Software is provided "AS IS," without a warranty of any kind. * ALL EXPRESS OR IMPLIED REPRESENTATIONS AND WARRANTIES, INCLUDING ANY * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. * * CAUCHO TECHNOLOGY AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE OR ANY THIRD PARTY AS A RESULT OF USING OR * DISTRIBUTING SOFTWARE. IN NO EVENT WILL Caucho OR ITS LICENSORS BE LIABLE * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR * INABILITY TO USE SOFTWARE, EVEN IF HE HAS BEEN ADVISED OF THE POSSIBILITY * OF SUCH DAMAGES. *//* * config.c is responsible for scanning the parsed registry and grabbing * relevant data. * * Important data include the web-app and the servlet-mapping so any filter * can properly dispatch the request. * * Also, config.c needs to grab the srun and srun-backup blocks to properly * send the requests to the proper JVM. */#ifdef WIN32#include <winsock2.h>#else#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#include <dirent.h>#endif#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#include <time.h>#include <sys/stat.h>#include "cse.h"#define CACHE_SIZE 16384typedef struct hash_t { char *host; int port; char *uri; int is_match; volatile int count;} hash_t;static hash_t g_url_cache[CACHE_SIZE];static location_t *cse_add_unique_location(web_app_t *app, char *prefix, char *suffix, int is_exact, int ignore){ config_t *config = app->config; location_t *loc; for (loc = app->locations; loc; loc = loc->next) { if (is_exact != loc->is_exact) continue; else if ((prefix == 0) != (loc->prefix == 0)) continue; else if (prefix && strcmp(prefix, loc->prefix)) continue; else if ((suffix == 0) != (loc->suffix == 0)) continue; else if (suffix && strcmp(suffix, loc->suffix)) continue; return loc; } loc = (location_t *) cse_alloc(config->p, sizeof(location_t)); memset(loc, 0, sizeof(location_t)); loc->next = app->locations; app->locations = loc; loc->application = app; loc->prefix = prefix; loc->suffix = suffix; loc->is_exact = is_exact; loc->ignore = ignore; /* LOG(("loc %s:%d %s %s %x %s\n", loc->host ? loc->host : "(null)", loc->port, loc->prefix ? loc->prefix : "(null)", loc->suffix ? loc->suffix : "(null)", loc->next, loc->ignore ? "ignore" : "")); */ return loc;}static voidcse_add_app_host(config_t *config, web_app_t *app, char *host_name, int port){ int i; caucho_host_t **new_aliases; int new_length; caucho_host_t *host; for (i = 0; i < app->host_alias_length; i++) { host = app->host_aliases[i]; if (! strcmp(host->host, host_name) && host->port == port) return; } new_length = app->host_alias_length + 1; new_aliases = (caucho_host_t **) cse_alloc(config->p, new_length * sizeof(caucho_host_t *)); memset(new_aliases, 0, new_length * sizeof(caucho_host_t *)); for (i = 0; i < app->host_alias_length; i++) { new_aliases[i] = app->host_aliases[i]; } host = (caucho_host_t *) cse_alloc(config->p, sizeof(caucho_host_t)); memset(host, 0, sizeof(caucho_host_t)); host->host = cse_strdup(config->p, host_name); host->port = port; new_aliases[new_length - 1] = host; app->host_aliases = new_aliases; app->host_alias_length = new_length;}static voidcse_add_hosts(config_t *config, web_app_t *app, char *host){ while (host && *host) { char host_buf[8192]; int port; int i; for (; *host && (isspace(*host) || *host == ','); host++) { } if (! *host) return; i = 0; for (; *host && *host != ':' && ! isspace(*host) && *host != ','; host++) host_buf[i++] = *host; host_buf[i] = 0; port = 0; if (*host == ':') port = atoi(host + 1); for (; *host && ! isspace(*host) && *host != ','; host++) { } cse_add_app_host(config, app, host_buf, port); }}static web_app_t *cse_add_web_app(config_t *config, char *host, char *prefix){ web_app_t *app; if (! host) host = ""; else if (! strcmp(host, "*")) host = ""; if (! prefix) prefix = ""; else if (! strcmp(prefix, "/")) prefix = ""; for (app = config->applications; app; app = app->next) { if ((host == 0) != (app->host == 0)) continue; else if (host && strcmp(host, app->host)) continue; else if (strcmp(prefix, app->prefix)) continue; return app; } app = (web_app_t *) cse_alloc(config->p, sizeof(web_app_t)); memset(app, 0, sizeof(web_app_t)); app->next = config->applications; config->applications = app; app->config = config; app->host = host ? host : ""; app->prefix = cse_strdup(config->p, prefix); cse_add_hosts(config, app, host); LOG(("new web-app host:%s path:%s\n", app->host, app->prefix)); return app;}/** * Add an application pattern to the list of recognized locations * * @param config the configuration * @param host the host for the pattern * @param prefix the web-app prefix * * @return the new application */static web_app_t *cse_add_application(config_t *config, char *host, char *prefix){ char loc_prefix[8192]; int i, j; if (host) { host = cse_strdup(config->p, host); for (i = 0; host[i]; i++) host[i] = tolower(host[i]); } i = 0; if (prefix && *prefix && *prefix != '/') loc_prefix[i++] = '/'; #ifdef WIN32 if (prefix) { for (j = 0; prefix[j]; j++) loc_prefix[i++] = tolower(prefix[j]); }#else if (prefix) { for (j = 0; prefix[j]; j++) loc_prefix[i++] = prefix[j]; }#endif loc_prefix[i] = 0; return cse_add_web_app(config, host, loc_prefix);}/** * Add a location pattern to the list of recognized locations * * @param app the containing application * @param pattern the url-pattern to match * * @return the new location */static voidcse_add_location(web_app_t *app, char *pattern, char *servlet_name){ config_t *config = app->config; char cleanPrefix[4096]; int prefixLength; int cleanLength; char *loc_prefix = 0; char *loc_suffix = 0; int loc_is_exact = 0; int ignore = 0; #ifdef WIN32 if (pattern) { int i; pattern = cse_strdup(config->p, pattern); for (i = 0; pattern[i]; i++) pattern[i] = tolower(pattern[i]); }#endif /* WIN32 */ cleanPrefix[0] = 0; if (pattern[0] && pattern[0] != '/' && pattern[0] != '*') strcpy(cleanPrefix, "/"); prefixLength = strlen(cleanPrefix); if (prefixLength > 0 && cleanPrefix[prefixLength - 1] == '/') cleanPrefix[prefixLength - 1] = 0; if (! pattern[0]) { loc_prefix = cse_strdup(config->p, cleanPrefix); loc_suffix = 0; } else if (pattern[0] == '*') { loc_prefix = cse_strdup(config->p, cleanPrefix); loc_suffix = pattern + 1; } else { if (pattern[0] != '/') strcat(cleanPrefix, "/"); strcat(cleanPrefix, pattern); cleanLength = strlen(cleanPrefix); if (strlen(pattern) <= 1) cleanPrefix[cleanLength - 1] = 0; else if (cleanPrefix[cleanLength - 1] != '*') loc_is_exact = 1; else if (cleanLength >= 2 && cleanPrefix[cleanLength - 2] == '/') cleanPrefix[cleanLength - 2] = 0; else if (cleanLength > 1) cleanPrefix[cleanLength - 1] = 0; loc_prefix = cse_strdup(config->p, cleanPrefix); loc_suffix = 0; } if (servlet_name && ! strcmp(servlet_name, "plugin_ignore")) ignore = 1; cse_add_unique_location(app, loc_prefix, loc_suffix, loc_is_exact, ignore);}/** * Returns true if the path is an absolute path */static intis_path_absolute(char *path){ return (path[0] == '/' || path[0] == '\\' || (path[1] == ':' && ((path[0] >= 'a' && path[0] <= 'z') || (path[0] >= 'A' && path[0] <= 'Z'))));}static char *cse_normalize_path(config_t *config, char *pwd, char *path){ char buf[4096]; if (is_path_absolute(path)) return path; sprintf(buf, "%s/%s", pwd, path); return cse_strdup(config->p, buf);}static char *cse_app_dir(config_t *config, registry_t *node, char *parent, char *war_expand_dir){ char buf[4096]; char *value; char *dot; if (! node) return parent; value = cse_find_value(node->first, "app-dir"); if ((! value || ! value[0]) && strcmp(node->key, "web-app")) value = cse_find_value(node->first, "root-dir"); if ((! value || ! value[0]) && strcmp(node->key, "web-app")) value = cse_find_value(node->first, "doc-dir"); value = cse_find_value(node->first, "app-dir"); if (! parent || ! *parent) parent = "."; if ((! value || ! value[0]) && ! strcmp(node->key, "web-app")) { value = node->value; if (value && value[0] == '/') value++; } if (! value || ! value[0]) { /* LOG(("app-dir(p) %s\n", parent)); */ return parent; } /* Handle war expansion */ dot = strrchr(value, '.'); if (war_expand_dir && dot && (! strcmp(dot, ".war") || ! strcmp(dot, ".jar"))) { char buf[1024]; int p; int i; strcpy(buf, war_expand_dir); strcat(buf, "/_"); i = strlen(buf); for (p = dot - value; p >= 0; p--) { if (value[p] == '/' || value[p] == '\\') break; } p++; for (; p < dot - value; p++) buf[i++] = value[p]; buf[i] = 0; /* LOG(("app-dir(w) %s\n", buf)); */ return strdup(buf); } /* XXX: in theory only for win32 */ if (is_path_absolute(value)) { /* LOG(("app-dir(a) %s\n", value)); */ return value; } /* since this only happens on startup, the memory leak is okay */ sprintf(buf, "%s/%s", parent, value); /* LOG(("app-dir(r) %s\n", buf)); */ return cse_strdup(config->p, buf);}static voidcse_init_web_app_contents(web_app_t *app, registry_t *node){ /* * The following can't be added automatically since it takes control * away from the configuration and breaks some user's configurations. */ /* * cse_add_location(config, host, prefix, "*.jsp", 0); * cse_add_location(config, host, prefix, "*.xtp", 0); */ /* WEB-INF passes to Resin so Resin can hide it. */ cse_add_location(app, "/WEB-INF*", 0); cse_add_location(app, "/META-INF*", 0); for (; node; node = node->next) { if (! strcmp(node->key, "servlet-mapping")) { registry_t *url_pattern = cse_next_link(node->first, "url-pattern"); registry_t *servlet_name = cse_next_link(node->first, "servlet-name"); if (url_pattern && url_pattern->value) cse_add_location(app, url_pattern->value, servlet_name ? servlet_name->value : 0); } }}/** * Adds a configuration file as a dependency. If the file changes, * the plugin will reread the configuration. * * @param config the server configuration.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -