config.c

来自「《jsp编程起步》里面的所有源代码」· C语言 代码 · 共 348 行

C
348
字号
/* * Copyright (c) 1999 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").  In particular, 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. * * 4. Caucho Technology requests that attribution be given to Resin *    in any manner possible.  We suggest using the "Resin Powered" *    button or creating a "powered by Resin(tm)" link to *    http://www.caucho.com for each page served by Resin. * * 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.       */#ifdef WIN32#include <winsock2.h>#else#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#endif#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include "cse.h"static location_t *cse_add_location(config_t *config, char *host, char *prefix, char *pattern){  location_t *location = (location_t *) cse_alloc(config, sizeof(location_t));  char cleanPrefix[4096];  int prefixLength;  int cleanLength;  cleanPrefix[0] = 0;  memset(location, 0, sizeof(*location));  location->next = config->locations;  config->locations = location;  location->host = host;  if (! prefix[0] && pattern[0] && pattern[0] != '/' && pattern[0] != '*')    strcpy(cleanPrefix, "/");  else if (prefix[0] && prefix[0] != '/')    strcpy(cleanPrefix, "/");  strcat(cleanPrefix, prefix);  prefixLength = strlen(cleanPrefix);  if (prefixLength > 0 && cleanPrefix[prefixLength - 1] == '/')    cleanPrefix[prefixLength - 1] = 0;  if (! pattern[0]) {    location->prefix = cse_strdup(config, cleanPrefix);    location->suffix = 0;    location->is_exact = 0;  }  else if (pattern[0] == '*') {    location->prefix = cse_strdup(config, cleanPrefix);    location->suffix = pattern + 1;    location->is_exact = 0;  }  else {    if (pattern[0] != '/')      strcat(cleanPrefix, "/");    strcat(cleanPrefix, pattern);    cleanLength = strlen(cleanPrefix);    if (cleanLength < 1 || cleanPrefix[cleanLength - 1] != '*')      location->is_exact = 1;    else if (cleanLength > 1)      cleanPrefix[cleanLength - 2] = 0;    else      cleanPrefix[cleanLength - 1] = 0;    location->prefix = cse_strdup(config, cleanPrefix);    location->suffix = 0;  }  cse_log("loc %s %s %s %x\n", location->host, location->prefix,	  location->suffix, location->next);  return location;}static voidcse_init_web_app(config_t *config, registry_t *node, char *host, char *prefix){  for (; node; node = node->next) {    if (! strcmp(node->key, "servlet-mapping")) {      registry_t *url_pattern = cse_next_link(node->first, "url-pattern");      if (url_pattern && url_pattern->value)	cse_add_location(config, host, prefix, url_pattern->value);    }  }}static voidcse_init_host(config_t *config, registry_t *node, char *host){  cse_init_web_app(config, node, host, "");  for (; node; node = node->next) {    if (node->value && ! strcmp(node->key, "web-app")) {      char *prefix = node->value;      cse_init_web_app(config, node->first, host, prefix);    }  }/*  cse_add_location(config, host, "", "*.jsp");  cse_add_location(config, host, "", "*.xtp");*/}static voidcse_init_server(config_t *config, registry_t *node){  cse_init_host(config, node, "");  for (; node; node = node->next) {    if (node->value && ! strcmp(node->key, "host")) {      char *name = node->value;      cse_init_host(config, node->first, name);    }  }}/** * Adds a new backup to the configuration */voidcse_add_host(config_t *config, const char *hostname, int port){  cse_add_host_int(config, hostname, port, 0);}/** * Adds a new backup to the configuration */voidcse_add_backup(config_t *config, const char *hostname, int port){  cse_add_host_int(config, hostname, port, 1);}static voidcse_init_host_node(config_t *config, registry_t *host_node,		   registry_t *port_node, int is_backup){  int port;  char *host;      if (port_node && port_node->value && *port_node->value)    port = atoi(port_node->value);  else    port = 6802;      if (host_node && host_node->value && *host_node->value)    host = host_node->value;  else    host = "localhost";  cse_add_host_int(config, host, port, is_backup);}static void cse_init_hosts(config_t *config, registry_t *top){  int has_srun = 0;  registry_t *node;    for (node = cse_next_link(top, "srun");       node;       node = cse_next_link(node->next, "srun")) {    registry_t *host_node = cse_next_link(node->first, "host");    registry_t *port_node = cse_next_link(node->first, "port");        has_srun = 1;    cse_init_host_node(config, host_node, port_node, 0);  }  for (node = cse_next_link(top, "srun-backup");       node;       node = cse_next_link(node->next, "srun-backup")) {    registry_t *host_node = cse_next_link(node->first, "host");    registry_t *port_node = cse_next_link(node->first, "port");        has_srun = 1;    cse_init_host_node(config, host_node, port_node, 1);  }  if (! has_srun) {    registry_t *host_node = cse_next_link(top, "srun-host");    registry_t *port_node = cse_next_link(top, "srun-port");    cse_init_host_node(config, host_node, port_node, 0);  }}voidcse_log_config(config_t *config){	location_t *loc = config->locations;	for (; loc; loc = loc->next) {	cse_log("cfg host:%s prefix:%s suffix:%s next:%x\n", 		loc->host ? loc->host : "null",		loc->prefix ? loc->prefix : "null",		loc->suffix ? loc->suffix : "null",		loc->next);	}}voidcse_init_config(config_t *config){  registry_t *root = 0;  registry_t *node = 0;  FILE *is;  is = fopen(config->path, "r");  if (! is) {    cse_error(config, "Resin cannot open config file `%s'\n", config->path);    return;  }  root = cse_parse(is, config);  fclose(is);  config->registry = root;  cse_print_registry(root);    if (root)    node = cse_next_link(root->first, "caucho.com");  if (node)    node = cse_next_link(node->first, "http-server");  if (node)    cse_init_server(config, node->first);  if (node)    cse_init_hosts(config, node->first);

  config->lock = cse_create_lock();}/** * tests if 'full' starts with 'part' */static intcse_starts_with(const char *full, const char *part){  char ch1, ch2;  while ((ch2 = *part++) && (ch1 = *full++) && ch1 == ch2) {  }  return ! ch2;}static intcse_match_suffix(const char *full, const char *suffix){  int len = strlen(suffix);  do {    char *match = strstr(full, suffix);    if (! match)      return 0;        if (! match[len] || match[len] == '/')      return 1;    full = match + len;  } while (*full);  return 0;}static intcse_match_location(location_t *loc, const char *host, const char *uri){  for (; loc; loc = loc->next) {    cse_log("match host:%s prefix:%s suffix:%s with host:%s uri:%s next:%x\n", 	    loc->host ? loc->host : "null",	    loc->prefix ? loc->prefix : "null",	    loc->suffix ? loc->suffix : "null",	    host, uri, loc->next);    if (*(loc->host) && strcmp(loc->host, host))      continue;    else if (loc->is_exact && ! strcmp(uri, loc->prefix))      return 1;        else if (loc->is_exact)      continue;          else if (! cse_starts_with(uri, loc->prefix))      continue;    if (loc->suffix && ! cse_match_suffix(uri, loc->suffix))      continue;    return 1;  }  return 0;}intcse_match_request(config_t *config, const char *host, const char *uri){  return (cse_match_location(config->locations, host, uri) ||	  cse_match_location(config->locations, "", uri));}

⌨️ 快捷键说明

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