CURLOPT_READFUNCTION.3 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. .\" generated by cd2nroff 0.1 from CURLOPT_READFUNCTION.md
  2. .TH CURLOPT_READFUNCTION 3 "2025-01-17" libcurl
  3. .SH NAME
  4. CURLOPT_READFUNCTION \- read callback for data uploads
  5. .SH SYNOPSIS
  6. .nf
  7. #include <curl/curl.h>
  8. size_t read_callback(char *buffer, size_t size, size_t nitems, void *userdata);
  9. CURLcode curl_easy_setopt(CURL *handle, CURLOPT_READFUNCTION, read_callback);
  10. .fi
  11. .SH DESCRIPTION
  12. Pass a pointer to your callback function, as the prototype shows above.
  13. This callback function gets called by libcurl as soon as it needs to read data
  14. in order to send it to the peer \- like if you ask it to upload or post data to
  15. the server. The data area pointed at by the pointer \fIbuffer\fP should be
  16. filled up with at most \fIsize\fP multiplied with \fInitems\fP number of bytes
  17. by your function. \fIsize\fP is always 1.
  18. Set the \fIuserdata\fP argument with the \fICURLOPT_READDATA(3)\fP option.
  19. Your function must return the actual number of bytes that it stored in the
  20. data area pointed at by the pointer \fIbuffer\fP. Returning 0 signals
  21. end\-of\-file to the library and causes it to stop the current transfer.
  22. If you stop the current transfer by returning 0 "pre\-maturely" (i.e before the
  23. server expected it, like when you have said you would upload N bytes and you
  24. upload less than N bytes), you may experience that the server "hangs" waiting
  25. for the rest of the data that is not sent.
  26. The read callback may return \fICURL_READFUNC_ABORT\fP to stop the current
  27. operation immediately, resulting in a \fICURLE_ABORTED_BY_CALLBACK\fP error
  28. code from the transfer.
  29. The callback can return \fICURL_READFUNC_PAUSE\fP to cause reading from this
  30. connection to pause. See \fIcurl_easy_pause(3)\fP for further details.
  31. \fBBugs\fP: when doing TFTP uploads, you must return the exact amount of data
  32. that the callback wants, or it is considered the final packet by the server
  33. end and the transfer ends there.
  34. If you set this callback pointer to NULL, or do not set it at all, the default
  35. internal read function is used. It is doing an fread() on the FILE * userdata
  36. set with \fICURLOPT_READDATA(3)\fP.
  37. You can set the total size of the data you are sending by using
  38. \fICURLOPT_INFILESIZE_LARGE(3)\fP or \fICURLOPT_POSTFIELDSIZE_LARGE(3)\fP,
  39. depending on the type of transfer. For some transfer types it may be required
  40. and it allows for better error checking.
  41. .SH DEFAULT
  42. fread(3)
  43. .SH PROTOCOLS
  44. This functionality affects all supported protocols
  45. .SH EXAMPLE
  46. .nf
  47. size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
  48. {
  49. FILE *readhere = (FILE *)userdata;
  50. curl_off_t nread;
  51. /* copy as much data as possible into the 'ptr' buffer, but no more than
  52. 'size' * 'nmemb' bytes! */
  53. size_t retcode = fread(ptr, size, nmemb, readhere);
  54. nread = (curl_off_t)retcode;
  55. fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
  56. " bytes from file\\n", nread);
  57. return retcode;
  58. }
  59. int main(int argc, char **argv)
  60. {
  61. FILE *file = fopen(argv[1], "rb");
  62. CURLcode result;
  63. CURL *curl = curl_easy_init();
  64. if(curl) {
  65. /* set callback to use */
  66. curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  67. /* pass in suitable argument to callback */
  68. curl_easy_setopt(curl, CURLOPT_READDATA, (void *)file);
  69. result = curl_easy_perform(curl);
  70. }
  71. }
  72. .fi
  73. .SH HISTORY
  74. CURL_READFUNC_PAUSE return code was added in 7.18.0 and CURL_READFUNC_ABORT
  75. was added in 7.12.1.
  76. .SH AVAILABILITY
  77. Added in curl 7.1
  78. .SH RETURN VALUE
  79. This returns CURLE_OK.
  80. .SH SEE ALSO
  81. .BR CURLOPT_POST (3),
  82. .BR CURLOPT_READDATA (3),
  83. .BR CURLOPT_SEEKFUNCTION (3),
  84. .BR CURLOPT_UPLOAD (3),
  85. .BR CURLOPT_UPLOAD_BUFFERSIZE (3),
  86. .BR CURLOPT_WRITEFUNCTION (3)