added cors headers and colors log
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Successful in 10s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Successful in 21s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Failing after 8s

This commit is contained in:
wt committed 2025-08-28 18:02:18 +07:00
1 parent 5840fded28
commit 28655e7b46
13 files changed
+160 -56

No files matched your search

+5 -5
View File
@@ -6,7 +6,7 @@ void add_user(struct mg_connection *c, struct mg_http_message *hm) {
request_with_token *requestdata = check_body(hm);
const bool valid = jwt_verifyJWT(requestdata->token, JWT_DEFAULT_SECRET);
if (!valid) {
mg_http_reply(c, 403, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 403, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("invalid token"));
return;
}
@@ -18,7 +18,7 @@ void add_user(struct mg_connection *c, struct mg_http_message *hm) {
cJSON *is_superuser = cJSON_GetObjectItem(jsonbody, "is_superuser");
if (username == nullptr || password == nullptr || email == nullptr ||
is_superuser == nullptr || group == nullptr) {
mg_http_reply(c, 400, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 400, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("must have <username>, <password>, <email>, <group, "
"optional>, <is_superuser, optional>"));
return;
@@ -27,7 +27,7 @@ void add_user(struct mg_connection *c, struct mg_http_message *hm) {
bool jwt_is_superuser =
cJSON_GetObjectItem(payload, "is_superuser")->valueint;
if (!jwt_is_superuser) {
mg_http_reply(c, 403, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 403, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("not authorized"));
cJSON_Delete(payload);
return;
@@ -38,7 +38,7 @@ void add_user(struct mg_connection *c, struct mg_http_message *hm) {
group->valuestring,
&user_is_superuser);
if (!new_db_user) {
mg_http_reply(c, 500, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 500, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("failed to create user"));
cJSON_Delete(payload);
return;
@@ -51,6 +51,6 @@ void add_user(struct mg_connection *c, struct mg_http_message *hm) {
is_superuser->valueint ? "true" : "false");
mg_http_reply(c, 200, "Content-Type: application/json\r\n", responsedata);
mg_http_reply(c, 200, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", responsedata);
cJSON_Delete(payload);
}
+10 -10
View File
@@ -6,7 +6,7 @@ void change_password(struct mg_connection *c, struct mg_http_message *hm) {
request_with_token *requestdata = check_body(hm);
const bool valid = jwt_verifyJWT(requestdata->token, JWT_DEFAULT_SECRET);
if (!valid) {
mg_http_reply(c, 403, "Content-Type: application/json\r\n", "{%m:%m}",
mg_http_reply(c, 403, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
MG_ESC("error"), MG_ESC("invalid token"));
return;
}
@@ -16,7 +16,7 @@ void change_password(struct mg_connection *c, struct mg_http_message *hm) {
cJSON *userid = cJSON_GetObjectItem(jsonbody, "userid");
if ((old_password == nullptr || new_password == nullptr) &&
userid == nullptr) {
mg_http_reply(c, 400, "Content-Type: application/json\r\n", "{%m:%m}",
mg_http_reply(c, 400, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
MG_ESC("error"),
MG_ESC("must have <old_password>, <new_password>"));
return;
@@ -27,7 +27,7 @@ void change_password(struct mg_connection *c, struct mg_http_message *hm) {
char *username = cJSON_GetObjectItem(payload, "username")->valuestring;
User *old_user_data = db_get_user_by_username(username);
if (!old_user_data) {
mg_http_reply(c, 500, "Content-Type: application/json\r\n", "{%m:%m}",
mg_http_reply(c, 500, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
MG_ESC("error"),
MG_ESC("failed to update user can't get old_user_data"));
cJSON_Delete(payload);
@@ -35,7 +35,7 @@ void change_password(struct mg_connection *c, struct mg_http_message *hm) {
char *password = old_user_data->password;
if (strcmp(password, old_password->valuestring)) {
mg_http_reply(c, 400, "Content-Type: application/json\r\n", "{%m:%m}",
mg_http_reply(c, 400, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
MG_ESC("error"), MG_ESC("invalid old password"));
return;
}
@@ -44,7 +44,7 @@ void change_password(struct mg_connection *c, struct mg_http_message *hm) {
old_user_data->id, old_user_data->username, old_user_data->email,
new_password->valuestring, old_user_data->group, &old_is_superuser);
if (!new_user_data) {
mg_http_reply(c, 500, "Content-Type: application/json\r\n", "{%m:%m}",
mg_http_reply(c, 500, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
MG_ESC("error"), MG_ESC("failed to update user can't update database"));
free_user(old_user_data);
cJSON_Delete(payload);
@@ -54,21 +54,21 @@ void change_password(struct mg_connection *c, struct mg_http_message *hm) {
char responsedata[64] = {0};
snprintf(responsedata, sizeof(responsedata), "{\"success\": true}");
mg_http_reply(c, 200, "Content-Type: application/json\r\n", responsedata);
mg_http_reply(c, 200, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", responsedata);
free_user(old_user_data);
cJSON_Delete(payload);
return;
}
bool is_superuser = cJSON_GetObjectItem(payload, "is_superuser")->valueint;
if (!is_superuser) {
mg_http_reply(c, 403, "Content-Type: application/json\r\n", "{%m:%m}",
mg_http_reply(c, 403, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
MG_ESC("error"), MG_ESC("user is not superuser"));
cJSON_Delete(payload);
return;
}
User *old_user_data = db_get_user_by_id(userid->valueint);
if (!old_user_data) {
mg_http_reply(c, 500, "Content-Type: application/json\r\n", "{%m:%m}",
mg_http_reply(c, 500, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
MG_ESC("error"),
MG_ESC("failed to update user can't get old_user_data"));
cJSON_Delete(payload);
@@ -79,7 +79,7 @@ void change_password(struct mg_connection *c, struct mg_http_message *hm) {
old_user_data->id, old_user_data->username, old_user_data->email,
new_password->valuestring, old_user_data->group, &old_is_superuser);
if (!new_user_data) {
mg_http_reply(c, 500, "Content-Type: application/json\r\n", "{%m:%m}",
mg_http_reply(c, 500, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
MG_ESC("error"), MG_ESC("failed to update user can't create new_user_data from root"));
free_user(old_user_data);
cJSON_Delete(payload);
@@ -88,7 +88,7 @@ void change_password(struct mg_connection *c, struct mg_http_message *hm) {
char responsedata[64] = {0};
snprintf(responsedata, sizeof(responsedata), "{\"success\": true}");
mg_http_reply(c, 200, "Content-Type: application/json\r\n", responsedata);
mg_http_reply(c, 200, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", responsedata);
free_user(old_user_data);
free_user(new_user_data);
cJSON_Delete(payload);
+8 -8
View File
@@ -6,7 +6,7 @@ void change_user(struct mg_connection *c, struct mg_http_message *hm) {
request_with_token *requestdata = check_body(hm);
const bool valid = jwt_verifyJWT(requestdata->token, JWT_DEFAULT_SECRET);
if (!valid) {
mg_http_reply(c, 403, "Content-Type: application/json\r\n", "{%m:%m}",
mg_http_reply(c, 403, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
MG_ESC("error"), MG_ESC("invalid token"));
return;
}
@@ -23,19 +23,19 @@ void change_user(struct mg_connection *c, struct mg_http_message *hm) {
old_user_data = db_get_user_by_id(userid->valueint);
}
if (!old_user_data) {
mg_http_reply(c, 404, "Content-Type: application/json\r\n", "{%m:%m}",
mg_http_reply(c, 404, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
MG_ESC("error"), MG_ESC("user not found"));
return;
}
char *password = old_user_data->password;
if (!password) {
mg_http_reply(c, 400, "Content-Type: application/json\r\n", "{%m:%m}",
mg_http_reply(c, 400, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
MG_ESC("error"), MG_ESC("password is required"));
return;
}
if (username == nullptr || email == nullptr || is_superuser == nullptr ||
group == nullptr) {
mg_http_reply(c, 400, "Content-Type: application/json\r\n", "{%m:%m}",
mg_http_reply(c, 400, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
MG_ESC("error"),
MG_ESC("must have <username>, <email>, <group, "
"optional>, <is_superuser, optional>"));
@@ -52,7 +52,7 @@ void change_user(struct mg_connection *c, struct mg_http_message *hm) {
group->valuestring, &user_is_superuser);
if (!updated_user_data) {
mg_http_reply(c, 500, "Content-Type: application/json\r\n", "{%m:%m}",
mg_http_reply(c, 500, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
MG_ESC("error"), MG_ESC("failed to update user change user request"));
cJSON_Delete(payload);
free_user(old_user_data);
@@ -66,7 +66,7 @@ void change_user(struct mg_connection *c, struct mg_http_message *hm) {
username->valuestring, email->valuestring, group->valuestring,
is_superuser->valueint ? "true" : "false");
mg_http_reply(c, 200, "Content-Type: application/json\r\n", responsedata);
mg_http_reply(c, 200, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", responsedata);
free_user(updated_user_data);
free_user(old_user_data);
cJSON_Delete(payload);
@@ -75,7 +75,7 @@ void change_user(struct mg_connection *c, struct mg_http_message *hm) {
bool req_is_superuser =
cJSON_GetObjectItem(payload, "is_superuser")->valueint;
if (!req_is_superuser) {
mg_http_reply(c, 403, "Content-Type: application/json\r\n", "{%m:%m}",
mg_http_reply(c, 403, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
MG_ESC("error"), MG_ESC("user is not superuser"));
cJSON_Delete(payload);
free_user(old_user_data);
@@ -99,7 +99,7 @@ void change_user(struct mg_connection *c, struct mg_http_message *hm) {
username->valuestring, email->valuestring, group->valuestring,
is_superuser->valueint ? "true" : "false");
mg_http_reply(c, 200, "Content-Type: application/json\r\n", responsedata);
mg_http_reply(c, 200, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", responsedata);
free_user(updated_user_data);
free_user(old_user_data);
cJSON_Delete(payload);
+5
View File
@@ -0,0 +1,5 @@
#include "cors.h"
void set_cors_headers(struct mg_connection *c, struct mg_http_message *hm) {
mg_http_reply(c, 200, "Access-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods: GET,POST,OPTIONS,PUT,DELETE\r\nAccess-Control-Allow-Headers: Authorization,X-Custom,Content-Type", "");
}
+5
View File
@@ -0,0 +1,5 @@
#pragma once
#include <mongoose.h>
void set_cors_headers(struct mg_connection *c, struct mg_http_message *hm);
+6 -6
View File
@@ -4,19 +4,19 @@
void delete_user(struct mg_connection *c, struct mg_http_message *hm) {
if (hm->body.len == 0) {
mg_http_reply(c, 400, "Content-Type: application/json\r\n", "{%m:%m}",
mg_http_reply(c, 400, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
MG_ESC("error"), MG_ESC("must have token"));
return;
}
request_with_token *requestdata = check_body(hm);
if (requestdata->error != NULL) {
mg_http_reply(c, 403, "Content-Type: application/json\r\n", "{%m:%m}",
mg_http_reply(c, 403, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
MG_ESC("error"), MG_ESC(requestdata->error));
return;
}
const bool valid = jwt_verifyJWT(requestdata->token, JWT_DEFAULT_SECRET);
if (!valid) {
mg_http_reply(c, 403, "Content-Type: application/json\r\n", "{%m:%m}",
mg_http_reply(c, 403, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
MG_ESC("error"), MG_ESC("invalid token"));
free_request_with_token(requestdata);
return;
@@ -31,14 +31,14 @@ void delete_user(struct mg_connection *c, struct mg_http_message *hm) {
snprintf(responsedata, sizeof(responsedata), "{\"success\": %s}",
user_deleted ? "true" : "false");
mg_http_reply(c, 200, "Content-Type: application/json\r\n", responsedata);
mg_http_reply(c, 200, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", responsedata);
free_request_with_token(requestdata);
cJSON_Delete(payload);
return;
}
bool is_superuser = cJSON_GetObjectItem(payload, "is_superuser")->valueint;
if (!is_superuser) {
mg_http_reply(c, 403, "Content-Type: application/json\r\n", "{%m:%m}",
mg_http_reply(c, 403, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
MG_ESC("error"), MG_ESC("user is not superuser"));
return;
}
@@ -47,7 +47,7 @@ void delete_user(struct mg_connection *c, struct mg_http_message *hm) {
snprintf(responsedata, sizeof(responsedata), "{\"success\": %s}",
user_deleted ? "true" : "false");
mg_http_reply(c, 200, "Content-Type: application/json\r\n", responsedata);
mg_http_reply(c, 200, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", responsedata);
free_request_with_token(requestdata);
cJSON_Delete(payload);
}
+3 -3
View File
@@ -5,18 +5,18 @@
void get_all_users(struct mg_connection *c, struct mg_http_message *hm) {
request_with_token *data = check_body(hm);
if (data->error != NULL) {
mg_http_reply(c, 403, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"), MG_ESC(data->error));
mg_http_reply(c, 403, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"), MG_ESC(data->error));
return;
}
if (!jwt_verifyJWT(data->token, JWT_DEFAULT_SECRET)) {
mg_http_reply(c, 403, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 403, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("invalid token"));
return;
}
cJSON *payload = jwt_get_payload(data->token);
bool is_superuser = cJSON_GetObjectItem(payload, "is_superuser")->valueint;
if (!is_superuser) {
mg_http_reply(c, 403, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 403, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("user is not superuser"));
return;
}
+6 -6
View File
@@ -3,7 +3,7 @@
void get_token(struct mg_connection *c, struct mg_http_message *hm) {
if (hm->body.len == 0) {
mg_http_reply(c, 400, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 400, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("must have username and password"));
return;
}
@@ -13,21 +13,21 @@ void get_token(struct mg_connection *c, struct mg_http_message *hm) {
if (username == NULL || password == NULL) {
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL) {
mg_http_reply(c, 400, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"), MG_ESC(error_ptr));
mg_http_reply(c, 400, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"), MG_ESC(error_ptr));
return;
}
mg_http_reply(c, 400, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 400, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("must have username and password"));
return;
}
User *db_user = db_get_user_by_username(username->valuestring);
if (!db_user) {
mg_http_reply(c, 400, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 400, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("couldn't find user"));
return;
}
if (strcmp(db_user->password, password->valuestring)) {
mg_http_reply(c, 400, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 400, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("wrong password"));
return;
}
@@ -41,7 +41,7 @@ void get_token(struct mg_connection *c, struct mg_http_message *hm) {
db_user->id, db_user->username, db_user->email, db_user->group,
db_user->is_superuser ? "true" : "false");
char *token = jwt_createJWT(header, payload, JWT_DEFAULT_SECRET);
mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("token"), MG_ESC(token));
mg_http_reply(c, 200, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("token"), MG_ESC(token));
free_user(db_user);
free(token);
}
+5 -5
View File
@@ -4,18 +4,18 @@
void get_user(struct mg_connection *c, struct mg_http_message *hm) {
if (hm->body.len == 0) {
mg_http_reply(c, 400, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 400, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("must have token"));
return;
}
request_with_token *requestdata = check_body(hm);
if (requestdata->error != NULL) {
mg_http_reply(c, 403, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"), MG_ESC(requestdata->error));
mg_http_reply(c, 403, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"), MG_ESC(requestdata->error));
return;
}
if (!jwt_verifyJWT(requestdata->token, JWT_DEFAULT_SECRET)) {
free_request_with_token(requestdata);
mg_http_reply(c, 403, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 403, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("invalid token"));
return;
}
@@ -27,7 +27,7 @@ void get_user(struct mg_connection *c, struct mg_http_message *hm) {
free_request_with_token(requestdata);
cJSON_Delete(payload);
free(username);
mg_http_reply(c, 404, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 404, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("user not found"));
return;
}
@@ -38,7 +38,7 @@ void get_user(struct mg_connection *c, struct mg_http_message *hm) {
user->id, user->username, user->email, user->group,
user->is_superuser ? "true" : "false");
mg_http_reply(c, 200, "Content-Type: application/json\r\n", responsedata);
mg_http_reply(c, 200, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", responsedata);
free_request_with_token(requestdata);
free(username);
free_user(user);
+77
View File
@@ -0,0 +1,77 @@
#ifndef C_COLORS_H
#define C_COLORS_H
// reset
#define RESET "\033[0m"
// regular colrs
#define GREEN "\033[0;32m" // GREEN
#define BLUE "\033[0;34m" // BLUE
#define YELLOW "\033[1;33m" // YELLOW
#define RED "\033[0;31m" // RED
#define BLACK "\033[0;30m" // BLACK
#define PURPLE "\033[0;35m" // PURPLE
#define CYAN "\033[0;36m" // CYAN
#define WHITE "\033[0;37m" // WHITE
// bold
#define BBLACK "\033[1;30m" // BLACK
#define BRED "\033[1;31m" // RED
#define BGREEN "\033[1;32m" // GREEN
#define BYELLOW "\033[1;33m" // YELLOW
#define BBLUE "\033[1;34m" // BLUE
#define BPURPLE "\033[1;35m" // PURPLE
#define BCYAN "\033[1;36m" // CYAN
#define BWHITE "\033[1;37m" // WHITE
// Underline
#define UBLACK "\033[4;30m" // BLACK
#define URED "\033[4;31m" // RED
#define UGREEN "\033[4;32m" // GREEN
#define UYELLOW "\033[4;33m" // YELLOW
#define UBLUE "\033[4;34m" // BLUE
#define UPURPLE "\033[4;35m" // PURPLE
#define UCYAN "\033[4;36m" // CYAN
#define UWHITE "\033[4;37m" // WHITE
// Background
#define ON_BLACK "\033[40m" // BLACK
#define ON_RED "\033[41m" // RED
#define ON_GREEN "\033[42m" // GREEN
#define ON_YELLOW "\033[43m" // YELLOW
#define ON_BLUE "\033[44m" // BLUE
#define ON_PURPLE "\033[45m" // PURPLE
#define ON_CYAN "\033[46m" // CYAN
#define ON_WHITE "\033[47m" // WHITE
// High Intensity
#define IBLACK "\033[0;90m" // BLACK
#define IRED "\033[0;91m" // RED
#define IGREEN "\033[0;92m" // GREEN
#define IYELLOW "\033[0;93m" // YELLOW
#define IBLUE "\033[0;94m" // BLUE
#define IPURPLE "\033[0;95m" // PURPLE
#define ICYAN "\033[0;96m" // CYAN
#define IWHITE "\033[0;97m" // WHITE
// Bold High Intensity
#define BIBLACK "\033[1;90m" // BLACK
#define BIRED "\033[1;91m" // RED
#define BIGREEN "\033[1;92m" // GREEN
#define BIYELLOW "\033[1;93m" // YELLOW
#define BIBLUE "\033[1;94m" // BLUE
#define BIPURPLE "\033[1;95m" // PURPLE
#define BICYAN "\033[1;96m" // CYAN
#define BIWHITE "\033[1;97m" // WHITE
// High Intensity backgrounds
#define ON_IBLACK "\033[0;100m" // BLACK
#define ON_IRED "\033[0;101m" // RED
#define ON_IGREEN "\033[0;102m" // GREEN
#define ON_IYELLOW "\033[0;103m" // YELLOW
#define ON_IBLUE "\033[0;104m" // BLUE
#define ON_IPURPLE "\033[0;105m" // PURPLE
#define ON_ICYAN "\033[0;106m" // CYAN
#define ON_IWHITE "\033[0;107m" // WHITE
#endif // C_COLORS_H
+6
View File
@@ -1,5 +1,7 @@
#include <colors.h>
#include <mongoose.h>
#include <sqlite3.h>
#include "cors.h"
#include "database.h"
#include "route.h"
#include "user.h"
@@ -11,6 +13,7 @@ static void root_handler(struct mg_connection *c, struct mg_http_message *hm) {
static const struct route routes[] = {
{"GET", "/#", root_handler},
{"OPTIONS", "/#", set_cors_headers},
{"POST", "/api/token", get_token},
{"POST", "/api/token/verify", verify_token},
{"POST", "/api/users", get_all_users},
@@ -30,6 +33,8 @@ void ev_handler(struct mg_connection *c, int ev, void *ev_data) {
for (int i = 0; routes[i].path != nullptr; i++) {
if (mg_match(hm->uri, mg_str(routes[i].path), nullptr) &&
mg_match(hm->method, mg_str(routes[i].method), nullptr)) {
mg_log("%s%.*s %s%.*s%s", GREEN, hm->uri.len, hm->uri.buf, BWHITE,
hm->method.len, hm->method.buf, RESET);
routes[i].handler(c, hm);
route_found = true;
break;
@@ -48,6 +53,7 @@ int main(void) {
db_get_users_from_file("users.json");
struct mg_mgr mgr;
mg_log_set(MG_LL_DEBUG);
mg_mgr_init(&mgr);
mg_http_listen(&mgr, "http://0.0.0.0:8222", ev_handler, nullptr);
while (true) {
+21 -10
View File
@@ -2,9 +2,12 @@
void register_user(struct mg_connection *c, struct mg_http_message *hm) {
if (hm->body.len == 0) {
mg_http_reply(c, 400, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("must have <username>, <password>, <email>, <group, "
"optional>, <is_superuser, optional>"));
mg_http_reply(
c, 400,
"Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n",
"{%m:%m}", MG_ESC("error"),
MG_ESC("must have <username>, <password>, <email>, <group, "
"optional>, <is_superuser, optional>"));
return;
}
cJSON *jsonbody = cJSON_Parse(hm->body.buf);
@@ -14,9 +17,12 @@ void register_user(struct mg_connection *c, struct mg_http_message *hm) {
cJSON *group = cJSON_GetObjectItem(jsonbody, "group");
cJSON *is_superuser = cJSON_GetObjectItem(jsonbody, "is_superuser");
if (username == nullptr || password == nullptr || email == nullptr) {
mg_http_reply(c, 400, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("must have <username>, <password>, <email>, <group, "
"optional>, <is_superuser, optional>"));
mg_http_reply(
c, 400,
"Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n",
"{%m:%m}", MG_ESC("error"),
MG_ESC("must have <username>, <password>, <email>, <group, "
"optional>, <is_superuser, optional>"));
return;
}
if (group == nullptr) {
@@ -28,10 +34,12 @@ void register_user(struct mg_connection *c, struct mg_http_message *hm) {
const bool is_superuser_value = is_superuser->valueint ? true : false;
User *new_user_data = db_insert_user(
username->valuestring, email->valuestring, password->valuestring,
group->valuestring, (const bool*)is_superuser_value);
group->valuestring, (const bool *) is_superuser_value);
if (new_user_data == nullptr) {
mg_http_reply(c, 500, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("failed to create user"));
mg_http_reply(
c, 500,
"Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n",
"{%m:%m}", MG_ESC("error"), MG_ESC("failed to create user"));
return;
}
char responsedata[2048] = {0};
@@ -41,6 +49,9 @@ void register_user(struct mg_connection *c, struct mg_http_message *hm) {
new_user_data->id, new_user_data->username, new_user_data->email,
new_user_data->group, new_user_data->is_superuser);
mg_http_reply(c, 200, "Content-Type: application/json\r\n", responsedata);
mg_http_reply(
c, 200,
"Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n",
responsedata);
free_user(new_user_data);
}
+3 -3
View File
@@ -5,14 +5,14 @@
void verify_token(struct mg_connection *c, struct mg_http_message *hm) {
const request_with_token *data = check_body(hm);
if (data->error != nullptr) {
mg_http_reply(c, 403, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"), MG_ESC(data->error));
mg_http_reply(c, 403, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"), MG_ESC(data->error));
return;
}
const bool valid = jwt_verifyJWT(data->token, JWT_DEFAULT_SECRET);
if (!valid) {
mg_http_reply(c, 403, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 403, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("invalid token"));
return;
}
mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{%m:true}", MG_ESC("valid"));
mg_http_reply(c, 200, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:true}", MG_ESC("valid"));
}