1
0
mirror of https://github.com/lxsang/ant-http synced 2024-07-05 22:19:47 +02:00

rules now work on port

This commit is contained in:
lxsang 2019-12-20 19:26:45 +01:00
parent 85cfce134b
commit f0ecc62162
5 changed files with 41 additions and 34 deletions

View File

@ -1,8 +1,4 @@
[SERVER] [SERVER]
; server port
; use 443 if one want to use
; SSL
port=8080
; plugin directory ; plugin directory
plugins=/opt/www/lib/ plugins=/opt/www/lib/
; plugin extension ; plugin extension
@ -15,6 +11,8 @@ tmpdir=/opt/www/tmp/
server_log = /var/log/antd.log server_log = /var/log/antd.log
; server error log ; server error log
error_log = /var/log/antd_error.log error_log = /var/log/antd_error.log
; max concurent connection
maxcon=200
; server backlocg ; server backlocg
backlog=5000 backlog=5000
; number of workers ; number of workers
@ -36,22 +34,7 @@ ssl.cipher=ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA
htdocs=/opt/www/htdocs htdocs=/opt/www/htdocs
; enable or disable SSL ; enable or disable SSL
ssl.enable=1 ssl.enable=1
; other config shoud be rules applied on this port
[PORT:80]
htdocs=/opt/www/htdocs
; enable or disable SSL
ssl.enable=0
; This enable some plugins to be initialised at server startup
[AUTOSTART]
; to start a plugin at server statup use:
;plugin = plugin_name_1
;plugin = plugin_name_2, etc
; sever rules
[RULES]
; For example the following rule will ; For example the following rule will
; convert a request of type: ; convert a request of type:
; name.example.com?rq=1 ; name.example.com?rq=1
@ -62,6 +45,29 @@ ssl.enable=0
; ^([a-zA-Z][a-zA-Z0-9]*)\.[a-zA-Z0-9]+\..*$ = /<1><url>?<query> ; ^([a-zA-Z][a-zA-Z0-9]*)\.[a-zA-Z0-9]+\..*$ = /<1><url>?<query>
; Sytax: [regular expression on the original request]=[new request rule] ; Sytax: [regular expression on the original request]=[new request rule]
[PORT:80]
htdocs=/opt/www/htdocs
; enable or disable SSL
ssl.enable=0
; other config shoud be rules applied on this port
; For example the following rule will
; convert a request of type:
; name.example.com?rq=1
;TO:
; example.com/name/?rq=1
; this is helpful to redirect many sub domains
; to a sub folder of the same server
; ^([a-zA-Z][a-zA-Z0-9]*)\.[a-zA-Z0-9]+\..*$ = /<1><url>?<query>
; Sytax: [regular expression on the original request]=[new request rule]
; This enable some plugins to be initialised at server startup
[AUTOSTART]
; to start a plugin at server statup use:
;plugin = plugin_name_1
;plugin = plugin_name_2, etc
[MIMES] [MIMES]
image/bmp=bmp image/bmp=bmp
image/jpeg=jpg,jpeg image/jpeg=jpg,jpeg

Binary file not shown.

View File

