C++ Curl回调不返回相同的字节数

C++ Curl回调不返回相同的字节数,c++,libcurl,C++,Libcurl,在尝试使用curl获取http数据时,我遇到了一个不一致的失败 libcurl: (23) Failed writing body (23048858 != 16058) 它不断地失败,然后开始工作。现在它又失败了。我四处寻找,大多数帖子要么归咎于回调,要么归咎于设置curl选项的顺序。我已经重新安排了选项,我没有发现回调有任何问题。事实上,当我调试回调时,输入参数与我实际得到的输出值相匹配。所以我不确定23048858这个值是从哪里来的 来源 SwReceiver* SwReceiver::

在尝试使用curl获取http数据时,我遇到了一个不一致的失败

libcurl: (23) Failed writing body (23048858 != 16058)
它不断地失败,然后开始工作。现在它又失败了。我四处寻找,大多数帖子要么归咎于回调,要么归咎于设置curl选项的顺序。我已经重新安排了选项,我没有发现回调有任何问题。事实上,当我调试回调时,输入参数与我实际得到的输出值相匹配。所以我不确定23048858这个值是从哪里来的

来源

SwReceiver* SwReceiver::instance;

SwReceiver *SwReceiver::getInstance(
  std::string &address,
  std::string &key,
  std::string &options)
{
  if(!instance)
  {
    instance = new SwReceiver(address, key, options);
    return instance;
  }
}

SwReceiver::SwReceiver(
  std::string &address,
  std::string &key,
  std::string &options)
{
  curl_global_init(CURL_GLOBAL_ALL);
  data = curl_easy_init();
  if(data)
  {
    std::string addrstr = address + '/' + key + '/' + options;
    // std::cout << "Address = " << addrstr << std::endl;
    curl_easy_setopt(data, CURLOPT_URL, addrstr.c_str());
    curl_easy_setopt(data, CURLOPT_WRITEDATA, (void *)&chunk);
    curl_easy_setopt(data, CURLOPT_USERAGENT, "libcurl-agent/1.0");
    curl_easy_setopt(data, CURLOPT_ERRORBUFFER, errorBuffer);
    curl_easy_setopt(data, CURLOPT_WRITEFUNCTION, writeMemoryCallback);
    memset(errorBuffer, 0, sizeof(CURL_ERROR_SIZE));
    /* DEBUG */
    curl_easy_setopt(data, CURLOPT_VERBOSE, 1);

    runQuery();
  }
  else
  {
    //TODO add to error buffer
  }
}

SwReceiver::~SwReceiver()
{
  delete(instance);
  curl_easy_cleanup(data);
}

void SwReceiver::runQuery()
{
  result = curl_easy_perform(data);
  if(result != CURLE_OK)
  {
    size_t len = strlen(errorBuffer);
    fprintf(stderr, "\nlibcurl: (%d) ", result);
    if(len)
    {
      fprintf(stderr, "%s%s", errorBuffer,
        ((errorBuffer[len - 1] != '\n') ? "\n" : ""));
    }
    else
    {
      fprintf(stderr, "%s\n", curl_easy_strerror(result));
    }
  }
}

MemoryStruct *SwReceiver::getData()
{
  return &chunk;
}

size_t SwReceiver::writeMemoryCallback(
  void *contents, size_t size, size_t nmemb, void *userp)
{
  size_t realSize = size * nmemb;
  struct MemoryStruct *mem = (struct MemoryStruct *)userp;

  char *ptr = (char*)realloc(mem->memory, mem->size + realSize + 1);
  if(ptr == NULL)
  {
    //TODO This should write to the error buffer
    std::cout << "Not enought memory!\n";
    return 0;
  }

  mem->memory = ptr;
  memcpy(&(mem->memory[mem->size]), contents, realSize);
  mem->size += realSize;
  mem->memory[mem->size] = 0;
}
 struct MemoryStruct
{
  char * memory;
  size_t size;

  MemoryStruct()
  : memory((char*)malloc(1)),
  size(0)
  {

  }
};
由于尺寸原因,我没有发布标题。如果你觉得我应该张贴让我知道

调试

