curl_mime_data_cb.3 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. .\" generated by cd2nroff 0.1 from curl_mime_data_cb.md
  2. .TH curl_mime_data_cb 3 "2025-01-17" libcurl
  3. .SH NAME
  4. curl_mime_data_cb \- set a callback\-based data source for a mime part\(aqs body
  5. .SH SYNOPSIS
  6. .nf
  7. #include <curl/curl.h>
  8. size_t readfunc(char *buffer, size_t size, size_t nitems, void *arg);
  9. int seekfunc(void *arg, curl_off_t offset, int origin);
  10. void freefunc(void *arg);
  11. CURLcode curl_mime_data_cb(curl_mimepart *part, curl_off_t datasize,
  12. curl_read_callback readfunc,
  13. curl_seek_callback seekfunc,
  14. curl_free_callback freefunc, void *arg);
  15. .fi
  16. .SH DESCRIPTION
  17. \fIcurl_mime_data_cb(3)\fP sets the data source of a mime part\(aqs body content
  18. from a data read callback function.
  19. \fIpart\fP is the part\(aqs to assign contents to.
  20. \fIreadfunc\fP is a pointer to a data read callback function, with a signature
  21. as shown by the above prototype. It may not be set to NULL.
  22. \fIseekfunc\fP is a pointer to a seek callback function, with a signature as
  23. shown by the above prototype. This function is used when resending data (i.e.:
  24. after a redirect); this pointer may be set to NULL, in which case a resend
  25. might not be not possible.
  26. \fIfreefunc\fP is a pointer to a user resource freeing callback function, with
  27. a signature as shown by the above prototype. If no resource is to be freed, it
  28. may safely be set to NULL. This function is called upon mime structure
  29. freeing.
  30. \fIarg\fP is a user defined argument to callback functions.
  31. The read callback function gets called by libcurl as soon as it needs to
  32. read data in order to send it to the peer \- like if you ask it to upload or
  33. post data to the server. The data area pointed at by the pointer \fIbuffer\fP
  34. should be filled up with at most \fIsize\fP multiplied with \fInitems\fP number
  35. of bytes by your function.
  36. Your read function must then return the actual number of bytes that it stored
  37. in that memory area. Returning 0 signals end\-of\-file to the library and cause
  38. it to stop the current transfer.
  39. If you stop the current transfer by returning 0 "pre\-maturely" (i.e. before
  40. the server expected it, like when you have said you intend to upload N bytes
  41. and yet you upload less than N bytes), you may experience that the server
  42. \&"hangs" waiting for the rest of the data that does not come.
  43. The read callback may return \fICURL_READFUNC_ABORT\fP to stop the current
  44. operation immediately, resulting in a \fICURLE_ABORTED_BY_CALLBACK\fP error
  45. code from the transfer.
  46. The callback can return \fICURL_READFUNC_PAUSE\fP to cause reading from this
  47. connection to pause. See \fIcurl_easy_pause(3)\fP for further details.
  48. The seek function gets called by libcurl to rewind input stream data or to
  49. seek to a certain position. The function shall work like fseek(3) or lseek(3)
  50. and it gets SEEK_SET, SEEK_CUR or SEEK_END as argument for \fIorigin\fP,
  51. although libcurl currently only passes SEEK_SET.
  52. The callback function must return \fICURL_SEEKFUNC_OK\fP on success,
  53. \fICURL_SEEKFUNC_FAIL\fP to cause the upload operation to fail or
  54. \fICURL_SEEKFUNC_CANTSEEK\fP to indicate that while the seek failed, libcurl
  55. is free to work around the problem if possible. The latter can sometimes be
  56. done by instead reading from the input or similar.
  57. Care must be taken if the part is bound to a curl easy handle that is later
  58. duplicated: the \fIarg\fP pointer argument is also duplicated, resulting in
  59. the pointed item to be shared between the original and the copied handle. In
  60. particular, special attention should be given to the \fIfreefunc\fP procedure
  61. code since it then gets called twice with the same argument.
  62. .SH PROTOCOLS
  63. This functionality affects http, imap and smtp
  64. .SH EXAMPLE
  65. Sending a huge data string causes the same amount of memory to be allocated:
  66. to avoid overhead resources consumption, one might want to use a callback
  67. source to avoid data duplication. In this case, original data must be retained
  68. until after the transfer terminates.
  69. .nf
  70. #include <string.h> /* for memcpy */
  71. char hugedata[512000];
  72. struct ctl {
  73. char *buffer;
  74. curl_off_t size;
  75. curl_off_t position;
  76. };
  77. size_t read_callback(char *buffer, size_t size, size_t nitems, void *arg)
  78. {
  79. struct ctl *p = (struct ctl *) arg;
  80. curl_off_t sz = p->size - p->position;
  81. nitems *= size;
  82. if(sz > nitems)
  83. sz = nitems;
  84. if(sz)
  85. memcpy(buffer, p->buffer + p->position, sz);
  86. p->position += sz;
  87. return sz;
  88. }
  89. int seek_callback(void *arg, curl_off_t offset, int origin)
  90. {
  91. struct ctl *p = (struct ctl *) arg;
  92. switch(origin) {
  93. case SEEK_END:
  94. offset += p->size;
  95. break;
  96. case SEEK_CUR:
  97. offset += p->position;
  98. break;
  99. }
  100. if(offset < 0)
  101. return CURL_SEEKFUNC_FAIL;
  102. p->position = offset;
  103. return CURL_SEEKFUNC_OK;
  104. }
  105. int main(void)
  106. {
  107. CURL *curl = curl_easy_init();
  108. if(curl) {
  109. curl_mime *mime = curl_mime_init(curl);
  110. curl_mimepart *part = curl_mime_addpart(mime);
  111. struct ctl hugectl;
  112. hugectl.buffer = hugedata;
  113. hugectl.size = sizeof(hugedata);
  114. hugectl.position = 0;
  115. curl_mime_data_cb(part, hugectl.size, read_callback, seek_callback, NULL,
  116. &hugectl);
  117. }
  118. }
  119. .fi
  120. .SH AVAILABILITY
  121. Added in curl 7.56.0
  122. .SH RETURN VALUE
  123. CURLE_OK or a CURL error code upon failure.
  124. .SH SEE ALSO
  125. .BR curl_easy_duphandle (3),
  126. .BR curl_mime_addpart (3),
  127. .BR curl_mime_data (3),
  128. .BR curl_mime_name (3)