1
1
Fork 0
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:
Bernardo Meurer Costa 2025-10-23 05:03:16 +00:00
parent 9a695f9067
commit f1968ea38e
No known key found for this signature in database
4 changed files with 22 additions and 7 deletions

View file

@ -209,7 +209,7 @@ std::vector<nlohmann::json> Fetch::fetchUrls(const std::vector<Pointer> & pointe
auto url = api.endpoint + "/objects/batch";
const auto & authHeader = api.authHeader;
FileTransferRequest request(parseURL(url));
request.post = true;
request.method = HttpMethod::POST;
Headers headers;
if (authHeader.has_value())
headers.push_back({"Authorization", *authHeader});

View file

@ -384,11 +384,11 @@ struct curlFileTransfer : public FileTransfer
if (settings.downloadSpeed.get() > 0)
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);
if (request.data) {
if (request.post) {
if (request.method == HttpMethod::POST) {
curl_easy_setopt(req, CURLOPT_POST, 1L);
curl_easy_setopt(req, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t) request.data->length());
} else {

View file

@ -139,7 +139,7 @@ protected:
try {
FileTransferRequest request(makeRequest(path));
request.head = true;
request.method = HttpMethod::HEAD;
getFileTransfer()->download(request);
return true;
} catch (FileTransferError & e) {

View file

@ -83,6 +83,15 @@ extern FileTransferSettings fileTransferSettings;
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.
* These are used with curl's CURLOPT_USERNAME and CURLOPT_PASSWORD options
@ -99,8 +108,7 @@ struct FileTransferRequest
VerbatimURL uri;
Headers headers;
std::string expectedETag;
bool head = false;
bool post = false;
HttpMethod method = HttpMethod::GET;
size_t tries = fileTransferSettings.tries;
unsigned int baseRetryTimeMs = RETRY_TIME_MS_DEFAULT;
ActivityId parentAct;
@ -129,7 +137,14 @@ struct FileTransferRequest
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: