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>
29 #include <dali/internal/system/common/file-writer.h>
31 using namespace Dali::Integration;
36 namespace TizenPlatform
39 namespace // unnamed namespace
42 const int CONNECTION_TIMEOUT_SECONDS( 30L );
43 const int TIMEOUT_SECONDS( 120L );
44 const long VERBOSE_MODE = 0L; // 0 == off, 1 == on
45 const long CLOSE_CONNECTION_ON_ERROR = 1L; // 0 == off, 1 == on
46 const long EXCLUDE_HEADER = 0L;
47 const long INCLUDE_HEADER = 1L;
48 const long INCLUDE_BODY = 0L;
49 const long EXCLUDE_BODY = 1L;
52 * Curl library environment. Direct initialize ensures it's constructed before adaptor
53 * or application creates any threads.
55 static Dali::TizenPlatform::Network::CurlEnvironment gCurlEnvironment;
57 void ConfigureCurlOptions( CURL* curlHandle, const std::string& url )
59 curl_easy_setopt( curlHandle, CURLOPT_URL, url.c_str() );
60 curl_easy_setopt( curlHandle, CURLOPT_VERBOSE, VERBOSE_MODE );
62 // CURLOPT_FAILONERROR is not fail-safe especially when authentication is involved ( see manual )
63 // Removed CURLOPT_FAILONERROR option
64 curl_easy_setopt( curlHandle, CURLOPT_CONNECTTIMEOUT, CONNECTION_TIMEOUT_SECONDS );
65 curl_easy_setopt( curlHandle, CURLOPT_TIMEOUT, TIMEOUT_SECONDS );
66 curl_easy_setopt( curlHandle, CURLOPT_HEADER, INCLUDE_HEADER );
67 curl_easy_setopt( curlHandle, CURLOPT_NOBODY, EXCLUDE_BODY );
68 curl_easy_setopt( curlHandle, CURLOPT_FOLLOWLOCATION, 1L );
69 curl_easy_setopt( curlHandle, CURLOPT_MAXREDIRS, 5L );
71 // If the proxy variable is set, ensure it's also used.
72 // In theory, this variable should be used by the curl library; however, something
74 char* proxy = std::getenv("http_proxy");
75 if( proxy != nullptr )
77 curl_easy_setopt( curlHandle, CURLOPT_PROXY, proxy);
82 // Without a write function or a buffer (file descriptor) to write to, curl will pump out
83 // header/body contents to stdout
84 size_t DummyWrite(char *ptr, size_t size, size_t nmemb, void *userdata)
91 std::vector< uint8_t > data;
94 size_t ChunkLoader(char *ptr, size_t size, size_t nmemb, void *userdata)
96 std::vector<ChunkData>* chunks = static_cast<std::vector<ChunkData>*>( userdata );
97 int numBytes = size*nmemb;
98 chunks->push_back( ChunkData() );
99 ChunkData& chunkData = (*chunks)[chunks->size()-1];
100 chunkData.data.reserve( numBytes );
101 memcpy( &chunkData.data[0], ptr, numBytes );
106 CURLcode DownloadFileDataWithSize( CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t dataSize )
108 CURLcode result( CURLE_OK );
111 Dali::Internal::Platform::FileWriter fileWriter( dataBuffer, dataSize );
112 FILE* dataBufferFilePointer = fileWriter.GetFile();
113 if( NULL != dataBufferFilePointer )
115 // we only want the body which contains the file data
116 curl_easy_setopt( curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER );
117 curl_easy_setopt( curlHandle, CURLOPT_NOBODY, INCLUDE_BODY );
119 // disable the write callback, and get curl to write directly into our data buffer
120 curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, NULL );
121 curl_easy_setopt( curlHandle, CURLOPT_WRITEDATA, dataBufferFilePointer );
123 // synchronous request of the body data
124 result = curl_easy_perform( curlHandle );
129 CURLcode DownloadFileDataByChunk( CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t& dataSize )
132 std::vector< ChunkData > chunks;
134 // we only want the body which contains the file data
135 curl_easy_setopt( curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER );
136 curl_easy_setopt( curlHandle, CURLOPT_NOBODY, INCLUDE_BODY );
138 // Enable the write callback.
139 curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, ChunkLoader );
140 curl_easy_setopt( curlHandle, CURLOPT_WRITEDATA, &chunks );
142 // synchronous request of the body data
143 CURLcode result = curl_easy_perform( curlHandle );
145 // chunks should now contain all of the chunked data. Reassemble into a single vector
147 for( size_t i=0; i<chunks.size() ; ++i )
149 dataSize += chunks[i].data.capacity();
151 dataBuffer.Resize(dataSize);
154 for( size_t i=0; i<chunks.size() ; ++i )
156 memcpy( &dataBuffer[offset], &chunks[i].data[0], chunks[i].data.capacity() );
157 offset += chunks[i].data.capacity();
163 bool DownloadFile( CURL* curlHandle,
164 const std::string& url,
165 Dali::Vector<uint8_t>& dataBuffer,
167 size_t maximumAllowedSizeBytes,
170 CURLcode result( CURLE_OK );
173 // setup curl to download just the header so we can extract the content length
174 ConfigureCurlOptions( curlHandle, url );
175 curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, DummyWrite);
176 if(errorBuffer != nullptr)
181 // perform the request to get the header
182 result = curl_easy_perform( curlHandle );
184 if( result != CURLE_OK)
186 if(errorBuffer != nullptr)
188 DALI_LOG_ERROR( "Failed to download http header for \"%s\" with error code %d (%s)\n", url.c_str(), result, &errorBuffer[0] );
192 DALI_LOG_ERROR( "Failed to download http header for \"%s\" with error code %d\n", url.c_str(), result);
197 // get the content length, -1 == size is not known
198 curl_easy_getinfo( curlHandle,CURLINFO_CONTENT_LENGTH_DOWNLOAD , &size );
201 if( size >= maximumAllowedSizeBytes )
203 DALI_LOG_ERROR( "File content length %f > max allowed %zu \"%s\" \n", size, maximumAllowedSizeBytes, url.c_str() );
208 // If we know the size up front, allocate once and avoid chunk copies.
209 dataSize = static_cast<size_t>( size );
210 result = DownloadFileDataWithSize( curlHandle, dataBuffer, dataSize );
214 result = DownloadFileDataByChunk( curlHandle, dataBuffer, dataSize );
217 if( result != CURLE_OK )
219 if( errorBuffer != nullptr )
221 DALI_LOG_ERROR( "Failed to download image file \"%s\" with error code %d\n", url.c_str(), result );
225 DALI_LOG_ERROR( "Failed to download image file \"%s\" with error code %d (%s)\n", url.c_str(), result, errorBuffer ); }
232 } // unnamed namespace
238 CurlEnvironment::CurlEnvironment()
240 // Must be called before we attempt any loads. e.g. by using curl_easy_init()
241 // and before we start any threads.
242 curl_global_init(CURL_GLOBAL_ALL);
245 CurlEnvironment::~CurlEnvironment()
247 curl_global_cleanup();
250 bool DownloadRemoteFileIntoMemory( const std::string& url,
251 Dali::Vector<uint8_t>& dataBuffer,
253 size_t maximumAllowedSizeBytes )
259 DALI_LOG_WARNING("empty url requested \n");
264 // start a libcurl easy session, this internally calls curl_global_init, if we ever have more than one download
265 // thread we need to explicity call curl_global_init() on startup from a single thread.
267 CURL* curlHandle = curl_easy_init();
270 std::vector<char> errorBuffer(CURL_ERROR_SIZE);
271 curl_easy_setopt( curlHandle, CURLOPT_ERRORBUFFER, &errorBuffer[0]);
272 result = DownloadFile( curlHandle, url, dataBuffer, dataSize, maximumAllowedSizeBytes, &errorBuffer[0]);
275 curl_easy_cleanup( curlHandle );
280 } // namespace Network
282 } // namespace TizenPlatform