xref: /aosp_15_r20/external/curl/docs/libcurl/opts/CURLOPT_XFERINFOFUNCTION.md (revision 6236dae45794135f37c4eb022389c904c8b0090d)
1*6236dae4SAndroid Build Coastguard Worker---
2*6236dae4SAndroid Build Coastguard Workerc: Copyright (C) Daniel Stenberg, <[email protected]>, et al.
3*6236dae4SAndroid Build Coastguard WorkerSPDX-License-Identifier: curl
4*6236dae4SAndroid Build Coastguard WorkerTitle: CURLOPT_XFERINFOFUNCTION
5*6236dae4SAndroid Build Coastguard WorkerSection: 3
6*6236dae4SAndroid Build Coastguard WorkerSource: libcurl
7*6236dae4SAndroid Build Coastguard WorkerSee-also:
8*6236dae4SAndroid Build Coastguard Worker  - CURLOPT_NOPROGRESS (3)
9*6236dae4SAndroid Build Coastguard Worker  - CURLOPT_XFERINFODATA (3)
10*6236dae4SAndroid Build Coastguard WorkerProtocol:
11*6236dae4SAndroid Build Coastguard Worker  - All
12*6236dae4SAndroid Build Coastguard WorkerAdded-in: 7.32.0
13*6236dae4SAndroid Build Coastguard Worker---
14*6236dae4SAndroid Build Coastguard Worker
15*6236dae4SAndroid Build Coastguard Worker# NAME
16*6236dae4SAndroid Build Coastguard Worker
17*6236dae4SAndroid Build Coastguard WorkerCURLOPT_XFERINFOFUNCTION - progress meter callback
18*6236dae4SAndroid Build Coastguard Worker
19*6236dae4SAndroid Build Coastguard Worker# SYNOPSIS
20*6236dae4SAndroid Build Coastguard Worker
21*6236dae4SAndroid Build Coastguard Worker~~~c
22*6236dae4SAndroid Build Coastguard Worker#include <curl/curl.h>
23*6236dae4SAndroid Build Coastguard Worker
24*6236dae4SAndroid Build Coastguard Workerint progress_callback(void *clientp,
25*6236dae4SAndroid Build Coastguard Worker                      curl_off_t dltotal,
26*6236dae4SAndroid Build Coastguard Worker                      curl_off_t dlnow,
27*6236dae4SAndroid Build Coastguard Worker                      curl_off_t ultotal,
28*6236dae4SAndroid Build Coastguard Worker                      curl_off_t ulnow);
29*6236dae4SAndroid Build Coastguard Worker
30*6236dae4SAndroid Build Coastguard WorkerCURLcode curl_easy_setopt(CURL *handle, CURLOPT_XFERINFOFUNCTION,
31*6236dae4SAndroid Build Coastguard Worker                          progress_callback);
32*6236dae4SAndroid Build Coastguard Worker~~~
33*6236dae4SAndroid Build Coastguard Worker
34*6236dae4SAndroid Build Coastguard Worker# DESCRIPTION
35*6236dae4SAndroid Build Coastguard Worker
36*6236dae4SAndroid Build Coastguard WorkerPass a pointer to your callback function, which should match the prototype
37*6236dae4SAndroid Build Coastguard Workershown above.
38*6236dae4SAndroid Build Coastguard Worker
39*6236dae4SAndroid Build Coastguard WorkerThis function gets called by libcurl instead of its internal equivalent with a
40*6236dae4SAndroid Build Coastguard Workerfrequent interval. While data is being transferred it gets called frequently,
41*6236dae4SAndroid Build Coastguard Workerand during slow periods like when nothing is being transferred it can slow
42*6236dae4SAndroid Build Coastguard Workerdown to about one call per second.
43*6236dae4SAndroid Build Coastguard Worker
44*6236dae4SAndroid Build Coastguard Worker*clientp* is the pointer set with CURLOPT_XFERINFODATA(3), it is not
45*6236dae4SAndroid Build Coastguard Workerused by libcurl but is only passed along from the application to the callback.
46*6236dae4SAndroid Build Coastguard Worker
47*6236dae4SAndroid Build Coastguard WorkerThe callback gets told how much data libcurl is about to transfer and has
48*6236dae4SAndroid Build Coastguard Workeralready transferred, in number of bytes. *dltotal* is the total number of
49*6236dae4SAndroid Build Coastguard Workerbytes libcurl expects to download in this transfer. *dlnow* is the number
50*6236dae4SAndroid Build Coastguard Workerof bytes downloaded so far. *ultotal* is the total number of bytes libcurl
51*6236dae4SAndroid Build Coastguard Workerexpects to upload in this transfer. *ulnow* is the number of bytes
52*6236dae4SAndroid Build Coastguard Workeruploaded so far.
53*6236dae4SAndroid Build Coastguard Worker
54*6236dae4SAndroid Build Coastguard WorkerUnknown/unused argument values passed to the callback are set to zero (like if
55*6236dae4SAndroid Build Coastguard Workeryou only download data, the upload size remains 0). Many times the callback is
56*6236dae4SAndroid Build Coastguard Workercalled one or more times first, before it knows the data sizes so a program
57*6236dae4SAndroid Build Coastguard Workermust be made to handle that.
58*6236dae4SAndroid Build Coastguard Worker
59*6236dae4SAndroid Build Coastguard WorkerReturn zero from the callback if everything is fine.
60*6236dae4SAndroid Build Coastguard Worker
61*6236dae4SAndroid Build Coastguard WorkerReturn 1 from this callback to make libcurl abort the transfer and return
62*6236dae4SAndroid Build Coastguard Worker*CURLE_ABORTED_BY_CALLBACK*.
63*6236dae4SAndroid Build Coastguard Worker
64*6236dae4SAndroid Build Coastguard WorkerIf your callback function returns CURL_PROGRESSFUNC_CONTINUE it makes libcurl
65*6236dae4SAndroid Build Coastguard Workerto continue executing the default progress function.
66*6236dae4SAndroid Build Coastguard Worker
67*6236dae4SAndroid Build Coastguard WorkerIf you transfer data with the multi interface, this function is not called
68*6236dae4SAndroid Build Coastguard Workerduring periods of idleness unless you call the appropriate libcurl function
69*6236dae4SAndroid Build Coastguard Workerthat performs transfers.
70*6236dae4SAndroid Build Coastguard Worker
71*6236dae4SAndroid Build Coastguard WorkerCURLOPT_NOPROGRESS(3) must be set to 0 to make this function actually
72*6236dae4SAndroid Build Coastguard Workerget called.
73*6236dae4SAndroid Build Coastguard Worker
74*6236dae4SAndroid Build Coastguard Worker# DEFAULT
75*6236dae4SAndroid Build Coastguard Worker
76*6236dae4SAndroid Build Coastguard WorkerNULL - use the internal progress meter. That is rarely wanted by users.
77*6236dae4SAndroid Build Coastguard Worker
78*6236dae4SAndroid Build Coastguard Worker# %PROTOCOLS%
79*6236dae4SAndroid Build Coastguard Worker
80*6236dae4SAndroid Build Coastguard Worker# EXAMPLE
81*6236dae4SAndroid Build Coastguard Worker
82*6236dae4SAndroid Build Coastguard Worker~~~c
83*6236dae4SAndroid Build Coastguard Workerstruct progress {
84*6236dae4SAndroid Build Coastguard Worker  char *private;
85*6236dae4SAndroid Build Coastguard Worker  size_t size;
86*6236dae4SAndroid Build Coastguard Worker};
87*6236dae4SAndroid Build Coastguard Worker
88*6236dae4SAndroid Build Coastguard Workerstatic size_t progress_callback(void *clientp,
89*6236dae4SAndroid Build Coastguard Worker                                curl_off_t dltotal,
90*6236dae4SAndroid Build Coastguard Worker                                curl_off_t dlnow,
91*6236dae4SAndroid Build Coastguard Worker                                curl_off_t ultotal,
92*6236dae4SAndroid Build Coastguard Worker                                curl_off_t ulnow)
93*6236dae4SAndroid Build Coastguard Worker{
94*6236dae4SAndroid Build Coastguard Worker  struct progress *memory = clientp;
95*6236dae4SAndroid Build Coastguard Worker  printf("my ptr: %p\n", memory->private);
96*6236dae4SAndroid Build Coastguard Worker
97*6236dae4SAndroid Build Coastguard Worker  /* use the values */
98*6236dae4SAndroid Build Coastguard Worker
99*6236dae4SAndroid Build Coastguard Worker  return 0; /* all is good */
100*6236dae4SAndroid Build Coastguard Worker}
101*6236dae4SAndroid Build Coastguard Worker
102*6236dae4SAndroid Build Coastguard Workerint main(void)
103*6236dae4SAndroid Build Coastguard Worker{
104*6236dae4SAndroid Build Coastguard Worker  CURL *curl = curl_easy_init();
105*6236dae4SAndroid Build Coastguard Worker  if(curl) {
106*6236dae4SAndroid Build Coastguard Worker    struct progress data;
107*6236dae4SAndroid Build Coastguard Worker
108*6236dae4SAndroid Build Coastguard Worker    /* pass struct to callback  */
109*6236dae4SAndroid Build Coastguard Worker    curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &data);
110*6236dae4SAndroid Build Coastguard Worker
111*6236dae4SAndroid Build Coastguard Worker    /* enable progress callback getting called */
112*6236dae4SAndroid Build Coastguard Worker    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
113*6236dae4SAndroid Build Coastguard Worker
114*6236dae4SAndroid Build Coastguard Worker    curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progress_callback);
115*6236dae4SAndroid Build Coastguard Worker  }
116*6236dae4SAndroid Build Coastguard Worker}
117*6236dae4SAndroid Build Coastguard Worker~~~
118*6236dae4SAndroid Build Coastguard Worker
119*6236dae4SAndroid Build Coastguard Worker# %AVAILABILITY%
120*6236dae4SAndroid Build Coastguard Worker
121*6236dae4SAndroid Build Coastguard Worker# RETURN VALUE
122*6236dae4SAndroid Build Coastguard Worker
123*6236dae4SAndroid Build Coastguard WorkerReturns CURLE_OK.
124