add rules supports (alpha)

This commit is contained in:
Xuan Sang LE 2018-02-03 18:50:07 +01:00
parent c544976c04
commit 60deb84111
4 changed files with 41 additions and 7 deletions

View File

@ -1,4 +1,5 @@
#define CONFIG "config.ini" #define CONFIG "config.ini"
#include "plugins/dictionary.h"
typedef struct { typedef struct {
int port; int port;
@ -7,6 +8,7 @@ typedef struct {
char *db_path; char *db_path;
char* htdocs; char* htdocs;
char* tmpdir; char* tmpdir;
dictionary rules;
}config_t; }config_t;
extern config_t server_config; extern config_t server_config;

View File

@ -10,7 +10,9 @@ void accept_request(int client)
int numchars; int numchars;
char method[255]; char method[255];
char url[4096]; char url[4096];
char path[512]; char path[1024];
char* token;
char *line;
size_t i, j; size_t i, j;
struct stat st; struct stat st;
@ -28,8 +30,8 @@ void accept_request(int client)
{ {
printf("METHOD NOT FOUND %s\n", method); printf("METHOD NOT FOUND %s\n", method);
// unimplemented // unimplemented
//while(get_line(client, buf, sizeof(buf)) > 0) printf("%s\n",buf );
unimplemented(client); unimplemented(client);
while(get_line(client, buf, sizeof(buf)) > 0) printf("%s\n",buf );
close(client); close(client);
return; 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); sprintf(path, server_config.htdocs);
strcat(path, url); strcat(path, url);
if (path[strlen(path) - 1] == '/') //if (path[strlen(path) - 1] == '/')
strcat(path, "index.html"); // strcat(path, "index.html");
if (stat(path, &st) == -1) { if (stat(path, &st) == -1) {
if(execute_plugin(client,url,method,query_string) < 0) if(execute_plugin(client,url,method,query_string) < 0)
not_found(client); not_found(client);
@ -293,6 +312,14 @@ void unimplemented(int client)
__t(client, "</BODY></HTML>"); __t(client, "</BODY></HTML>");
} }
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.");
}
/** /**

View File

@ -7,7 +7,6 @@
#include <pthread.h> #include <pthread.h>
#include <signal.h> #include <signal.h>
#include <sys/socket.h> #include <sys/socket.h>
#include "plugins/dictionary.h"
#include "plugins/handle.h" #include "plugins/handle.h"
#include "plugin_manager.h" #include "plugin_manager.h"
@ -31,7 +30,7 @@ void not_found(int);
void serve_file(int, const char *); void serve_file(int, const char *);
int startup(unsigned *); int startup(unsigned *);
void unimplemented(int); void unimplemented(int);
void badrequest(int);
void ws_confirm_request(int, const char*); void ws_confirm_request(int, const char*);
char* post_url_decode(int client,int len); char* post_url_decode(int client,int len);
dictionary decode_url_request(const char* query); dictionary decode_url_request(const char* query);

View File

@ -22,7 +22,12 @@ static int config_handler(void* conf, const char* section, const char* name,
pconfig->htdocs = strdup(value); pconfig->htdocs = strdup(value);
} else if(MATCH("SERVER", "tmpdir")) { } else if(MATCH("SERVER", "tmpdir")) {
pconfig->tmpdir = strdup(value); 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 // The server section must be added before the autostart section
// auto start plugin // auto start plugin
plugin_load(value); plugin_load(value);
@ -56,6 +61,7 @@ void load_config(const char* file)
server_config.db_path = "databases/"; server_config.db_path = "databases/";
server_config.htdocs = "htdocs"; server_config.htdocs = "htdocs";
server_config.tmpdir = "tmp"; server_config.tmpdir = "tmp";
server_config.rules = dict();
if (ini_parse(file, config_handler, &server_config) < 0) { if (ini_parse(file, config_handler, &server_config) < 0) {
LOG("Can't load '%s'\n. Used defaut configuration", file); LOG("Can't load '%s'\n. Used defaut configuration", file);
} }