@ -74,7 +74,6 @@ void server_log(const char* fmt, ...)
#endif #endif
void destroy_config() void destroy_config()
{ {
list_free(&(server_config.rules));
freedict(server_config.handlers); freedict(server_config.handlers);
if (server_config.plugins_dir) if (server_config.plugins_dir)
free(server_config.plugins_dir); free(server_config.plugins_dir);
@ -111,6 +110,7 @@ void destroy_config()
{ {
close(cnf->sock); close(cnf->sock);
} }
list_free(&(cnf->rules));
} }
freedict(server_config.ports); freedict(server_config.ports);
} }
@ -178,11 +178,6 @@ static int config_handler(void *conf, const char *section, const char *name,
pconfig->ssl_cipher = strdup(value); pconfig->ssl_cipher = strdup(value);
} }
#endif #endif
else if (strcmp(section, "RULES") == 0)
{
list_put_s(&pconfig->rules, name);
list_put_s(&pconfig->rules, value);
}
else if (strcmp(section, "FILEHANDLER") == 0) else if (strcmp(section, "FILEHANDLER") == 0)
{ {
dput(pconfig->handlers, name, strdup(value)); dput(pconfig->handlers, name, strdup(value));
@ -208,6 +203,7 @@ static int config_handler(void *conf, const char *section, const char *name,
p = (port_config_t*) malloc( sizeof(port_config_t)); p = (port_config_t*) malloc( sizeof(port_config_t));
p->htdocs = NULL; p->htdocs = NULL;
p->sock = -1; p->sock = -1;
p->rules = list_init();
dput(pconfig->ports,buf, p); dput(pconfig->ports,buf, p);
p->port = atoi(buf); p->port = atoi(buf);
} }
@ -221,6 +217,12 @@ static int config_handler(void *conf, const char *section, const char *name,
if(p->usessl) if(p->usessl)
pconfig->enable_ssl = 1; pconfig->enable_ssl = 1;
} }
else
{
// other thing should be rules
list_put_s(&p->rules, name);
list_put_s(&p->rules, value);
}
} }
else else
{ {
@ -263,7 +265,6 @@ void load_config(const char *file)
server_config.tmpdir = "tmp/"; server_config.tmpdir = "tmp/";
server_config.n_workers = 4; server_config.n_workers = 4;
server_config.backlog = 1000; server_config.backlog = 1000;
server_config.rules = list_init();
server_config.handlers = dict(); server_config.handlers = dict();
server_config.maxcon = 100; server_config.maxcon = 100;
server_config.connection = 0; server_config.connection = 0;
@ -694,7 +695,7 @@ int startup(unsigned *port)
return (httpd); return (httpd);
} }
char *apply_rules(const char *host, char *url) char *apply_rules(list_t rules, const char *host, char *url)
{ {
// rule check // rule check
char *query_string = url; char *query_string = url;
@ -706,12 +707,12 @@ char *apply_rules(const char *host, char *url)
query_string++; query_string++;
} }
//char* oldurl = strdup(url); //char* oldurl = strdup(url);
int size = list_size(server_config.rules); int size = list_size(rules);
for (int i = 0; i < size; i += 2) for (int i = 0; i < size; i += 2)
{ {
char *k, *v; char *k, *v;
k = list_at(server_config.rules, i)->value.s; k = list_at(rules, i)->value.s;
v = list_at(server_config.rules, i + 1)->value.s; v = list_at(rules, i + 1)->value.s;
// 1 group // 1 group
if (rule_check(k, v, host, url, query_string, url)) if (rule_check(k, v, host, url, query_string, url))
{ {
@ -777,7 +778,7 @@ void *decode_request_header(void *data)
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
strcat(buf, url); strcat(buf, url);
LOG("Original query: %s", url); LOG("Original query: %s", url);
query = apply_rules(host, buf); query = apply_rules(rq->client->port_config->rules, host, buf);
LOG("Processed query: %s", query); LOG("Processed query: %s", query);
dput(rq->request, "RESOURCE_PATH", url_decode(buf)); dput(rq->request, "RESOURCE_PATH", url_decode(buf));
if (query) if (query)

View File

@ -37,6 +37,7 @@ typedef struct {
int usessl; int usessl;
char* htdocs; char* htdocs;
int sock; int sock;
list_t rules;
} port_config_t; } port_config_t;
typedef struct{ typedef struct{
@ -73,7 +74,6 @@ typedef struct {
char *db_path; char *db_path;
//char* htdocs; //char* htdocs;
char* tmpdir; char* tmpdir;
list_t rules;
dictionary_t handlers; dictionary_t handlers;
int backlog; int backlog;
int maxcon; int maxcon;

View File

@ -41,7 +41,7 @@ void __init_plugin__(const char* pl,config_t* conf){
__plugin__.name = strdup(pl); __plugin__.name = strdup(pl);
__plugin__.dbpath= conf->db_path; __plugin__.dbpath= conf->db_path;
__plugin__.pdir = conf->plugins_dir; __plugin__.pdir = conf->plugins_dir;
__plugin__.tmpdir = = sconf->tmpdir; __plugin__.tmpdir = conf->tmpdir;
__plugin__.raw_body = 0; __plugin__.raw_body = 0;
init(); init();
}; };