summaryrefslogtreecommitdiff
path: root/Source/Core/Common/HttpRequest.cpp
diff options
context:
space:
mode:
authorLéo Lam <leo@leolam.fr>2019-05-27 18:59:23 +0200
committerGitHub <noreply@github.com>2019-05-27 18:59:23 +0200
commite3be168328dc0bfaa0172505fbcf0863509b5189 (patch)
tree24f4f9439168bec3f4f1fda1d201fa71493136f1 /Source/Core/Common/HttpRequest.cpp
parent204af41e735c7c107d228e043488c2f891a03547 (diff)
parentab2adfb0a76e4e46a08f632cfb0525c40e7731f4 (diff)
Merge pull request #8124 from lioncash/http
Common/HttpRequest: Minor changes and simplifications
Diffstat (limited to 'Source/Core/Common/HttpRequest.cpp')
-rw-r--r--Source/Core/Common/HttpRequest.cpp31
1 files changed, 10 insertions, 21 deletions
diff --git a/Source/Core/Common/HttpRequest.cpp b/Source/Core/Common/HttpRequest.cpp
index 8098e315bd..8929185a86 100644
--- a/Source/Core/Common/HttpRequest.cpp
+++ b/Source/Core/Common/HttpRequest.cpp
@@ -39,17 +39,13 @@ public:
std::string EscapeComponent(const std::string& string);
private:
- static std::mutex s_curl_was_inited_mutex;
- static bool s_curl_was_inited;
+ static inline std::once_flag s_curl_was_initialized;
ProgressCallback m_callback;
std::unique_ptr<CURL, decltype(&curl_easy_cleanup)> m_curl{nullptr, curl_easy_cleanup};
};
-std::mutex HttpRequest::Impl::s_curl_was_inited_mutex;
-bool HttpRequest::Impl::s_curl_was_inited = false;
-
HttpRequest::HttpRequest(std::chrono::milliseconds timeout_ms, ProgressCallback callback)
- : m_impl(std::make_unique<Impl>(timeout_ms, callback))
+ : m_impl(std::make_unique<Impl>(timeout_ms, std::move(callback)))
{
}
@@ -107,16 +103,9 @@ int HttpRequest::Impl::CurlProgressCallback(Impl* impl, double dlnow, double dlt
}
HttpRequest::Impl::Impl(std::chrono::milliseconds timeout_ms, ProgressCallback callback)
- : m_callback(callback)
+ : m_callback(std::move(callback))
{
- {
- std::lock_guard<std::mutex> lk(s_curl_was_inited_mutex);
- if (!s_curl_was_inited)
- {
- curl_global_init(CURL_GLOBAL_DEFAULT);
- s_curl_was_inited = true;
- }
- }
+ std::call_once(s_curl_was_initialized, [] { curl_global_init(CURL_GLOBAL_DEFAULT); });
m_curl.reset(curl_easy_init());
if (!m_curl)
@@ -197,14 +186,14 @@ HttpRequest::Response HttpRequest::Impl::Fetch(const std::string& url, Method me
curl_slist* list = nullptr;
Common::ScopeGuard list_guard{[&list] { curl_slist_free_all(list); }};
- for (const std::pair<std::string, std::optional<std::string>>& header : headers)
+ for (const auto& [name, value] : headers)
{
- if (!header.second)
- list = curl_slist_append(list, (header.first + ":").c_str());
- else if (header.second->empty())
- list = curl_slist_append(list, (header.first + ";").c_str());
+ if (!value)
+ list = curl_slist_append(list, (name + ':').c_str());
+ else if (value->empty())
+ list = curl_slist_append(list, (name + ';').c_str());
else
- list = curl_slist_append(list, (header.first + ": " + *header.second).c_str());
+ list = curl_slist_append(list, (name + ": " + *value).c_str());
}
curl_easy_setopt(m_curl.get(), CURLOPT_HTTPHEADER, list);