优化:
添加主要处理函数Processing_IP_addresses(src_ip); 数据处理放到cache.c 暂时未发现Bug
This commit is contained in:
84
libcurl.c
84
libcurl.c
@@ -27,8 +27,76 @@ static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, voi
|
||||
return realsize;
|
||||
}
|
||||
|
||||
char *GetLocalAddr(char *url)
|
||||
{
|
||||
CURL *curl_handle;
|
||||
CURLcode res;
|
||||
|
||||
struct curl_slist *headers = NULL;
|
||||
struct MemoryStruct chunk;
|
||||
|
||||
chunk.memory = malloc(1); /* 将根据上述再分配的需要增长 */
|
||||
chunk.size = 0; /* 此时没有数据 */
|
||||
|
||||
curl_global_init(CURL_GLOBAL_ALL);
|
||||
|
||||
/* 初始化curl会话 */
|
||||
curl_handle = curl_easy_init();
|
||||
|
||||
char *p = NULL;
|
||||
char *buff;
|
||||
|
||||
p = strstr(url, "-H");
|
||||
if (p) {
|
||||
|
||||
buff = (char *)alloca(p - url + 1);
|
||||
if (buff == NULL)
|
||||
perror("out of memory.");
|
||||
|
||||
memset(buff, 0, p - url + 1);
|
||||
memcpy(buff, url, (int)(p - url - 1));
|
||||
|
||||
// 赋值header值
|
||||
headers = curl_slist_append(headers, p + 3);
|
||||
|
||||
// 设置header
|
||||
curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headers);
|
||||
curl_easy_setopt(curl_handle, CURLOPT_URL, buff);
|
||||
|
||||
} else {
|
||||
/* 指定要获取的URL */
|
||||
curl_easy_setopt(curl_handle, CURLOPT_URL, url);
|
||||
|
||||
}
|
||||
|
||||
/* 将所有数据发送到此函数 */
|
||||
//对于同一次阻塞的curl_easy_perform而言,在写完获取的数据之前,会多次调用 WriteMemoryCallback
|
||||
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
|
||||
|
||||
/* 将"chunk"结构传递给回调函数 */
|
||||
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
|
||||
|
||||
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
|
||||
|
||||
//对于同一次阻塞的curl_easy_perform而言,在写完获取的数据之前,会多次调用 WriteMemoryCallback
|
||||
res = curl_easy_perform(curl_handle);
|
||||
|
||||
if (res != CURLE_OK) {
|
||||
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
|
||||
} else {
|
||||
//printf("%lu bytes retrieved\n", (unsigned long)chunk.size);
|
||||
//printf("%s", chunk.memory);
|
||||
;
|
||||
}
|
||||
|
||||
curl_easy_cleanup(curl_handle);
|
||||
curl_global_cleanup();
|
||||
|
||||
return chunk.memory;
|
||||
}
|
||||
|
||||
// 获取公网IP
|
||||
char *curl_get_area(char *ip)
|
||||
char *CurlGetIpArea(char *ip)
|
||||
{
|
||||
char url[256] = { 0 };
|
||||
snprintf(url, sizeof(url), "https://qifu.baidu.com/ip/geo/v1/district?ip=%s", ip);
|
||||
@@ -114,7 +182,10 @@ void parse_string_field(cJSON *parent, const char *field_name, char *output, siz
|
||||
// 函数用于从 JSON 字符串解析为结构体
|
||||
int parse_json_to_struct(const char *json_string, Response *response)
|
||||
{
|
||||
if (!response) return -1;
|
||||
if (!json_string || !response) {
|
||||
fprintf(stderr, "Invalid input parameters!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 初始化结构体
|
||||
memset(response, 0, sizeof(Response));
|
||||
@@ -152,7 +223,12 @@ int parse_json_to_struct(const char *json_string, Response *response)
|
||||
parse_string_field(data_item, "region", response->data.region, sizeof(response->data.region));
|
||||
}
|
||||
|
||||
snprintf(response->continent_country, 256, "%s%s", response->data.continent, response->data.country);
|
||||
// ----------- 拼接 continent_country ---------------
|
||||
if (snprintf(response->continent_country, sizeof(response->continent_country), "%s%s", response->data.continent, response->data.country) >= sizeof(response->continent_country)) {
|
||||
fprintf(stderr, "continent_country truncated!\n");
|
||||
}
|
||||
|
||||
//snprintf(response->continent_country, 256, "%s%s", response->data.continent, response->data.country);
|
||||
|
||||
// 清理 JSON 对象
|
||||
cJSON_Delete(root);
|
||||
@@ -186,4 +262,4 @@ int main(int argc, char *argv[])
|
||||
|
||||
free(p);
|
||||
}
|
||||
*/
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user