mirror of
https://github.com/NixOS/nix
synced 2025-06-25 19:01:16 +02:00
Add ability to POST to FileTransfer
Plus, switched CURLOPT_PROGRESSFUNCTION to CURLOPT_XFERINFOFUNCTION since docs say it's deprecated
This commit is contained in:
parent
7756b2286d
commit
4c42b1c7cb
2 changed files with 23 additions and 5 deletions
|
@ -94,7 +94,7 @@ struct curlFileTransfer : public FileTransfer
|
||||||
: fileTransfer(fileTransfer)
|
: fileTransfer(fileTransfer)
|
||||||
, request(request)
|
, request(request)
|
||||||
, act(*logger, lvlTalkative, actFileTransfer,
|
, act(*logger, lvlTalkative, actFileTransfer,
|
||||||
fmt(request.data ? "uploading '%s'" : "downloading '%s'", request.uri),
|
request.post ? "" : fmt(request.data ? "uploading '%s'" : "downloading '%s'", request.uri),
|
||||||
{request.uri}, request.parentAct)
|
{request.uri}, request.parentAct)
|
||||||
, callback(std::move(callback))
|
, callback(std::move(callback))
|
||||||
, finalSink([this](std::string_view data) {
|
, finalSink([this](std::string_view data) {
|
||||||
|
@ -271,11 +271,21 @@ struct curlFileTransfer : public FileTransfer
|
||||||
return getInterrupted();
|
return getInterrupted();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int silentProgressCallback(double dltotal, double dlnow)
|
||||||
|
{
|
||||||
|
return getInterrupted();
|
||||||
|
}
|
||||||
|
|
||||||
static int progressCallbackWrapper(void * userp, double dltotal, double dlnow, double ultotal, double ulnow)
|
static int progressCallbackWrapper(void * userp, double dltotal, double dlnow, double ultotal, double ulnow)
|
||||||
{
|
{
|
||||||
return ((TransferItem *) userp)->progressCallback(dltotal, dlnow);
|
return ((TransferItem *) userp)->progressCallback(dltotal, dlnow);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int silentProgressCallbackWrapper(void * userp, double dltotal, double dlnow, double ultotal, double ulnow)
|
||||||
|
{
|
||||||
|
return ((TransferItem *) userp)->silentProgressCallback(dltotal, dlnow);
|
||||||
|
}
|
||||||
|
|
||||||
static int debugCallback(CURL * handle, curl_infotype type, char * data, size_t size, void * userptr)
|
static int debugCallback(CURL * handle, curl_infotype type, char * data, size_t size, void * userptr)
|
||||||
{
|
{
|
||||||
if (type == CURLINFO_TEXT)
|
if (type == CURLINFO_TEXT)
|
||||||
|
@ -332,8 +342,11 @@ struct curlFileTransfer : public FileTransfer
|
||||||
curl_easy_setopt(req, CURLOPT_HEADERFUNCTION, TransferItem::headerCallbackWrapper);
|
curl_easy_setopt(req, CURLOPT_HEADERFUNCTION, TransferItem::headerCallbackWrapper);
|
||||||
curl_easy_setopt(req, CURLOPT_HEADERDATA, this);
|
curl_easy_setopt(req, CURLOPT_HEADERDATA, this);
|
||||||
|
|
||||||
curl_easy_setopt(req, CURLOPT_PROGRESSFUNCTION, progressCallbackWrapper);
|
if (request.post)
|
||||||
curl_easy_setopt(req, CURLOPT_PROGRESSDATA, this);
|
curl_easy_setopt(req, CURLOPT_XFERINFOFUNCTION, silentProgressCallbackWrapper);
|
||||||
|
else
|
||||||
|
curl_easy_setopt(req, CURLOPT_XFERINFOFUNCTION, progressCallbackWrapper);
|
||||||
|
curl_easy_setopt(req, CURLOPT_XFERINFODATA, this);
|
||||||
curl_easy_setopt(req, CURLOPT_NOPROGRESS, 0);
|
curl_easy_setopt(req, CURLOPT_NOPROGRESS, 0);
|
||||||
|
|
||||||
curl_easy_setopt(req, CURLOPT_HTTPHEADER, requestHeaders);
|
curl_easy_setopt(req, CURLOPT_HTTPHEADER, requestHeaders);
|
||||||
|
@ -345,6 +358,9 @@ struct curlFileTransfer : public FileTransfer
|
||||||
curl_easy_setopt(req, CURLOPT_NOBODY, 1);
|
curl_easy_setopt(req, CURLOPT_NOBODY, 1);
|
||||||
|
|
||||||
if (request.data) {
|
if (request.data) {
|
||||||
|
if (request.post)
|
||||||
|
curl_easy_setopt(req, CURLOPT_POST, 1L);
|
||||||
|
else
|
||||||
curl_easy_setopt(req, CURLOPT_UPLOAD, 1L);
|
curl_easy_setopt(req, CURLOPT_UPLOAD, 1L);
|
||||||
curl_easy_setopt(req, CURLOPT_READFUNCTION, readCallbackWrapper);
|
curl_easy_setopt(req, CURLOPT_READFUNCTION, readCallbackWrapper);
|
||||||
curl_easy_setopt(req, CURLOPT_READDATA, this);
|
curl_easy_setopt(req, CURLOPT_READDATA, this);
|
||||||
|
@ -418,6 +434,7 @@ struct curlFileTransfer : public FileTransfer
|
||||||
if (httpStatus == 304 && result.etag == "")
|
if (httpStatus == 304 && result.etag == "")
|
||||||
result.etag = request.expectedETag;
|
result.etag = request.expectedETag;
|
||||||
|
|
||||||
|
if (!request.post)
|
||||||
act.progress(result.bodySize, result.bodySize);
|
act.progress(result.bodySize, result.bodySize);
|
||||||
done = true;
|
done = true;
|
||||||
callback(std::move(result));
|
callback(std::move(result));
|
||||||
|
|
|
@ -64,6 +64,7 @@ struct FileTransferRequest
|
||||||
std::string expectedETag;
|
std::string expectedETag;
|
||||||
bool verifyTLS = true;
|
bool verifyTLS = true;
|
||||||
bool head = false;
|
bool head = false;
|
||||||
|
bool post = false;
|
||||||
size_t tries = fileTransferSettings.tries;
|
size_t tries = fileTransferSettings.tries;
|
||||||
unsigned int baseRetryTimeMs = 250;
|
unsigned int baseRetryTimeMs = 250;
|
||||||
ActivityId parentAct;
|
ActivityId parentAct;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue