diff --git a/config.h b/config.h index 1881fdb..52ed09a 100644 --- a/config.h +++ b/config.h @@ -1,4 +1,5 @@ #define CONFIG "config.ini" +#include "plugins/dictionary.h" typedef struct { int port; @@ -7,6 +8,7 @@ typedef struct { char *db_path; char* htdocs; char* tmpdir; + dictionary rules; }config_t; extern config_t server_config; \ No newline at end of file diff --git a/http_server.c b/http_server.c index 7bc8439..2acaa30 100644 --- a/http_server.c +++ b/http_server.c @@ -10,7 +10,9 @@ void accept_request(int client) int numchars; char method[255]; char url[4096]; - char path[512]; + char path[1024]; + char* token; + char *line; size_t i, j; struct stat st; @@ -28,8 +30,8 @@ void accept_request(int client) { printf("METHOD NOT FOUND %s\n", method); // unimplemented + //while(get_line(client, buf, sizeof(buf)) > 0) printf("%s\n",buf ); unimplemented(client); - while(get_line(client, buf, sizeof(buf)) > 0) printf("%s\n",buf ); close(client); return; } @@ -57,10 +59,27 @@ void accept_request(int client) } } + // get the HOST header + line = read_line(client); + trim(line, '\n'); + trim(line, '\r'); + token = strsep(&line,":"); + trim(token,' '); + trim(line,' '); + if(strcasecmp(token, "HOST")) + { + badrequest(client); + close(client); + return; + } + + // perform rule check in domain + + sprintf(path, server_config.htdocs); strcat(path, url); - if (path[strlen(path) - 1] == '/') - strcat(path, "index.html"); + //if (path[strlen(path) - 1] == '/') + // strcat(path, "index.html"); if (stat(path, &st) == -1) { if(execute_plugin(client,url,method,query_string) < 0) not_found(client); @@ -293,6 +312,14 @@ void unimplemented(int client) __t(client, ""); } +void badrequest(int client) +{ + set_status(client,400,"Bad Request"); + __t(client,SERVER_STRING); + __t(client,"Content-Type: text/html"); + response(client,""); + __t(client,"The request could not be understood by the server due to malformed syntax."); +} /** diff --git a/http_server.h b/http_server.h index b5d7978..36e4ffe 100644 --- a/http_server.h +++ b/http_server.h @@ -7,7 +7,6 @@ #include #include #include -#include "plugins/dictionary.h" #include "plugins/handle.h" #include "plugin_manager.h" @@ -31,7 +30,7 @@ void not_found(int); void serve_file(int, const char *); int startup(unsigned *); void unimplemented(int); - +void badrequest(int); void ws_confirm_request(int, const char*); char* post_url_decode(int client,int len); dictionary decode_url_request(const char* query); diff --git a/httpd.c b/httpd.c index 7801b47..eccc24b 100644 --- a/httpd.c +++ b/httpd.c @@ -22,7 +22,12 @@ static int config_handler(void* conf, const char* section, const char* name, pconfig->htdocs = strdup(value); } else if(MATCH("SERVER", "tmpdir")) { pconfig->tmpdir = strdup(value); - }else if(strcmp(section,"AUTOSTART")==0){ + } + else if (strcmp(section, "RULES") == 0) + { + dput( pconfig->rules, strdup(name),strdup(value)); + } + else if(strcmp(section,"AUTOSTART")==0){ // The server section must be added before the autostart section // auto start plugin plugin_load(value); @@ -56,6 +61,7 @@ void load_config(const char* file) server_config.db_path = "databases/"; server_config.htdocs = "htdocs"; server_config.tmpdir = "tmp"; + server_config.rules = dict(); if (ini_parse(file, config_handler, &server_config) < 0) { LOG("Can't load '%s'\n. Used defaut configuration", file); }