2024-01-17 18:32:44 +08:00
|
|
|
---
|
2024-02-28 18:28:10 +08:00
|
|
|
c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2024-01-17 18:32:44 +08:00
|
|
|
SPDX-License-Identifier: curl
|
|
|
|
Title: curl_mime_data_cb
|
|
|
|
Section: 3
|
|
|
|
Source: libcurl
|
|
|
|
See-also:
|
|
|
|
- curl_easy_duphandle (3)
|
|
|
|
- curl_mime_addpart (3)
|
|
|
|
- curl_mime_data (3)
|
|
|
|
- curl_mime_name (3)
|
|
|
|
---
|
|
|
|
|
|
|
|
# NAME
|
|
|
|
|
2017-09-03 00:47:10 +08:00
|
|
|
curl_mime_data_cb - set a callback-based data source for a mime part's body
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
# SYNOPSIS
|
|
|
|
|
|
|
|
~~~c
|
2021-11-26 21:20:18 +08:00
|
|
|
#include <curl/curl.h>
|
|
|
|
|
2017-09-03 00:47:10 +08:00
|
|
|
size_t readfunc(char *buffer, size_t size, size_t nitems, void *arg);
|
2021-11-26 21:20:18 +08:00
|
|
|
|
2017-09-03 00:47:10 +08:00
|
|
|
int seekfunc(void *arg, curl_off_t offset, int origin);
|
2021-11-26 21:20:18 +08:00
|
|
|
|
2017-09-03 00:47:10 +08:00
|
|
|
void freefunc(void *arg);
|
2021-11-26 21:20:18 +08:00
|
|
|
|
|
|
|
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);
|
2024-01-17 18:32:44 +08:00
|
|
|
~~~
|
|
|
|
|
|
|
|
# DESCRIPTION
|
|
|
|
|
|
|
|
curl_mime_data_cb(3) sets the data source of a mime part's body content
|
2017-09-04 15:20:24 +08:00
|
|
|
from a data read callback function.
|
2017-09-03 00:47:10 +08:00
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
*part* is the part's to assign contents to.
|
2017-09-04 15:20:24 +08:00
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
*readfunc* is a pointer to a data read callback function, with a signature
|
2017-09-04 15:20:24 +08:00
|
|
|
as shown by the above prototype. It may not be set to NULL.
|
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
*seekfunc* is a pointer to a seek callback function, with a signature as
|
2023-08-22 23:40:39 +08:00
|
|
|
shown by the above prototype. This function is used when resending data (i.e.:
|
|
|
|
after a redirect); this pointer may be set to NULL, in which case a resend
|
|
|
|
might not be not possible.
|
2017-09-04 15:20:24 +08:00
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
*freefunc* is a pointer to a user resource freeing callback function, with
|
2017-09-04 15:20:24 +08:00
|
|
|
a signature as shown by the above prototype. If no resource is to be freed, it
|
2023-08-22 23:40:39 +08:00
|
|
|
may safely be set to NULL. This function is called upon mime structure
|
2017-09-04 15:20:24 +08:00
|
|
|
freeing.
|
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
*arg* is a user defined argument to callback functions.
|
2017-09-03 00:47:10 +08:00
|
|
|
|
|
|
|
The read callback function gets called by libcurl as soon as it needs to
|
|
|
|
read data in order to send it to the peer - like if you ask it to upload or
|
2024-01-17 18:32:44 +08:00
|
|
|
post data to the server. The data area pointed at by the pointer *buffer*
|
|
|
|
should be filled up with at most *size* multiplied with *nitems* number
|
2017-09-03 00:47:10 +08:00
|
|
|
of bytes by your function.
|
|
|
|
|
|
|
|
Your read function must then return the actual number of bytes that it stored
|
2023-08-22 23:40:39 +08:00
|
|
|
in that memory area. Returning 0 signals end-of-file to the library and cause
|
|
|
|
it to stop the current transfer.
|
2017-09-03 00:47:10 +08:00
|
|
|
|
2023-08-22 23:40:39 +08:00
|
|
|
If you stop the current transfer by returning 0 "pre-maturely" (i.e. before
|
|
|
|
the server expected it, like when you have said you intend to upload N bytes
|
|
|
|
and yet you upload less than N bytes), you may experience that the server
|
|
|
|
"hangs" waiting for the rest of the data that does not come.
|
2017-09-03 00:47:10 +08:00
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
The read callback may return *CURL_READFUNC_ABORT* to stop the current
|
|
|
|
operation immediately, resulting in a *CURLE_ABORTED_BY_CALLBACK* error
|
2017-09-03 00:47:10 +08:00
|
|
|
code from the transfer.
|
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
The callback can return *CURL_READFUNC_PAUSE* to cause reading from this
|
|
|
|
connection to pause. See curl_easy_pause(3) for further details.
|
2017-09-03 00:47:10 +08:00
|
|
|
|
|
|
|
The seek function gets called by libcurl to rewind input stream data or to
|
|
|
|
seek to a certain position. The function shall work like fseek(3) or lseek(3)
|
2024-01-17 18:32:44 +08:00
|
|
|
and it gets SEEK_SET, SEEK_CUR or SEEK_END as argument for *origin*,
|
2017-09-03 00:47:10 +08:00
|
|
|
although libcurl currently only passes SEEK_SET.
|
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
The callback function must return *CURL_SEEKFUNC_OK* on success,
|
|
|
|
*CURL_SEEKFUNC_FAIL* to cause the upload operation to fail or
|
|
|
|
*CURL_SEEKFUNC_CANTSEEK* to indicate that while the seek failed, libcurl
|
2017-09-03 00:47:10 +08:00
|
|
|
is free to work around the problem if possible. The latter can sometimes be
|
|
|
|
done by instead reading from the input or similar.
|
|
|
|
|
2018-01-15 02:43:12 +08:00
|
|
|
Care must be taken if the part is bound to a curl easy handle that is later
|
2024-01-17 18:32:44 +08:00
|
|
|
duplicated: the *arg* pointer argument is also duplicated, resulting in
|
2023-08-22 23:40:39 +08:00
|
|
|
the pointed item to be shared between the original and the copied handle. In
|
2024-01-17 18:32:44 +08:00
|
|
|
particular, special attention should be given to the *freefunc* procedure
|
2023-08-22 23:40:39 +08:00
|
|
|
code since it then gets called twice with the same argument.
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
# EXAMPLE
|
|
|
|
|
2023-08-22 23:40:39 +08:00
|
|
|
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.
|
2024-01-17 18:32:44 +08:00
|
|
|
~~~c
|
2023-12-04 17:50:42 +08:00
|
|
|
#include <string.h> /* for memcpy */
|
2017-09-03 00:47:10 +08:00
|
|
|
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;
|
|
|
|
}
|
2017-09-04 21:58:10 +08:00
|
|
|
|
2023-12-04 17:50:42 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2024-01-17 18:32:44 +08:00
|
|
|
~~~
|
|
|
|
|
|
|
|
# AVAILABILITY
|
|
|
|
|
2021-10-25 14:54:08 +08:00
|
|
|
As long as at least one of HTTP, SMTP or IMAP is enabled. Added in 7.56.0.
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
# RETURN VALUE
|
|
|
|
|
2021-10-25 14:54:08 +08:00
|
|
|
CURLE_OK or a CURL error code upon failure.
|