1---
2c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
3SPDX-License-Identifier: curl
4Title: CURLOPT_READFUNCTION
5Section: 3
6Source: libcurl
7See-also:
8  - CURLOPT_POST (3)
9  - CURLOPT_READDATA (3)
10  - CURLOPT_SEEKFUNCTION (3)
11  - CURLOPT_UPLOAD (3)
12  - CURLOPT_UPLOAD_BUFFERSIZE (3)
13  - CURLOPT_WRITEFUNCTION (3)
14Protocol:
15  - All
16Added-in: 7.1
17---
18
19# NAME
20
21CURLOPT_READFUNCTION - read callback for data uploads
22
23# SYNOPSIS
24
25~~~c
26#include <curl/curl.h>
27
28size_t read_callback(char *buffer, size_t size, size_t nitems, void *userdata);
29
30CURLcode curl_easy_setopt(CURL *handle, CURLOPT_READFUNCTION, read_callback);
31~~~
32
33# DESCRIPTION
34
35Pass a pointer to your callback function, as the prototype shows above.
36
37This callback function gets called by libcurl as soon as it needs to read data
38in order to send it to the peer - like if you ask it to upload or post data to
39the server. The data area pointed at by the pointer *buffer* should be
40filled up with at most *size* multiplied with *nitems* number of bytes
41by your function. *size* is always 1.
42
43Set the *userdata* argument with the CURLOPT_READDATA(3) option.
44
45Your function must return the actual number of bytes that it stored in the
46data area pointed at by the pointer *buffer*. Returning 0 signals
47end-of-file to the library and causes it to stop the current transfer.
48
49If you stop the current transfer by returning 0 "pre-maturely" (i.e before the
50server expected it, like when you have said you would upload N bytes and you
51upload less than N bytes), you may experience that the server "hangs" waiting
52for the rest of the data that is not sent.
53
54The read callback may return *CURL_READFUNC_ABORT* to stop the current
55operation immediately, resulting in a *CURLE_ABORTED_BY_CALLBACK* error
56code from the transfer.
57
58The callback can return *CURL_READFUNC_PAUSE* to cause reading from this
59connection to pause. See curl_easy_pause(3) for further details.
60
61**Bugs**: when doing TFTP uploads, you must return the exact amount of data
62that the callback wants, or it is considered the final packet by the server
63end and the transfer ends there.
64
65If you set this callback pointer to NULL, or do not set it at all, the default
66internal read function is used. It is doing an fread() on the FILE * userdata
67set with CURLOPT_READDATA(3).
68
69You can set the total size of the data you are sending by using
70CURLOPT_INFILESIZE_LARGE(3) or CURLOPT_POSTFIELDSIZE_LARGE(3),
71depending on the type of transfer. For some transfer types it may be required
72and it allows for better error checking.
73
74# DEFAULT
75
76fread(3)
77
78# %PROTOCOLS%
79
80# EXAMPLE
81
82~~~c
83size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
84{
85  FILE *readhere = (FILE *)userdata;
86  curl_off_t nread;
87
88  /* copy as much data as possible into the 'ptr' buffer, but no more than
89     'size' * 'nmemb' bytes. */
90  size_t retcode = fread(ptr, size, nmemb, readhere);
91
92  nread = (curl_off_t)retcode;
93
94  fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
95          " bytes from file\n", nread);
96  return retcode;
97}
98
99int main(int argc, char **argv)
100{
101  FILE *file = fopen(argv[1], "rb");
102  CURLcode result;
103
104  CURL *curl = curl_easy_init();
105  if(curl) {
106    /* set callback to use */
107    curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
108
109    /* pass in suitable argument to callback */
110    curl_easy_setopt(curl, CURLOPT_READDATA, (void *)file);
111
112    result = curl_easy_perform(curl);
113  }
114}
115~~~
116
117# HISTORY
118
119CURL_READFUNC_PAUSE return code was added in 7.18.0 and CURL_READFUNC_ABORT
120was added in 7.12.1.
121
122# %AVAILABILITY%
123
124# RETURN VALUE
125
126This returns CURLE_OK.
127