2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/imaging/common/file-download.h>
22 #include <dali/integration-api/debug.h>
24 #include <curl/curl.h>
28 #include <dali/internal/system/common/file-writer.h>
30 #ifdef TPK_CURL_ENABLED
31 #include <tpkp_curl.h>
32 #endif // TPK_CURL_ENABLED
34 using namespace Dali::Integration;
39 namespace TizenPlatform
42 namespace // unnamed namespace
45 const int CONNECTION_TIMEOUT_SECONDS( 30L );
46 const int TIMEOUT_SECONDS( 120L );
47 const long VERBOSE_MODE = 0L; // 0 == off, 1 == on
48 const long CLOSE_CONNECTION_ON_ERROR = 1L; // 0 == off, 1 == on
49 const long EXCLUDE_HEADER = 0L;
50 const long INCLUDE_HEADER = 1L;
51 const long INCLUDE_BODY = 0L;
52 const long EXCLUDE_BODY = 1L;
55 * Curl library environment. Direct initialize ensures it's constructed before adaptor
56 * or application creates any threads.
58 static Dali::TizenPlatform::Network::CurlEnvironment gCurlEnvironment;
60 void ConfigureCurlOptions( CURL* curlHandle, const std::string& url )
62 curl_easy_setopt( curlHandle, CURLOPT_URL, url.c_str() );
63 curl_easy_setopt( curlHandle, CURLOPT_VERBOSE, VERBOSE_MODE );
65 // CURLOPT_FAILONERROR is not fail-safe especially when authentication is involved ( see manual )
66 // Removed CURLOPT_FAILONERROR option
67 curl_easy_setopt( curlHandle, CURLOPT_CONNECTTIMEOUT, CONNECTION_TIMEOUT_SECONDS );
68 curl_easy_setopt( curlHandle, CURLOPT_TIMEOUT, TIMEOUT_SECONDS );
69 curl_easy_setopt( curlHandle, CURLOPT_HEADER, INCLUDE_HEADER );
70 curl_easy_setopt( curlHandle, CURLOPT_NOBODY, EXCLUDE_BODY );
72 #ifdef TPK_CURL_ENABLED
73 // Apply certificate pinning on Tizen
74 curl_easy_setopt( curlHandle, CURLOPT_SSL_CTX_FUNCTION, tpkp_curl_ssl_ctx_callback );
75 #endif // TPK_CURL_ENABLED
78 // Without a write function or a buffer (file descriptor) to write to, curl will pump out
79 // header/body contents to stdout
80 size_t DummyWrite(char *ptr, size_t size, size_t nmemb, void *userdata)
87 std::vector< uint8_t > data;
90 size_t ChunkLoader(char *ptr, size_t size, size_t nmemb, void *userdata)
92 std::vector<ChunkData>* chunks = static_cast<std::vector<ChunkData>*>( userdata );
93 int numBytes = size*nmemb;
94 chunks->push_back( ChunkData() );
95 ChunkData& chunkData = (*chunks)[chunks->size()-1];
96 chunkData.data.reserve( numBytes );
97 memcpy( &chunkData.data[0], ptr, numBytes );
102 CURLcode DownloadFileDataWithSize( CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t dataSize )
104 CURLcode result( CURLE_OK );
107 Dali::Internal::Platform::FileWriter fileWriter( dataBuffer, dataSize );
108 FILE* dataBufferFilePointer = fileWriter.GetFile();
109 if( NULL != dataBufferFilePointer )
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 );
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 );
119 // synchronous request of the body data
120 result = curl_easy_perform( curlHandle );
125 CURLcode DownloadFileDataByChunk( CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t& dataSize )
128 std::vector< ChunkData > chunks;
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 );
134 // Enable the write callback.
135 curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, ChunkLoader );
136 curl_easy_setopt( curlHandle, CURLOPT_WRITEDATA, &chunks );
138 // synchronous request of the body data
139 CURLcode result = curl_easy_perform( curlHandle );
141 // chunks should now contain all of the chunked data. Reassemble into a single vector
143 for( size_t i=0; i<chunks.size() ; ++i )
145 dataSize += chunks[i].data.capacity();
147 dataBuffer.Resize(dataSize);
150 for( size_t i=0; i<chunks.size() ; ++i )
152 memcpy( &dataBuffer[offset], &chunks[i].data[0], chunks[i].data.capacity() );
153 offset += chunks[i].data.capacity();
159 bool DownloadFile( CURL* curlHandle,
160 const std::string& url,
161 Dali::Vector<uint8_t>& dataBuffer,
163 size_t maximumAllowedSizeBytes )
165 CURLcode result( CURLE_OK );
168 // setup curl to download just the header so we can extract the content length
169 ConfigureCurlOptions( curlHandle, url );
171 curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, DummyWrite);
173 // perform the request to get the header
174 result = curl_easy_perform( curlHandle );
176 if( result != CURLE_OK)
178 DALI_LOG_ERROR( "Failed to download http header for \"%s\" with error code %d\n", url.c_str(), result );
182 // get the content length, -1 == size is not known
183 curl_easy_getinfo( curlHandle,CURLINFO_CONTENT_LENGTH_DOWNLOAD , &size );
186 if( size >= maximumAllowedSizeBytes )
188 DALI_LOG_ERROR( "File content length %f > max allowed %zu \"%s\" \n", size, maximumAllowedSizeBytes, url.c_str() );
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 );
199 result = DownloadFileDataByChunk( curlHandle, dataBuffer, dataSize );
202 if( result != CURLE_OK )
204 DALI_LOG_ERROR( "Failed to download image file \"%s\" with error code %d\n", url.c_str(), result );
211 } // unnamed namespace
217 std::mutex* CurlEnvironment::mMutexs = NULL;
219 CurlEnvironment::CurlEnvironment()
221 // Must be called before we attempt any loads. e.g. by using curl_easy_init()
222 // and before we start any threads.
223 curl_global_init(CURL_GLOBAL_ALL);
225 // libcurl with openssl needs locking_function and thread id for threadsafe
226 // https://curl.haxx.se/libcurl/c/threadsafe.html
227 // https://www.openssl.org/docs/man1.0.2/crypto/threads.html#DESCRIPTION
228 // SetLockingFunction sets locking_function and get thread id by the guide.
229 SetLockingFunction();
232 CurlEnvironment::~CurlEnvironment()
234 UnsetLockingFunction();
236 curl_global_cleanup();
239 // libcurl with openssl needs locking_function and thread id for threadsafe
240 // https://curl.haxx.se/libcurl/c/threadsafe.html
241 // https://www.openssl.org/docs/man1.0.2/crypto/threads.html#DESCRIPTION
242 void CurlEnvironment::OnOpenSSLLocking( int mode, int n, const char* file, int line )
244 if( mode & CRYPTO_LOCK )
254 void CurlEnvironment::GetThreadId( CRYPTO_THREADID* tid )
256 // If dali uses c++ thread, we may replace pthread_self() to this_thread::get_id()
257 CRYPTO_THREADID_set_numeric( tid, static_cast< unsigned long > ( pthread_self() ) );
260 void CurlEnvironment::SetLockingFunction()
262 if( mMutexs != NULL )
267 mMutexs = new std::mutex[ CRYPTO_num_locks() ];
269 CRYPTO_THREADID_set_callback( &CurlEnvironment::GetThreadId );
270 CRYPTO_set_locking_callback( &CurlEnvironment::OnOpenSSLLocking );
273 void CurlEnvironment::UnsetLockingFunction()
275 if( mMutexs == NULL )
280 CRYPTO_THREADID_set_callback( NULL );
281 CRYPTO_set_locking_callback( NULL );
286 bool DownloadRemoteFileIntoMemory( const std::string& url,
287 Dali::Vector<uint8_t>& dataBuffer,
289 size_t maximumAllowedSizeBytes )
293 DALI_LOG_WARNING("empty url requested \n");
297 // start a libcurl easy session, this internally calls curl_global_init, if we ever have more than one download
298 // thread we need to explicity call curl_global_init() on startup from a single thread.
300 CURL* curlHandle = curl_easy_init();
302 bool result = DownloadFile( curlHandle, url, dataBuffer, dataSize, maximumAllowedSizeBytes);
305 curl_easy_cleanup( curlHandle );
307 #ifdef TPK_CURL_ENABLED
308 // Clean up tpkp(the module for certificate pinning) resources on Tizen
310 #endif // TPK_CURL_ENABLED
315 } // namespace Network
317 } // namespace TizenPlatform