(gdb)
(gdb) break 79
Breakpoint 1 at 0x40240c: file swreceiver.cpp, line 79.
(gdb) run
Starting program: /home/ubuntu/src/stickweather/stickweather
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff1de9700 (LWP 26989)]
[Thread 0x7ffff1de9700 (LWP 26989) exited]
*   Trying 34.193.12.42...
* Connected to api.darksky.net (34.193.12.42) port 443 (#0)
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/certs/ca-certificates.crt
  CApath: /etc/ssl/certs
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server accepted to use http/1.1
* Server certificate:
*        subject: CN=darksky.net
*        start date: Apr 26 00:00:00 2019 GMT
*        expire date: May 26 12:00:00 2020 GMT
*        subjectAltName: api.darksky.net matched
*        issuer: C=US; O=Amazon; OU=Server CA 1B; CN=Amazon
*        SSL certificate verify ok.
> GET /forecast/<hidden>/37.8267,-122.4233 HTTP/1.1
Host: api.darksky.net
User-Agent: libcurl-agent/1.0
Accept: */*

< HTTP/1.1 200 OK
< Date: Tue, 22 Oct 2019 14:18:40 GMT
< Content-Type: application/json; charset=utf-8
< Content-Length: 28035
< Connection: keep-alive
< X-Authentication-Time: 687ms
< X-Forecast-API-Calls: 13
< Cache-Control: max-age=60
< Expires: Tue, 22 Oct 2019 14:19:39 +0000
< X-Response-Time: 333.671ms
< Vary: Accept-Encoding
<

Thread 1 "stickweather" hit Breakpoint 1, SwReceiver::writeMemoryCallback (contents=0x650be6, size=1, nmemb=16058,
    userp=0x635848) at swreceiver.cpp:82
82        size_t realSize = size * nmemb;
(gdb)
(gdb)突破79
0x40240c处的断点1:文件swreceiver.cpp,第79行。
(gdb)运行
启动程序:/home/ubuntu/src/stickweather/stickweather
[已启用使用libthread_db的线程调试]
使用主机libthread_db library“/lib/x86_64-linux-gnu/libthread_db.so.1”。
[新螺纹0x7ffff1de9700(LWP 26989)]
[线程0x7ffff1de9700(LWP 26989)已退出]
*正在尝试34.193.12.42。。。
*已连接到api.darksky.net(34.193.12.42)端口443(#0)
*ALPN,提供http/1.1
*密码选择:全部:!出口:!出口40:!出口56:!阿努尔:!低:!RC4:@强度
*已成功设置证书验证位置:
*CAfile:/etc/ssl/certs/ca-certificates.crt
CApath:/etc/ssl/certs
*使用TLSv1.2/ECDHE-RSA-AES128-GCM-SHA256的SSL连接
*ALPN,接受使用http/1.1的服务器
*服务器证书:
*主题:CN=darksky.net
*开始日期:2019年4月26日00:00:00 GMT
*过期日期:5月26日12:00:00格林威治标准时间2020
*subjectAltName:api.darksky.net匹配
*发行人:C=美国;O=亚马逊;OU=服务器CA 1B;CN=亚马逊
*SSL证书验证正常。
>GET/forecast//37.8267,-122.4233 HTTP/1.1
主机:api.darksky.net
用户代理:libcurl代理/1.0
接受:*/*
writeMemoryCallback不会返回写入的字节数,在底部控件到达函数末尾时不会返回,因此在x86上它可能会返回垃圾。我怀疑这就是其他价值的来源


writeMemoryCallback不会返回写入的字节数,在底部控件到达函数末尾时不会返回,因此在x86上它可能会返回垃圾。我怀疑这就是其他价值的来源


删除(实例)不是一个好主意,因为这样会加倍销毁唯一的实例。注意。谢谢。花几分钟时间考虑一下您的回调返回的值。(在编译器中启用更多警告。)回调是从curl的网页复制的。他们没有使用返回值。你认为这会有影响吗?您指的是哪个编译器警告?如果您指的是,您的代码看起来非常像它,只是示例确实返回了一个值。如果您指的是其他内容,我相信curl项目会希望有bug报告。
delete(instance)不是一个好主意,因为这样会加倍销毁唯一的实例。注意。谢谢。花几分钟时间考虑一下您的回调返回的值。(在编译器中启用更多警告。)回调是从curl的网页复制的。他们没有使用返回值。你认为这会有影响吗?您指的是哪个编译器警告?如果您指的是,您的代码看起来非常像它,只是示例确实返回了一个值。如果你指的是其他的东西,我相信curl项目会喜欢一个bug报告。对,这很有意义。我没有意识到回调实际上使用的是返回值。我仔细检查了curl的网站,结果是正确的。我一定是转录错了。谢谢,没错,这很有道理。我没有意识到回调实际上使用的是返回值。我仔细检查了curl的网站,结果是正确的。我一定是转录错了。谢谢