添加服务器响应处理函数,支持用户注册、登录、删除账号等功能的错误处理和成功响应
This commit is contained in:
parent
93df543bb3
commit
5c95293ec2
45
include/server/response.h
Normal file
45
include/server/response.h
Normal file
@ -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
|
203
src/server/response.c
Normal file
203
src/server/response.c
Normal file
@ -0,0 +1,203 @@
|
||||
#include "server/types.h"
|
||||
#include <server/response.h>
|
||||
|
||||
#include <civetweb.h>
|
||||
|
||||
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);
|
Loading…
Reference in New Issue
Block a user