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 <openssl/crypto.h>
26 #include <curl/curl.h>
29 #include <dali/internal/system/common/file-writer.h>
31 #ifdef TPK_CURL_ENABLED
32 #include <tpkp_curl.h>
33 #endif // TPK_CURL_ENABLED
35 using namespace Dali::Integration;
40 namespace TizenPlatform
43 namespace // unnamed namespace
45 const long EXCLUDE_HEADER = 0L;
46 const long INCLUDE_BODY = 0L;
49 * Curl library environment. Direct initialize ensures it's constructed before adaptor
50 * or application creates any threads.
52 static Dali::TizenPlatform::Network::CurlEnvironment gCurlEnvironment;
54 // Without a write function or a buffer (file descriptor) to write to, curl will pump out
55 // header/body contents to stdout
56 size_t __cdecl DummyWrite(char *ptr, size_t size, size_t nmemb, void *userdata)
63 std::vector< uint8_t > data;
66 size_t __cdecl ChunkLoader(char *ptr, size_t size, size_t nmemb, void *userdata)
68 std::vector<ChunkData>* chunks = static_cast<std::vector<ChunkData>*>( userdata );
69 int numBytes = size*nmemb;
70 chunks->push_back( ChunkData() );
71 ChunkData& chunkData = (*chunks)[chunks->size()-1];
72 chunkData.data.reserve( numBytes );
73 memcpy( chunkData.data.data(), ptr, numBytes );
78 CURLcode DownloadFileDataWithSize( CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t dataSize )
80 CURLcode result( CURLE_OK );
83 Dali::Internal::Platform::FileWriter fileWriter( dataBuffer, dataSize );
84 FILE* dataBufferFilePointer = fileWriter.GetFile();
85 if( NULL != dataBufferFilePointer )
87 // we only want the body which contains the file data
88 curl_easy_setopt( curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER );
89 curl_easy_setopt( curlHandle, CURLOPT_NOBODY, INCLUDE_BODY );
91 // disable the write callback, and get curl to write directly into our data buffer
92 Network::CurlEnvironment::InitWriteFunction( curlHandle );
94 curl_easy_setopt( curlHandle, CURLOPT_WRITEDATA, dataBufferFilePointer );
96 // synchronous request of the body data
97 result = curl_easy_perform( curlHandle );
102 CURLcode DownloadFileDataByChunk( CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t& dataSize )
105 std::vector< ChunkData > chunks;
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 );
111 // Enable the write callback.
112 curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, ChunkLoader );
113 curl_easy_setopt( curlHandle, CURLOPT_WRITEDATA, &chunks );
115 // synchronous request of the body data
116 CURLcode result = curl_easy_perform( curlHandle );
118 // chunks should now contain all of the chunked data. Reassemble into a single vector
120 for( size_t i=0; i<chunks.size() ; ++i )
122 dataSize += chunks[i].data.capacity();
124 dataBuffer.Resize(dataSize);
127 for( size_t i=0; i<chunks.size() ; ++i )
129 memcpy( &dataBuffer[offset], chunks[i].data.data(), chunks[i].data.capacity() );
130 offset += chunks[i].data.capacity();
136 bool DownloadFile( CURL* curlHandle,
137 const std::string& url,
138 Dali::Vector<uint8_t>& dataBuffer,
140 size_t maximumAllowedSizeBytes )
142 CURLcode result( CURLE_OK );
145 // setup curl to download just the header so we can extract the content length
146 Network::CurlEnvironment::ConfigureCurlOptions( curlHandle, url );
148 curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, DummyWrite);
150 // perform the request to get the header
151 result = curl_easy_perform( curlHandle );
153 if( result != CURLE_OK)
155 DALI_LOG_ERROR( "Failed to download http header for \"%s\" with error code %d\n", url.c_str(), result );
159 // get the content length, -1 == size is not known
160 curl_easy_getinfo( curlHandle,CURLINFO_CONTENT_LENGTH_DOWNLOAD , &size );
163 if( size >= maximumAllowedSizeBytes )
165 DALI_LOG_ERROR( "File content length %f > max allowed %zu \"%s\" \n", size, maximumAllowedSizeBytes, url.c_str() );
170 // If we know the size up front, allocate once and avoid chunk copies.
171 dataSize = static_cast<size_t>( size );
172 result = DownloadFileDataWithSize( curlHandle, dataBuffer, dataSize );
176 result = DownloadFileDataByChunk( curlHandle, dataBuffer, dataSize );
179 if( result != CURLE_OK )
181 DALI_LOG_ERROR( "Failed to download image file \"%s\" with error code %d\n", url.c_str(), result );
188 } // unnamed namespace
194 std::mutex* CurlEnvironment::mMutexs = NULL;
196 CurlEnvironment::CurlEnvironment()
198 // Must be called before we attempt any loads. e.g. by using curl_easy_init()
199 // and before we start any threads.
200 curl_global_init(CURL_GLOBAL_ALL);
202 // libcurl with openssl needs locking_function and thread id for threadsafe
203 // https://curl.haxx.se/libcurl/c/threadsafe.html
204 // https://www.openssl.org/docs/man1.0.2/crypto/threads.html#DESCRIPTION
205 // SetLockingFunction sets locking_function and get thread id by the guide.
206 SetLockingFunction();
209 CurlEnvironment::~CurlEnvironment()
211 UnsetLockingFunction();
213 curl_global_cleanup();
216 // libcurl with openssl needs locking_function and thread id for threadsafe
217 // https://curl.haxx.se/libcurl/c/threadsafe.html
218 // https://www.openssl.org/docs/man1.0.2/crypto/threads.html#DESCRIPTION
219 void CurlEnvironment::OnOpenSSLLocking( int mode, int n, const char* file, int line )
221 if( mode & CRYPTO_LOCK )
231 void CurlEnvironment::SetLockingFunction()
233 if( mMutexs != NULL )
238 mMutexs = new std::mutex[ CRYPTO_num_locks() ];
240 CRYPTO_set_id_callback( &CurlEnvironment::GetThreadId );
241 CRYPTO_set_locking_callback( &CurlEnvironment::OnOpenSSLLocking );
244 void CurlEnvironment::UnsetLockingFunction()
246 if( mMutexs == NULL )
251 CRYPTO_set_id_callback( NULL );
252 CRYPTO_set_locking_callback( NULL );
258 bool DownloadRemoteFileIntoMemory( const std::string& url,
259 Dali::Vector<uint8_t>& dataBuffer,
261 size_t maximumAllowedSizeBytes )
265 DALI_LOG_WARNING("empty url requested \n");
269 // start a libcurl easy session, this internally calls curl_global_init, if we ever have more than one download
270 // thread we need to explicity call curl_global_init() on startup from a single thread.
272 CURL* curlHandle = curl_easy_init();
274 bool result = DownloadFile( curlHandle, url, dataBuffer, dataSize, maximumAllowedSizeBytes);
277 curl_easy_cleanup( curlHandle );
279 #ifdef TPK_CURL_ENABLED
280 // Clean up tpkp(the module for certificate pinning) resources on Tizen
282 #endif // TPK_CURL_ENABLED
287 } // namespace Network
289 } // namespace TizenPlatform