如何在libcurl中发送长PUT数据而不使用文件指针? [英] How do I send long PUT data in libcurl without using file pointers?

查看:529
本文介绍了如何在libcurl中发送长PUT数据而不使用文件指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试与Google Contact API进行连接,为了更新远程联系人,我需要使用PUT请求,而不是POST。



由于我想要PUT的数据已经在内存中,我真的不想处理文件指针,这似乎是CURLOPT_READDATA的默认行为。因此,我明白我需要使用CURLOPT_READFUNCTION除了,但我不能理解的逻辑。



这里是相关的文档: http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTREADFUNCTION p>

打印传入的变量,我看到 nmemb = 16372 size = 1 。例如,如果我的数据大小为100K,我需要实现什么逻辑?



谢谢。


c> CURLOPT_READDATA ,但是如果你使用 CURLOPT_READFUNCTION / code>,它可以是任何用户指定的指针。您可以创建一个简单的结构,如:

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

其中 data 是PUT数据, code> len 是长度(剩余)。



然后,将 CURLOPT_READDATA 设置为指向该结构的初始化实例的指针。您将在 CURLOPT_READFUNCTION 中将其传递为 userdata 。在该函数中,执行以下操作:

  size_t curl_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;

这基本上计算出复制的数量,复制它,然后更新长度和指针。在 to_copy 行上,我们计算最小值,因为它们都受到剩余量和curl缓冲区的大小的限制。最后,我们返回复制的字节数,如curl所要求的。当你在内容结尾 user_data-> len (因此 to_copy )将为0.没有将被复制并返回0结束传输。


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.

解决方案

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.

这篇关于如何在libcurl中发送长PUT数据而不使用文件指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