[dali_2.3.42] Merge branch 'devel/master'
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / common / file-download.cpp
index 6b50a47..de90305 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include <pthread.h>
 #include <cstdlib>
 #include <cstring>
+#include <sstream>
 
 // INTERNAL INCLUDES
+#include <dali/devel-api/adaptor-framework/environment-variable.h>
+#include <dali/internal/system/common/environment-variables.h>
 #include <dali/internal/system/common/file-writer.h>
+#include <dali/public-api/common/dali-common.h>
 
 using namespace Dali::Integration;
 
@@ -36,9 +40,101 @@ namespace TizenPlatform
 {
 namespace // unnamed namespace
 {
+const char* HTTP_PROXY_ENV = "http_proxy";
+
+inline void LogCurlResult(CURLcode result, char* errorBuffer, std::string url, std::string prefix)
+{
+  if(result != CURLE_OK)
+  {
+    if(errorBuffer != nullptr)
+    {
+      DALI_LOG_ERROR("%s \"%s\" with error code %d\n", prefix.c_str(), url.c_str(), result);
+    }
+    else
+    {
+      DALI_LOG_ERROR("%s \"%s\" with error code %d (%s)\n", prefix.c_str(), url.c_str(), result, errorBuffer);
+    }
+  }
+}
+
+std::string ConvertDataReadable(uint8_t* data, const size_t size, const size_t width)
+{
+  std::ostringstream oss;
+
+  for(size_t i = 0u; i < size; ++i)
+  {
+    if(i > 0u && (i % width) == 0u)
+    {
+      oss << '\n';
+    }
+    oss << ((data[i] >= 0x20 && data[i] < 0x80) ? static_cast<char>(data[i]) : '.');
+  }
+
+  return oss.str();
+}
+
+int CurloptVerboseLogTrace(CURL* handle, curl_infotype type, char* data, size_t size, void* clientp)
+{
+  std::ostringstream oss;
+  (void)handle; /* prevent compiler warning */
+  (void)clientp;
+
+  switch(type)
+  {
+    case CURLINFO_TEXT:
+    {
+      oss << "== Info: " << std::string(data, size) << "\n";
+
+      DALI_FALLTHROUGH;
+    }
+    default: /* in case a new one is introduced to shock us */
+    {
+      return 0;
+    }
+
+    case CURLINFO_HEADER_OUT:
+    {
+      oss << "=> Send header\n";
+      break;
+    }
+    case CURLINFO_DATA_OUT:
+    {
+      oss << "=> Send data\n";
+      break;
+    }
+    case CURLINFO_SSL_DATA_OUT:
+    {
+      oss << "=> Send SSL data\n";
+      break;
+    }
+    case CURLINFO_HEADER_IN:
+    {
+      oss << "<= Recv header\n";
+      break;
+    }
+    case CURLINFO_DATA_IN:
+    {
+      oss << "<= Recv data\n";
+      break;
+    }
+    case CURLINFO_SSL_DATA_IN:
+    {
+      oss << "<= Recv SSL data\n";
+      break;
+    }
+  }
+
+  oss << "data size : " << size << " bytes\n";
+  oss << "data : \n";
+  oss << ConvertDataReadable(reinterpret_cast<uint8_t*>(data), size, 0x40);
+
+  DALI_LOG_DEBUG_INFO("Verbose curl log : %s", oss.str().c_str());
+
+  return 0;
+}
+
 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;
 const long INCLUDE_HEADER            = 1L;
@@ -46,6 +142,24 @@ const long INCLUDE_BODY              = 0L;
 const long EXCLUDE_BODY              = 1L;
 
 /**
+ * @brief Get the Curlopt Verbose Mode value from environment.
+ *
+ * @return 0 if verbose mode off. 1 if verbose mode on.
+ */
+long GetCurloptVerboseMode()
+{
+  static long verboseMode       = 0;
+  static bool verboseModeSetted = false;
+  if(DALI_UNLIKELY(!verboseModeSetted))
+  {
+    auto verboseModeString = EnvironmentVariable::GetEnvironmentVariable(DALI_ENV_CURLOPT_VERBOSE_MODE);
+    verboseMode            = verboseModeString ? (std::strtol(verboseModeString, nullptr, 10) > 0 ? 1 : 0) : 0;
+  }
+
+  return verboseMode;
+}
+
+/**
  * Curl library environment. Direct initialize ensures it's constructed before adaptor
  * or application creates any threads.
  */
@@ -53,8 +167,10 @@ static Dali::TizenPlatform::Network::CurlEnvironment gCurlEnvironment;
 
 void ConfigureCurlOptions(CURL* curlHandle, const std::string& url)
 {
+  auto verboseMode = GetCurloptVerboseMode(); // 0 : off, 1 : on
+
   curl_easy_setopt(curlHandle, CURLOPT_URL, url.c_str());
-  curl_easy_setopt(curlHandle, CURLOPT_VERBOSE, VERBOSE_MODE);
+  curl_easy_setopt(curlHandle, CURLOPT_VERBOSE, verboseMode);
 
   // CURLOPT_FAILONERROR is not fail-safe especially when authentication is involved ( see manual )
   // Removed CURLOPT_FAILONERROR option
@@ -66,10 +182,15 @@ void ConfigureCurlOptions(CURL* curlHandle, const std::string& url)
   curl_easy_setopt(curlHandle, CURLOPT_FOLLOWLOCATION, 1L);
   curl_easy_setopt(curlHandle, CURLOPT_MAXREDIRS, 5L);
 
+  if(verboseMode != 0)
+  {
+    curl_easy_setopt(curlHandle, CURLOPT_DEBUGFUNCTION, CurloptVerboseLogTrace);
+  }
+
   // If the proxy variable is set, ensure it's also used.
   // In theory, this variable should be used by the curl library; however, something
   // is overriding it.
-  char* proxy = std::getenv("http_proxy");
+  const char* proxy = Dali::EnvironmentVariable::GetEnvironmentVariable(HTTP_PROXY_ENV);
   if(proxy != nullptr)
   {
     curl_easy_setopt(curlHandle, CURLOPT_PROXY, proxy);
@@ -106,8 +227,11 @@ CURLcode DownloadFileDataWithSize(CURL* curlHandle, Dali::Vector<uint8_t>& dataB
   // create
   Dali::Internal::Platform::FileWriter fileWriter(dataBuffer, dataSize);
   FILE*                                dataBufferFilePointer = fileWriter.GetFile();
+
   if(NULL != dataBufferFilePointer)
   {
+    setbuf(dataBufferFilePointer, NULL); // Turn buffering off
+
     // we only want the body which contains the file data
     curl_easy_setopt(curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER);
     curl_easy_setopt(curlHandle, CURLOPT_NOBODY, INCLUDE_BODY);
@@ -119,6 +243,12 @@ CURLcode DownloadFileDataWithSize(CURL* curlHandle, Dali::Vector<uint8_t>& dataB
     // synchronous request of the body data
     result = curl_easy_perform(curlHandle);
   }
+  else
+  {
+    DALI_LOG_ERROR("Fail to open buffer writter with size : %zu!\n", dataSize);
+    // @todo : Need to check that is it correct error code?
+    result = CURLE_READ_ERROR;
+  }
   return result;
 }
 
@@ -167,7 +297,6 @@ bool DownloadFile(CURL*                  curlHandle,
                   char*                  errorBuffer)
 {
   CURLcode result(CURLE_OK);
-  double   size(0);
 
   // setup curl to download just the header so we can extract the content length
   ConfigureCurlOptions(curlHandle, url);
@@ -194,36 +323,42 @@ bool DownloadFile(CURL*                  curlHandle,
   }
 
   // get the content length, -1 == size is not known
-  curl_easy_getinfo(curlHandle, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &size);
+  curl_off_t size{0};
+  curl_easy_getinfo(curlHandle, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &size);
 
-  if(size >= maximumAllowedSizeBytes)
+  if(size == -1)
+  {
+    result = DownloadFileDataByChunk(curlHandle, dataBuffer, dataSize);
+  }
+  else if(size >= static_cast<curl_off_t>(maximumAllowedSizeBytes))
   {
-    DALI_LOG_ERROR("File content length %f > max allowed %zu \"%s\" \n", size, maximumAllowedSizeBytes, url.c_str());
+    DALI_LOG_ERROR("File content length %" CURL_FORMAT_CURL_OFF_T " > max allowed %zu \"%s\" \n", size, maximumAllowedSizeBytes, url.c_str());
     return false;
   }
-  else if(size > 0)
+  else
   {
     // If we know the size up front, allocate once and avoid chunk copies.
     dataSize = static_cast<size_t>(size);
     result   = DownloadFileDataWithSize(curlHandle, dataBuffer, dataSize);
+    if(result != CURLE_OK)
+    {
+      LogCurlResult(result, errorBuffer, url, "Failed to download file, trying to load by chunk");
+      // In the case where the size is wrong (e.g. on a proxy server that rewrites data),
+      // the data buffer will be corrupt. In this case, try again using the chunk writer.
+      result = DownloadFileDataByChunk(curlHandle, dataBuffer, dataSize);
+    }
   }
-  else
-  {
-    result = DownloadFileDataByChunk(curlHandle, dataBuffer, dataSize);
-  }
+
+  LogCurlResult(result, errorBuffer, url, "Failed to download image file");
 
   if(result != CURLE_OK)
   {
-    if(errorBuffer != nullptr)
-    {
-      DALI_LOG_ERROR("Failed to download image file \"%s\" with error code %d\n", url.c_str(), result);
-    }
-    else
-    {
-      DALI_LOG_ERROR("Failed to download image file \"%s\" with error code %d (%s)\n", url.c_str(), result, errorBuffer);
-    }
     return false;
   }
+  else if(DALI_UNLIKELY(dataSize == 0u))
+  {
+    DALI_LOG_WARNING("Warning : Download data size is 0! url : %s\n", url.c_str());
+  }
   return true;
 }