Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions sdk/core/azure-core/src/http/curl/curl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1308,23 +1308,27 @@ std::unique_ptr<CurlNetworkConnection> CurlConnectionPool::ExtractOrCreateCurlCo
+ std::string("curl_easy_init returned Null"));
}
CURLcode result;

auto curlConnection = std::make_unique<CurlConnection>(newHandle, connectionKey);
Comment thread
gearama marked this conversation as resolved.
Outdated
// Libcurl setup before open connection (url, connect_only, timeout)
if (!SetLibcurlOption(newHandle, CURLOPT_URL, request.GetUrl().GetAbsoluteUrl().data(), &result))
if (!SetLibcurlOption(
curlConnection->GetHandle(),
CURLOPT_URL,
request.GetUrl().GetAbsoluteUrl().data(),
&result))
{
throw Azure::Core::Http::TransportException(
_detail::DefaultFailedToGetNewConnectionTemplate + hostDisplayName + ". "
+ std::string(curl_easy_strerror(result)));
}

if (port != 0 && !SetLibcurlOption(newHandle, CURLOPT_PORT, port, &result))
if (port != 0 && !SetLibcurlOption(curlConnection->GetHandle(), CURLOPT_PORT, port, &result))
{
throw Azure::Core::Http::TransportException(
_detail::DefaultFailedToGetNewConnectionTemplate + hostDisplayName + ". "
+ std::string(curl_easy_strerror(result)));
}

