c++ - Libcurl Returns Corrupt Data
问题描述
curl_easy_setopt(m_pCurl, CURLOPT_URL, szURL);
curl_easy_setopt(m_pCurl, CURLOPT_UPLOAD,0);
curl_easy_setopt(m_pCurl, CURLOPT_WRITEFUNCTION, s_Write_Data);
curl_easy_setopt(m_pCurl, CURLOPT_WRITEDATA, this);
curl_easy_setopt(m_pCurl, CURLOPT_CONNECTTIMEOUT, 10);
curl_easy_setopt(m_pCurl, CURLOPT_NOPROGRESS, 0);
curl_easy_setopt(m_pCurl, CURLOPT_PROGRESSFUNCTION, s_Progress);
curl_easy_setopt(m_pCurl, CURLOPT_PROGRESSDATA, this);
curl_easy_setopt(m_pCurl, CURLOPT_HTTP_CONTENT_DECODING, 0L);
Above are all my opts for the transfer. The download returns a .tar.gz file which using command line curl or WGET returns absolutely fine, using libcurl the returned data is about 8k short, doing a cmp shows that the 2 files change within the first 10 bytes. Been looking through this for 2 days now with no sign of hope. Tried various different combinations of opts.
s_Write_Data is implemented as:
size_t CCurlManager::s_Write_Data(void *buffer, size_t size, size_t nmemb, void *userp)
{
CCurlManager *pThis = reinterpret_cast<CCurlManager *>(userp);
return pThis->WriteData(buffer, size, nmemb);
}
The response header I get from curl verbose:
HTTP/1.1 200 OK
Date: Mon, 17 Dec 2018 13:56:39 GMT
Server: Apache/2.2.13 (Linux/SUSE)
X-Powered-By: PHP/5.4.20
Content-Length: 393800
Content-Disposition: attachment; filename="test.tar.gz"
Connection: close
Content-Type: application/octet-stream
解决方案
这是 wxString (wxWidgets) 的一个问题,wxString(char*, int) 为最多 int 字符创建一个 char* 字符串,或者直到达到一个空字符,这在大多数其他 (char*, int) 中是非标准的) 只读取 int 字符的字符串构造函数。
基本上,我在 WriteData 函数中创建的字符串在达到空字符时终止。
推荐阅读
- javascript - 我需要正则表达式的银行路由号码在 javascript 中以 01 到 12 或 21 到 32 开头
- javascript - nodejs 响应 HTML 文件,其中包含带有 src 属性的脚本标记
- elasticsearch - 如何将 Ui Path orchestrator 与 Kibana 链接
- angular - 如何在打字稿中创建具有未知数量属性的接口?
- flutter - 扑动中GoogleMapController的含义是什么?
- javascript - Firebase 和 Express,每次更改 firebase 的值时,我的 api 都会发送重复响应
- jenkins - 使用 BeanStalk、GitHub、CodePipeline 和 Jenkins 为 CI/CD 优化子网配置
- swift - 如何在 SwiftUI 中检查 json 数据的键是否存在?
- pytorch - "MisconfigurationError: No TPU devices were found" even when TPU is connected in PyTorch Lightning
- apache-spark - 在 Spark 数据框中的子字符串之后提取第一次出现的字符串?