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