Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
459 views
in Technique[技术] by (71.8m points)

c++ - How do I send long PUT data in libcurl without using file pointers?

I'm trying to interface with the Google Contact API, and in order to update a contact on the remote side, I need to use a PUT request instead of POST.

Since the data I want to PUT is already in memory, I really don't want to deal with file pointers, which seems to be the default behavior of CURLOPT_READDATA. Thus, I understand I need to use CURLOPT_READFUNCTION in addition to that but I'm failing to understand the logic.

Here is the relevant doc: http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTREADFUNCTION

Printing the variables passed in, I see nmemb=16372 and size=1. If my data is going to be of size 100K, for example, what is the logic that I need to implement?

Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You will still want to use CURLOPT_READDATA, however if you use CURLOPT_READFUNCTION, it can be any user-specified pointer. You can create a simple structure like:

struct put_data
{
  char *data;
  size_t len;
};

where data is the PUT data and len is the length (remaining).

Then, set CURLOPT_READDATA to a pointer to an initialized instance of that structure. You will be passed it in CURLOPT_READFUNCTION as userdata. In that function, do something like:

size_t curl_size = nmemb * size;
size_t to_copy = (userdata->len < curl_size) ? userdata->len : curl_size;
memcpy(ptr, userdata->data, to_copy);
userdata->len -= to_copy;
userdata->data += to_copy;
return to_copy;

This basically figures out the amount to copy, copies it, then updates the length and pointer. On the to_copy line, we calculate the minimum because were are bounded by both the amount remaining and the size of curl's buffer. Finally, we return the number of bytes copied, as required by curl. When you're at the end of content user_data->len (and thus to_copy) will be 0. Nothing will be copied and returning 0 ends the transfer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...