【发布时间】:2021-09-16 16:38:58
【问题描述】:
什么时候可以安全地重新使用CURLOPT_POSTFIELDS 缓冲区?
我有以下代码:
void handle_data(uint8_t *data, size_t dataLen)
{
static uint8_t s_buffer[500];
static size_t s_bufferIdx;
if (s_buffer + dataLen > sizeof(s_buffer))
{
CURL *handle = curl_easy_init();
curl_easy_setopt(handle, CURLOPT_URL, url);
curl_easy_setopt(handle, CURLOPT_POSTFIELDS, s_buffer);
curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE, s_bufferIdx);
curl_multi_add_handle(s_curlM, handle);
curl_multi_perform(s_curlMultiHandle, NULL);
s_bufferIdx = 0; // "Re-use" the buffer
}
memcpy(&s_buffer[s_bufferIdx], data, dataLen);
s_bufferIdx += dataLen;
// Check completed transfers and cleanup handles with `curl_multi_info_read` here
}
我猜这不安全,所以我需要为每个句柄分配一个缓冲区,但我看不到将 context pointer 附加到 CURL 句柄的方法,所以我需要维护句柄映射+ 当我从curl_multi_info_read 获得CURLMSG_DONE 时要清理的缓冲区?
【问题讨论】: