diff --git a/include/server/response.h b/include/server/response.h new file mode 100644 index 0000000..d930bb4 --- /dev/null +++ b/include/server/response.h @@ -0,0 +1,45 @@ +#ifndef SERVER_RESPONSE_H +#define SERVER_RESPONSE_H + +#include "types.h" + +void res_null_req(mg_connection* conn); +void res_must_get(mg_connection* conn); +void res_must_post(mg_connection* conn); +void res_need_token(mg_connection* conn); +void res_auth_fail(mg_connection* conn); +void res_check_exist_fail(mg_connection* conn); +void res_user_exist(mg_connection* conn); +void res_not_exist(mg_connection* conn); +void res_check_permission_fail(mg_connection* conn); +void res_permission_denied(mg_connection* conn); +void res_need_user_id(mg_connection* conn); +void res_need_password(mg_connection* conn); +void res_(mg_connection* conn); +void res_(mg_connection* conn); +void res_(mg_connection* conn); +void res_(mg_connection* conn); + + +void res_delete_account_fail(mg_connection* conn); +void res_delete_account(mg_connection* conn); + +void res_login_fail(mg_connection* conn); +void res_incorrect(mg_connection* conn); +void res_login(mg_connection* conn, const char* token); + +void res_register_fail(mg_connection* conn); +void res_register(mg_connection* conn); + +void res_repasswd_fail(mg_connection* conn); +void res_repasswd(mg_connection* conn); + +void res_logout_fail(mg_connection* conn); +void res_logout(mg_connection* conn); + +void res_(mg_connection* conn); +void res_(mg_connection* conn); +void res_(mg_connection* conn); +void res_(mg_connection* conn); + +#endif \ No newline at end of file diff --git a/src/server/response.c b/src/server/response.c new file mode 100644 index 0000000..5cfc9f0 --- /dev/null +++ b/src/server/response.c @@ -0,0 +1,203 @@ +#include "server/types.h" +#include + +#include + +void res_null_req(mg_connection* conn) +{ + mg_printf(conn, + "HTTP/1.1 400 Bad Request\r\n" + "Content-Type: application/json\r\n" + "Access-Control-Allow-Origin: *\r\n\r\n" + "{\"error\":\"null request\"}"); +} +void res_must_get(mg_connection* conn) +{ + mg_printf(conn, + "HTTP/1.1 405 Method Not Allowed\r\n" + "Content-Type: application/json\r\n" + "Access-Control-Allow-Origin: *\r\n\r\n" + "{\"error\":\"must send get request\"}"); +} +void res_must_post(mg_connection* conn) +{ + mg_printf(conn, + "HTTP/1.1 405 Method Not Allowed\r\n" + "Content-Type: application/json\r\n" + "Access-Control-Allow-Origin: *\r\n\r\n" + "{\"error\":\"must send post request\"}"); +} +void res_need_token(mg_connection* conn) +{ + mg_printf(conn, + "HTTP/1.1 400 Bad Request\r\n" + "Content-Type: application/json\r\n" + "Access-Control-Allow-Origin: *\r\n\r\n" + "{\"error\":\"need token\"}"); +} +void res_auth_fail(mg_connection* conn) +{ + mg_printf(conn, + "HTTP/1.1 401 Unauthorized\r\n" + "Content-Type: application/json\r\n" + "Access-Control-Allow-Origin: *\r\n\r\n" + "{\"error\":\"auth failed\"}"); +} +void res_check_exist_fail(mg_connection* conn) +{ + mg_printf(conn, + "HTTP/1.1 500 Internal Server Error\r\n" + "Content-Type: application/json\r\n" + "Access-Control-Allow-Origin: *\r\n\r\n" + "{\"error\":\"failed to check user existence\"}"); +} +void res_user_exist(mg_connection* conn) +{ + mg_printf(conn, + "HTTP/1.1 409 Conflict\r\n" + "Content-Type: application/json\r\n" + "Access-Control-Allow-Origin: *\r\n\r\n" + "{\"error\":\"user already exists\"}"); +} +void res_not_exist(mg_connection* conn) +{ + mg_printf(conn, + "HTTP/1.1 404 Not Found\r\n" + "Content-Type: application/json\r\n" + "Access-Control-Allow-Origin: *\r\n\r\n" + "{\"error\":\"user does not exist\"}"); +} +void res_check_permission_fail(mg_connection* conn) +{ + mg_printf(conn, + "HTTP/1.1 500 Internal Server Error\r\n" + "Content-Type: application/json\r\n" + "Access-Control-Allow-Origin: *\r\n\r\n" + "{\"error\":\"failed to check user permission\"}"); +} +void res_permission_denied(mg_connection* conn) +{ + mg_printf(conn, + "HTTP/1.1 403 Forbidden\r\n" + "Content-Type: application/json\r\n" + "Access-Control-Allow-Origin: *\r\n\r\n" + "{\"error\":\"permission denied\"}"); +} +void res_need_user_id(mg_connection* conn) +{ + mg_printf(conn, + "HTTP/1.1 400 Bad Request\r\n" + "Content-Type: application/json\r\n" + "Access-Control-Allow-Origin: *\r\n\r\n" + "{\"error\":\"need user_id\"}"); +} +void res_need_password(mg_connection* conn) +{ + mg_printf(conn, + "HTTP/1.1 400 Bad Request\r\n" + "Content-Type: application/json\r\n" + "Access-Control-Allow-Origin: *\r\n\r\n" + "{\"error\":\"need password\"}"); +} +void res_(mg_connection* conn); +void res_(mg_connection* conn); +void res_(mg_connection* conn); +void res_(mg_connection* conn); + + +void res_delete_account_fail(mg_connection* conn) +{ + mg_printf(conn, + "HTTP/1.1 500 Internal Server Error\r\n" + "Content-Type: application/json\r\n" + "Access-Control-Allow-Origin: *\r\n\r\n" + "{\"error\":\"failed to delete account\"}"); +} +void res_delete_account(mg_connection* conn) +{ + mg_printf(conn, + "HTTP/1.1 200 OK\r\n" + "Content-Type: application/json\r\n" + "Access-Control-Allow-Origin: *\r\n\r\n" + "{\"success\":\"delete account success\"}"); +} + +void res_login_fail(mg_connection* conn) +{ + mg_printf(conn, + "HTTP/1.1 500 Internal Server Error\r\n" + "Content-Type: application/json\r\n" + "Access-Control-Allow-Origin: *\r\n\r\n" + "{\"error\":\"failed to login\"}"); +} +void res_incorrect(mg_connection* conn) +{ + mg_printf(conn, + "HTTP/1.1 401 Unauthorized\r\n" + "Content-Type: application/json\r\n" + "Access-Control-Allow-Origin: *\r\n\r\n" + "{\"error\":\"incorrect password or user id\"}"); +} +void res_login(mg_connection* conn, const char* token) +{ + mg_printf(conn, + "HTTP/1.1 200 OK\r\n" + "Content-Type: application/json\r\n" + "Access-Control-Allow-Origin: *\r\n\r\n" + "{\"success\":\"login success\", \"token\":\"%s\"}", + token); +} + +void res_register_fail(mg_connection* conn) +{ + mg_printf(conn, + "HTTP/1.1 500 Internal Server Error\r\n" + "Content-Type: application/json\r\n" + "Access-Control-Allow-Origin: *\r\n\r\n" + "{\"error\":\"failed to register\"}"); +} +void res_register(mg_connection* conn) +{ + mg_printf(conn, + "HTTP/1.1 200 OK\r\n" + "Content-Type: application/json\r\n" + "Access-Control-Allow-Origin: *\r\n\r\n" + "{\"success\":\"registration success\"}"); +} + +void res_repasswd_fail(mg_connection* conn) +{ + mg_printf(conn, + "HTTP/1.1 500 Internal Server Error\r\n" + "Content-Type: application/json\r\n" + "Access-Control-Allow-Origin: *\r\n\r\n" + "{\"error\":\"failed to repasswd account\"}"); +} +void res_repasswd(mg_connection* conn) +{ + mg_printf(conn, + "HTTP/1.1 200 OK\r\n" + "Content-Type: application/json\r\n" + "Access-Control-Allow-Origin: *\r\n\r\n" + "{\"success\":\"repasswd account success\"}"); +} + +void res_logout_fail(mg_connection* conn) +{ + mg_printf(conn, + "HTTP/1.1 500 Internal Server Error\r\n" + "Content-Type: application/json\r\n" + "Access-Control-Allow-Origin: *\r\n\r\n" + "{\"error\":\"failed to logout\"}"); +} +void res_logout(mg_connection* conn) +{ + mg_printf(conn, + "HTTP/1.1 200 OK\r\n" + "Content-Type: application/json\r\n" + "Access-Control-Allow-Origin: *\r\n\r\n" + "{\"success\":\"logout success\"}"); +} + +void res_(mg_connection* conn); +void res_(mg_connection* conn); \ No newline at end of file