curl_mime_data_cb.3 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. .\" **************************************************************************
  2. .\" * _ _ ____ _
  3. .\" * Project ___| | | | _ \| |
  4. .\" * / __| | | | |_) | |
  5. .\" * | (__| |_| | _ <| |___
  6. .\" * \___|\___/|_| \_\_____|
  7. .\" *
  8. .\" * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. .\" *
  10. .\" * This software is licensed as described in the file COPYING, which
  11. .\" * you should have received as part of this distribution. The terms
  12. .\" * are also available at https://curl.se/docs/copyright.html.
  13. .\" *
  14. .\" * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. .\" * copies of the Software, and permit persons to whom the Software is
  16. .\" * furnished to do so, under the terms of the COPYING file.
  17. .\" *
  18. .\" * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. .\" * KIND, either express or implied.
  20. .\" *
  21. .\" * SPDX-License-Identifier: curl
  22. .\" *
  23. .\" **************************************************************************
  24. .TH curl_mime_data_cb 3 "22 August 2017" "libcurl" "libcurl"
  25. .SH NAME
  26. curl_mime_data_cb - set a callback-based data source for a mime part's body
  27. .SH SYNOPSIS
  28. .nf
  29. #include <curl/curl.h>
  30. size_t readfunc(char *buffer, size_t size, size_t nitems, void *arg);
  31. int seekfunc(void *arg, curl_off_t offset, int origin);
  32. void freefunc(void *arg);
  33. CURLcode curl_mime_data_cb(curl_mimepart *part, curl_off_t datasize,
  34. curl_read_callback readfunc,
  35. curl_seek_callback seekfunc,
  36. curl_free_callback freefunc, void *arg);
  37. .fi
  38. .SH DESCRIPTION
  39. \fIcurl_mime_data_cb(3)\fP sets the data source of a mime part's body content
  40. from a data read callback function.
  41. \fIpart\fP is the part's to assign contents to.
  42. \fIreadfunc\fP is a pointer to a data read callback function, with a signature
  43. as shown by the above prototype. It may not be set to NULL.
  44. \fIseekfunc\fP is a pointer to a seek callback function, with a signature as
  45. shown by the above prototype. This function will be used upon resending data
  46. (i.e.: after a redirect); this pointer may be set to NULL, in which case a
  47. resend is not possible.
  48. \fIfreefunc\fP is a pointer to a user resource freeing callback function, with
  49. a signature as shown by the above prototype. If no resource is to be freed, it
  50. may safely be set to NULL. This function will be called upon mime structure
  51. freeing.
  52. \fIarg\fP is a user defined argument to callback functions.
  53. The read callback function gets called by libcurl as soon as it needs to
  54. read data in order to send it to the peer - like if you ask it to upload or
  55. post data to the server. The data area pointed at by the pointer \fIbuffer\fP
  56. should be filled up with at most \fIsize\fP multiplied with \fInitems\fP number
  57. of bytes by your function.
  58. Your read function must then return the actual number of bytes that it stored
  59. in that memory area. Returning 0 will signal end-of-file to the library and
  60. cause it to stop the current transfer.
  61. If you stop the current transfer by returning 0 "pre-maturely" (i.e. before the
  62. server expected it, like when you have said you will upload N bytes and you
  63. upload less than N bytes), you may experience that the server "hangs" waiting
  64. for the rest of the data that will not come.
  65. The read callback may return \fICURL_READFUNC_ABORT\fP to stop the current
  66. operation immediately, resulting in a \fICURLE_ABORTED_BY_CALLBACK\fP error
  67. code from the transfer.
  68. The callback can return \fICURL_READFUNC_PAUSE\fP to cause reading from this
  69. connection to pause. See \fIcurl_easy_pause(3)\fP for further details.
  70. The seek function gets called by libcurl to rewind input stream data or to
  71. seek to a certain position. The function shall work like fseek(3) or lseek(3)
  72. and it gets SEEK_SET, SEEK_CUR or SEEK_END as argument for \fIorigin\fP,
  73. although libcurl currently only passes SEEK_SET.
  74. The callback function must return \fICURL_SEEKFUNC_OK\fP on success,
  75. \fICURL_SEEKFUNC_FAIL\fP to cause the upload operation to fail or
  76. \fICURL_SEEKFUNC_CANTSEEK\fP to indicate that while the seek failed, libcurl
  77. is free to work around the problem if possible. The latter can sometimes be
  78. done by instead reading from the input or similar.
  79. Care must be taken if the part is bound to a curl easy handle that is later
  80. duplicated: the \fIarg\fP pointer argument is also duplicated, resulting in
  81. the pointed item to be shared between the original and the copied handle.
  82. In particular, special attention should be given to the \fIfreefunc\fP
  83. procedure code since it will be called twice with the same argument.
  84. .SH EXAMPLE
  85. Sending a huge data string will cause the same amount of memory to be
  86. allocated: to avoid overhead resources consumption, one might want to use a
  87. callback source to avoid data duplication. In this case, original data
  88. must be retained until after the transfer terminates.
  89. .nf
  90. char hugedata[512000];
  91. struct ctl {
  92. char *buffer;
  93. curl_off_t size;
  94. curl_off_t position;
  95. };
  96. size_t read_callback(char *buffer, size_t size, size_t nitems, void *arg)
  97. {
  98. struct ctl *p = (struct ctl *) arg;
  99. curl_off_t sz = p->size - p->position;
  100. nitems *= size;
  101. if(sz > nitems)
  102. sz = nitems;
  103. if(sz)
  104. memcpy(buffer, p->buffer + p->position, sz);
  105. p->position += sz;
  106. return sz;
  107. }
  108. int seek_callback(void *arg, curl_off_t offset, int origin)
  109. {
  110. struct ctl *p = (struct ctl *) arg;
  111. switch(origin) {
  112. case SEEK_END:
  113. offset += p->size;
  114. break;
  115. case SEEK_CUR:
  116. offset += p->position;
  117. break;
  118. }
  119. if(offset < 0)
  120. return CURL_SEEKFUNC_FAIL;
  121. p->position = offset;
  122. return CURL_SEEKFUNC_OK;
  123. }
  124. CURL *easy = curl_easy_init();
  125. curl_mime *mime = curl_mime_init(easy);
  126. curl_mimepart *part = curl_mime_addpart(mime);
  127. struct ctl hugectl;
  128. hugectl.buffer = hugedata;
  129. hugectl.size = sizeof hugedata;
  130. hugectl.position = 0;
  131. curl_mime_data_cb(part, hugectl.size, read_callback, seek_callback, NULL,
  132. &hugectl);
  133. .SH AVAILABILITY
  134. As long as at least one of HTTP, SMTP or IMAP is enabled. Added in 7.56.0.
  135. .SH RETURN VALUE
  136. CURLE_OK or a CURL error code upon failure.
  137. .SH "SEE ALSO"
  138. .BR curl_mime_addpart "(3),"
  139. .BR curl_mime_data "(3),"
  140. .BR curl_mime_name "(3),"
  141. .BR curl_easy_duphandle "(3)"