Fix reporting of HTTP body size when a result callback is used

This commit is contained in:
Eelco Dolstra 2018-08-06 11:31:14 +02:00
parent 2825e05d21
commit 4361a4331f
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE
2 changed files with 5 additions and 1 deletions

View file

@ -140,6 +140,7 @@ struct CurlDownloader : public Downloader
size_t writeCallback(void * contents, size_t size, size_t nmemb) size_t writeCallback(void * contents, size_t size, size_t nmemb)
{ {
size_t realSize = size * nmemb; size_t realSize = size * nmemb;
result.bodySize += realSize;
if (request.dataCallback) if (request.dataCallback)
request.dataCallback((char *) contents, realSize); request.dataCallback((char *) contents, realSize);
else else
@ -162,6 +163,7 @@ struct CurlDownloader : public Downloader
auto ss = tokenizeString<vector<string>>(line, " "); auto ss = tokenizeString<vector<string>>(line, " ");
status = ss.size() >= 2 ? ss[1] : ""; status = ss.size() >= 2 ? ss[1] : "";
result.data = std::make_shared<std::string>(); result.data = std::make_shared<std::string>();
result.bodySize = 0;
encoding = ""; encoding = "";
} else { } else {
auto i = line.find(':'); auto i = line.find(':');
@ -296,6 +298,7 @@ struct CurlDownloader : public Downloader
curl_easy_setopt(req, CURLOPT_NETRC, CURL_NETRC_OPTIONAL); curl_easy_setopt(req, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
result.data = std::make_shared<std::string>(); result.data = std::make_shared<std::string>();
result.bodySize = 0;
} }
void finish(CURLcode code) void finish(CURLcode code)
@ -309,7 +312,7 @@ struct CurlDownloader : public Downloader
result.effectiveUrl = effectiveUrlCStr; result.effectiveUrl = effectiveUrlCStr;
debug("finished %s of '%s'; curl status = %d, HTTP status = %d, body = %d bytes", debug("finished %s of '%s'; curl status = %d, HTTP status = %d, body = %d bytes",
request.verb(), request.uri, code, httpStatus, result.data ? result.data->size() : 0); request.verb(), request.uri, code, httpStatus, result.bodySize);
if (code == CURLE_WRITE_ERROR && result.etag == request.expectedETag) { if (code == CURLE_WRITE_ERROR && result.etag == request.expectedETag) {
code = CURLE_OK; code = CURLE_OK;

View file

@ -38,6 +38,7 @@ struct DownloadResult
std::string etag; std::string etag;
std::string effectiveUrl; std::string effectiveUrl;
std::shared_ptr<std::string> data; std::shared_ptr<std::string> data;
uint64_t bodySize = 0;
}; };
class Store; class Store;