summaryrefslogtreecommitdiff
path: root/0041-use-CURLOPT_XFERINFOFUNCTION-instead-of-deprecated-C.patch
blob: af12eeca35728e3e66da7568dd8650076b63f4e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
From d0b0baa3f2624b6de0ca92c051c154f0cff43f1a Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
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 <zhangxiaoyu58@huawei.com>
---
 .../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 <stdbool.h>
 #include <stddef.h>
+#include <curl/curl.h>
 
 #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