Add add_mime_part().
Makes code more readable and decreases duplication.
This commit is contained in:
parent
5b246a8cad
commit
f0a35bfd5b
@ -294,6 +294,18 @@ private:
|
|||||||
*/
|
*/
|
||||||
void add_parameters_to_uri(string &uri, const parametermap ¶meters);
|
void add_parameters_to_uri(string &uri, const parametermap ¶meters);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* @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`.
|
* @brief Convert parametermap to `*curl_mime`.
|
||||||
*
|
*
|
||||||
|
@ -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,
|
curl_mime *CURLWrapper::parameters_to_curl_mime(string &uri,
|
||||||
const parametermap ¶meters)
|
const parametermap ¶meters)
|
||||||
{
|
{
|
||||||
debuglog << "Building HTTP form.\n";
|
debuglog << "Building HTTP form.\n";
|
||||||
|
|
||||||
curl_mime *mime{curl_mime_init(_connection)};
|
curl_mime *mime{curl_mime_init(_connection)};
|
||||||
|
|
||||||
for (const auto ¶m : parameters)
|
for (const auto ¶m : parameters)
|
||||||
{
|
{
|
||||||
if (replace_parameter_in_uri(uri, param))
|
if (replace_parameter_in_uri(uri, param))
|
||||||
@ -352,52 +377,16 @@ curl_mime *CURLWrapper::parameters_to_curl_mime(string &uri,
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
CURLcode code;
|
|
||||||
if (holds_alternative<string_view>(param.second))
|
if (holds_alternative<string_view>(param.second))
|
||||||
{
|
{
|
||||||
curl_mimepart *part{curl_mime_addpart(mime)};
|
add_mime_part(mime, param.first, get<string_view>(param.second));
|
||||||
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';
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
for (const auto &arg : get<vector<string_view>>(param.second))
|
for (const auto &arg : get<vector<string_view>>(param.second))
|
||||||
{
|
{
|
||||||
curl_mimepart *part{curl_mime_addpart(mime)};
|
const string_view name{string(param.first) += "[]"};
|
||||||
if (part == nullptr)
|
add_mime_part(mime, name, arg);
|
||||||
{
|
|
||||||
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';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user