📄 config.c
字号:
if (! is) { cse_error(config, "Resin can't open config file `%s'\n", config->path); /* XXX: need an atomic switch? */ *p_config = *config; return; } root = cse_parse(is, config, config->path); fclose(is); config->registry = root; /* cse_print_registry(root); */ if (root) node = cse_next_link(root->first, "caucho.com"); if (node) { registry_t *java = cse_next_link(node->first, "java"); if (java) { char *work_dir = cse_find_value(node->first, "work-dir"); if (! work_dir) { } else if (is_path_absolute(work_dir)) config->work_dir = work_dir; else { char buf[1024]; sprintf(buf, "%s/%s", config->resin_home, work_dir); config->work_dir = cse_strdup(config->p, buf); } } } if (! config->work_dir) { char buf[1024]; sprintf(buf, "%s/work", config->resin_home); config->work_dir = cse_strdup(config->p, buf); } if (node) node = cse_next_link(node->first, "http-server"); if (node) { registry_t *subnode; char *value; subnode = cse_next_link(node->first, "caucho-status"); caucho_status = cse_find_value(node->first, "caucho-status"); if (subnode && (! caucho_status || ! strcmp(caucho_status, "true") || ! strcmp(caucho_status, "yes"))) config->disable_caucho_status = 0; config->session_url_prefix = cse_find_value(node->first, "session-url-prefix"); if (! config->session_url_prefix || ! config->session_url_prefix[0]) config->session_url_prefix = ";jsessionid="; config->session_url_prefix_length = strlen(config->session_url_prefix); config->alt_session_prefix = cse_find_value(node->first, "alternate-session-url-prefix"); if (config->alt_session_prefix && config->alt_session_prefix[0] == '/') config->alt_session_prefix += 1; if (config->alt_session_prefix && ! config->alt_session_prefix[0]) config->alt_session_prefix = 0; if (config->alt_session_prefix) config->alt_session_prefix_length = strlen(config->alt_session_prefix); config->session_cookie = cse_find_value(node->first, "session-cookie"); if (! config->session_cookie || ! *config->session_cookie) config->session_cookie = "JSESSIONID="; else { char buf[256]; sprintf(buf, "%s=", config->session_cookie); config->session_cookie = cse_strdup(config->p, buf); } value = cse_find_value(node->first, "sticky-sessions"); if (value && (! strcmp(value, "false") || ! strcmp(value, "no"))) config->disable_sticky_sessions = 1; value = cse_find_value(node->first, "sessions-failover"); if (value && (! strcmp(value, "false") || ! strcmp(value, "no"))) config->disable_session_failover = 1; } if (node) cse_init_server(config, node); if (node) cse_init_hosts(config, node->first); if (node) cse_init_error_page(config, node->first); if (! config->lock) config->lock = cse_create_lock(config); /* XXX: need an atomic switch? */ *p_config = *config;}/** * Check the configuration files for any changes. If there are any changes, * reload the configuration files. */voidcse_update_config(config_t *config, int now){ struct stat st; depend_t *depend; int has_changed = 0; int diff = config->last_update - now; if (diff > -15 && diff < 15) return; config->last_update = now; for (depend = config->depend_list; depend && ! has_changed; depend = depend->next) { if (stat(depend->path, &st)) has_changed = depend->last_modified != 0; else if (depend->last_modified != st.st_mtime) has_changed = 1; else if (depend->last_size != st.st_size) has_changed = 1; if (has_changed) { LOG(("%s mod:%d->%d size:%d->%d\n", depend->path, depend->last_modified, st.st_mtime, depend->last_size, st.st_size)); } } if (has_changed) { cse_lock(config->lock); cse_close_sockets(config); cse_free_pool(config->p); cse_init_config(config); cse_unlock(config->lock); }}/** * tests if 'full' starts with 'part' * * If it's not an exact match, the next character of 'full' must be '/'. * That way, a servlet mapping of '/foo/ *' will match /foo, /foo/bar, but * not /foolish. */static intcse_starts_with(const char *full, const char *part){ char ch1, ch2; while ((ch2 = *part++) && (ch1 = *full++) && ch1 == ch2) { } if (ch2) return 0; else if (! *full) return 1; else if (*full == '/') return 1;#ifdef WIN32 /* special case so web-inf. will match */ if (full[0] != '.') return 0; else if (full[1] == 0 || full[1] == '/' || full[1] == '\\') return 1; else if (full[1] != '.') return 0; else if (full[2] == 0 || full[2] == '/' || full[2] == '\\') return 1;#endif return 0;}static intcse_match_suffix(const char *full, const char *suffix){ int len = strlen(suffix); int ch; do { char *match = strstr(full, suffix); if (! match) return 0; if (! match[len] || match[len] == '/') return 1;#ifdef WIN32 if ((ch = match[len]) == '.' || ch == ' ') { for (; (ch = match[len]) == '.' || ch == ' '; len++) { } if (! match[len] || match[len] == '/') return 1; }#endif full = match + len; } while (*full); return 0;}static inthex_to_digit(int hex){ if (hex >= '0' && hex <= '9') return hex - '0'; else if (hex >= 'a' && hex <= 'f') return hex - 'a' + 10; else if (hex >= 'A' && hex <= 'F') return hex - 'A' + 10; else return 0;}static intcse_is_match(config_t *config, const char *raw_host, int port, const char *raw_uri, int unescape){ char uri[16 * 1024]; char host[1024]; char *suburi; int len = sizeof(uri) - 1; int host_len = sizeof(host) - 1; web_app_t *app_ptr; web_app_t *app; int has_host; unsigned int best_len; location_t *loc; int i, k; int ch; int test_ch = config->session_url_prefix[0]; k = 0; for (i = 0; (ch = raw_uri[i]) && i + 1 < len; i++) { /* strip the session_url_prefix */ if (ch == test_ch && ! strncmp(raw_uri + i, config->session_url_prefix, config->session_url_prefix_length)) { break; } if (ch == '%' && unescape) { int h1 = raw_uri[i + 1]; if (h1 == 'u') { ch = hex_to_digit(raw_uri[i + 2]); ch = 16 * ch + hex_to_digit(raw_uri[i + 3]); ch = 16 * ch + hex_to_digit(raw_uri[i + 4]); ch = 16 * ch + hex_to_digit(raw_uri[i + 5]); i += 4; } else { ch = hex_to_digit(h1); ch = 16 * ch + hex_to_digit(raw_uri[i + 2]); i += 2; if ((ch & 0xf0) == 0xc0 && raw_uri[i + 1] == '%' && i + 3 < len) { int ch2 = hex_to_digit(raw_uri[i + 2]); ch2 = 16 * ch2 + hex_to_digit(raw_uri[i + 3]); if (ch2 >= 0x80) { ch = ((ch & 0x3f) << 6) + (ch2 & 0x3f); i += 3; } } } } else if (ch == ':') break; if (ch == '/' && k > 0 && (uri[k - 1] == '/' || uri[k - 1] == '\\')) continue;#ifdef WIN32 if (ch >= 0 && isupper(ch)) uri[k++] = tolower(ch); else if (ch >= 0) uri[k++] = ch;#else if (ch >= 0) uri[k++] = ch;#endif } uri[k] = 0; for (i = 0; raw_host && (ch = raw_host[i]) && i + 1 < host_len; i++) { if (isupper(ch)) host[i] = tolower(ch); else host[i] = ch; } host[i] = 0; has_host = 0; best_len = 0; app = 0; for (app_ptr = config->applications; app_ptr; app_ptr = app_ptr->next) { /** * If have matched a host, then the default host is ignored. */ if (has_host && app_ptr->host && ! *app_ptr->host) continue; /** * The uri prefix must match. */ if (! cse_starts_with(uri, app_ptr->prefix)) continue; if (app_ptr->host_alias_length > 0) { int i; for (i = 0; i < app_ptr->host_alias_length; i++) { caucho_host_t *host_alias = app_ptr->host_aliases[i]; if (strcmp(host_alias->host, host)) continue; if (! port || ! host_alias->port || port == host_alias->port) break; } /* If no host matches, then this isn't a match. */ if (i >= app_ptr->host_alias_length) continue; if (! has_host) { has_host = 1; best_len = 0; app = 0; } } if (strlen(app_ptr->prefix) < best_len) continue; LOG(("app-match host:%s%s with host:%s uri:%s\n", app_ptr->host ? app_ptr->host : "null", app_ptr->prefix ? app_ptr->prefix : "", host, uri)); best_len = strlen(app_ptr->prefix); app = app_ptr; } if (! app) return 0; suburi = uri + best_len; for (loc = app->locations; loc; loc = loc->next) { LOG(("match host:%s%s prefix:%s suffix:%s with host:%s uri:%s next:%x\n", app->host ? app->host : "null", app->prefix ? app->prefix : "null", loc->prefix ? loc->prefix : "null", loc->suffix ? loc->suffix : "null", host, uri, loc->next)); if (loc->is_exact && ! strcmp(suburi, loc->prefix)) { return ! loc->ignore; } else if (loc->is_exact) continue; else if (! cse_starts_with(suburi, loc->prefix)) continue; else if (loc->suffix && ! cse_match_suffix(suburi, loc->suffix)) continue; return ! loc->ignore; } if (strstr(suburi, "/j_security_check")) return 1; return 0;}/** * Tests if the request matches a Resin URL. * * @param config the plugin configuration structure * @param host the request's host * @param port the request's port * @param url the request's uri */intcse_match_request(config_t *config, const char *host, int port, const char *uri, int unescape){ int hash = port; int i; int is_match = 0; hash_t *entry; char *test_uri; char *test_host; int test_port; int test_count; int test_match; if (! host) host = ""; if (! uri) uri = ""; for (i = 0; host[i]; i++) hash = 65531 * hash + host[i]; for (i = 0; uri[i]; i++) hash = 65531 * hash + uri[i]; if (hash < 0) hash = -hash; hash = hash % CACHE_SIZE; entry = &g_url_cache[hash]; test_count = entry->count; test_uri = entry->uri; test_host = entry->host; test_port = entry->port; test_match = entry->is_match; if (test_count != entry->count) { } else if (! test_uri || strcmp(test_uri, uri)) { } else if (! test_host || strcmp(test_host, host)) { } else if (test_port && test_port != port) { } else return test_match; is_match = cse_is_match(config, host, port, uri, unescape); cse_lock(config->lock); entry->count++; entry->is_match = is_match; if (entry->uri) { free(entry->host); free(entry->uri); entry->uri = 0; entry->host = 0; } LOG(("entry %s %s\n", host, uri)); entry->host = strdup(host ? host : ""); entry->uri = strdup(uri); entry->port = port; entry->count++; cse_unlock(config->lock); return is_match;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -