[dali_2.3.23] Merge branch 'devel/master'
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / common / file-download.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // HEADER
19 #include <dali/internal/imaging/common/file-download.h>
20
21 // EXTERNAL INCLUDES
22 #include <curl/curl.h>
23 #include <dali/integration-api/debug.h>
24 #include <pthread.h>
25 #include <cstdlib>
26 #include <cstring>
27 #include <sstream>
28
29 // INTERNAL INCLUDES
30 #include <dali/devel-api/adaptor-framework/environment-variable.h>
31 #include <dali/internal/system/common/environment-variables.h>
32 #include <dali/internal/system/common/file-writer.h>
33 #include <dali/public-api/common/dali-common.h>
34
35 using namespace Dali::Integration;
36
37 namespace Dali
38 {
39 namespace TizenPlatform
40 {
41 namespace // unnamed namespace
42 {
43 inline void LogCurlResult(CURLcode result, char* errorBuffer, std::string url, std::string prefix)
44 {
45   if(result != CURLE_OK)
46   {
47     if(errorBuffer != nullptr)
48     {
49       DALI_LOG_ERROR("%s \"%s\" with error code %d\n", prefix.c_str(), url.c_str(), result);
50     }
51     else
52     {
53       DALI_LOG_ERROR("%s \"%s\" with error code %d (%s)\n", prefix.c_str(), url.c_str(), result, errorBuffer);
54     }
55   }
56 }
57
58 std::string ConvertDataReadable(uint8_t* data, const size_t size, const size_t width)
59 {
60   std::ostringstream oss;
61
62   for(size_t i = 0u; i < size; ++i)
63   {
64     if(i > 0u && (i % width) == 0u)
65     {
66       oss << '\n';
67     }
68     oss << ((data[i] >= 0x20 && data[i] < 0x80) ? static_cast<char>(data[i]) : '.');
69   }
70
71   return oss.str();
72 }
73
74 int CurloptVerboseLogTrace(CURL* handle, curl_infotype type, char* data, size_t size, void* clientp)
75 {
76   std::ostringstream oss;
77   (void)handle; /* prevent compiler warning */
78   (void)clientp;
79
80   switch(type)
81   {
82     case CURLINFO_TEXT:
83     {
84       oss << "== Info: " << std::string(data, size) << "\n";
85
86       DALI_FALLTHROUGH;
87     }
88     default: /* in case a new one is introduced to shock us */
89     {
90       return 0;
91     }
92
93     case CURLINFO_HEADER_OUT:
94     {
95       oss << "=> Send header\n";
96       break;
97     }
98     case CURLINFO_DATA_OUT:
99     {
100       oss << "=> Send data\n";
101       break;
102     }
103     case CURLINFO_SSL_DATA_OUT:
104     {
105       oss << "=> Send SSL data\n";
106       break;
107     }
108     case CURLINFO_HEADER_IN:
109     {
110       oss << "<= Recv header\n";
111       break;
112     }
113     case CURLINFO_DATA_IN:
114     {
115       oss << "<= Recv data\n";
116       break;
117     }
118     case CURLINFO_SSL_DATA_IN:
119     {
120       oss << "<= Recv SSL data\n";
121       break;
122     }
123   }
124
125   oss << "data size : " << size << " bytes\n";
126   oss << "data : \n";
127   oss << ConvertDataReadable(reinterpret_cast<uint8_t*>(data), size, 0x40);
128
129   DALI_LOG_DEBUG_INFO("Verbose curl log : %s", oss.str().c_str());
130
131   return 0;
132 }
133
134 const int  CONNECTION_TIMEOUT_SECONDS(30L);
135 const int  TIMEOUT_SECONDS(120L);
136 const long CLOSE_CONNECTION_ON_ERROR = 1L; // 0 == off, 1 == on
137 const long EXCLUDE_HEADER            = 0L;
138 const long INCLUDE_HEADER            = 1L;
139 const long INCLUDE_BODY              = 0L;
140 const long EXCLUDE_BODY              = 1L;
141
142 /**
143  * @brief Get the Curlopt Verbose Mode value from environment.
144  *
145  * @return 0 if verbose mode off. 1 if verbose mode on.
146  */
147 long GetCurloptVerboseMode()
148 {
149   static long verboseMode       = 0;
150   static bool verboseModeSetted = false;
151   if(DALI_UNLIKELY(!verboseModeSetted))
152   {
153     auto verboseModeString = EnvironmentVariable::GetEnvironmentVariable(DALI_ENV_CURLOPT_VERBOSE_MODE);
154     verboseMode            = verboseModeString ? (std::strtol(verboseModeString, nullptr, 10) > 0 ? 1 : 0) : 0;
155   }
156
157   return verboseMode;
158 }
159
160 /**
161  * Curl library environment. Direct initialize ensures it's constructed before adaptor
162  * or application creates any threads.
163  */
164 static Dali::TizenPlatform::Network::CurlEnvironment gCurlEnvironment;
165
166 void ConfigureCurlOptions(CURL* curlHandle, const std::string& url)
167 {
168   auto verboseMode = GetCurloptVerboseMode(); // 0 : off, 1 : on
169
170   curl_easy_setopt(curlHandle, CURLOPT_URL, url.c_str());
171   curl_easy_setopt(curlHandle, CURLOPT_VERBOSE, verboseMode);
172
173   // CURLOPT_FAILONERROR is not fail-safe especially when authentication is involved ( see manual )
174   // Removed CURLOPT_FAILONERROR option
175   curl_easy_setopt(curlHandle, CURLOPT_CONNECTTIMEOUT, CONNECTION_TIMEOUT_SECONDS);
176   curl_easy_setopt(curlHandle, CURLOPT_TIMEOUT, TIMEOUT_SECONDS);
177   curl_easy_setopt(curlHandle, CURLOPT_HEADER, INCLUDE_HEADER);
178   curl_easy_setopt(curlHandle, CURLOPT_NOBODY, EXCLUDE_BODY);
179   curl_easy_setopt(curlHandle, CURLOPT_NOSIGNAL, 1L);
180   curl_easy_setopt(curlHandle, CURLOPT_FOLLOWLOCATION, 1L);
181   curl_easy_setopt(curlHandle, CURLOPT_MAXREDIRS, 5L);
182
183   if(verboseMode != 0)
184   {
185     curl_easy_setopt(curlHandle, CURLOPT_DEBUGFUNCTION, CurloptVerboseLogTrace);
186   }
187
188   // If the proxy variable is set, ensure it's also used.
189   // In theory, this variable should be used by the curl library; however, something
190   // is overriding it.
191   char* proxy = std::getenv("http_proxy");
192   if(proxy != nullptr)
193   {
194     curl_easy_setopt(curlHandle, CURLOPT_PROXY, proxy);
195   }
196 }
197
198 // Without a write function or a buffer (file descriptor) to write to, curl will pump out
199 // header/body contents to stdout
200 size_t DummyWrite(char* ptr, size_t size, size_t nmemb, void* userdata)
201 {
202   return size * nmemb;
203 }
204
205 struct ChunkData
206 {
207   std::vector<uint8_t> data;
208 };
209
210 size_t ChunkLoader(char* ptr, size_t size, size_t nmemb, void* userdata)
211 {
212   std::vector<ChunkData>* chunks   = static_cast<std::vector<ChunkData>*>(userdata);
213   int                     numBytes = size * nmemb;
214   chunks->push_back(ChunkData());
215   ChunkData& chunkData = (*chunks)[chunks->size() - 1];
216   chunkData.data.reserve(numBytes);
217   memcpy(&chunkData.data[0], ptr, numBytes);
218   return numBytes;
219 }
220
221 CURLcode DownloadFileDataWithSize(CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t dataSize)
222 {
223   CURLcode result(CURLE_OK);
224
225   // create
226   Dali::Internal::Platform::FileWriter fileWriter(dataBuffer, dataSize);
227   FILE*                                dataBufferFilePointer = fileWriter.GetFile();
228
229   if(NULL != dataBufferFilePointer)
230   {
231     setbuf(dataBufferFilePointer, NULL); // Turn buffering off
232
233     // we only want the body which contains the file data
234     curl_easy_setopt(curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER);
235     curl_easy_setopt(curlHandle, CURLOPT_NOBODY, INCLUDE_BODY);
236
237     // disable the write callback, and get curl to write directly into our data buffer
238     curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, NULL);
239     curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, dataBufferFilePointer);
240
241     // synchronous request of the body data
242     result = curl_easy_perform(curlHandle);
243   }
244   else
245   {
246     DALI_LOG_ERROR("Fail to open buffer writter with size : %zu!\n", dataSize);
247     // @todo : Need to check that is it correct error code?
248     result = CURLE_READ_ERROR;
249   }
250   return result;
251 }
252
253 CURLcode DownloadFileDataByChunk(CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t& dataSize)
254 {
255   // create
256   std::vector<ChunkData> chunks;
257
258   // we only want the body which contains the file data
259   curl_easy_setopt(curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER);
260   curl_easy_setopt(curlHandle, CURLOPT_NOBODY, INCLUDE_BODY);
261
262   // Enable the write callback.
263   curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, ChunkLoader);
264   curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, &chunks);
265
266   // synchronous request of the body data
267   CURLcode result = curl_easy_perform(curlHandle);
268
269   // chunks should now contain all of the chunked data. Reassemble into a single vector
270   dataSize = 0;
271   for(size_t i = 0; i < chunks.size(); ++i)
272   {
273     dataSize += chunks[i].data.capacity();
274   }
275   dataBuffer.ResizeUninitialized(dataSize);
276
277   if(DALI_LIKELY(dataSize > 0))
278   {
279     std::uint8_t* dataBufferPtr = dataBuffer.Begin();
280     for(size_t i = 0; i < chunks.size(); ++i)
281     {
282       memcpy(dataBufferPtr, &chunks[i].data[0], chunks[i].data.capacity());
283       dataBufferPtr += chunks[i].data.capacity();
284     }
285   }
286
287   return result;
288 }
289
290 bool DownloadFile(CURL*                  curlHandle,
291                   const std::string&     url,
292                   Dali::Vector<uint8_t>& dataBuffer,
293                   size_t&                dataSize,
294                   size_t                 maximumAllowedSizeBytes,
295                   char*                  errorBuffer)
296 {
297   CURLcode result(CURLE_OK);
298
299   // setup curl to download just the header so we can extract the content length
300   ConfigureCurlOptions(curlHandle, url);
301   curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, DummyWrite);
302   if(errorBuffer != nullptr)
303   {
304     errorBuffer[0] = 0;
305   }
306
307   // perform the request to get the header
308   result = curl_easy_perform(curlHandle);
309
310   if(result != CURLE_OK)
311   {
312     if(errorBuffer != nullptr)
313     {
314       DALI_LOG_ERROR("Failed to download http header for \"%s\" with error code %d (%s)\n", url.c_str(), result, &errorBuffer[0]);
315     }
316     else
317     {
318       DALI_LOG_ERROR("Failed to download http header for \"%s\" with error code %d\n", url.c_str(), result);
319     }
320     return false;
321   }
322
323   // get the content length, -1 == size is not known
324   curl_off_t size{0};
325   curl_easy_getinfo(curlHandle, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &size);
326
327   if(size == -1)
328   {
329     result = DownloadFileDataByChunk(curlHandle, dataBuffer, dataSize);
330   }
331   else if(size >= static_cast<curl_off_t>(maximumAllowedSizeBytes))
332   {
333     DALI_LOG_ERROR("File content length %" CURL_FORMAT_CURL_OFF_T " > max allowed %zu \"%s\" \n", size, maximumAllowedSizeBytes, url.c_str());
334     return false;
335   }
336   else
337   {
338     // If we know the size up front, allocate once and avoid chunk copies.
339     dataSize = static_cast<size_t>(size);
340     result   = DownloadFileDataWithSize(curlHandle, dataBuffer, dataSize);
341     if(result != CURLE_OK)
342     {
343       LogCurlResult(result, errorBuffer, url, "Failed to download file, trying to load by chunk");
344       // In the case where the size is wrong (e.g. on a proxy server that rewrites data),
345       // the data buffer will be corrupt. In this case, try again using the chunk writer.
346       result = DownloadFileDataByChunk(curlHandle, dataBuffer, dataSize);
347     }
348   }
349
350   LogCurlResult(result, errorBuffer, url, "Failed to download image file");
351
352   if(result != CURLE_OK)
353   {
354     return false;
355   }
356   else if(DALI_UNLIKELY(dataSize == 0u))
357   {
358     DALI_LOG_WARNING("Warning : Download data size is 0! url : %s\n", url.c_str());
359   }
360   return true;
361 }
362
363 } // unnamed namespace
364
365 namespace Network
366 {
367 CurlEnvironment::CurlEnvironment()
368 {
369   // Must be called before we attempt any loads. e.g. by using curl_easy_init()
370   // and before we start any threads.
371   curl_global_init(CURL_GLOBAL_ALL);
372 }
373
374 CurlEnvironment::~CurlEnvironment()
375 {
376   curl_global_cleanup();
377 }
378
379 bool DownloadRemoteFileIntoMemory(const std::string&     url,
380                                   Dali::Vector<uint8_t>& dataBuffer,
381                                   size_t&                dataSize,
382                                   size_t                 maximumAllowedSizeBytes)
383 {
384   bool result = false;
385
386   if(url.empty())
387   {
388     DALI_LOG_WARNING("empty url requested \n");
389     return false;
390   }
391
392   // start a libcurl easy session, this internally calls curl_global_init, if we ever have more than one download
393   // thread we need to explicity call curl_global_init() on startup from a single thread.
394
395   CURL* curlHandle = curl_easy_init();
396   if(curlHandle)
397   {
398     std::vector<char> errorBuffer(CURL_ERROR_SIZE);
399     curl_easy_setopt(curlHandle, CURLOPT_ERRORBUFFER, &errorBuffer[0]);
400     result = DownloadFile(curlHandle, url, dataBuffer, dataSize, maximumAllowedSizeBytes, &errorBuffer[0]);
401
402     // clean up session
403     curl_easy_cleanup(curlHandle);
404   }
405   return result;
406 }
407
408 } // namespace Network
409
410 } // namespace TizenPlatform
411
412 } // namespace Dali