2 * Copyright (c) 2018 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>
25 #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 // Without a write function or a buffer (file descriptor) to write to, curl will pump out
60 // header/body contents to stdout
61 size_t __cdecl DummyWrite(char *ptr, size_t size, size_t nmemb, void *userdata)
68 std::vector< uint8_t > data;
71 size_t __cdecl ChunkLoader(char *ptr, size_t size, size_t nmemb, void *userdata)
73 std::vector<ChunkData>* chunks = static_cast<std::vector<ChunkData>*>( userdata );
74 int numBytes = size*nmemb;
75 if( chunks != nullptr )
77 chunks->push_back( ChunkData() );
78 ChunkData& chunkData = (*chunks)[chunks->size()-1];
79 chunkData.data.reserve( numBytes );
80 memcpy( chunkData.data.data(), ptr, numBytes );
85 static size_t __cdecl WriteFunction( void *input, size_t uSize, size_t uCount, void *avg )
87 fwrite( (const char*)input, uSize, uCount, (FILE*)avg );
88 return uSize * uCount;
91 void InitWriteFunction( void* curlHandle )
93 curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, WriteFunction );
96 CURLcode DownloadFileDataWithSize( CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t dataSize )
98 CURLcode result( CURLE_OK );
101 Dali::Internal::Platform::FileWriter fileWriter( dataBuffer, dataSize );
102 FILE* dataBufferFilePointer = fileWriter.GetFile();
103 if( nullptr != dataBufferFilePointer )
105 // we only want the body which contains the file data
106 curl_easy_setopt( curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER );
107 curl_easy_setopt( curlHandle, CURLOPT_NOBODY, INCLUDE_BODY );
109 // disable the write callback, and get curl to write directly into our data buffer
110 InitWriteFunction( curlHandle );
112 curl_easy_setopt( curlHandle, CURLOPT_WRITEDATA, dataBufferFilePointer );
114 // synchronous request of the body data
115 result = curl_easy_perform( curlHandle );
120 CURLcode DownloadFileDataByChunk( CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t& dataSize )
123 std::vector< ChunkData > chunks;
125 // we only want the body which contains the file data
126 curl_easy_setopt( curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER );
127 curl_easy_setopt( curlHandle, CURLOPT_NOBODY, INCLUDE_BODY );
129 // Enable the write callback.
130 curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, ChunkLoader );
131 curl_easy_setopt( curlHandle, CURLOPT_WRITEDATA, &chunks );
133 // synchronous request of the body data
134 CURLcode result = curl_easy_perform( curlHandle );
136 // chunks should now contain all of the chunked data. Reassemble into a single vector
138 for( size_t i=0; i<chunks.size() ; ++i )
140 dataSize += chunks[i].data.capacity();
142 dataBuffer.Resize(dataSize);
145 for( size_t i=0; i<chunks.size() ; ++i )
147 memcpy( &dataBuffer[offset], chunks[i].data.data(), chunks[i].data.capacity() );
148 offset += chunks[i].data.capacity();
154 void ConfigureCurlOptions( void* curlHandle, const std::string& url )
156 curl_easy_setopt( curlHandle, CURLOPT_URL, url.c_str() );
157 //curl_easy_setopt( curlHandle, CURLOPT_VERBOSE, VERBOSE_MODE );
158 curl_easy_setopt( curlHandle, CURLOPT_PROXY, "109.123.100.31:3128" );
160 // CURLOPT_FAILONERROR is not fail-safe especially when authentication is involved ( see manual )
161 // Removed CURLOPT_FAILONERROR option
162 curl_easy_setopt( curlHandle, CURLOPT_CONNECTTIMEOUT, CONNECTION_TIMEOUT_SECONDS );
163 curl_easy_setopt( curlHandle, CURLOPT_HEADER, INCLUDE_HEADER );
164 curl_easy_setopt( curlHandle, CURLOPT_NOBODY, EXCLUDE_BODY );
167 bool DownloadFile( CURL* curlHandle,
168 const std::string& url,
169 Dali::Vector<uint8_t>& dataBuffer,
171 size_t maximumAllowedSizeBytes )
173 CURLcode result( CURLE_OK );
176 // setup curl to download just the header so we can extract the content length
177 ConfigureCurlOptions( curlHandle, url );
179 curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, DummyWrite);
181 // perform the request to get the header
182 result = curl_easy_perform( curlHandle );
184 if( result != CURLE_OK)
186 DALI_LOG_ERROR( "Failed to download http header for \"%s\" with error code %d\n", url.c_str(), result );
190 // get the content length, -1 == size is not known
191 curl_easy_getinfo( curlHandle,CURLINFO_CONTENT_LENGTH_DOWNLOAD , &size );
194 if( size >= maximumAllowedSizeBytes )
196 DALI_LOG_ERROR( "File content length %f > max allowed %zu \"%s\" \n", size, maximumAllowedSizeBytes, url.c_str() );
201 // If we know the size up front, allocate once and avoid chunk copies.
202 dataSize = static_cast<size_t>( size );
203 result = DownloadFileDataWithSize( curlHandle, dataBuffer, dataSize );
207 result = DownloadFileDataByChunk( curlHandle, dataBuffer, dataSize );
210 if( result != CURLE_OK )
212 DALI_LOG_ERROR( "Failed to download image file \"%s\" with error code %d\n", url.c_str(), result );
219 } // unnamed namespace
225 CurlEnvironment::CurlEnvironment()
227 // Must be called before we attempt any loads. e.g. by using curl_easy_init()
228 // and before we start any threads.
229 curl_global_init(CURL_GLOBAL_ALL);
232 CurlEnvironment::~CurlEnvironment()
234 curl_global_cleanup();
237 bool DownloadRemoteFileIntoMemory( const std::string& url,
238 Dali::Vector<uint8_t>& dataBuffer,
240 size_t maximumAllowedSizeBytes )
244 DALI_LOG_WARNING("empty url requested \n");
248 // start a libcurl easy session, this internally calls curl_global_init, if we ever have more than one download
249 // thread we need to explicity call curl_global_init() on startup from a single thread.
251 CURL* curlHandle = curl_easy_init();
253 bool result = DownloadFile( curlHandle, url, dataBuffer, dataSize, maximumAllowedSizeBytes);
256 curl_easy_cleanup( curlHandle );
258 #ifdef TPK_CURL_ENABLED
259 // Clean up tpkp(the module for certificate pinning) resources on Tizen
261 #endif // TPK_CURL_ENABLED
266 } // namespace Network
268 } // namespace TizenPlatform