X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=platform-abstractions%2Ftizen%2Fresource-loader%2Fnetwork%2Ffile-download.cpp;h=b276678350c90782e76cfef54203ed5f41d41878;hb=b1f227365a03d621f6c5bf464d8ec8e601ec979f;hp=80f67faf3a126d818342dc0d3efd2266ee83f99a;hpb=d8f0d0809cf9c706f1f199b32abeedd1136feac5;p=platform%2Fcore%2Fuifw%2Fdali-adaptor.git diff --git a/platform-abstractions/tizen/resource-loader/network/file-download.cpp b/platform-abstractions/tizen/resource-loader/network/file-download.cpp index 80f67fa..b276678 100755 --- a/platform-abstractions/tizen/resource-loader/network/file-download.cpp +++ b/platform-abstractions/tizen/resource-loader/network/file-download.cpp @@ -20,7 +20,9 @@ // EXTERNAL INCLUDES #include +#include #include +#include #include // INTERNAL INCLUDES @@ -55,14 +57,13 @@ const long EXCLUDE_BODY = 1L; */ static Dali::TizenPlatform::Network::CurlEnvironment gCurlEnvironment; - void ConfigureCurlOptions( CURL* curlHandle, const std::string& url ) { curl_easy_setopt( curlHandle, CURLOPT_URL, url.c_str() ); curl_easy_setopt( curlHandle, CURLOPT_VERBOSE, VERBOSE_MODE ); // CURLOPT_FAILONERROR is not fail-safe especially when authentication is involved ( see manual ) - curl_easy_setopt( curlHandle, CURLOPT_FAILONERROR, CLOSE_CONNECTION_ON_ERROR ); + // Removed CURLOPT_FAILONERROR option curl_easy_setopt( curlHandle, CURLOPT_CONNECTTIMEOUT, CONNECTION_TIMEOUT_SECONDS ); curl_easy_setopt( curlHandle, CURLOPT_HEADER, INCLUDE_HEADER ); curl_easy_setopt( curlHandle, CURLOPT_NOBODY, EXCLUDE_BODY ); @@ -212,18 +213,74 @@ bool DownloadFile( CURL* curlHandle, namespace Network { +std::mutex* CurlEnvironment::mMutexs = NULL; + CurlEnvironment::CurlEnvironment() { // Must be called before we attempt any loads. e.g. by using curl_easy_init() // and before we start any threads. curl_global_init(CURL_GLOBAL_ALL); + + // libcurl with openssl needs locking_function and thread id for threadsafe + // https://curl.haxx.se/libcurl/c/threadsafe.html + // https://www.openssl.org/docs/man1.0.2/crypto/threads.html#DESCRIPTION + // SetLockingFunction sets locking_function and get thread id by the guide. + SetLockingFunction(); } CurlEnvironment::~CurlEnvironment() { + UnsetLockingFunction(); + curl_global_cleanup(); } +// libcurl with openssl needs locking_function and thread id for threadsafe +// https://curl.haxx.se/libcurl/c/threadsafe.html +// https://www.openssl.org/docs/man1.0.2/crypto/threads.html#DESCRIPTION +void CurlEnvironment::OnOpenSSLLocking( int mode, int n, const char* file, int line ) +{ + if( mode & CRYPTO_LOCK ) + { + mMutexs[n].lock(); + } + else + { + mMutexs[n].unlock(); + } +} + +unsigned long CurlEnvironment::GetThreadId() +{ + // If dali uses c++ thread, we may replace pthread_self() to this_thread::get_id() + return static_cast< unsigned long >( pthread_self() ); +} + +void CurlEnvironment::SetLockingFunction() +{ + if( mMutexs != NULL ) + { + return; + } + + mMutexs = new std::mutex[ CRYPTO_num_locks() ]; + + CRYPTO_set_id_callback( &CurlEnvironment::GetThreadId ); + CRYPTO_set_locking_callback( &CurlEnvironment::OnOpenSSLLocking ); +} + +void CurlEnvironment::UnsetLockingFunction() +{ + if( mMutexs == NULL ) + { + return; + } + + CRYPTO_set_id_callback( NULL ); + CRYPTO_set_locking_callback( NULL ); + delete [] mMutexs; + mMutexs = NULL; +} bool DownloadRemoteFileIntoMemory( const std::string& url, Dali::Vector& dataBuffer,