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 using namespace Dali::Integration;
35 namespace TizenPlatform
38 namespace // unnamed namespace
41 const int CONNECTION_TIMEOUT_SECONDS( 30L );
42 const long VERBOSE_MODE = 0L; // 0 == off, 1 == on
43 const long CLOSE_CONNECTION_ON_ERROR = 1L; // 0 == off, 1 == on
44 const long EXCLUDE_HEADER = 0L;
45 const long INCLUDE_HEADER = 1L;
46 const long INCLUDE_BODY = 0L;
47 const long EXCLUDE_BODY = 1L;
50 * Curl library environment. Direct initialize ensures it's constructed before adaptor
51 * or application creates any threads.
53 static Dali::TizenPlatform::Network::CurlEnvironment gCurlEnvironment;
55 // Without a write function or a buffer (file descriptor) to write to, curl will pump out
56 // header/body contents to stdout
57 size_t __cdecl DummyWrite(char *ptr, size_t size, size_t nmemb, void *userdata)
64 std::vector< uint8_t > data;
67 size_t __cdecl ChunkLoader(char *ptr, size_t size, size_t nmemb, void *userdata)
69 std::vector<ChunkData>* chunks = static_cast<std::vector<ChunkData>*>( userdata );
70 int numBytes = size*nmemb;
71 if( chunks != nullptr )
73 chunks->push_back( ChunkData() );
74 ChunkData& chunkData = (*chunks)[chunks->size()-1];
75 chunkData.data.reserve( numBytes );
76 memcpy( chunkData.data.data(), ptr, numBytes );
81 static size_t __cdecl WriteFunction( void *input, size_t uSize, size_t uCount, void *avg )
83 fwrite( (const char*)input, uSize, uCount, (FILE*)avg );
84 return uSize * uCount;
87 void InitWriteFunction( void* curlHandle )
89 curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, WriteFunction );
92 CURLcode DownloadFileDataWithSize( CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t dataSize )
94 CURLcode result( CURLE_OK );
97 Dali::Internal::Platform::FileWriter fileWriter( dataBuffer, dataSize );
98 FILE* dataBufferFilePointer = fileWriter.GetFile();
99 if( nullptr != dataBufferFilePointer )
101 // we only want the body which contains the file data
102 curl_easy_setopt( curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER );
103 curl_easy_setopt( curlHandle, CURLOPT_NOBODY, INCLUDE_BODY );
105 // disable the write callback, and get curl to write directly into our data buffer
106 InitWriteFunction( curlHandle );
108 curl_easy_setopt( curlHandle, CURLOPT_WRITEDATA, dataBufferFilePointer );
110 // synchronous request of the body data
111 result = curl_easy_perform( curlHandle );
116 CURLcode DownloadFileDataByChunk( CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t& dataSize )
119 std::vector< ChunkData > chunks;
121 // we only want the body which contains the file data
122 curl_easy_setopt( curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER );
123 curl_easy_setopt( curlHandle, CURLOPT_NOBODY, INCLUDE_BODY );
125 // Enable the write callback.
126 curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, ChunkLoader );
127 curl_easy_setopt( curlHandle, CURLOPT_WRITEDATA, &chunks );
129 // synchronous request of the body data
130 CURLcode result = curl_easy_perform( curlHandle );
132 // chunks should now contain all of the chunked data. Reassemble into a single vector
134 for( size_t i=0; i<chunks.size() ; ++i )
136 dataSize += chunks[i].data.capacity();
138 dataBuffer.Resize(dataSize);
141 for( size_t i=0; i<chunks.size() ; ++i )
143 memcpy( &dataBuffer[offset], chunks[i].data.data(), chunks[i].data.capacity() );
144 offset += chunks[i].data.capacity();
150 void ConfigureCurlOptions( void* curlHandle, const std::string& url )
152 curl_easy_setopt( curlHandle, CURLOPT_URL, url.c_str() );
153 //curl_easy_setopt( curlHandle, CURLOPT_VERBOSE, VERBOSE_MODE );
154 curl_easy_setopt( curlHandle, CURLOPT_PROXY, "109.123.100.31:3128" );
156 // CURLOPT_FAILONERROR is not fail-safe especially when authentication is involved ( see manual )
157 // Removed CURLOPT_FAILONERROR option
158 curl_easy_setopt( curlHandle, CURLOPT_CONNECTTIMEOUT, CONNECTION_TIMEOUT_SECONDS );
159 curl_easy_setopt( curlHandle, CURLOPT_HEADER, INCLUDE_HEADER );
160 curl_easy_setopt( curlHandle, CURLOPT_NOBODY, EXCLUDE_BODY );
163 bool DownloadFile( CURL* curlHandle,
164 const std::string& url,
165 Dali::Vector<uint8_t>& dataBuffer,
167 size_t maximumAllowedSizeBytes )
169 CURLcode result( CURLE_OK );
172 // setup curl to download just the header so we can extract the content length
173 ConfigureCurlOptions( curlHandle, url );
175 curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, DummyWrite);
177 // perform the request to get the header
178 result = curl_easy_perform( curlHandle );
180 if( result != CURLE_OK)
182 DALI_LOG_ERROR( "Failed to download http header for \"%s\" with error code %d\n", url.c_str(), result );
186 // get the content length, -1 == size is not known
187 curl_easy_getinfo( curlHandle,CURLINFO_CONTENT_LENGTH_DOWNLOAD , &size );
190 if( size >= maximumAllowedSizeBytes )
192 DALI_LOG_ERROR( "File content length %f > max allowed %zu \"%s\" \n", size, maximumAllowedSizeBytes, url.c_str() );
197 // If we know the size up front, allocate once and avoid chunk copies.
198 dataSize = static_cast<size_t>( size );
199 result = DownloadFileDataWithSize( curlHandle, dataBuffer, dataSize );
203 result = DownloadFileDataByChunk( curlHandle, dataBuffer, dataSize );
206 if( result != CURLE_OK )
208 DALI_LOG_ERROR( "Failed to download image file \"%s\" with error code %d\n", url.c_str(), result );
215 } // unnamed namespace
221 CurlEnvironment::CurlEnvironment()
223 // Must be called before we attempt any loads. e.g. by using curl_easy_init()
224 // and before we start any threads.
225 curl_global_init(CURL_GLOBAL_ALL);
228 CurlEnvironment::~CurlEnvironment()
230 curl_global_cleanup();
233 bool DownloadRemoteFileIntoMemory( const std::string& url,
234 Dali::Vector<uint8_t>& dataBuffer,
236 size_t maximumAllowedSizeBytes )
240 DALI_LOG_WARNING("empty url requested \n");
244 // start a libcurl easy session, this internally calls curl_global_init, if we ever have more than one download
245 // thread we need to explicity call curl_global_init() on startup from a single thread.
247 CURL* curlHandle = curl_easy_init();
249 bool result = DownloadFile( curlHandle, url, dataBuffer, dataSize, maximumAllowedSizeBytes);
252 curl_easy_cleanup( curlHandle );
257 } // namespace Network
259 } // namespace TizenPlatform