From 9c9b2c242a2fec5d096ef15acca720fba3f88268 Mon Sep 17 00:00:00 2001 From: Victor Vazquez Date: Mon, 14 Sep 2020 20:44:04 +0000 Subject: [PATCH] throw transport exception when getting new connection fails (#640) * throw transport exception when getting new connection fails * use function instead of macro * Update function to PascalCase --- sdk/core/azure-core/src/http/curl/curl.cpp | 58 +++++++++++++--------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/sdk/core/azure-core/src/http/curl/curl.cpp b/sdk/core/azure-core/src/http/curl/curl.cpp index 24808d76c..d68f60640 100644 --- a/sdk/core/azure-core/src/http/curl/curl.cpp +++ b/sdk/core/azure-core/src/http/curl/curl.cpp @@ -8,6 +8,23 @@ #include #include +namespace { +template +inline void SetLibcurlOption( + CURL* handle, + CURLoption option, + T value, + std::string const& errorMessage) +{ + auto result = curl_easy_setopt(handle, option, value); + if (result != CURLE_OK) + { + throw Azure::Core::Http::TransportException( + errorMessage + ". " + std::string(curl_easy_strerror(result))); + } +} +} // namespace + using namespace Azure::Core::Http; // To wait for a socket to be ready to be read/write @@ -868,39 +885,32 @@ std::unique_ptr CurlConnectionPool::GetCurlConnection(Request& r auto newConnection = std::make_unique(host); // Libcurl setup before open connection (url, connet_only, timeout) - auto result = curl_easy_setopt( - newConnection->GetHandle(), CURLOPT_URL, request.GetUrl().GetAbsoluteUrl().data()); - if (result != CURLE_OK) - { - throw std::runtime_error( - Details::c_DefaultFailedToGetNewConnectionTemplate + host + ". " - + std::string(curl_easy_strerror(result))); - } + SetLibcurlOption( + newConnection->GetHandle(), + CURLOPT_URL, + request.GetUrl().GetAbsoluteUrl().data(), + Details::c_DefaultFailedToGetNewConnectionTemplate + host); - result = curl_easy_setopt(newConnection->GetHandle(), CURLOPT_CONNECT_ONLY, 1L); - if (result != CURLE_OK) - { - throw std::runtime_error( - Details::c_DefaultFailedToGetNewConnectionTemplate + host + ". " - + std::string(curl_easy_strerror(result))); - } + SetLibcurlOption( + newConnection->GetHandle(), + CURLOPT_CONNECT_ONLY, + 1L, + Details::c_DefaultFailedToGetNewConnectionTemplate + host); // curl_easy_setopt(newConnection->GetHandle(), CURLOPT_VERBOSE, 1L); // Set timeout to 24h. Libcurl will fail uploading on windows if timeout is: // timeout >= 25 days. Fails as soon as trying to upload any data // 25 days < timeout > 1 days. Fail on huge uploads ( > 1GB) - result = curl_easy_setopt(newConnection->GetHandle(), CURLOPT_TIMEOUT, 60L * 60L * 24L); - if (result != CURLE_OK) - { - throw std::runtime_error( - Details::c_DefaultFailedToGetNewConnectionTemplate + host + ". " - + std::string(curl_easy_strerror(result))); - } + SetLibcurlOption( + newConnection->GetHandle(), + CURLOPT_TIMEOUT, + 60L * 60L * 24L, + Details::c_DefaultFailedToGetNewConnectionTemplate + host); - result = curl_easy_perform(newConnection->GetHandle()); + auto result = curl_easy_perform(newConnection->GetHandle()); if (result != CURLE_OK) { - throw std::runtime_error( + throw Http::TransportException( Details::c_DefaultFailedToGetNewConnectionTemplate + host + ". " + std::string(curl_easy_strerror(result))); }