FIX: thread safe of http extra headers

Change-Id: I6ffa424be7ccb6abd78a66cc8be535f038b05469
Jira: none
This commit is contained in:
chunmao.guo 2024-02-01 14:13:13 +08:00 committed by Lane.Wei
parent fdcd6085d2
commit 5cf0871ea1
1 changed files with 3 additions and 0 deletions

View File

@ -84,6 +84,7 @@ struct CurlGlobalInit
std::unique_ptr<CurlGlobalInit> CurlGlobalInit::instance;
std::map<std::string, std::string> extra_headers;
std::mutex g_mutex;
struct Http::priv
{
@ -470,6 +471,7 @@ void Http::priv::http_perform()
Http::Http(const std::string &url) : p(new priv(url)) {
std::lock_guard<std::mutex> l(g_mutex);
for (auto it = extra_headers.begin(); it != extra_headers.end(); it++)
this->header(it->first, it->second);
}
@ -740,6 +742,7 @@ Http Http::del(std::string url)
void Http::set_extra_headers(std::map<std::string, std::string> headers)
{
std::lock_guard<std::mutex> l(g_mutex);
extra_headers.swap(headers);
}