Revert "Revert "Migrate to openssl 1.1""
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / common / file-download.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 5875156..3a5367e
@@ -22,7 +22,6 @@
 #include <dali/integration-api/debug.h>
 #include <pthread.h>
 #include <curl/curl.h>
-#include <openssl/crypto.h>
 #include <cstring>
 
 // INTERNAL INCLUDES
@@ -44,6 +43,7 @@ namespace // unnamed namespace
 {
 
 const int CONNECTION_TIMEOUT_SECONDS( 30L );
+const int TIMEOUT_SECONDS( 120L );
 const long VERBOSE_MODE = 0L;                // 0 == off, 1 == on
 const long CLOSE_CONNECTION_ON_ERROR = 1L;   // 0 == off, 1 == on
 const long EXCLUDE_HEADER = 0L;
@@ -65,6 +65,7 @@ void ConfigureCurlOptions( CURL* curlHandle, const std::string& url )
   // CURLOPT_FAILONERROR is not fail-safe especially when authentication is involved ( see manual )
   // Removed CURLOPT_FAILONERROR option
   curl_easy_setopt( curlHandle, CURLOPT_CONNECTTIMEOUT, CONNECTION_TIMEOUT_SECONDS );
+  curl_easy_setopt( curlHandle, CURLOPT_TIMEOUT, TIMEOUT_SECONDS );
   curl_easy_setopt( curlHandle, CURLOPT_HEADER, INCLUDE_HEADER );
   curl_easy_setopt( curlHandle, CURLOPT_NOBODY, EXCLUDE_BODY );
 
@@ -174,7 +175,7 @@ bool DownloadFile( CURL* curlHandle,
 
   if( result != CURLE_OK)
   {
-    DALI_LOG_WARNING( "Failed to download http header for \"%s\" with error code %d\n", url.c_str(), result );
+    DALI_LOG_ERROR( "Failed to download http header for \"%s\" with error code %d\n", url.c_str(), result );
     return false;
   }
 
@@ -184,7 +185,7 @@ bool DownloadFile( CURL* curlHandle,
 
   if( size >= maximumAllowedSizeBytes )
   {
-    DALI_LOG_WARNING( "File content length %f > max allowed %zu \"%s\" \n", size, maximumAllowedSizeBytes, url.c_str() );
+    DALI_LOG_ERROR( "File content length %f > max allowed %zu \"%s\" \n", size, maximumAllowedSizeBytes, url.c_str() );
     return false;
   }
   else if( size > 0 )
@@ -200,7 +201,7 @@ bool DownloadFile( CURL* curlHandle,
 
   if( result != CURLE_OK )
   {
-    DALI_LOG_WARNING( "Failed to download image file \"%s\" with error code %d\n", url.c_str(), result );
+    DALI_LOG_ERROR( "Failed to download image file \"%s\" with error code %d\n", url.c_str(), result );
     return false;
   }
   return true;
@@ -213,80 +214,25 @@ 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<uint8_t>& dataBuffer,
                                    size_t& dataSize,
                                    size_t maximumAllowedSizeBytes )
 {
+  bool result = false;
+
   if( url.empty() )
   {
     DALI_LOG_WARNING("empty url requested \n");
@@ -297,17 +243,18 @@ bool DownloadRemoteFileIntoMemory( const std::string& url,
   // thread we need to explicity call curl_global_init() on startup from a single thread.
 
   CURL* curlHandle = curl_easy_init();
+  if ( curlHandle )
+  {
+    result = DownloadFile( curlHandle, url, dataBuffer,  dataSize, maximumAllowedSizeBytes);
 
-  bool result = DownloadFile( curlHandle, url, dataBuffer,  dataSize, maximumAllowedSizeBytes);
-
-  // clean up session
-  curl_easy_cleanup( curlHandle );
+    // clean up session
+    curl_easy_cleanup( curlHandle );
 
 #ifdef TPK_CURL_ENABLED
-  // Clean up tpkp(the module for certificate pinning) resources on Tizen
-  tpkp_curl_cleanup();
+    // Clean up tpkp(the module for certificate pinning) resources on Tizen
+    tpkp_curl_cleanup();
 #endif // TPK_CURL_ENABLED
-
+  }
   return result;
 }