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