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