OCT 1.细化JSON规范错误类型

This commit is contained in:
dongwenze 2023-03-28 18:06:46 +08:00
parent b256424f71
commit e21d9994e0
4 changed files with 46 additions and 21 deletions

View File

@ -24,7 +24,7 @@ typedef enum {
TYPE_QUE_USR = 2, TYPE_QUE_USR = 2,
} PROTO_POST_TYPE; } PROTO_POST_TYPE;
const char *proto_msg_validation(const char *pJsonStr, const char *msgJson); const char *proto_msg_validation(const char *pJsonStr, const char *msgJson, const char *errMsg);
const char *proto_create_new(cJSON *pMsgCtx, int httpCode); const char *proto_create_new(cJSON *pMsgCtx, int httpCode);
const char *proto_decode_context(const char *pString, unsigned int *pVer, unsigned long long *pTm, int *pErrCode); const char *proto_decode_context(const char *pString, unsigned int *pVer, unsigned long long *pTm, int *pErrCode);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -61,8 +61,7 @@ extern "C" {
ERR_CODE(ERR_MENU_EXIT, 49, "菜单执行完后自动退出") \ ERR_CODE(ERR_MENU_EXIT, 49, "菜单执行完后自动退出") \
ERR_CODE(ERR_HTTP_UNSUP_METHOD, 50, "不支持的 HTTP 请求方法") \ ERR_CODE(ERR_HTTP_UNSUP_METHOD, 50, "不支持的 HTTP 请求方法") \
ERR_CODE(ERR_HTTP_UNSUP_PAGE, 51, "找不到 HTTP 服务") \ ERR_CODE(ERR_HTTP_UNSUP_PAGE, 51, "找不到 HTTP 服务") \
ERR_CODE(ERR_PROTO_DECODE, 52, "HTTP 协议解析失败") \ ERR_CODE(ERR_PROTO_DECODE, 52, "HTTP 协议解析失败")
ERR_CODE(ERR_MSG_CONTENT, 53, "msgContent内容不正确")
#define GENERATE_ENUM(ENUM, no, x) ENUM, #define GENERATE_ENUM(ENUM, no, x) ENUM,
typedef enum { typedef enum {

View File

@ -77,7 +77,7 @@ const char *proto_schema_validation(const char *pJsonStr) {
return NULL; return NULL;
} }
const char *proto_msg_validation(const char *pJsonStr, const char *msgJson) { const char *proto_msg_validation(const char *pJsonStr, const char *msgJson, const char *errMsg) {
json_object *pJs = json_tokener_parse(pJsonStr); json_object *pJs = json_tokener_parse(pJsonStr);
if (!pJs) { if (!pJs) {
cJSON *pRspRoot = cJSON_CreateObject(); cJSON *pRspRoot = cJSON_CreateObject();
@ -95,7 +95,7 @@ const char *proto_msg_validation(const char *pJsonStr, const char *msgJson) {
cJSON *pRspRoot = cJSON_CreateObject(); cJSON *pRspRoot = cJSON_CreateObject();
cJSON_AddNumberToObject(pRspRoot, "status", ERR_JSON_VALID_SCH); cJSON_AddNumberToObject(pRspRoot, "status", ERR_JSON_VALID_SCH);
cJSON_AddStringToObject(pRspRoot, "message", getErrorEnumDesc(ERR_JSON_VALID_SCH)); cJSON_AddStringToObject(pRspRoot, "message", getErrorEnumDesc(ERR_JSON_VALID_SCH));
cJSON_AddStringToObject(pRspRoot, "details", getErrorEnumDesc(ERR_MSG_CONTENT)); cJSON_AddStringToObject(pRspRoot, "details", errMsg);
json_object_put(pSc); json_object_put(pSc);
json_object_put(pJs); json_object_put(pJs);
return proto_create_new(pRspRoot, 200); return proto_create_new(pRspRoot, 200);

View File

@ -48,10 +48,29 @@ static uv_rwlock_t g_uvCacheLock;
#ifdef JSON_SCHEMA_ON #ifdef JSON_SCHEMA_ON
// clang-format off // clang-format off
const char *g_json_msg[] = { typedef struct {
R"({"type":"object","required":["rangeSet"],"properties":{"rangeSet":{"type":"array","minItems":1,"items":{"type":"object","required":["dhcpRange"]}}}})", const char *pSchJson;
R"({"type":"object","required":["dhcpRange"],"properties":{"dhcpRange":{"type":"array","minItems":1}}})", const char *pErrMsg;
R"({"type":"object","required":["userMac"],"properties":{"userMac":{"type":"array","minItems":1}}})"}; } JSON_POST_CTX;
static JSON_POST_CTX g_add_msg[] = {
{R"({"type":"object","required":["rangeSet"]})", "Missing required field [rangeSet]"},
{R"({"properties":{"rangeSet":{"type":"array","minItems":1}}})", "No content in field [rangeSet]"},
{R"({"properties":{"rangeSet":{"items":{"type":"object","required":["dhcpRange"]}}}})", "Missing required field [dhcpRange]"},
// {R"({"properties":{"rangeSet":{"items":{"properties":{"dhcpRange":{"type":"string","minLength":15}}}}}})", "Error in field [dhcpRange]"}
};
static JSON_POST_CTX g_del_msg[] = {
{R"({"type":"object","required":["dhcpRange"]})", "Missing required field [dhcpRange]"},
{R"({"properties":{"dhcpRange":{"type":"array","minItems":1}}})", "No content in field [dhcpRange]"},
// {R"({"properties":{"dhcpRange":{"items":{"type":"string","minLength":15}}}})", "Error in field [dhcpRange]"}
};
static JSON_POST_CTX g_que_msg[] = {
{R"({"type":"object","required":["userMac"]})", "Missing required field [userMac]"},
{R"({"properties":{"userMac":{"type":"array","minItems":1}}})", "No content in field [userMac]"},
// {R"({"properties":{"userMac":{"items":{"type":"string","minLength":17,"maxLength":17}}}})", "Error in field [userMac]"}
};
// clang-format on // clang-format on
#endif #endif
@ -83,11 +102,13 @@ static int dhcp_get_user_info(const char **pRsp, const char *pRequest) {
} }
const char *pSchJson; const char *pSchJson;
pSchJson = proto_msg_validation(pStrContent, g_json_msg[TYPE_QUE_USR]); for(auto i : g_que_msg) {
pSchJson = proto_msg_validation(pStrContent, i.pSchJson, i.pErrMsg);
if (pSchJson != nullptr && strlen(pSchJson) > 0) { if (pSchJson != nullptr && strlen(pSchJson) > 0) {
*pRsp = pSchJson; *pRsp = pSchJson;
return ERR_SUCCESS; return ERR_SUCCESS;
} }
}
free((void *)pSchJson); free((void *)pSchJson);
#endif #endif
@ -369,11 +390,13 @@ static int add_dhcpd_rangeset(const char **pRsp, const char *pRequest) {
} }
const char *pSchJson; const char *pSchJson;
pSchJson = proto_msg_validation(pStrContent, g_json_msg[TYPE_ADD_RNG]); for(auto i : g_add_msg) {
pSchJson = proto_msg_validation(pStrContent, i.pSchJson, i.pErrMsg);
if (pSchJson != nullptr && strlen(pSchJson) > 0) { if (pSchJson != nullptr && strlen(pSchJson) > 0) {
*pRsp = pSchJson; *pRsp = pSchJson;
return ERR_SUCCESS; return ERR_SUCCESS;
} }
}
free((void *)pSchJson); free((void *)pSchJson);
#endif #endif
@ -483,11 +506,14 @@ static int delete_dhcpd_rangeset(const char **pRsp, const char *pRequest) {
} }
const char *pSchJson; const char *pSchJson;
pSchJson = proto_msg_validation(pStrContent, g_json_msg[TYPE_DEL_RNG]); for(auto i : g_del_msg) {
pSchJson = proto_msg_validation(pStrContent, i.pSchJson, i.pErrMsg);
if (pSchJson != nullptr && strlen(pSchJson) > 0) { if (pSchJson != nullptr && strlen(pSchJson) > 0) {
*pRsp = pSchJson; *pRsp = pSchJson;
return ERR_SUCCESS; return ERR_SUCCESS;
} }
}
free((void*)pSchJson);
#endif #endif
cJSON *pRoot = cJSON_Parse(pStrContent); cJSON *pRoot = cJSON_Parse(pStrContent);