Last active
January 2, 2018 17:31
-
-
Save cosysn/c4d0b851c001809e1eed0b774b6a6013 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <cjson/cJSON.h> | |
#define ERR_INVALID_URL (-10) | |
#define ERR (-1) | |
int filter_single_sensitive_word(const cJSON *item, char *name) | |
{ | |
cJSON *subitem = NULL; | |
char *tmp = NULL; | |
if (NULL == item) | |
{ | |
fprintf(stderr, "Parameter is invalid\n"); | |
return ERR; | |
} | |
subitem = cJSON_GetObjectItem(item, name); | |
if (cJSON_IsString(subitem) && (subitem->valuestring != NULL)) | |
{ | |
for (tmp = subitem->valuestring; *tmp; tmp++) | |
{ | |
*tmp = '*'; | |
} | |
} | |
return 0; | |
} | |
int filter_sensitive_word(char *url, int size) | |
{ | |
int ret = 0; | |
char *filter_str = NULL; | |
/*json字符串非法,解析url失败*/ | |
cJSON *url_json = cJSON_Parse(url); | |
if (NULL == url_json) | |
{ | |
const char *error_ptr = cJSON_GetErrorPtr(); | |
if (error_ptr != NULL) | |
{ | |
fprintf(stderr, "Error before: %s\n", error_ptr); | |
} | |
ret = ERR_INVALID_URL; | |
return ret; | |
} | |
/**/ | |
ret = filter_single_sensitive_word(url_json, "ak"); | |
if (0 != ret) | |
{ | |
fprintf(stderr, "filter single sensitive word fail\n"); | |
return ret; | |
} | |
ret = filter_single_sensitive_word(url_json, "sk"); | |
if (0 != ret) | |
{ | |
fprintf(stderr, "filter single sensitive word fail\n"); | |
return ret; | |
} | |
filter_str = cJSON_PrintUnformatted(url_json); | |
strncpy(url, filter_str, size); | |
url[size-1] = '\0'; | |
//fprintf(stderr, "%s\n", cJSON_PrintUnformatted(url_json)); | |
cJSON_Delete(url_json); | |
return 0; | |
} | |
int main() | |
{ | |
int ret = 0; | |
//char url[] = "{\"source\":\"ssdj\",\"ad\": \"sjdhjfdsfjd\",\"tk\":\"abc\",\"sd\":\"hsdfjdshd\"}"; | |
char *url = "{\"dk\":}"; | |
ret = filter_sensitive_word(url, sizeof(url)); | |
if ((ret == 0) || (ret == ERR_INVALID_URL)) | |
{ | |
printf("%s\n", url); | |
} | |
else | |
{ | |
printf("filter sensitive word fail, errno[%d]", ret); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment