6b50a476367b966667122a2d944a122b4bcc5275
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / common / file-download.cpp
1 /*
2  * Copyright (c) 2022 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
28 // INTERNAL INCLUDES
29 #include <dali/internal/system/common/file-writer.h>
30
31 using namespace Dali::Integration;
32
33 namespace Dali
34 {
35 namespace TizenPlatform
36 {
37 namespace // unnamed namespace
38 {
39 const int  CONNECTION_TIMEOUT_SECONDS(30L);
40 const int  TIMEOUT_SECONDS(120L);
41 const long VERBOSE_MODE              = 0L; // 0 == off, 1 == on
42 const long CLOSE_CONNECTION_ON_ERROR = 1L; // 0 == off, 1 == on
43 const long EXCLUDE_HEADER            = 0L;
44 const long INCLUDE_HEADER            = 1L;
45 const long INCLUDE_BODY              = 0L;
46 const long EXCLUDE_BODY              = 1L;
47
48 /**
49  * Curl library environment. Direct initialize ensures it's constructed before adaptor
50  * or application creates any threads.
51  */
52 static Dali::TizenPlatform::Network::CurlEnvironment gCurlEnvironment;
53
54 void ConfigureCurlOptions(CURL* curlHandle, const std::string& url)
55 {
56   curl_easy_setopt(curlHandle, CURLOPT_URL, url.c_str());
57   curl_easy_setopt(curlHandle, CURLOPT_VERBOSE, VERBOSE_MODE);
58
59   // CURLOPT_FAILONERROR is not fail-safe especially when authentication is involved ( see manual )
60   // Removed CURLOPT_FAILONERROR option
61   curl_easy_setopt(curlHandle, CURLOPT_CONNECTTIMEOUT, CONNECTION_TIMEOUT_SECONDS);
62   curl_easy_setopt(curlHandle, CURLOPT_TIMEOUT, TIMEOUT_SECONDS);
63   curl_easy_setopt(curlHandle, CURLOPT_HEADER, INCLUDE_HEADER);
64   curl_easy_setopt(curlHandle, CURLOPT_NOBODY, EXCLUDE_BODY);
65   curl_easy_setopt(curlHandle, CURLOPT_NOSIGNAL, 1L);
66   curl_easy_setopt(curlHandle, CURLOPT_FOLLOWLOCATION, 1L);
67   curl_easy_setopt(curlHandle, CURLOPT_MAXREDIRS, 5L);
68
69   // If the proxy variable is set, ensure it's also used.
70   // In theory, this variable should be used by the curl library; however, something
71   // is overriding it.
72   char* proxy = std::getenv("http_proxy");
73   if(proxy != nullptr)
74   {
75     curl_easy_setopt(curlHandle, CURLOPT_PROXY, proxy);
76   }
77 }
78
79 // Without a write function or a buffer (file descriptor) to write to, curl will pump out
80 // header/body contents to stdout
81 size_t DummyWrite(char* ptr, size_t size, size_t nmemb, void* userdata)
82 {
83   return size * nmemb;
84 }
85
86 struct ChunkData
87 {
88   std::vector<uint8_t> data;
89 };
90
91 size_t ChunkLoader(char* ptr, size_t size, size_t nmemb, void* userdata)
92 {
93   std::vector<ChunkData>* chunks   = static_cast<std::vector<ChunkData>*>(userdata);
94   int                     numBytes = size * nmemb;
95   chunks->push_back(ChunkData());
96   ChunkData& chunkData = (*chunks)[chunks->size() - 1];
97   chunkData.data.reserve(numBytes);
98   memcpy(&chunkData.data[0], ptr, numBytes);
99   return numBytes;
100 }
101
102 CURLcode DownloadFileDataWithSize(CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t dataSize)
103 {
104   CURLcode result(CURLE_OK);
105
106   // create
107   Dali::Internal::Platform::FileWriter fileWriter(dataBuffer, dataSize);
108   FILE*                                dataBufferFilePointer = fileWriter.GetFile();
109   if(NULL != dataBufferFilePointer)
110   {
111     // we only want the body which contains the file data
112     curl_easy_setopt(curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER);
113     curl_easy_setopt(curlHandle, CURLOPT_NOBODY, INCLUDE_BODY);
114
115     // disable the write callback, and get curl to write directly into our data buffer
116     curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, NULL);
117     curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, dataBufferFilePointer);
118
119     // synchronous request of the body data
120     result = curl_easy_perform(curlHandle);
121   }
122   return result;
123 }
124
125 CURLcode DownloadFileDataByChunk(CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t& dataSize)
126 {
127   // create
128   std::vector<ChunkData> chunks;
129
130   // we only want the body which contains the file data
131   curl_easy_setopt(curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER);
132   curl_easy_setopt(curlHandle, CURLOPT_NOBODY, INCLUDE_BODY);
133
134   // Enable the write callback.
135   curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, ChunkLoader);
136   curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, &chunks);
137
138   // synchronous request of the body data
139   CURLcode result = curl_easy_perform(curlHandle);
140
141   // chunks should now contain all of the chunked data. Reassemble into a single vector
142   dataSize = 0;
143   for(size_t i = 0; i < chunks.size(); ++i)
144   {
145     dataSize += chunks[i].data.capacity();
146   }
147   dataBuffer.ResizeUninitialized(dataSize);
148
149   if(DALI_LIKELY(dataSize > 0))
150   {
151     std::uint8_t* dataBufferPtr = dataBuffer.Begin();
152     for(size_t i = 0; i < chunks.size(); ++i)
153     {
154       memcpy(dataBufferPtr, &chunks[i].data[0], chunks[i].data.capacity());
155       dataBufferPtr += chunks[i].data.capacity();
156     }
157   }
158
159   return result;
160 }
161
162 bool DownloadFile(CURL*                  curlHandle,
163                   const std::string&     url,
164                   Dali::Vector<uint8_t>& dataBuffer,
165                   size_t&                dataSize,
166                   size_t                 maximumAllowedSizeBytes,
167                   char*                  errorBuffer)
168 {
169   CURLcode result(CURLE_OK);
170   double   size(0);
171
172   // setup curl to download just the header so we can extract the content length
173   ConfigureCurlOptions(curlHandle, url);
174   curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, DummyWrite);
175   if(errorBuffer != nullptr)
176   {
177     errorBuffer[0] = 0;
178   }
179
180   // perform the request to get the header
181   result = curl_easy_perform(curlHandle);
182
183   if(result != CURLE_OK)
184   {
185     if(errorBuffer != nullptr)
186     {
187       DALI_LOG_ERROR("Failed to download http header for \"%s\" with error code %d (%s)\n", url.c_str(), result, &errorBuffer[0]);
188     }
189     else
190     {
191       DALI_LOG_ERROR("Failed to download http header for \"%s\" with error code %d\n", url.c_str(), result);
192     }
193     return false;
194   }
195
196   // get the content length, -1 == size is not known
197   curl_easy_getinfo(curlHandle, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &size);
198
199   if(size >= maximumAllowedSizeBytes)
200   {
201     DALI_LOG_ERROR("File content length %f > max allowed %zu \"%s\" \n", size, maximumAllowedSizeBytes, url.c_str());
202     return false;
203   }
204   else if(size > 0)
205   {
206     // If we know the size up front, allocate once and avoid chunk copies.
207     dataSize = static_cast<size_t>(size);
208     result   = DownloadFileDataWithSize(curlHandle, dataBuffer, dataSize);
209   }
210   else
211   {
212     result = DownloadFileDataByChunk(curlHandle, dataBuffer, dataSize);
213   }
214
215   if(result != CURLE_OK)
216   {
217     if(errorBuffer != nullptr)
218     {
219       DALI_LOG_ERROR("Failed to download image file \"%s\" with error code %d\n", url.c_str(), result);
220     }
221     else
222     {
223       DALI_LOG_ERROR("Failed to download image file \"%s\" with error code %d (%s)\n", url.c_str(), result, errorBuffer);
224     }
225     return false;
226   }
227   return true;
228 }
229
230 } // unnamed namespace
231
232 namespace Network
233 {
234 CurlEnvironment::CurlEnvironment()
235 {
236   // Must be called before we attempt any loads. e.g. by using curl_easy_init()
237   // and before we start any threads.
238   curl_global_init(CURL_GLOBAL_ALL);
239 }
240
241 CurlEnvironment::~CurlEnvironment()
242 {
243   curl_global_cleanup();
244 }
245
246 bool DownloadRemoteFileIntoMemory(const std::string&     url,
247                                   Dali::Vector<uint8_t>& dataBuffer,
248                                   size_t&                dataSize,
249                                   size_t                 maximumAllowedSizeBytes)
250 {
251   bool result = false;
252
253   if(url.empty())
254   {
255     DALI_LOG_WARNING("empty url requested \n");
256     return false;
257   }
258
259   // start a libcurl easy session, this internally calls curl_global_init, if we ever have more than one download
260   // thread we need to explicity call curl_global_init() on startup from a single thread.
261
262   CURL* curlHandle = curl_easy_init();
263   if(curlHandle)
264   {
265     std::vector<char> errorBuffer(CURL_ERROR_SIZE);
266     curl_easy_setopt(curlHandle, CURLOPT_ERRORBUFFER, &errorBuffer[0]);
267     result = DownloadFile(curlHandle, url, dataBuffer, dataSize, maximumAllowedSizeBytes, &errorBuffer[0]);
268
269     // clean up session
270     curl_easy_cleanup(curlHandle);
271   }
272   return result;
273 }
274
275 } // namespace Network
276
277 } // namespace TizenPlatform
278
279 } // namespace Dali