Skip to content

Commit

Permalink
Migrate curl_formadd from form API to mime API (deprecated in Ubuntu …
Browse files Browse the repository at this point in the history
…Noble) (#415)

* Migrate curl_formadd from form API to mime API

Signed-off-by: Jose Luis Rivero <[email protected]>

* Restore curl_easy_setopt call and use MIMEPOST

Signed-off-by: Jose Luis Rivero <[email protected]>

---------

Signed-off-by: Jose Luis Rivero <[email protected]>
Co-authored-by: Nate Koenig <[email protected]>
  • Loading branch information
j-rivero and nkoenig authored Apr 29, 2024
1 parent 9cdec8c commit 3dbc344
Showing 1 changed file with 17 additions and 26 deletions.
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);
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

0 comments on commit 3dbc344

Please sign in to comment.