From 28655e7b463c147d12af1c5a3ef99a658ba00415 Mon Sep 17 00:00:00 2001 From: TolaMironcenko Date: Thu, 28 Aug 2025 18:02:18 +0700 Subject: [PATCH] added cors headers and colors log --- src/add_user.c | 10 +++--- src/change_password.c | 20 +++++------ src/change_user.c | 16 ++++----- src/cors.c | 5 +++ src/cors.h | 5 +++ src/delete_user.c | 12 +++---- src/get_all_users.c | 6 ++-- src/get_token.c | 12 +++---- src/get_user.c | 10 +++--- src/lib/colors.h | 77 +++++++++++++++++++++++++++++++++++++++++++ src/main.c | 6 ++++ src/register_user.c | 31 +++++++++++------ src/verify_token.c | 6 ++-- 13 files changed, 160 insertions(+), 56 deletions(-) create mode 100644 src/cors.c create mode 100644 src/cors.h create mode 100755 src/lib/colors.h diff --git a/src/add_user.c b/src/add_user.c index b089abd..618d27f 100644 --- a/src/add_user.c +++ b/src/add_user.c @@ -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 , , , , ")); 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); } diff --git a/src/change_password.c b/src/change_password.c index c1b5c5f..ddcee84 100644 --- a/src/change_password.c +++ b/src/change_password.c @@ -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 , ")); 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); diff --git a/src/change_user.c b/src/change_user.c index e4c42b7..f34aeb0 100644 --- a/src/change_user.c +++ b/src/change_user.c @@ -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 , , , ")); @@ -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); diff --git a/src/cors.c b/src/cors.c new file mode 100644 index 0000000..85dac94 --- /dev/null +++ b/src/cors.c @@ -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", ""); +} diff --git a/src/cors.h b/src/cors.h new file mode 100644 index 0000000..a079f73 --- /dev/null +++ b/src/cors.h @@ -0,0 +1,5 @@ +#pragma once + +#include + +void set_cors_headers(struct mg_connection *c, struct mg_http_message *hm); diff --git a/src/delete_user.c b/src/delete_user.c index c3e1cfa..6a39bb7 100644 --- a/src/delete_user.c +++ b/src/delete_user.c @@ -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); } diff --git a/src/get_all_users.c b/src/get_all_users.c index 8163d7d..dd530ca 100644 --- a/src/get_all_users.c +++ b/src/get_all_users.c @@ -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; } diff --git a/src/get_token.c b/src/get_token.c index 46c4109..07467ba 100644 --- a/src/get_token.c +++ b/src/get_token.c @@ -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); } diff --git a/src/get_user.c b/src/get_user.c index c970f15..79b79d0 100644 --- a/src/get_user.c +++ b/src/get_user.c @@ -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); diff --git a/src/lib/colors.h b/src/lib/colors.h new file mode 100755 index 0000000..98570c0 --- /dev/null +++ b/src/lib/colors.h @@ -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 diff --git a/src/main.c b/src/main.c index 7989303..8a5219e 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,7 @@ +#include #include #include +#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) { diff --git a/src/register_user.c b/src/register_user.c index 426108f..0514d88 100644 --- a/src/register_user.c +++ b/src/register_user.c @@ -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 , , , , ")); + 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 , , , , ")); 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 , , , , ")); + 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 , , , , ")); 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); } diff --git a/src/verify_token.c b/src/verify_token.c index 9e6e23f..d4402ad 100644 --- a/src/verify_token.c +++ b/src/verify_token.c @@ -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")); }