Add add_mime_part().

Makes code more readable and decreases duplication.
This commit is contained in:
tastytea 2020-01-11 13:17:35 +01:00
parent 5b246a8cad
commit f0a35bfd5b
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
2 changed files with 40 additions and 39 deletions

View File

@ -294,6 +294,18 @@ private:
*/
void add_parameters_to_uri(string &uri, const parametermap &parameters);
/*!
* @brief Add `*curl_mimepart` to `*curl_mime`.
*
* @param mime Initialized `*curl_mime`.
* @param name Name of the field.
* @param data Data of the field, or \@filename.
*
* @since 0.1.1
*/
void add_mime_part(curl_mime *mime,
string_view name, string_view data) const;
/*!
* @brief Convert parametermap to `*curl_mime`.
*

View File

@ -339,12 +339,37 @@ void CURLWrapper::add_parameters_to_uri(string &uri,
}
}
void CURLWrapper::add_mime_part(curl_mime *mime,
string_view name, string_view data) const
{
curl_mimepart *part{curl_mime_addpart(mime)};
if (part == nullptr)
{
throw CURLException{"Could not build HTTP form."};
}
CURLcode code{curl_mime_name(part, name.data())};
if (code != CURLE_OK)
{
throw CURLException{code, "Could not build HTTP form."};
}
code = curl_mime_data(part, data.data(), CURL_ZERO_TERMINATED);
if (code != CURLE_OK)
{
throw CURLException{code, "Could not build HTTP form."};
}
debuglog << "Set form part: " << name << " = " << data << '\n';
}
curl_mime *CURLWrapper::parameters_to_curl_mime(string &uri,
const parametermap &parameters)
{
debuglog << "Building HTTP form.\n";
curl_mime *mime{curl_mime_init(_connection)};
for (const auto &param : parameters)
{
if (replace_parameter_in_uri(uri, param))
@ -352,52 +377,16 @@ curl_mime *CURLWrapper::parameters_to_curl_mime(string &uri,
continue;
}
CURLcode code;
if (holds_alternative<string_view>(param.second))
{
curl_mimepart *part{curl_mime_addpart(mime)};
if (part == nullptr)
{
throw CURLException{"Could not build HTTP form."};
}
code = curl_mime_name(part, param.first.data());
if (code != CURLE_OK)
{
throw CURLException{code, "Could not build HTTP form."};
}
code = curl_mime_data(part, get<string_view>(param.second).data(),
CURL_ZERO_TERMINATED);
if (code != CURLE_OK)
{
throw CURLException{code, "Could not build HTTP form."};
}
debuglog << "Set form part: " << param.first << " = "
<< get<string_view>(param.second) << '\n';
add_mime_part(mime, param.first, get<string_view>(param.second));
}
else
{
for (const auto &arg : get<vector<string_view>>(param.second))
{
curl_mimepart *part{curl_mime_addpart(mime)};
if (part == nullptr)
{
throw CURLException{"Could not build HTTP form."};
}
const string name{string(param.first) += "[]"};
code = curl_mime_name(part, name.c_str());
if (code != CURLE_OK)
{
throw CURLException{code, "Could not build HTTP form."};
}
code = curl_mime_data(part, arg.data(), CURL_ZERO_TERMINATED);
if (code != CURLE_OK)
{
throw CURLException{code, "Could not build HTTP form."};
}
debuglog << "Set form part: " << name << " = " << arg << '\n';
const string_view name{string(param.first) += "[]"};
add_mime_part(mime, name, arg);
}
}
}