From 5c95293ec2284f942ea6ded091d1e1193447b8c5 Mon Sep 17 00:00:00 2001 From: keqingmoe Date: Fri, 27 Dec 2024 19:13:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9C=8D=E5=8A=A1=E5=99=A8?= =?UTF-8?q?=E5=93=8D=E5=BA=94=E5=A4=84=E7=90=86=E5=87=BD=E6=95=B0=EF=BC=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=94=A8=E6=88=B7=E6=B3=A8=E5=86=8C=E3=80=81?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E3=80=81=E5=88=A0=E9=99=A4=E8=B4=A6=E5=8F=B7?= =?UTF-8?q?=E7=AD=89=E5=8A=9F=E8=83=BD=E7=9A=84=E9=94=99=E8=AF=AF=E5=A4=84?= =?UTF-8?q?=E7=90=86=E5=92=8C=E6=88=90=E5=8A=9F=E5=93=8D=E5=BA=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/server/response.h | 45 +++++++++ src/server/response.c | 203 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 248 insertions(+) create mode 100644 include/server/response.h create mode 100644 src/server/response.c 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