From 4b441b3d09eed5786e589854b182cb08b4699b9d Mon Sep 17 00:00:00 2001 From: Xuan Sang LE Date: Fri, 23 Feb 2018 19:54:16 +0100 Subject: [PATCH] fix rules orders in config file --- http_server.c | 51 ++++++++++++++++++++++++++++++++++----------------- http_server.h | 2 +- httpd.c | 7 ++++--- libs/handle.h | 2 +- 4 files changed, 40 insertions(+), 22 deletions(-) diff --git a/http_server.c b/http_server.c index 2dd8c10..2c49204 100644 --- a/http_server.c +++ b/http_server.c @@ -134,7 +134,7 @@ end: antd_close(client); } -void rule_check(association it, const char* host, const char* _url, const char* _query, char* buf) +int rule_check(const char*k, const char* v, const char* host, const char* _url, const char* _query, char* buf) { // first perfom rule check on host, if not success, perform on url regmatch_t key_matches[10]; @@ -147,16 +147,16 @@ void rule_check(association it, const char* host, const char* _url, const char* int idx = 0; memset(rep,0,10); // 1 group - if(!host || !(ret = regex_match(it->key,host, 10, key_matches)) ) + if(!host || !(ret = regex_match(k,host, 10, key_matches)) ) { target = url; - ret = regex_match(it->key,url, 10, key_matches); + ret = regex_match(k,url, 10, key_matches); } else target = host; - if(!ret) return; - tmp = (char*) it->value; + if(!ret) return 0; + tmp = (char*) v; char * search = "<([a-zA-Z0-9]+)>"; //printf("match again %s\n",tmp); while((ret = regex_match( search,tmp, 2, val_matches))) @@ -185,9 +185,19 @@ void rule_check(association it, const char* host, const char* _url, const char* //break; } // now modify the match 2 group - if(idx > 0) buf[idx] = '\0'; + if(idx > 0) + { + if(tmp) + { + // copy the remainning of tmp + memcpy(buf+idx, tmp, strlen(tmp)); + idx += strlen(tmp); + } + buf[idx] = '\0'; + } free(url); free(query); + return 1; } /**********************************************************************/ /* Put the entire contents of a file out on a socket. This function @@ -413,19 +423,26 @@ char* apply_rules(const char* host, char*url) query_string++; } //char* oldurl = strdup(url); - for_each_assoc(it, server_config.rules) + int size = list_size(server_config.rules); + for(int i = 0; i < size; i+= 2) { + char *k, *v; + k = list_at(server_config.rules, i)->value.s; + v = list_at(server_config.rules, i+1)->value.s; // 1 group - rule_check(it,host, url, query_string, url); - query_string = url; - while ((*query_string != '?') && (*query_string != '\0')) - query_string++; - if (*query_string == '?') - { - *query_string = '\0'; - query_string++; + if(rule_check(k, v,host, url, query_string, url)){ + query_string = url; + + while ((*query_string != '?') && (*query_string != '\0')) + query_string++; + if (*query_string == '?') + { + *query_string = '\0'; + query_string++; + } } } + return strdup(query_string); } /** @@ -501,13 +518,13 @@ dictionary decode_request(void* client,const char* method, char* url) } } //if(line) free(line); - + query = apply_rules(host, url); if(strcmp(method,"GET") == 0) { - query = apply_rules(host, url); if(host) free(host); if(query) { + LOG("Query: %s\n", query); request = decode_url_request(query); free(query); } diff --git a/http_server.h b/http_server.h index d9884ac..2deebb5 100644 --- a/http_server.h +++ b/http_server.h @@ -33,7 +33,7 @@ void serve_file(void*, const char *); int startup(unsigned *); void unimplemented(void*); void badrequest(void*); -void rule_check(association, const char* , const char* , const char* , char*); +int rule_check(const char*, const char*, const char* , const char* , const char* , char*); void ws_confirm_request(void*, const char*); char* post_url_decode(void* client,int len); dictionary decode_url_request(const char* query); diff --git a/httpd.c b/httpd.c index 0163970..70d5df6 100644 --- a/httpd.c +++ b/httpd.c @@ -98,7 +98,8 @@ static int config_handler(void* conf, const char* section, const char* name, #endif else if (strcmp(section, "RULES") == 0) { - dput( pconfig->rules, strdup(name),strdup(value)); + list_put_s(&pconfig->rules, strdup(name)); + list_put_s(&pconfig->rules, strdup(value)); } else if (strcmp(section, "FILEHANDLER") == 0) { @@ -139,7 +140,7 @@ void load_config(const char* file) server_config.htdocs = "htdocs"; server_config.tmpdir = "tmp"; server_config.backlog = 100; - server_config.rules = dict(); + server_config.rules = list_init(); server_config.handlers = dict(); #ifdef USE_OPENSSL server_config.usessl = 0; @@ -161,7 +162,7 @@ void load_config(const char* file) init_file_system(); } void stop_serve(int dummy) { - free(server_config.rules); + list_free(&server_config.rules); free(server_config.handlers); unload_all_plugin(); } diff --git a/libs/handle.h b/libs/handle.h index 84de8f0..91ca369 100644 --- a/libs/handle.h +++ b/libs/handle.h @@ -36,7 +36,7 @@ typedef struct { char *db_path; char* htdocs; char* tmpdir; - dictionary rules; + list rules; dictionary handlers; int backlog; #ifdef USE_OPENSSL