Sortix nightly manual
This manual documents Sortix nightly, a development build that has not been officially released. You can instead view this document in the latest official manual.
curl_mime_data_cb(3) | Library Functions Manual | curl_mime_data_cb(3) |
NAME
curl_mime_data_cb - set a callback-based data source for a mime part's bodySYNOPSIS
#include <curl/curl.h>
size_t readfunc(char *buffer, size_t size, size_t nitems, void *arg);
int seekfunc(void *arg, curl_off_t offset, int origin);
void freefunc(void *arg);
CURLcode curl_mime_data_cb(curl_mimepart *part, curl_off_t datasize,
curl_read_callback readfunc,
curl_seek_callback seekfunc,
curl_free_callback freefunc, void *arg);
DESCRIPTION
curl_mime_data_cb(3) sets the data source of a mime part's body content from a data read callback function.PROTOCOLS
This functionality affects http, imap and smtpEXAMPLE
Sending a huge data string causes the same amount of memory to be allocated: to avoid overhead resources consumption, one might want to use a callback source to avoid data duplication. In this case, original data must be retained until after the transfer terminates.#include <string.h> /* for memcpy */
char hugedata[512000];
struct ctl {
char *buffer;
curl_off_t size;
curl_off_t position;
};
size_t read_callback(char *buffer, size_t size, size_t nitems, void *arg)
{
struct ctl *p = (struct ctl *) arg;
curl_off_t sz = p->size - p->position;
nitems *= size;
if(sz > nitems)
sz = nitems;
if(sz)
memcpy(buffer, p->buffer + p->position, sz);
p->position += sz;
return sz;
}
int seek_callback(void *arg, curl_off_t offset, int origin)
{
struct ctl *p = (struct ctl *) arg;
switch(origin) {
case SEEK_END:
offset += p->size;
break;
case SEEK_CUR:
offset += p->position;
break;
}
if(offset < 0)
return CURL_SEEKFUNC_FAIL;
p->position = offset;
return CURL_SEEKFUNC_OK;
}
int main(void)
{
CURL *curl = curl_easy_init();
if(curl) {
curl_mime *mime = curl_mime_init(curl);
curl_mimepart *part = curl_mime_addpart(mime);
struct ctl hugectl;
hugectl.buffer = hugedata;
hugectl.size = sizeof(hugedata);
hugectl.position = 0;
curl_mime_data_cb(part, hugectl.size, read_callback, seek_callback, NULL,
&hugectl);
}
}
AVAILABILITY
Added in curl 7.56.0RETURN VALUE
CURLE_OK or a CURL error code upon failure.SEE ALSO
curl_easy_duphandle(3), curl_mime_addpart(3), curl_mime_data(3), curl_mime_name(3)2024-11-21 | libcurl |