add curl_custom_init instead of init curl directly

This commit is contained in:
Jeffrey Hsu 2024-09-09 15:06:54 +08:00
parent 1e3d3e5a83
commit 426db0ede6

59
main.c
View File

@ -38,10 +38,9 @@ struct enc
long long timestamp;
};
CURLcode curl_get(const char *, struct memory *);
long long current_timestamp();
CURLcode curl_get(CURL *, const char *, struct memory *);
size_t cb(char *, size_t, size_t, void *);
struct enc get_enc();
struct enc get_enc(CURL *);
void parise(void *);
void exit_handler();
@ -88,34 +87,37 @@ int main(int argc, char *argv[])
return 0;
}
CURLcode curl_get(const char *const url, struct memory *chunk)
CURLcode curl_custom_init(CURL **curl, struct curl_slist **headers)
{
int status;
CURL *curl = NULL;
struct curl_slist *headers = NULL;
*curl = curl_easy_init();
curl = curl_easy_init();
if (curl == NULL)
if (*curl == NULL)
return CURLE_FAILED_INIT;
headers = curl_slist_append(headers, "Accept: application/json, text/javascript, */*; q=0.01");
headers = curl_slist_append(headers, "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like "
"Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0");
*headers = curl_slist_append(*headers, "Accept: application/json, text/javascript, */*; q=0.01");
*headers =
curl_slist_append(*headers, "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like "
"Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0");
curl_easy_setopt(*curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(*curl, CURLOPT_DNS_SERVERS, DNS);
curl_easy_setopt(*curl, CURLOPT_USE_SSL, true);
curl_easy_setopt(*curl, CURLOPT_WRITEFUNCTION, cb);
return CURLE_OK;
}
CURLcode curl_get(CURL *curl, const char *const url, struct memory *chunk)
{
int status;
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_DNS_SERVERS, DNS);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_USE_SSL, true);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)chunk);
status = curl_easy_perform(curl);
if (status != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(status));
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
return status;
}
@ -143,7 +145,7 @@ size_t cb(char *data, size_t size, size_t nmemb, void *clientp)
return realsize;
}
struct enc get_enc()
struct enc get_enc(CURL *curl)
{
char url[200];
struct enc ret = {0};
@ -155,7 +157,7 @@ struct enc get_enc()
REGEN_ENC_URL_F(url, ret.timestamp);
if (curl_get(url, &data) != CURLE_OK)
if (curl_get(curl, url, &data) != CURLE_OK)
return ret;
DPRINT("%s\n", data.response);
@ -176,6 +178,14 @@ struct enc get_enc()
void parise(void *arg)
{
const int idx = (int)arg;
CURL *curl = NULL;
struct curl_slist *headers = NULL;
if (curl_custom_init(&curl, &headers) != CURLE_OK)
{
fprintf(stderr, "T%d: curl_custom_init() failed\n", idx);
return;
}
while (flag)
{
@ -187,13 +197,12 @@ void parise(void *arg)
const cJSON *data = NULL;
const cJSON *errorMsg = NULL;
cJSON *json = NULL;
if ((enc = get_enc()).enc == NULL)
if ((enc = get_enc(curl)).enc == NULL)
continue;
REGEN_URL_F(url, enc.enc, enc.timestamp);
if (curl_get(url, &json_c) != CURLE_OK)
if (curl_get(curl, url, &json_c) != CURLE_OK)
goto clean;
json = cJSON_Parse(json_c.response);
@ -214,6 +223,8 @@ void parise(void *arg)
cJSON_Delete(json);
}
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
printf("\rSignal Received: exit. T%d exiting\n", idx);
}