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>
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 int TIMEOUT_SECONDS( 120L );
43 const long VERBOSE_MODE = 0L; // 0 == off, 1 == on
44 const long CLOSE_CONNECTION_ON_ERROR = 1L; // 0 == off, 1 == on
45 const long EXCLUDE_HEADER = 0L;
46 const long INCLUDE_HEADER = 1L;
47 const long INCLUDE_BODY = 0L;
48 const long EXCLUDE_BODY = 1L;
51 * Curl library environment. Direct initialize ensures it's constructed before adaptor
52 * or application creates any threads.
54 static Dali::TizenPlatform::Network::CurlEnvironment gCurlEnvironment;
56 void ConfigureCurlOptions( CURL* curlHandle, const std::string& url )
58 curl_easy_setopt( curlHandle, CURLOPT_URL, url.c_str() );
59 curl_easy_setopt( curlHandle, CURLOPT_VERBOSE, VERBOSE_MODE );
61 // CURLOPT_FAILONERROR is not fail-safe especially when authentication is involved ( see manual )
62 // Removed CURLOPT_FAILONERROR option
63 curl_easy_setopt( curlHandle, CURLOPT_CONNECTTIMEOUT, CONNECTION_TIMEOUT_SECONDS );
64 curl_easy_setopt( curlHandle, CURLOPT_TIMEOUT, TIMEOUT_SECONDS );
65 curl_easy_setopt( curlHandle, CURLOPT_HEADER, INCLUDE_HEADER );
66 curl_easy_setopt( curlHandle, CURLOPT_NOBODY, EXCLUDE_BODY );
69 // Without a write function or a buffer (file descriptor) to write to, curl will pump out
70 // header/body contents to stdout
71 size_t DummyWrite(char *ptr, size_t size, size_t nmemb, void *userdata)
78 std::vector< uint8_t > data;
81 size_t ChunkLoader(char *ptr, size_t size, size_t nmemb, void *userdata)
83 std::vector<ChunkData>* chunks = static_cast<std::vector<ChunkData>*>( userdata );
84 int numBytes = size*nmemb;
85 chunks->push_back( ChunkData() );
86 ChunkData& chunkData = (*chunks)[chunks->size()-1];
87 chunkData.data.reserve( numBytes );
88 memcpy( &chunkData.data[0], ptr, numBytes );
93 CURLcode DownloadFileDataWithSize( CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t dataSize )
95 CURLcode result( CURLE_OK );
98 Dali::Internal::Platform::FileWriter fileWriter( dataBuffer, dataSize );
99 FILE* dataBufferFilePointer = fileWriter.GetFile();
100 if( NULL != dataBufferFilePointer )
102 // we only want the body which contains the file data
103 curl_easy_setopt( curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER );
104 curl_easy_setopt( curlHandle, CURLOPT_NOBODY, INCLUDE_BODY );
106 // disable the write callback, and get curl to write directly into our data buffer
107 curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, NULL );
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[0], chunks[i].data.capacity() );
144 offset += chunks[i].data.capacity();
150 bool DownloadFile( CURL* curlHandle,
151 const std::string& url,
152 Dali::Vector<uint8_t>& dataBuffer,
154 size_t maximumAllowedSizeBytes )
156 CURLcode result( CURLE_OK );
159 // setup curl to download just the header so we can extract the content length
160 ConfigureCurlOptions( curlHandle, url );
162 curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, DummyWrite);
164 // perform the request to get the header
165 result = curl_easy_perform( curlHandle );
167 if( result != CURLE_OK)
169 DALI_LOG_ERROR( "Failed to download http header for \"%s\" with error code %d\n", url.c_str(), result );
173 // get the content length, -1 == size is not known
174 curl_easy_getinfo( curlHandle,CURLINFO_CONTENT_LENGTH_DOWNLOAD , &size );
177 if( size >= maximumAllowedSizeBytes )
179 DALI_LOG_ERROR( "File content length %f > max allowed %zu \"%s\" \n", size, maximumAllowedSizeBytes, url.c_str() );
184 // If we know the size up front, allocate once and avoid chunk copies.
185 dataSize = static_cast<size_t>( size );
186 result = DownloadFileDataWithSize( curlHandle, dataBuffer, dataSize );
190 result = DownloadFileDataByChunk( curlHandle, dataBuffer, dataSize );
193 if( result != CURLE_OK )
195 DALI_LOG_ERROR( "Failed to download image file \"%s\" with error code %d\n", url.c_str(), result );
202 } // unnamed namespace
208 CurlEnvironment::CurlEnvironment()
210 // Must be called before we attempt any loads. e.g. by using curl_easy_init()
211 // and before we start any threads.
212 curl_global_init(CURL_GLOBAL_ALL);
215 CurlEnvironment::~CurlEnvironment()
217 curl_global_cleanup();
220 bool DownloadRemoteFileIntoMemory( const std::string& url,
221 Dali::Vector<uint8_t>& dataBuffer,
223 size_t maximumAllowedSizeBytes )
229 DALI_LOG_WARNING("empty url requested \n");
233 // start a libcurl easy session, this internally calls curl_global_init, if we ever have more than one download
234 // thread we need to explicity call curl_global_init() on startup from a single thread.
236 CURL* curlHandle = curl_easy_init();
239 result = DownloadFile( curlHandle, url, dataBuffer, dataSize, maximumAllowedSizeBytes);
242 curl_easy_cleanup( curlHandle );
247 } // namespace Network
249 } // namespace TizenPlatform