refactoring - now check return code from curl_easy_getinfo

This commit is contained in:
Fufu Fang 2021-09-03 12:47:48 +01:00
parent c64a139b46
commit 08eb04fb0e
4 changed files with 59 additions and 21 deletions

View File

@ -341,11 +341,23 @@ static void HTML_to_LinkTable(GumboNode * node, LinkTable * linktbl)
void Link_set_file_stat(Link * this_link, CURL * curl)
{
long http_resp;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp);
CURLcode ret =
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
if (http_resp == HTTP_OK) {
double cl = 0;
curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &cl);
curl_easy_getinfo(curl, CURLINFO_FILETIME, &(this_link->time));
ret =
curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &cl);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
ret =
curl_easy_getinfo(curl, CURLINFO_FILETIME, &(this_link->time));
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
if (cl <= 0) {
this_link->type = LINK_INVALID;
} else {
@ -774,7 +786,11 @@ TransferStruct Link_download_full(Link * link)
long http_resp = 0;
do {
transfer_blocking(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp);
CURLcode ret =
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
if (HTTP_temp_failure(http_resp)) {
lprintf(warning,
"URL: %s, HTTP %ld, retrying later.\n",
@ -790,14 +806,17 @@ TransferStruct Link_download_full(Link * link)
}
while (HTTP_temp_failure(http_resp));
curl_easy_getinfo(curl, CURLINFO_FILETIME, &(link->time));
CURLcode ret =
curl_easy_getinfo(curl, CURLINFO_FILETIME, &(link->time));
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
curl_easy_cleanup(curl);
return ts;
}
long
Link_download(Link *link, char *output_buf, size_t req_size,
off_t offset)
Link_download(Link * link, char *output_buf, size_t req_size, off_t offset)
{
if (!link) {
lprintf(fatal, "Invalid supplied\n");
@ -840,7 +859,11 @@ range requests\n");
FREE(header.data);
long http_resp;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp);
CURLcode ret =
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
if (!((http_resp != HTTP_OK) ||
(http_resp != HTTP_PARTIAL_CONTENT) ||
(http_resp != HTTP_RANGE_NOT_SATISFIABLE))) {
@ -851,7 +874,10 @@ range requests\n");
}
curl_off_t recv;
curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD_T, &recv);
ret = curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD_T, &recv);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
/* The extra 1 byte is probably for '\0' */
if (recv - 1 == (long int) req_size) {

View File

@ -119,7 +119,8 @@ long path_download(const char *path, char *output_buf, size_t size,
* \brief Download a Link
* \return the number of bytes downloaded
*/
long Link_download(Link *link, char *output_buf, size_t req_size, off_t offset);
long Link_download(Link * link, char *output_buf, size_t req_size,
off_t offset);
/**
* \brief find the link associated with a path

View File

@ -120,17 +120,27 @@ curl_process_msgs(CURLMsg * curl_msg, int n_running_curl, int n_mesgs)
if (curl_msg->msg == CURLMSG_DONE) {
TransferStruct *ts;
CURL *curl = curl_msg->easy_handle;
curl_easy_getinfo(curl_msg->easy_handle, CURLINFO_PRIVATE,
&ts);
CURLcode ret =
curl_easy_getinfo(curl_msg->easy_handle, CURLINFO_PRIVATE,
&ts);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
ts->transferring = 0;
char *url = NULL;
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
ret = curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
/*
* Wait for 5 seconds if we get HTTP 429
*/
long http_resp = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp);
ret = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
if (HTTP_temp_failure(http_resp)) {
if (!slept) {
lprintf(warning,
@ -306,10 +316,13 @@ void NetworkSystem_init(void)
crypto_lock_init();
}
void transfer_blocking(CURL *curl)
void transfer_blocking(CURL * curl)
{
TransferStruct *ts;
curl_easy_getinfo(curl, CURLINFO_PRIVATE, &ts);
CURLcode ret = curl_easy_getinfo(curl, CURLINFO_PRIVATE, &ts);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
lprintf(network_lock_debug,
"thread %x: locking transfer_lock;\n", pthread_self());
@ -334,7 +347,7 @@ void transfer_blocking(CURL *curl)
// }
void transfer_nonblocking(CURL *curl)
void transfer_nonblocking(CURL * curl)
{
lprintf(network_lock_debug,
"thread %x: locking transfer_lock;\n", pthread_self());

View File

@ -58,8 +58,7 @@ void PTHREAD_MUTEX_UNLOCK(pthread_mutex_t * x)
i = pthread_mutex_unlock(x);
if (i) {
lprintf(fatal,
"thread %x: %d, %s\n",
pthread_self(), i, strerror(i));
"thread %x: %d, %s\n", pthread_self(), i, strerror(i));
}
}
@ -69,8 +68,7 @@ void PTHREAD_MUTEX_LOCK(pthread_mutex_t * x)
i = pthread_mutex_lock(x);
if (i) {
lprintf(fatal,
"thread %x: %d, %s\n",
pthread_self(), i, strerror(i));
"thread %x: %d, %s\n", pthread_self(), i, strerror(i));
}
}