libcurl (c api) READFUNCTION for http PUT 永远阻塞
Posted
技术标签:
【中文标题】libcurl (c api) READFUNCTION for http PUT 永远阻塞【英文标题】:libcurl (c api) READFUNCTION for http PUT blocking forever 【发布时间】:2011-03-01 21:38:35 【问题描述】:我将 libcurl 用于 RESTful 库。我在使用 PUT 消息时遇到了两个问题,我只是想通过 put 发送一个小内容,例如“hello”。
当我按照 curl.haxx.se 上的手册操作并返回 0 表示我已完成内容时,我的 PUT 块的 READFUNCTION 会持续很长时间(分钟)。 (在 os X 上)当我返回大于 0 的东西时,它的成功速度要快得多(
当我在我的 linux 机器 (ubuntu 10.4) 上运行它时,如果我更改行为以返回写入的大小,则此阻塞事件似乎永远不会返回方式更多的数据,它失败,来自服务器的“太多数据”消息。我的 readfunction 如下,任何帮助将不胜感激。 我正在使用 libcurl 7.20.1
typedef struct
void *data;
int body_size;
int bytes_remaining;
int bytes_written;
postdata;
size_t readfunc(void *ptr, size_t size, size_t nmemb, void *stream)
if(stream)
postdata *ud = (postdata*)stream;
if(ud->bytes_remaining)
if(ud->body_size > size*nmemb)
memcpy(ptr, ud->data+ud->bytes_written, size*nmemb);
ud->bytes_written+=size+nmemb;
ud->bytes_remaining = ud->body_size-size*nmemb;
return size*nmemb;
else
memcpy(ptr, ud->data+ud->bytes_written, ud->bytes_remaining);
ud->bytes_remaining=0;
return 0;
【问题讨论】:
【参考方案1】:typedef struct
void *data;
int body_size;
int bytes_remaining;
int bytes_written;
postdata;
size_t readfunc(void *ptr, size_t size, size_t nmemb, void *stream)
if(stream)
postdata *ud = (postdata*)stream;
if(ud->bytes_remaining)
if(ud->bytes_remaining > size*nmemb)
memcpy(ptr, ud->data+ud->bytes_written, size*nmemb);
ud->bytes_written+=size+nmemb;
ud->bytes_remaining -= size*nmemb;
return size*nmemb;
else
memcpy(ptr, ud->data+ud->bytes_written, ud->bytes_remaining);
ud->bytes_remaining=0;
return 0;
我觉得比较合理
【讨论】:
您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。【参考方案2】:来自手册页(man curl_easy_setopt):
CURLOPT_READFUNCTION
Function pointer that should match the following prototype: size_t function( void *ptr, size_t size, size_t nmemb, void *stream); This function gets called by libcurl as soon as
it needs to read data in order to send it to the peer. The data area pointed at by the pointer ptr may be filled with at most size multiplied with nmemb number of bytes. Your
function must return the actual number of bytes that you stored in that memory area. Returning 0 will signal end-of-file to the library and cause it to stop the current trans-
fer.
If you stop the current transfer by returning 0 "pre-maturely" (i.e before the server expected it, like when you've told you will upload N bytes and you upload less than N
bytes), you may experience that the server "hangs" waiting for the rest of the data that won't come.
The read callback may return CURL_READFUNC_ABORT to stop the current operation immediately, resulting in a CURLE_ABORTED_BY_CALLBACK error code from the transfer (Added in
7.12.1)
If you set the callback pointer to NULL, or doesn't set it at all, the default internal read function will be used. It is simply doing an fread() on the FILE * stream set with
CURLOPT_READDATA.
所以返回 CURL_READFUNC_ABORT 以停止操作
【讨论】:
以上是关于libcurl (c api) READFUNCTION for http PUT 永远阻塞的主要内容,如果未能解决你的问题,请参考以下文章
GuzzleHttp\Exception\RequestException cURL 错误 3:(参见 https://curl.haxx.se/libcurl/c/libcurl-errors.ht