From d0b0baa3f2624b6de0ca92c051c154f0cff43f1a Mon Sep 17 00:00:00 2001 From: zhangxiaoyu Date: Tue, 14 Mar 2023 10:33:38 +0800 Subject: [PATCH 41/53] use CURLOPT_XFERINFOFUNCTION instead of deprecated CURLOPT_PROGRESSFUNCTION since curl 7.32.0 Signed-off-by: zhangxiaoyu --- .../modules/image/oci/registry/http_request.c | 12 +++++++++++ src/utils/http/http.c | 21 ++++++++++++++----- src/utils/http/http.h | 7 +++++++ test/image/oci/registry/registry_ut.cc | 8 +++++++ 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/daemon/modules/image/oci/registry/http_request.c b/src/daemon/modules/image/oci/registry/http_request.c index f29c2017..ce8b7667 100644 --- a/src/daemon/modules/image/oci/registry/http_request.c +++ b/src/daemon/modules/image/oci/registry/http_request.c @@ -691,6 +691,16 @@ static int progress(void *p, double dltotal, double dlnow, double ultotal, doubl return 0; } +static int xfer(void *p, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) +{ + bool *cancel = p; + if (*cancel) { + // return nonzero code means abort transition + return -1; + } + return 0; +} + int http_request_file(pull_descriptor *desc, const char *url, const char **custom_headers, char *file, resp_data_type type, CURLcode *errcode) { @@ -721,6 +731,8 @@ int http_request_file(pull_descriptor *desc, const char *url, const char **custo options->show_progress = 1; options->progressinfo = &desc->cancel; options->progress_info_op = progress; + options->xferinfo = &desc->cancel; + options->xferinfo_op = xfer; options->timeout = true; ret = setup_common_options(desc, options, url, custom_headers); diff --git a/src/utils/http/http.c b/src/utils/http/http.c index bf163d86..986f1f0d 100644 --- a/src/utils/http/http.c +++ b/src/utils/http/http.c @@ -219,12 +219,23 @@ static void http_custom_general_options(CURL *curl_handle, const struct http_get /* disable progress meter, set to 0L to enable and disable debug output */ if (options->show_progress == 0) { curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L); - } else if (options->show_progress && options->progressinfo && options->progress_info_op) { - curl_easy_setopt(curl_handle, CURLOPT_PROGRESSFUNCTION, options->progress_info_op); - /* pass the struct pointer into the progress function */ - curl_easy_setopt(curl_handle, CURLOPT_PROGRESSDATA, options->progressinfo); - curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 0L); } else { + /* libcurl support option CURLOPT_XFERINFOFUNCTION when version >= 7.32.0 + * #define CURL_VERSION_BITS(x,y,z) ((x)<<16|(y)<<8|(z)) + * CURL_VERSION_BITS(7,32,0) = 0x072000 */ +#if (LIBCURL_VERSION_NUM >= 0x072000) + if (options->xferinfo && options->xferinfo_op) { + curl_easy_setopt(curl_handle, CURLOPT_XFERINFOFUNCTION, options->xferinfo_op); + /* pass the struct pointer into the progress function */ + curl_easy_setopt(curl_handle, CURLOPT_XFERINFODATA, options->xferinfo); + } +#else + if (options->progressinfo && options->progress_info_op) { + curl_easy_setopt(curl_handle, CURLOPT_PROGRESSFUNCTION, options->progress_info_op); + /* pass the struct pointer into the progress function */ + curl_easy_setopt(curl_handle, CURLOPT_PROGRESSDATA, options->progressinfo); + } +#endif curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 0L); } diff --git a/src/utils/http/http.h b/src/utils/http/http.h index 343d92c3..cdd6d64f 100644 --- a/src/utils/http/http.h +++ b/src/utils/http/http.h @@ -17,6 +17,7 @@ #include #include +#include #ifdef __cplusplus extern "C" { @@ -25,6 +26,9 @@ extern "C" { typedef int(*progress_info_func)(void *p, double dltotal, double dlnow, double ultotal, double ulnow); +typedef int(*xferinfo_func)(void *p, + curl_off_t dltotal, curl_off_t dlnow, + curl_off_t ultotal, curl_off_t ulnow); struct http_get_options { unsigned with_head : 1, /* if set, means write output with response HEADER */ @@ -77,6 +81,9 @@ struct http_get_options { void *progressinfo; progress_info_func progress_info_op; + + void *xferinfo; + xferinfo_func xferinfo_op; }; #define HTTP_RES_OK 0 diff --git a/test/image/oci/registry/registry_ut.cc b/test/image/oci/registry/registry_ut.cc index 8d9ea92b..f4f8a763 100644 --- a/test/image/oci/registry/registry_ut.cc +++ b/test/image/oci/registry/registry_ut.cc @@ -221,6 +221,14 @@ int invokeHttpRequestV2(const char *url, struct http_get_options *options, long if (options->progress_info_op(options->progressinfo, 0, 0, 0, 0) != 0) { return -1; } + + cancel = (bool *)options->xferinfo; + while (!(*cancel)) { + sleep(1); // schedule out to let cancel variable set to be true + } + if (options->xferinfo_op(options->xferinfo, 0, 0, 0, 0) != 0) { + return -1; + } } } else if (util_has_prefix(url, "http://hub-mirror.c.163.com/v2/library/busybox/blobs/sha256:91f30d77")) { if (retry) { -- 2.25.1