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>
26 #include <../ExInclude/InternalFileOperation.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
46 const int CONNECTION_TIMEOUT_SECONDS( 30L );
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 // Without a write function or a buffer (file descriptor) to write to, curl will pump out
61 // header/body contents to stdout
62 size_t __cdecl DummyWrite(char *ptr, size_t size, size_t nmemb, void *userdata)
69 std::vector< uint8_t > data;
72 size_t __cdecl ChunkLoader(char *ptr, size_t size, size_t nmemb, void *userdata)
74 std::vector<ChunkData>* chunks = static_cast<std::vector<ChunkData>*>( userdata );
75 int numBytes = size*nmemb;
76 if( chunks != nullptr )
78 chunks->push_back( ChunkData() );
79 ChunkData& chunkData = (*chunks)[chunks->size()-1];
80 chunkData.data.reserve( numBytes );
81 memcpy( chunkData.data.data(), ptr, numBytes );
86 static size_t __cdecl WriteFunction( void *input, size_t uSize, size_t uCount, void *avg )
88 fwrite( input, uSize, uCount, (FILE*)avg );
89 return uSize * uCount;
92 void InitWriteFunction( void* curlHandle )
94 curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, WriteFunction );
97 CURLcode DownloadFileDataWithSize( CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t dataSize )
99 CURLcode result( CURLE_OK );
102 Dali::Internal::Platform::FileWriter fileWriter( dataBuffer, dataSize );
103 FILE* dataBufferFilePointer = fileWriter.GetFile();
104 if( nullptr != dataBufferFilePointer )
106 // we only want the body which contains the file data
107 curl_easy_setopt( curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER );
108 curl_easy_setopt( curlHandle, CURLOPT_NOBODY, INCLUDE_BODY );
110 // disable the write callback, and get curl to write directly into our data buffer
111 InitWriteFunction( curlHandle );
113 curl_easy_setopt( curlHandle, CURLOPT_WRITEDATA, dataBufferFilePointer );
115 // synchronous request of the body data
116 result = curl_easy_perform( curlHandle );
121 CURLcode DownloadFileDataByChunk( CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t& dataSize )
124 std::vector< ChunkData > chunks;
126 // we only want the body which contains the file data
127 curl_easy_setopt( curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER );
128 curl_easy_setopt( curlHandle, CURLOPT_NOBODY, INCLUDE_BODY );
130 // Enable the write callback.
131 curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, ChunkLoader );
132 curl_easy_setopt( curlHandle, CURLOPT_WRITEDATA, &chunks );
134 // synchronous request of the body data
135 CURLcode result = curl_easy_perform( curlHandle );
137 // chunks should now contain all of the chunked data. Reassemble into a single vector
139 for( size_t i=0; i<chunks.size() ; ++i )
141 dataSize += chunks[i].data.capacity();
143 dataBuffer.Resize(dataSize);
146 for( size_t i=0; i<chunks.size() ; ++i )
148 memcpy( &dataBuffer[offset], chunks[i].data.data(), chunks[i].data.capacity() );
149 offset += chunks[i].data.capacity();
155 void ConfigureCurlOptions( void* curlHandle, const std::string& url )
157 curl_easy_setopt( curlHandle, CURLOPT_URL, url.c_str() );
158 //curl_easy_setopt( curlHandle, CURLOPT_VERBOSE, VERBOSE_MODE );
159 curl_easy_setopt( curlHandle, CURLOPT_PROXY, "109.123.100.31:3128" );
161 // CURLOPT_FAILONERROR is not fail-safe especially when authentication is involved ( see manual )
162 // Removed CURLOPT_FAILONERROR option
163 curl_easy_setopt( curlHandle, CURLOPT_CONNECTTIMEOUT, CONNECTION_TIMEOUT_SECONDS );
164 curl_easy_setopt( curlHandle, CURLOPT_HEADER, INCLUDE_HEADER );
165 curl_easy_setopt( curlHandle, CURLOPT_NOBODY, EXCLUDE_BODY );
168 bool DownloadFile( CURL* curlHandle,
169 const std::string& url,
170 Dali::Vector<uint8_t>& dataBuffer,
172 size_t maximumAllowedSizeBytes )
174 CURLcode result( CURLE_OK );
177 // setup curl to download just the header so we can extract the content length
178 ConfigureCurlOptions( curlHandle, url );
180 curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, DummyWrite);
182 // perform the request to get the header
183 result = curl_easy_perform( curlHandle );
185 if( result != CURLE_OK)
187 DALI_LOG_ERROR( "Failed to download http header for \"%s\" with error code %d\n", url.c_str(), result );
191 // get the content length, -1 == size is not known
192 curl_easy_getinfo( curlHandle,CURLINFO_CONTENT_LENGTH_DOWNLOAD , &size );
195 if( size >= maximumAllowedSizeBytes )
197 DALI_LOG_ERROR( "File content length %f > max allowed %zu \"%s\" \n", size, maximumAllowedSizeBytes, url.c_str() );
202 // If we know the size up front, allocate once and avoid chunk copies.
203 dataSize = static_cast<size_t>( size );
204 result = DownloadFileDataWithSize( curlHandle, dataBuffer, dataSize );
208 result = DownloadFileDataByChunk( curlHandle, dataBuffer, dataSize );
211 if( result != CURLE_OK )
213 DALI_LOG_ERROR( "Failed to download image file \"%s\" with error code %d\n", url.c_str(), result );
220 } // unnamed namespace
226 CurlEnvironment::CurlEnvironment()
228 // Must be called before we attempt any loads. e.g. by using curl_easy_init()
229 // and before we start any threads.
230 curl_global_init(CURL_GLOBAL_ALL);
233 CurlEnvironment::~CurlEnvironment()
235 curl_global_cleanup();
238 bool DownloadRemoteFileIntoMemory( const std::string& url,
239 Dali::Vector<uint8_t>& dataBuffer,
241 size_t maximumAllowedSizeBytes )
245 DALI_LOG_WARNING("empty url requested \n");
249 // start a libcurl easy session, this internally calls curl_global_init, if we ever have more than one download
250 // thread we need to explicity call curl_global_init() on startup from a single thread.
252 CURL* curlHandle = curl_easy_init();
254 bool result = DownloadFile( curlHandle, url, dataBuffer, dataSize, maximumAllowedSizeBytes);
257 curl_easy_cleanup( curlHandle );
259 #ifdef TPK_CURL_ENABLED
260 // Clean up tpkp(the module for certificate pinning) resources on Tizen
262 #endif // TPK_CURL_ENABLED
267 } // namespace Network
269 } // namespace TizenPlatform