Add dummy POST handling for whitelist and blacklist

Signed-off-by: Mcat12 <newtoncat12@yahoo.com>
This commit is contained in:
Mcat12
2017-08-08 23:02:59 -04:00
parent 551351b178
commit ebe0631e5c
5 changed files with 43 additions and 5 deletions
+1
View File
@@ -27,6 +27,7 @@ void getDBstats(int *sock, char type);
// Endpoints under /dns/
void getList(int *sock, char type, char list_type);
void addList(int *sock, char type, char list_type, char *data);
void getPiholeStatus(int *sock, char type);
// General API commands
+6
View File
@@ -56,3 +56,9 @@ void getPiholeStatus(int *sock, char type)
sendAPIResponse(*sock, type);
ssend(*sock, "\"status\":%i", status == 1 ? 0 : 1);
}
void addList(int *sock, char type, char list_type, char *data)
{
sendAPIResponse(*sock, type);
ssend(*sock, "\"status\": \"success\"");
}
+33 -1
View File
@@ -126,7 +126,26 @@ void process_socket_request(char *client_message, int *sock)
}
}
void process_api_request(char *client_message, int *sock, bool header)
char* getPayload(char *http_message)
{
char *data_start;
char *unix_newline = strstr(http_message, "\n\n");
char *win_newline = strstr(http_message, "\r\n\r\n");
if(unix_newline != NULL)
data_start = unix_newline + 2;
else if(win_newline != NULL)
data_start = win_newline + 4;
else
return NULL;
if(strlen(data_start) == 0)
return NULL;
return data_start;
}
void process_api_request(char *client_message, char *full_message, int *sock, bool header)
{
char type;
if(header)
@@ -134,6 +153,11 @@ void process_api_request(char *client_message, int *sock, bool header)
else
type = API;
if(debug)
logg("Received API request: %s", full_message);
char *data = getPayload(full_message);
if(command(client_message, "GET /stats/summary"))
{
getStats(sock, type);
@@ -191,10 +215,18 @@ void process_api_request(char *client_message, int *sock, bool header)
{
getList(sock, type, WHITELIST);
}
else if(command(client_message, "POST /dns/whitelist"))
{
addList(sock, type, WHITELIST, data);
}
else if(command(client_message, "GET /dns/blacklist"))
{
getList(sock, type, BLACKLIST);
}
else if(command(client_message, "POST /dns/blacklist"))
{
addList(sock, type, BLACKLIST, data);
}
else if(command(client_message, "GET /dns/status"))
{
getPiholeStatus(sock, type);
+1 -2
View File
@@ -43,9 +43,8 @@ void *socket_listening_thread(void *args);
void *api_listening_thread(void *args);
void process_socket_request(char *client_message, int *sock);
void process_api_request(char *client_message, int *sock, bool header);
void process_api_request(char *client_message, char *full_message, int *sock, bool header);
bool command(char *client_message, const char* cmd);
void formatNumber(bool raw, int n, char* buffer);
void read_gravity_files(void);
int countlines(const char* fname);
+2 -2
View File
@@ -312,7 +312,7 @@ void *api_connection_handler_thread(void *socket_desc)
// Clear client message receive buffer
memset(client_message, 0, sizeof client_message);
if(strncmp(message, "GET ", 4) == 0)
if(strncmp(message, "GET ", 4) == 0 || strncmp(message, "POST ", 5) == 0)
{
// HTTP requests can be simple or full.
// A simple request contains one line only, and looks like this:
@@ -352,7 +352,7 @@ void *api_connection_handler_thread(void *socket_desc)
else
{
enable_thread_lock(threadname);
process_api_request(request, &sock, header);
process_api_request(request, message, &sock, header);
disable_thread_lock(threadname);
}