mirror of
https://github.com/NixOS/nix.git
synced 2025-11-08 19:46:02 +01:00
refactor(libstore): replace HTTP method boolean flags with enum
Replace the individual boolean flags (head, post) with a unified HttpMethod enum struct in FileTransferRequest.
This commit is contained in:
parent
9a695f9067
commit
f1968ea38e
4 changed files with 22 additions and 7 deletions
|
|
@ -209,7 +209,7 @@ std::vector<nlohmann::json> Fetch::fetchUrls(const std::vector<Pointer> & pointe
|
||||||
auto url = api.endpoint + "/objects/batch";
|
auto url = api.endpoint + "/objects/batch";
|
||||||
const auto & authHeader = api.authHeader;
|
const auto & authHeader = api.authHeader;
|
||||||
FileTransferRequest request(parseURL(url));
|
FileTransferRequest request(parseURL(url));
|
||||||
request.post = true;
|
request.method = HttpMethod::POST;
|
||||||
Headers headers;
|
Headers headers;
|
||||||
if (authHeader.has_value())
|
if (authHeader.has_value())
|
||||||
headers.push_back({"Authorization", *authHeader});
|
headers.push_back({"Authorization", *authHeader});
|
||||||
|
|
|
||||||
|
|
@ -384,11 +384,11 @@ struct curlFileTransfer : public FileTransfer
|
||||||
if (settings.downloadSpeed.get() > 0)
|
if (settings.downloadSpeed.get() > 0)
|
||||||
curl_easy_setopt(req, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) (settings.downloadSpeed.get() * 1024));
|
curl_easy_setopt(req, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) (settings.downloadSpeed.get() * 1024));
|
||||||
|
|
||||||
if (request.head)
|
if (request.method == HttpMethod::HEAD)
|
||||||
curl_easy_setopt(req, CURLOPT_NOBODY, 1);
|
curl_easy_setopt(req, CURLOPT_NOBODY, 1);
|
||||||
|
|
||||||
if (request.data) {
|
if (request.data) {
|
||||||
if (request.post) {
|
if (request.method == HttpMethod::POST) {
|
||||||
curl_easy_setopt(req, CURLOPT_POST, 1L);
|
curl_easy_setopt(req, CURLOPT_POST, 1L);
|
||||||
curl_easy_setopt(req, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t) request.data->length());
|
curl_easy_setopt(req, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t) request.data->length());
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -139,7 +139,7 @@ protected:
|
||||||
|
|
||||||
try {
|
try {
|
||||||
FileTransferRequest request(makeRequest(path));
|
FileTransferRequest request(makeRequest(path));
|
||||||
request.head = true;
|
request.method = HttpMethod::HEAD;
|
||||||
getFileTransfer()->download(request);
|
getFileTransfer()->download(request);
|
||||||
return true;
|
return true;
|
||||||
} catch (FileTransferError & e) {
|
} catch (FileTransferError & e) {
|
||||||
|
|
|
||||||
|
|
@ -83,6 +83,15 @@ extern FileTransferSettings fileTransferSettings;
|
||||||
|
|
||||||
extern const unsigned int RETRY_TIME_MS_DEFAULT;
|
extern const unsigned int RETRY_TIME_MS_DEFAULT;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HTTP methods supported by FileTransfer.
|
||||||
|
*/
|
||||||
|
enum struct HttpMethod {
|
||||||
|
GET,
|
||||||
|
HEAD,
|
||||||
|
POST,
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Username and optional password for HTTP basic authentication.
|
* Username and optional password for HTTP basic authentication.
|
||||||
* These are used with curl's CURLOPT_USERNAME and CURLOPT_PASSWORD options
|
* These are used with curl's CURLOPT_USERNAME and CURLOPT_PASSWORD options
|
||||||
|
|
@ -99,8 +108,7 @@ struct FileTransferRequest
|
||||||
VerbatimURL uri;
|
VerbatimURL uri;
|
||||||
Headers headers;
|
Headers headers;
|
||||||
std::string expectedETag;
|
std::string expectedETag;
|
||||||
bool head = false;
|
HttpMethod method = HttpMethod::GET;
|
||||||
bool post = false;
|
|
||||||
size_t tries = fileTransferSettings.tries;
|
size_t tries = fileTransferSettings.tries;
|
||||||
unsigned int baseRetryTimeMs = RETRY_TIME_MS_DEFAULT;
|
unsigned int baseRetryTimeMs = RETRY_TIME_MS_DEFAULT;
|
||||||
ActivityId parentAct;
|
ActivityId parentAct;
|
||||||
|
|
@ -129,7 +137,14 @@ struct FileTransferRequest
|
||||||
|
|
||||||
std::string verb() const
|
std::string verb() const
|
||||||
{
|
{
|
||||||
return data ? "upload" : "download";
|
switch (method) {
|
||||||
|
case HttpMethod::HEAD:
|
||||||
|
case HttpMethod::GET:
|
||||||
|
return "download";
|
||||||
|
case HttpMethod::POST:
|
||||||
|
return "upload";
|
||||||
|
}
|
||||||
|
unreachable();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue