OCT 1.修改JSON字段规范格式检查

This commit is contained in:
dongwenze 2023-03-15 18:12:07 +08:00
parent 05838baa80
commit f06cc2b748
4 changed files with 68 additions and 53 deletions

View File

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

View File

@ -49,7 +49,7 @@ extern "C" {
ERR_CODE(ERR_MQ_CONN_SERVER, 37, "消息队列连接服务器失败") \
ERR_CODE(ERR_MQ_SEND_MSG, 38, "消息队列发送消息失败") \
ERR_CODE(ERR_JSON_CREAT_OBJ, 39, "创建JSON对象失败") \
ERR_CODE(ERR_JSON_PRASE_OBJ, 40, "解析JSON对象失败") \
ERR_CODE(ERR_JSON_PARSE_OBJ, 40, "解析JSON对象失败") \
ERR_CODE(ERR_JSON_VALID_SCH, 41, "JSON数据验证失败") \
ERR_CODE(ERR_CREATE_NETIF, 42, "创建网络接口失败") \
ERR_CODE(ERR_CREATE_PPPOE_NETIF, 43, "创建PPPoE网络接口失败") \
@ -61,8 +61,8 @@ 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_PROTO_DECODE, 52, "HTTP 协议解析失败") \
ERR_CODE(ERR_MSG_CONTENT, 53, "msgContent内容不正确")
#define GENERATE_ENUM(ENUM, no, x) ENUM,
typedef enum {

View File

@ -30,18 +30,12 @@ typedef struct {
} JSON_SCHEMA_CTX, *PJSON_SCHEMA_CTX;
static JSON_SCHEMA_CTX g_json_sch[] = {
{"{\"type\":\"object\",\"required\":[\"ver\"]}", "Missing required field [ver]" },
{"{\"type\":\"object\",\"required\":[\"cryptoType\"]}", "Missing required field [cryptoType]"},
{"{\"type\":\"object\",\"required\":[\"timeStamp\"]}", "Missing required field [timeStamp]" },
{"{\"type\":\"object\",\"required\":[\"ver\"],\"properties\":{\"ver\":{\"type\":\"integer\"}}}", "Incorrect field [ver]"},
{"{\"type\":\"object\",\"required\":[\"cryptoType\"],\"properties\":{\"cryptoType\":{\"type\":\"integer\",\"minimum\":0,\"maximum\":4}}}", "Incorrect field [cryptoType]"},
{"{\"type\":\"object\",\"required\":[\"timeStamp\"],\"properties\":{\"timeStamp\":{\"type\":\"integer\"}}}", "Incorrect field [timeStamp]"},
{"{\"type\":\"object\",\"required\":[\"msgContent\"]}", "Missing required field [msgContent]"},
};
static JSON_SCHEMA_CTX g_json_msg[] = {
{"{\"type\":\"object\",\"required\":[\"rangeSet\"],\"properties\":{\"rangeSet\":{\"type\":\"array\",\"minItems\":1,\"items\":{\"type\":\"object\",\"required\":[\"dhcpRange\"]}}}}", "Incorrect [rangeSet]"},
{"{\"type\":\"object\",\"required\":[\"dhcpRange\"],\"properties\":{\"dhcpRange\":{\"type\":\"array\",\"minItems\":1}}}", "Incorrect [dhcpRange]"},
{"{\"type\":\"object\",\"required\":[\"userMac\"],\"properties\":{\"userMac\":{\"type\":\"array\",\"minItems\":1}}}", "Incorrect [userMac]"},
};
const char *proto_schema_validation(const char *pJsonStr) {
int i;
@ -49,8 +43,9 @@ const char *proto_schema_validation(const char *pJsonStr) {
if (!pJs) {
cJSON *pRspRoot = cJSON_CreateObject();
cJSON_AddNumberToObject(pRspRoot, "status", ERR_JSON_PRASE_OBJ);
cJSON_AddStringToObject(pRspRoot, "message", getErrorEnumDesc(ERR_JSON_PRASE_OBJ));
cJSON_AddNumberToObject(pRspRoot, "status", ERR_JSON_PARSE_OBJ);
cJSON_AddStringToObject(pRspRoot, "message", getErrorEnumDesc(ERR_JSON_PARSE_OBJ));
printf("ERR_JSON_PARSE_OBJ\n");
return proto_create_new(pRspRoot, 200);
}
@ -79,25 +74,25 @@ const char *proto_schema_validation(const char *pJsonStr) {
return NULL;
}
const char *proto_msg_validation(const char *pJsonStr, PROTO_POST_TYPE type_id) {
const char *proto_msg_validation(const char *pJsonStr, const char *msgJson) {
json_object *pJs = json_tokener_parse(pJsonStr);
if (!pJs) {
cJSON *pRspRoot = cJSON_CreateObject();
cJSON_AddNumberToObject(pRspRoot, "status", ERR_JSON_PRASE_OBJ);
cJSON_AddStringToObject(pRspRoot, "message", getErrorEnumDesc(ERR_JSON_PRASE_OBJ));
cJSON_AddNumberToObject(pRspRoot, "status", ERR_JSON_PARSE_OBJ);
cJSON_AddStringToObject(pRspRoot, "message", getErrorEnumDesc(ERR_JSON_PARSE_OBJ));
return proto_create_new(pRspRoot, 200);
}
json_object *pSc = json_tokener_parse(g_json_msg[type_id].pSchJson);
json_object *pSc = json_tokener_parse(msgJson);
if (!pSc) {
LOG_MOD(error, ZLOG_MOD_PROTO, "Json schema format error: [%s]\n", g_json_msg[type_id].pSchJson);
LOG_MOD(error, ZLOG_MOD_PROTO, "Json schema format error: [%s]\n", msgJson);
}
if (jdac_validate(pJs, pSc) != JDAC_ERR_VALID) {
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", g_json_msg[type_id].pErrMsg);
cJSON_AddStringToObject(pRspRoot, "details", getErrorEnumDesc(ERR_MSG_CONTENT));
json_object_put(pSc);
json_object_put(pJs);
return proto_create_new(pRspRoot, 200);
@ -109,15 +104,15 @@ const char *proto_msg_validation(const char *pJsonStr, PROTO_POST_TYPE type_id)
}
#endif
const char *proto_decode_context(const char *pString, unsigned int *pVer,
unsigned long long *pTm, PROTO_POST_TYPE type_id, int *pErrCode) {
const char *proto_decode_context(const char *pString, unsigned int *pVer, unsigned long long *pTm, int *pErrCode) {
cJSON *pMsgCtx;
unsigned char *pBase64;
int decodeSize;
unsigned int outSize = 0;
char *pMsgContent = NULL;
cJSON *pRoot;
#ifdef JSON_SCHEMA_ON
const char *pSchJson;
const char *pSchJson;
#endif
if (pErrCode == NULL) {
@ -167,19 +162,7 @@ const char *proto_decode_context(const char *pString, unsigned int *pVer,
}
}
cJSON *pMsgCtx = cJSON_GetObjectItem(pRoot, "msgContent");
char *msgCont = cJSON_Print(pMsgCtx);
#ifdef JSON_SCHEMA_ON
pSchJson = proto_msg_validation(msgCont, type_id);
free((void *)msgCont);
if (pSchJson != NULL && strlen(pSchJson) > 0) {
*pErrCode = ERR_JSON_VALID_SCH;
cJSON_Delete(pRoot);
return pSchJson;
}
#endif
pMsgCtx = cJSON_GetObjectItem(pRoot, "msgContent");
if (!pMsgCtx) {
cJSON_Delete(pRoot);

View File

@ -43,23 +43,31 @@ static PIPTV_DEV_SET g_iptvNewDevs = nullptr;
static PIPTV_DEV_SET g_iptvCacheDevs = nullptr;
static uv_rwlock_t g_uvCacheLock;
#ifdef JSON_SCHEMA_ON
const char *g_json_msg[] = {
"{\"type\":\"object\",\"required\":[\"rangeSet\"],\"properties\":{\"rangeSet\":{\"type\":\"array\",\"minItems\":1,\"items\":{\"type\":\"object\",\"required\":[\"dhcpRange\"]}}}}",
"{\"type\":\"object\",\"required\":[\"dhcpRange\"],\"properties\":{\"dhcpRange\":{\"type\":\"array\",\"minItems\":1}}}",
"{\"type\":\"object\",\"required\":[\"userMac\"],\"properties\":{\"userMac\":{\"type\":\"array\",\"minItems\":1}}}"
};
#endif
static int dhcp_get_user_info(const char **pRsp, const char *pRequest) {
char logBuff[512];
const char *pStrContent;
int k;
int errCode;
int errCode = 0;
dhcpMap::iterator p;
if (pRequest == nullptr || strlen(pRequest) == 0) {
sprintf(logBuff, "Requeset Json");
sprintf(logBuff, "Request Json");
logDHCPMess(logBuff, 1);
return ERR_INPUT_PARAMS;
}
pStrContent = proto_decode_context(pRequest, nullptr, nullptr, TYPE_QUE_USR, &errCode);
pStrContent = proto_decode_context(pRequest, nullptr, nullptr, &errCode);
if (pStrContent == nullptr) {
sprintf(logBuff, "Requeset Json error %s", pRequest);
sprintf(logBuff, "Request Json error %s", pRequest);
logDHCPMess(logBuff, 1);
return ERR_PROTO_DECODE;
}
@ -69,20 +77,28 @@ static int dhcp_get_user_info(const char **pRsp, const char *pRequest) {
*pRsp = pStrContent;
return ERR_SUCCESS;
}
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;
}
free((void *)pSchJson);
#endif
cJSON *pRoot = cJSON_Parse(pStrContent);
free((void *)pStrContent);
if (!pRoot) {
return ERR_JSON_PRASE_OBJ;
return ERR_JSON_PARSE_OBJ;
}
cJSON *pUserMac = cJSON_GetObjectItem(pRoot, "userMac");
if (!pUserMac) {
cJSON_Delete(pRoot);
return ERR_JSON_PRASE_OBJ;
return ERR_JSON_PARSE_OBJ;
}
cJSON *pRspMsg = cJSON_CreateObject();
@ -326,18 +342,18 @@ static int add_dhcpd_rangeset(const char **pRsp, const char *pRequest) {
OBJ_DHCP_RNG range;
cJSON *pRspRoot;
cJSON *pExpandArray;
int errCode;
int errCode = 0;
if (pRequest == nullptr || strlen(pRequest) == 0) {
sprintf(logBuff, "Requeset Json");
sprintf(logBuff, "Request Json");
logDHCPMess(logBuff, 1);
return ERR_INPUT_PARAMS;
}
pStrContent = proto_decode_context(pRequest, nullptr, nullptr, TYPE_ADD_RNG, &errCode);
pStrContent = proto_decode_context(pRequest, nullptr, nullptr, &errCode);
if (pStrContent == nullptr) {
sprintf(logBuff, "Requeset Json error %s", pRequest);
sprintf(logBuff, "Request Json error %s", pRequest);
logDHCPMess(logBuff, 1);
return ERR_PROTO_DECODE;
}
@ -347,13 +363,21 @@ static int add_dhcpd_rangeset(const char **pRsp, const char *pRequest) {
*pRsp = pStrContent;
return ERR_SUCCESS;
}
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;
}
free((void *)pSchJson);
#endif
cJSON *pRoot = cJSON_Parse(pStrContent);
free((void *)pStrContent);
if (!pRoot) {
return ERR_JSON_PRASE_OBJ;
return ERR_JSON_PARSE_OBJ;
}
cJSON *prange_set = cJSON_GetObjectItem(pRoot, "rangeSet");
@ -433,17 +457,17 @@ static int delete_dhcpd_rangeset(const char **pRsp, const char *pRequest) {
data13 dhcpRanges[MAX_DHCP_RANGES];
PHASH_MAP delMap = nullptr;
int resCount = 0;
int errCode;
int errCode = 0;
if (pRequest == nullptr || strlen(pRequest) == 0) {
sprintf(logBuff, "Requeset Json");
sprintf(logBuff, "Request Json");
logDHCPMess(logBuff, 1);
return ERR_INPUT_PARAMS;
}
pStrContent = proto_decode_context(pRequest, nullptr, nullptr, TYPE_DEL_RNG, &errCode);
pStrContent = proto_decode_context(pRequest, nullptr, nullptr, &errCode);
if (pStrContent == nullptr) {
sprintf(logBuff, "Requeset Json error %s", pRequest);
sprintf(logBuff, "Request Json error %s", pRequest);
logDHCPMess(logBuff, 1);
return ERR_PROTO_DECODE;
}
@ -453,12 +477,19 @@ static int delete_dhcpd_rangeset(const char **pRsp, const char *pRequest) {
*pRsp = pStrContent;
return ERR_SUCCESS;
}
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;
}
#endif
cJSON *pRoot = cJSON_Parse(pStrContent);
free((void *)pStrContent);
if (!pRoot) {
return ERR_JSON_PRASE_OBJ;
return ERR_JSON_PARSE_OBJ;
}
cJSON *pdhcp_range = cJSON_GetObjectItem(pRoot, "dhcpRange");