Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate curl_formadd from form API to mime API (deprecated in Ubuntu Noble) #415

Merged
merged 4 commits into from
Apr 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 17 additions & 26 deletions src/RestClient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ size_t RestWriteMemoryCallback(void *_buffer, size_t _size, size_t _nmemb,
}

/////////////////////////////////////////////////
struct curl_httppost *BuildFormPost(
void AddFormPost(
curl_mime * const multipart,
const std::multimap<std::string, std::string> &_form)
{
struct curl_httppost *formpost = nullptr;
struct curl_httppost *lastptr = nullptr;
for (const auto &[key, value] : _form)
{
curl_mimepart *part = curl_mime_addpart(multipart);
// follow same convention as curl cmdline tool
// field starting with @ indicates path to file to upload
// others are standard fields to describe the file
Expand Down Expand Up @@ -171,26 +171,17 @@ struct curl_httppost *BuildFormPost(
}
}

curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, key.c_str(),
CURLFORM_FILENAME, uploadFilename.c_str(),
CURLFORM_FILE, path.c_str(),
CURLFORM_CONTENTTYPE, contentType.c_str(),
CURLFORM_END);
curl_mime_name(part, key.c_str());
curl_mime_filename(part, uploadFilename.c_str());
curl_mime_filedata(part, path.c_str());
curl_mime_type(part, contentType.c_str());
}
else
{
// standard key:value fields
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, key.c_str(),
CURLFORM_COPYCONTENTS, value.c_str(),
CURLFORM_END);
curl_mime_name(part, key.c_str());
curl_mime_data(part, value.c_str(), CURL_ZERO_TERMINATED);
}
}

return formpost;
}

/////////////////////////////////////////////////
Expand Down Expand Up @@ -294,7 +285,7 @@ RestResponse Rest::Request(HttpMethod _method,
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 3L);

std::ifstream ifs;
struct curl_httppost *formpost = nullptr;
curl_mime *multipart = curl_mime_init(curl);

// Send the request.
if (_method == HttpMethod::GET)
Expand All @@ -303,9 +294,11 @@ RestResponse Rest::Request(HttpMethod _method,
}
else if (_method == HttpMethod::PATCH_FORM)
{
formpost = BuildFormPost(_form);
AddFormPost(multipart, _form);

curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);

curl_easy_setopt(curl, CURLOPT_MIMEPOST, multipart);
}
else if (_method == HttpMethod::POST)
{
Expand All @@ -314,8 +307,8 @@ RestResponse Rest::Request(HttpMethod _method,
}
else if (_method == HttpMethod::POST_FORM)
{
formpost = BuildFormPost(_form);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
AddFormPost(multipart, _form);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be the following line here?

curl_easy_setopt(curl, CURLOPT_MIMEPOST, multipart);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch! reviewing the deprecation instruction you are right.

curl_easy_setopt(curl, CURLOPT_MIMEPOST, multipart);
}
else if (_method == HttpMethod::DELETE)
{
Expand Down Expand Up @@ -355,9 +348,6 @@ RestResponse Rest::Request(HttpMethod _method,
// Update the header data.
res.headers = headerData;

if (formpost)
curl_formfree(formpost);

// free encoded path char*
if (encodedPath)
curl_free(encodedPath);
Expand All @@ -366,6 +356,7 @@ RestResponse Rest::Request(HttpMethod _method,
curl_slist_free_all(headers);

// Cleaning.
curl_mime_free(multipart);
curl_easy_cleanup(curl);

if (ifs.is_open())
Expand Down