diff --git a/srcs/libs/include/proto.h b/srcs/libs/include/proto.h index 3a53f7c..aa5d56c 100644 --- a/srcs/libs/include/proto.h +++ b/srcs/libs/include/proto.h @@ -24,7 +24,7 @@ typedef enum { TYPE_QUE_USR = 2, } 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_decode_context(const char *pString, unsigned int *pVer, unsigned long long *pTm, int *pErrCode); #ifdef __cplusplus diff --git a/srcs/libs/include/user_errno.h b/srcs/libs/include/user_errno.h index 3982104..d474216 100644 --- a/srcs/libs/include/user_errno.h +++ b/srcs/libs/include/user_errno.h @@ -61,8 +61,7 @@ extern "C" { ERR_CODE(ERR_MENU_EXIT, 49, "菜单执行完后自动退出") \ ERR_CODE(ERR_HTTP_UNSUP_METHOD, 50, "不支持的 HTTP 请求方法") \ ERR_CODE(ERR_HTTP_UNSUP_PAGE, 51, "找不到 HTTP 服务") \ - ERR_CODE(ERR_PROTO_DECODE, 52, "HTTP 协议解析失败") \ - ERR_CODE(ERR_MSG_CONTENT, 53, "msgContent内容不正确") + ERR_CODE(ERR_PROTO_DECODE, 52, "HTTP 协议解析失败") #define GENERATE_ENUM(ENUM, no, x) ENUM, typedef enum { diff --git a/srcs/libs/protocol/protocol.c b/srcs/libs/protocol/protocol.c index d0044d7..3c7c92f 100644 --- a/srcs/libs/protocol/protocol.c +++ b/srcs/libs/protocol/protocol.c @@ -77,7 +77,7 @@ const char *proto_schema_validation(const char *pJsonStr) { 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); if (!pJs) { cJSON *pRspRoot = cJSON_CreateObject(); @@ -95,7 +95,7 @@ const char *proto_msg_validation(const char *pJsonStr, const char *msgJson) { cJSON *pRspRoot = cJSON_CreateObject(); cJSON_AddNumberToObject(pRspRoot, "status", 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(pJs); return proto_create_new(pRspRoot, 200); diff --git a/srcs/opendhcp183/query.cpp b/srcs/opendhcp183/query.cpp index 7b442e1..67d20e1 100644 --- a/srcs/opendhcp183/query.cpp +++ b/srcs/opendhcp183/query.cpp @@ -48,10 +48,29 @@ static uv_rwlock_t g_uvCacheLock; #ifdef JSON_SCHEMA_ON // clang-format off -const char *g_json_msg[] = { - R"({"type":"object","required":["rangeSet"],"properties":{"rangeSet":{"type":"array","minItems":1,"items":{"type":"object","required":["dhcpRange"]}}}})", - R"({"type":"object","required":["dhcpRange"],"properties":{"dhcpRange":{"type":"array","minItems":1}}})", - R"({"type":"object","required":["userMac"],"properties":{"userMac":{"type":"array","minItems":1}}})"}; +typedef struct { + const char *pSchJson; + const char *pErrMsg; +} 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 #endif @@ -83,10 +102,12 @@ static int dhcp_get_user_info(const char **pRsp, const char *pRequest) { } const char *pSchJson; - pSchJson = proto_msg_validation(pStrContent, g_json_msg[TYPE_QUE_USR]); - if (pSchJson != nullptr && strlen(pSchJson) > 0) { - *pRsp = pSchJson; - return ERR_SUCCESS; + for(auto i : g_que_msg) { + pSchJson = proto_msg_validation(pStrContent, i.pSchJson, i.pErrMsg); + if (pSchJson != nullptr && strlen(pSchJson) > 0) { + *pRsp = pSchJson; + return ERR_SUCCESS; + } } free((void *)pSchJson); #endif @@ -369,10 +390,12 @@ static int add_dhcpd_rangeset(const char **pRsp, const char *pRequest) { } const char *pSchJson; - pSchJson = proto_msg_validation(pStrContent, g_json_msg[TYPE_ADD_RNG]); - if (pSchJson != nullptr && strlen(pSchJson) > 0) { - *pRsp = pSchJson; - return ERR_SUCCESS; + for(auto i : g_add_msg) { + pSchJson = proto_msg_validation(pStrContent, i.pSchJson, i.pErrMsg); + if (pSchJson != nullptr && strlen(pSchJson) > 0) { + *pRsp = pSchJson; + return ERR_SUCCESS; + } } free((void *)pSchJson); #endif @@ -483,11 +506,14 @@ static int delete_dhcpd_rangeset(const char **pRsp, const char *pRequest) { } const char *pSchJson; - pSchJson = proto_msg_validation(pStrContent, g_json_msg[TYPE_DEL_RNG]); - if (pSchJson != nullptr && strlen(pSchJson) > 0) { - *pRsp = pSchJson; - return ERR_SUCCESS; + for(auto i : g_del_msg) { + pSchJson = proto_msg_validation(pStrContent, i.pSchJson, i.pErrMsg); + if (pSchJson != nullptr && strlen(pSchJson) > 0) { + *pRsp = pSchJson; + return ERR_SUCCESS; + } } + free((void*)pSchJson); #endif cJSON *pRoot = cJSON_Parse(pStrContent);