if (!SetLibcurlOption(newHandle, CURLOPT_CONNECT_ONLY, 1L, &result))
if (!SetLibcurlOption(curlConnection->GetHandle(), CURLOPT_CONNECT_ONLY, 1L, &result))
{
throw Azure::Core::Http::TransportException(
_detail::DefaultFailedToGetNewConnectionTemplate + hostDisplayName + ". "
Expand All @@ -1334,7 +1338,7 @@ std::unique_ptr<CurlNetworkConnection> CurlConnectionPool::ExtractOrCreateCurlCo
// 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)
if (!SetLibcurlOption(newHandle, CURLOPT_TIMEOUT, 60L * 60L * 24L, &result))
if (!SetLibcurlOption(curlConnection->GetHandle(), CURLOPT_TIMEOUT, 60L * 60L * 24L, &result))
{
throw Azure::Core::Http::TransportException(
_detail::DefaultFailedToGetNewConnectionTemplate + hostDisplayName + ". "
Expand All @@ -1343,7 +1347,11 @@ std::unique_ptr<CurlNetworkConnection> CurlConnectionPool::ExtractOrCreateCurlCo

if (options.ConnectionTimeout != Azure::Core::Http::_detail::DefaultConnectionTimeout)
{
if (!SetLibcurlOption(newHandle, CURLOPT_CONNECTTIMEOUT_MS, options.ConnectionTimeout, &result))
if (!SetLibcurlOption(
curlConnection->GetHandle(),
CURLOPT_CONNECTTIMEOUT_MS,
options.ConnectionTimeout,
&result))
{
throw Azure::Core::Http::TransportException(
_detail::DefaultFailedToGetNewConnectionTemplate + hostDisplayName
Expand All @@ -1358,7 +1366,8 @@ std::unique_ptr<CurlNetworkConnection> CurlConnectionPool::ExtractOrCreateCurlCo
*/
if (options.Proxy)
{
if (!SetLibcurlOption(newHandle, CURLOPT_PROXY, options.Proxy->c_str(), &result))
if (!SetLibcurlOption(
curlConnection->GetHandle(), CURLOPT_PROXY, options.Proxy->c_str(), &result))
{
throw Azure::Core::Http::TransportException(
_detail::DefaultFailedToGetNewConnectionTemplate + hostDisplayName
Expand All @@ -1369,7 +1378,8 @@ std::unique_ptr<CurlNetworkConnection> CurlConnectionPool::ExtractOrCreateCurlCo

if (!options.CAInfo.empty())
{
if (!SetLibcurlOption(newHandle, CURLOPT_CAINFO, options.CAInfo.c_str(), &result))
if (!SetLibcurlOption(
curlConnection->GetHandle(), CURLOPT_CAINFO, options.CAInfo.c_str(), &result))
{
throw Azure::Core::Http::TransportException(
_detail::DefaultFailedToGetNewConnectionTemplate + hostDisplayName
Expand All @@ -1384,7 +1394,7 @@ std::unique_ptr<CurlNetworkConnection> CurlConnectionPool::ExtractOrCreateCurlCo
sslOption |= CURLSSLOPT_NO_REVOKE;
}

if (!SetLibcurlOption(newHandle, CURLOPT_SSL_OPTIONS, sslOption, &result))
if (!SetLibcurlOption(curlConnection->GetHandle(), CURLOPT_SSL_OPTIONS, sslOption, &result))
{
throw Azure::Core::Http::TransportException(
_detail::DefaultFailedToGetNewConnectionTemplate + hostDisplayName
Expand All @@ -1394,7 +1404,7 @@ std::unique_ptr<CurlNetworkConnection> CurlConnectionPool::ExtractOrCreateCurlCo

if (!options.SslVerifyPeer)
{
if (!SetLibcurlOption(newHandle, CURLOPT_SSL_VERIFYPEER, 0L, &result))
if (!SetLibcurlOption(curlConnection->GetHandle(), CURLOPT_SSL_VERIFYPEER, 0L, &result))
{
throw Azure::Core::Http::TransportException(
_detail::DefaultFailedToGetNewConnectionTemplate + hostDisplayName
Expand All @@ -1404,7 +1414,7 @@ std::unique_ptr<CurlNetworkConnection> CurlConnectionPool::ExtractOrCreateCurlCo

if (options.NoSignal)
{
if (!SetLibcurlOption(newHandle, CURLOPT_NOSIGNAL, 1L, &result))
if (!SetLibcurlOption(curlConnection->GetHandle(), CURLOPT_NOSIGNAL, 1L, &result))
{
throw Azure::Core::Http::TransportException(
_detail::DefaultFailedToGetNewConnectionTemplate + hostDisplayName
Expand All @@ -1416,30 +1426,32 @@ std::unique_ptr<CurlNetworkConnection> CurlConnectionPool::ExtractOrCreateCurlCo
// curl-transport adapter supports only HTTP/1.1
// https://github.com/Azure/azure-sdk-for-cpp/issues/2848
// The libcurl uses HTTP/2 by default, if it can be negotiated with a server on handshake.
if (!SetLibcurlOption(newHandle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1, &result))
if (!SetLibcurlOption(
curlConnection->GetHandle(), CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1, &result))
{
throw Azure::Core::Http::TransportException(
_detail::DefaultFailedToGetNewConnectionTemplate + hostDisplayName
+ ". Failed to set libcurl HTTP/1.1" + ". " + std::string(curl_easy_strerror(result)));
}

// Make libcurl to support only TLS v1.2 or later
if (!SetLibcurlOption(newHandle, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2, &result))
if (!SetLibcurlOption(
curlConnection->GetHandle(), CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2, &result))
{
throw Azure::Core::Http::TransportException(
_detail::DefaultFailedToGetNewConnectionTemplate + hostDisplayName
+ ". Failed enforcing TLS v1.2 or greater. " + std::string(curl_easy_strerror(result)));
}

auto performResult = curl_easy_perform(newHandle);
auto performResult = curl_easy_perform(curlConnection->GetHandle());
if (performResult != CURLE_OK)
{
throw Http::TransportException(
_detail::DefaultFailedToGetNewConnectionTemplate + hostDisplayName + ". "
+ std::string(curl_easy_strerror(performResult)));
}

return std::make_unique<CurlConnection>(newHandle, connectionKey);
Comment thread
gearama marked this conversation as resolved.
return curlConnection;
}

// Move the connection back to the connection pool. Push it to the front so it becomes the
Expand Down
6 changes: 6 additions & 0 deletions sdk/core/azure-core/src/http/curl/curl_connection_private.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ namespace Azure { namespace Core { namespace Http {
}
}

/**
* @brief Gets the connection handle
*
*/
CURL* GetHandle() { return m_handle; }

Comment thread
gearama marked this conversation as resolved.
Outdated
/**
* @brief Destructor.
* @details Cleans up CURL (invokes `curl_easy_cleanup()`).
Expand Down