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 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;
54 * Curl library environment. Direct initialize ensures it's constructed before adaptor
55 * or application creates any threads.
57 static Dali::TizenPlatform::Network::CurlEnvironment gCurlEnvironment;
59 void ConfigureCurlOptions( CURL* curlHandle, const std::string& url )
61 curl_easy_setopt( curlHandle, CURLOPT_URL, url.c_str() );
62 curl_easy_setopt( curlHandle, CURLOPT_VERBOSE, VERBOSE_MODE );
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 );
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
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)
85 std::vector< uint8_t > data;
88 size_t ChunkLoader(char *ptr, size_t size, size_t nmemb, void *userdata)
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 );
100 CURLcode DownloadFileDataWithSize( CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t dataSize )
102 CURLcode result( CURLE_OK );
105 Dali::Internal::Platform::FileWriter fileWriter( dataBuffer, dataSize );
106 FILE* dataBufferFilePointer = fileWriter.GetFile();
107 if( NULL != dataBufferFilePointer )
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 );
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 );
117 // synchronous request of the body data
118 result = curl_easy_perform( curlHandle );
123 CURLcode DownloadFileDataByChunk( CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t& dataSize )
126 std::vector< ChunkData > chunks;
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 );
132 // Enable the write callback.
133 curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, ChunkLoader );
134 curl_easy_setopt( curlHandle, CURLOPT_WRITEDATA, &chunks );
136 // synchronous request of the body data
137 CURLcode result = curl_easy_perform( curlHandle );
139 // chunks should now contain all of the chunked data. Reassemble into a single vector
141 for( size_t i=0; i<chunks.size() ; ++i )
143 dataSize += chunks[i].data.capacity();
145 dataBuffer.Resize(dataSize);
148 for( size_t i=0; i<chunks.size() ; ++i )
150 memcpy( &dataBuffer[offset], &chunks[i].data[0], chunks[i].data.capacity() );
151 offset += chunks[i].data.capacity();
157 bool DownloadFile( CURL* curlHandle,
158 const std::string& url,
159 Dali::Vector<uint8_t>& dataBuffer,
161 size_t maximumAllowedSizeBytes )
163 CURLcode result( CURLE_OK );
166 // setup curl to download just the header so we can extract the content length
167 ConfigureCurlOptions( curlHandle, url );
169 curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, DummyWrite);
171 // perform the request to get the header
172 result = curl_easy_perform( curlHandle );
174 if( result != CURLE_OK)
176 DALI_LOG_ERROR( "Failed to download http header for \"%s\" with error code %d\n", url.c_str(), result );
180 // get the content length, -1 == size is not known
181 curl_easy_getinfo( curlHandle,CURLINFO_CONTENT_LENGTH_DOWNLOAD , &size );
184 if( size >= maximumAllowedSizeBytes )
186 DALI_LOG_ERROR( "File content length %f > max allowed %zu \"%s\" \n", size, maximumAllowedSizeBytes, url.c_str() );
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 );
197 result = DownloadFileDataByChunk( curlHandle, dataBuffer, dataSize );
200 if( result != CURLE_OK )
202 DALI_LOG_ERROR( "Failed to download image file \"%s\" with error code %d\n", url.c_str(), result );
209 } // unnamed namespace
215 std::mutex* CurlEnvironment::mMutexs = NULL;
217 CurlEnvironment::CurlEnvironment()
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);
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();
230 CurlEnvironment::~CurlEnvironment()
232 UnsetLockingFunction();
234 curl_global_cleanup();
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 )
242 if( mode & CRYPTO_LOCK )
252 void CurlEnvironment::GetThreadId( CRYPTO_THREADID* tid )
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() ) );
258 void CurlEnvironment::SetLockingFunction()
260 if( mMutexs != NULL )
265 mMutexs = new std::mutex[ CRYPTO_num_locks() ];
267 CRYPTO_THREADID_set_callback( &CurlEnvironment::GetThreadId );
268 CRYPTO_set_locking_callback( &CurlEnvironment::OnOpenSSLLocking );
271 void CurlEnvironment::UnsetLockingFunction()
273 if( mMutexs == NULL )
278 CRYPTO_THREADID_set_callback( NULL );
279 CRYPTO_set_locking_callback( NULL );
284 bool DownloadRemoteFileIntoMemory( const std::string& url,
285 Dali::Vector<uint8_t>& dataBuffer,
287 size_t maximumAllowedSizeBytes )
291 DALI_LOG_WARNING("empty url requested \n");
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.
298 CURL* curlHandle = curl_easy_init();
300 bool result = DownloadFile( curlHandle, url, dataBuffer, dataSize, maximumAllowedSizeBytes);
303 curl_easy_cleanup( curlHandle );
305 #ifdef TPK_CURL_ENABLED
306 // Clean up tpkp(the module for certificate pinning) resources on Tizen
308 #endif // TPK_CURL_ENABLED
313 } // namespace Network
315 } // namespace TizenPlatform