Skip to content
Permalink
Browse files

Merge pull request #8124 from lioncash/http

Common/HttpRequest: Minor changes and simplifications
  • Loading branch information...
leoetlino committed May 27, 2019
2 parents 204af41 + ab2adfb commit e3be168328dc0bfaa0172505fbcf0863509b5189
Showing with 10 additions and 21 deletions.
  1. +10 −21 Source/Core/Common/HttpRequest.cpp
@@ -39,17 +39,13 @@ class HttpRequest::Impl final
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);

0 comments on commit e3be168

Please sign in to comment.
You can’t perform that action at this time.