a6ab471d74bbfb860f7659631cfc780dcb26753e
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / windows / file-download-win.cpp
1 /*
2  * Copyright (c) 2018 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 <dali/integration-api/debug.h>
23 #include <pthread.h>
24 #include <openssl/crypto.h>
25 #include <cstring>
26 #include <curl/curl.h>
27 #include <../ExInclude/InternalFileOperation.h>
28
29 // INTERNAL INCLUDES
30 #include <dali/internal/system/common/file-writer.h>
31
32 #ifdef TPK_CURL_ENABLED
33 #include <tpkp_curl.h>
34 #endif // TPK_CURL_ENABLED
35
36 using namespace Dali::Integration;
37
38 namespace Dali
39 {
40
41 namespace TizenPlatform
42 {
43
44 namespace // unnamed namespace
45 {
46
47 const int CONNECTION_TIMEOUT_SECONDS( 30L );
48 const long VERBOSE_MODE = 0L;                // 0 == off, 1 == on
49 const long CLOSE_CONNECTION_ON_ERROR = 1L;   // 0 == off, 1 == on
50 const long EXCLUDE_HEADER = 0L;
51 const long INCLUDE_HEADER = 1L;
52 const long INCLUDE_BODY = 0L;
53 const long EXCLUDE_BODY = 1L;
54
55 /**
56  * Curl library environment. Direct initialize ensures it's constructed before adaptor
57  * or application creates any threads.
58  */
59 static Dali::TizenPlatform::Network::CurlEnvironment gCurlEnvironment;
60
61 // Without a write function or a buffer (file descriptor) to write to, curl will pump out
62 // header/body contents to stdout
63 size_t __cdecl DummyWrite(char *ptr, size_t size, size_t nmemb, void *userdata)
64 {
65   return size * nmemb;
66 }
67
68 struct ChunkData
69 {
70   std::vector< uint8_t > data;
71 };
72
73 size_t __cdecl ChunkLoader(char *ptr, size_t size, size_t nmemb, void *userdata)
74 {
75   std::vector<ChunkData>* chunks = static_cast<std::vector<ChunkData>*>( userdata );
76   int numBytes = size*nmemb;
77   if( chunks != nullptr )
78   {
79     chunks->push_back( ChunkData() );
80     ChunkData& chunkData = (*chunks)[chunks->size()-1];
81     chunkData.data.reserve( numBytes );
82     memcpy( chunkData.data.data(), ptr, numBytes );
83   }
84   return numBytes;
85 }
86
87 static size_t __cdecl WriteFunction( void *input, size_t uSize, size_t uCount, void *avg )
88 {
89   fwrite( input, uSize, uCount, (FILE*)avg );
90   return uSize * uCount;
91 }
92
93 void InitWriteFunction( void* curlHandle )
94 {
95   curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, WriteFunction );
96 }
97
98 CURLcode DownloadFileDataWithSize( CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t dataSize )
99 {
100   CURLcode result( CURLE_OK );
101
102   // create
103   Dali::Internal::Platform::FileWriter fileWriter( dataBuffer, dataSize );
104   FILE* dataBufferFilePointer = fileWriter.GetFile();
105   if( nullptr != dataBufferFilePointer )
106   {
107     // we only want the body which contains the file data
108     curl_easy_setopt( curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER );
109     curl_easy_setopt( curlHandle, CURLOPT_NOBODY, INCLUDE_BODY );
110
111     // disable the write callback, and get curl to write directly into our data buffer
112     InitWriteFunction( curlHandle );
113
114     curl_easy_setopt( curlHandle, CURLOPT_WRITEDATA, dataBufferFilePointer );
115
116     // synchronous request of the body data
117     result = curl_easy_perform( curlHandle );
118   }
119   return result;
120 }
121
122 CURLcode DownloadFileDataByChunk( CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t& dataSize )
123 {
124   // create
125   std::vector< ChunkData > chunks;
126
127   // we only want the body which contains the file data
128   curl_easy_setopt( curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER );
129   curl_easy_setopt( curlHandle, CURLOPT_NOBODY, INCLUDE_BODY );
130
131   // Enable the write callback.
132   curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, ChunkLoader );
133   curl_easy_setopt( curlHandle, CURLOPT_WRITEDATA, &chunks );
134
135   // synchronous request of the body data
136   CURLcode result = curl_easy_perform( curlHandle );
137
138   // chunks should now contain all of the chunked data. Reassemble into a single vector
139   dataSize = 0;
140   for( size_t i=0; i<chunks.size() ; ++i )
141   {
142     dataSize += chunks[i].data.capacity();
143   }
144   dataBuffer.Resize(dataSize);
145
146   size_t offset = 0;
147   for( size_t i=0; i<chunks.size() ; ++i )
148   {
149     memcpy( &dataBuffer[offset], chunks[i].data.data(), chunks[i].data.capacity() ); 
150     offset += chunks[i].data.capacity();
151   }
152
153   return result;
154 }
155
156 void ConfigureCurlOptions( void* curlHandle, const std::string& url )
157 {
158   curl_easy_setopt( curlHandle, CURLOPT_URL, url.c_str() );
159   //curl_easy_setopt( curlHandle, CURLOPT_VERBOSE, VERBOSE_MODE );
160   curl_easy_setopt( curlHandle, CURLOPT_PROXY, "109.123.100.31:3128" );
161
162   // CURLOPT_FAILONERROR is not fail-safe especially when authentication is involved ( see manual )
163   // Removed CURLOPT_FAILONERROR option
164   curl_easy_setopt( curlHandle, CURLOPT_CONNECTTIMEOUT, CONNECTION_TIMEOUT_SECONDS );
165   curl_easy_setopt( curlHandle, CURLOPT_HEADER, INCLUDE_HEADER );
166   curl_easy_setopt( curlHandle, CURLOPT_NOBODY, EXCLUDE_BODY );
167 }
168
169 bool DownloadFile( CURL* curlHandle,
170                    const std::string& url,
171                    Dali::Vector<uint8_t>& dataBuffer,
172                    size_t& dataSize,
173                    size_t maximumAllowedSizeBytes )
174 {
175   CURLcode result( CURLE_OK );
176   double size(0);
177
178   // setup curl to download just the header so we can extract the content length
179   ConfigureCurlOptions( curlHandle, url );
180
181   curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, DummyWrite);
182
183   // perform the request to get the header
184   result = curl_easy_perform( curlHandle );
185
186   if( result != CURLE_OK)
187   {
188     DALI_LOG_ERROR( "Failed to download http header for \"%s\" with error code %d\n", url.c_str(), result );
189     return false;
190   }
191
192   // get the content length, -1 == size is not known
193   curl_easy_getinfo( curlHandle,CURLINFO_CONTENT_LENGTH_DOWNLOAD , &size );
194
195
196   if( size >= maximumAllowedSizeBytes )
197   {
198     DALI_LOG_ERROR( "File content length %f > max allowed %zu \"%s\" \n", size, maximumAllowedSizeBytes, url.c_str() );
199     return false;
200   }
201   else if( size > 0 )
202   {
203     // If we know the size up front, allocate once and avoid chunk copies.
204     dataSize = static_cast<size_t>( size );
205     result = DownloadFileDataWithSize( curlHandle, dataBuffer, dataSize );
206   }
207   else
208   {
209     result = DownloadFileDataByChunk( curlHandle, dataBuffer, dataSize );
210   }
211
212   if( result != CURLE_OK )
213   {
214     DALI_LOG_ERROR( "Failed to download image file \"%s\" with error code %d\n", url.c_str(), result );
215     return false;
216   }
217   return true;
218 }
219
220
221 } // unnamed namespace
222
223
224 namespace Network
225 {
226
227 std::mutex* CurlEnvironment::mMutexs = NULL;
228
229 CurlEnvironment::CurlEnvironment()
230 {
231   // Must be called before we attempt any loads. e.g. by using curl_easy_init()
232   // and before we start any threads.
233   curl_global_init(CURL_GLOBAL_ALL);
234
235  // libcurl with openssl needs locking_function and thread id for threadsafe
236  // https://curl.haxx.se/libcurl/c/threadsafe.html
237  // https://www.openssl.org/docs/man1.0.2/crypto/threads.html#DESCRIPTION
238  // SetLockingFunction sets locking_function and get thread id by the guide.
239   SetLockingFunction();
240 }
241
242 CurlEnvironment::~CurlEnvironment()
243 {
244   UnsetLockingFunction();
245
246   curl_global_cleanup();
247 }
248
249 // libcurl with openssl needs locking_function and thread id for threadsafe
250 // https://curl.haxx.se/libcurl/c/threadsafe.html
251 // https://www.openssl.org/docs/man1.0.2/crypto/threads.html#DESCRIPTION
252 void CurlEnvironment::OnOpenSSLLocking( int mode, int n, const char* file, int line )
253 {
254   if( mode & CRYPTO_LOCK )
255   {
256     mMutexs[n].lock();
257   }
258   else
259   {
260     mMutexs[n].unlock();
261   }
262 }
263
264 void CurlEnvironment::SetLockingFunction()
265 {
266   if( mMutexs != NULL )
267   {
268     return;
269   }
270
271   mMutexs = new std::mutex[ CRYPTO_num_locks() ];
272
273   CRYPTO_set_id_callback( &CurlEnvironment::GetThreadId );
274   CRYPTO_set_locking_callback( &CurlEnvironment::OnOpenSSLLocking );
275 }
276
277 void CurlEnvironment::UnsetLockingFunction()
278 {
279   if( mMutexs == NULL )
280   {
281     return;
282   }
283
284   CRYPTO_set_id_callback( NULL );
285   CRYPTO_set_locking_callback( NULL );
286
287   delete [] mMutexs;
288   mMutexs = NULL;
289 }
290
291 bool DownloadRemoteFileIntoMemory( const std::string& url,
292                                    Dali::Vector<uint8_t>& dataBuffer,
293                                    size_t& dataSize,
294                                    size_t maximumAllowedSizeBytes )
295 {
296   if( url.empty() )
297   {
298     DALI_LOG_WARNING("empty url requested \n");
299     return false;
300   }
301
302   // start a libcurl easy session, this internally calls curl_global_init, if we ever have more than one download
303   // thread we need to explicity call curl_global_init() on startup from a single thread.
304
305   CURL* curlHandle = curl_easy_init();
306
307   bool result = DownloadFile( curlHandle, url, dataBuffer,  dataSize, maximumAllowedSizeBytes);
308
309   // clean up session
310   curl_easy_cleanup( curlHandle );
311
312 #ifdef TPK_CURL_ENABLED
313   // Clean up tpkp(the module for certificate pinning) resources on Tizen
314   tpkp_curl_cleanup();
315 #endif // TPK_CURL_ENABLED
316
317   return result;
318 }
319
320 } // namespace Network
321
322 } // namespace TizenPlatform
323
324 } // namespace Dali