gpt4 book ai didi

c++ - curl 回调未返回相同数量的字节

转载 作者:行者123 更新时间:2023-11-28 01:16:34 30 4
gpt4 key购买 nike

我在尝试使用 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;

最佳答案

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

https://curl.haxx.se/libcurl/c/CURLOPT_WRITEFUNCTION.html

关于c++ - curl 回调未返回相同数量的字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58506676/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com