Browse Source

Allow multiple config lines for lists, patterns and header lines

bel2125 8 years ago
parent
commit
c29c1b6e9a
3 changed files with 30 additions and 18 deletions
  1. 3 1
      include/civetweb.h
  2. 11 11
      src/civetweb.c
  3. 16 6
      src/main.c

+ 3 - 1
include/civetweb.h

@@ -520,7 +520,9 @@ enum {
 	CONFIG_TYPE_FILE = 0x3,
 	CONFIG_TYPE_DIRECTORY = 0x4,
 	CONFIG_TYPE_BOOLEAN = 0x5,
-	CONFIG_TYPE_EXT_PATTERN = 0x6
+	CONFIG_TYPE_EXT_PATTERN = 0x6,
+	CONFIG_TYPE_STRING_LIST = 0x7,
+	CONFIG_TYPE_STRING_MULTILINE = 0x8
 };
 
 

+ 11 - 11
src/civetweb.c

@@ -1843,7 +1843,7 @@ enum {
 	SSL_CERTIFICATE_CHAIN,
 	NUM_THREADS,
 	RUN_AS_USER,
-	REWRITE,
+	URL_REWRITE_PATTERN,
 	HIDE_FILES,
 	REQUEST_TIMEOUT,
 	KEEP_ALIVE_TIMEOUT,
@@ -1908,20 +1908,20 @@ enum {
 /* Config option name, config types, default value */
 static struct mg_option config_options[] = {
     {"cgi_pattern", CONFIG_TYPE_EXT_PATTERN, "**.cgi$|**.pl$|**.php$"},
-    {"cgi_environment", CONFIG_TYPE_STRING, NULL},
+    {"cgi_environment", CONFIG_TYPE_STRING_LIST, NULL},
     {"put_delete_auth_file", CONFIG_TYPE_FILE, NULL},
     {"cgi_interpreter", CONFIG_TYPE_FILE, NULL},
-    {"protect_uri", CONFIG_TYPE_STRING, NULL},
+    {"protect_uri", CONFIG_TYPE_STRING_LIST, NULL},
     {"authentication_domain", CONFIG_TYPE_STRING, "mydomain.com"},
     {"enable_auth_domain_check", CONFIG_TYPE_BOOLEAN, "yes"},
     {"ssi_pattern", CONFIG_TYPE_EXT_PATTERN, "**.shtml$|**.shtm$"},
-    {"throttle", CONFIG_TYPE_STRING, NULL},
+    {"throttle", CONFIG_TYPE_STRING_LIST, NULL},
     {"access_log_file", CONFIG_TYPE_FILE, NULL},
     {"enable_directory_listing", CONFIG_TYPE_BOOLEAN, "yes"},
     {"error_log_file", CONFIG_TYPE_FILE, NULL},
     {"global_auth_file", CONFIG_TYPE_FILE, NULL},
     {"index_files",
-     CONFIG_TYPE_STRING,
+     CONFIG_TYPE_STRING_LIST,
 #ifdef USE_LUA
      "index.xhtml,index.html,index.htm,index.lp,index.lsp,index.lua,index.cgi,"
      "index.shtml,index.php"},
@@ -1929,15 +1929,15 @@ static struct mg_option config_options[] = {
      "index.xhtml,index.html,index.htm,index.cgi,index.shtml,index.php"},
 #endif
     {"enable_keep_alive", CONFIG_TYPE_BOOLEAN, "no"},
-    {"access_control_list", CONFIG_TYPE_STRING, NULL},
-    {"extra_mime_types", CONFIG_TYPE_STRING, NULL},
-    {"listening_ports", CONFIG_TYPE_STRING, "8080"},
+    {"access_control_list", CONFIG_TYPE_STRING_LIST, NULL},
+    {"extra_mime_types", CONFIG_TYPE_STRING_LIST, NULL},
+    {"listening_ports", CONFIG_TYPE_STRING_LIST, "8080"},
     {"document_root", CONFIG_TYPE_DIRECTORY, NULL},
     {"ssl_certificate", CONFIG_TYPE_FILE, NULL},
     {"ssl_certificate_chain", CONFIG_TYPE_FILE, NULL},
     {"num_threads", CONFIG_TYPE_NUMBER, "50"},
     {"run_as_user", CONFIG_TYPE_STRING, NULL},
-    {"url_rewrite_patterns", CONFIG_TYPE_STRING, NULL},
+    {"url_rewrite_patterns", CONFIG_TYPE_STRING_LIST, NULL},
     {"hide_files_patterns", CONFIG_TYPE_EXT_PATTERN, NULL},
     {"request_timeout_ms", CONFIG_TYPE_NUMBER, "30000"},
     {"keep_alive_timeout_ms", CONFIG_TYPE_NUMBER, "500"},
@@ -1993,7 +1993,7 @@ static struct mg_option config_options[] = {
 #if defined(USE_LUA)
     {"lua_background_script", CONFIG_TYPE_FILE, NULL},
 #endif
-    {"additional_header", CONFIG_TYPE_STRING, NULL},
+    {"additional_header", CONFIG_TYPE_STRING_MULTILINE, NULL},
     {"max_request_size", CONFIG_TYPE_NUMBER, "16384"},
 
     {NULL, CONFIG_TYPE_UNKNOWN, NULL}};
@@ -6172,7 +6172,7 @@ interpret_uri(struct mg_connection *conn,    /* in: request (must be valid) */
 	}
 
 	/* Step 6: URI rewriting */
-	rewrite = conn->ctx->config[REWRITE];
+	rewrite = conn->ctx->config[URL_REWRITE_PATTERN];
 	while ((rewrite = next_option(rewrite, &a, &b)) != NULL) {
 		if ((match_len = match_prefix(a.ptr, a.len, uri)) > 0) {
 			mg_snprintf(conn,

+ 16 - 6
src/main.c

@@ -515,6 +515,7 @@ set_option(char **options, const char *name, const char *value)
 {
 	int i, type;
 	const struct mg_option *default_options = mg_get_valid_options();
+	const char *multi_sep = NULL;
 
 	for (i = 0; main_config_options[i].name != NULL; i++) {
 		if (0 == strcmp(name, main_config_options[i].name)) {
@@ -535,7 +536,7 @@ set_option(char **options, const char *name, const char *value)
 		/* unknown option */
 		return 0;
 	case CONFIG_TYPE_NUMBER:
-		/* integer number > 0, e.g. number of threads */
+		/* integer number >= 0, e.g. number of threads */
 		if (atol(value) < 0) {
 			/* invalid number */
 			return 0;
@@ -544,6 +545,14 @@ set_option(char **options, const char *name, const char *value)
 	case CONFIG_TYPE_STRING:
 		/* any text */
 		break;
+	case CONFIG_TYPE_STRING_LIST:
+		/* list of text items, separated by , */
+		multi_sep = ",";
+		break;
+	case CONFIG_TYPE_STRING_MULTILINE:
+		/* lines of text, separated by carriage return line feed */
+		multi_sep = "\r\n";
+		break;
 	case CONFIG_TYPE_BOOLEAN:
 		/* boolean value, yes or no */
 		if ((0 != strcmp(value, "yes")) && (0 != strcmp(value, "no"))) {
@@ -557,7 +566,8 @@ set_option(char **options, const char *name, const char *value)
 		 * verify_existence later */
 		break;
 	case CONFIG_TYPE_EXT_PATTERN:
-		/* list of file extentions */
+		/* list of patterns, separated by | */
+		multi_sep = "|";
 		break;
 	default:
 		die("Unknown option type - option %s", name);
@@ -571,14 +581,14 @@ set_option(char **options, const char *name, const char *value)
 			options[2 * i + 2] = NULL;
 			break;
 		} else if (!strcmp(options[2 * i], name)) {
-			if (!strcmp(name, "additional_header")) {
+			if (multi_sep) {
 				/* Option already set. Overwrite */
-				char *s =
-				    malloc(strlen(options[2 * i + 1]) + 3 + strlen(value));
+				char *s = malloc(strlen(options[2 * i + 1]) + strlen(multi_sep)
+				                 + strlen(value) + 1);
 				if (!s) {
 					die("Out of memory");
 				}
-				sprintf(s, "%s\r\n%s", options[2 * i + 1], value);
+				sprintf(s, "%s%s%s", options[2 * i + 1], multi_sep, value);
 				free(options[2 * i + 1]);
 				options[2 * i + 1] = s;
 			} else {