mirror of
https://github.com/Syping/dtranslatebot.git
synced 2026-04-01 05:20:22 +02:00
http_request: improve memory safety
This commit is contained in:
parent
cf134599a7
commit
ee245fbb79
1 changed files with 15 additions and 6 deletions
|
|
@ -30,10 +30,13 @@ http_response http_request::get(const std::string &url, const dpp::http_headers
|
|||
curl_slist *header_slist = nullptr;
|
||||
if (!headers.empty()) {
|
||||
for (const auto &header : headers) {
|
||||
header_slist = curl_slist_append(header_slist, std::string(header.first + ": " + header.second).c_str());
|
||||
if (!header_slist)
|
||||
curl_slist *new_header_slist = curl_slist_append(header_slist, std::string(header.first + ": " + header.second).c_str());
|
||||
if (!new_header_slist) {
|
||||
curl_slist_free_all(header_slist);
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
header_slist = new_header_slist;
|
||||
}
|
||||
}
|
||||
if (!header_slist)
|
||||
curl_easy_setopt(instance, CURLOPT_HTTPHEADER, header_slist);
|
||||
|
|
@ -52,16 +55,22 @@ http_response http_request::post(const std::string &url, const std::string &cont
|
|||
curl_easy_setopt(instance, CURLOPT_URL, url.c_str());
|
||||
curl_slist *header_slist = nullptr;
|
||||
if (!content_type.empty()) {
|
||||
header_slist = curl_slist_append(header_slist, std::string("Content-Type: " + content_type).c_str());
|
||||
if (!header_slist)
|
||||
curl_slist *new_header_slist = curl_slist_append(header_slist, std::string("Content-Type: " + content_type).c_str());
|
||||
if (!new_header_slist) {
|
||||
curl_slist_free_all(header_slist);
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
header_slist = new_header_slist;
|
||||
}
|
||||
if (!headers.empty()) {
|
||||
for (const auto &header : headers) {
|
||||
header_slist = curl_slist_append(header_slist, std::string(header.first + ": " + header.second).c_str());
|
||||
if (!header_slist)
|
||||
curl_slist *new_header_slist = curl_slist_append(header_slist, std::string(header.first + ": " + header.second).c_str());
|
||||
if (!new_header_slist) {
|
||||
curl_slist_free_all(header_slist);
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
header_slist = new_header_slist;
|
||||
}
|
||||
}
|
||||
if (!header_slist)
|
||||
curl_easy_setopt(instance, CURLOPT_HTTPHEADER, header_slist);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue