2 * Copyright (c) 2021 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 <curl/curl.h>
23 #include <dali/integration-api/debug.h>
28 #include <dali/internal/system/common/file-writer.h>
30 using namespace Dali::Integration;
34 namespace TizenPlatform
36 namespace // unnamed namespace
38 const int CONNECTION_TIMEOUT_SECONDS(30L);
39 const long VERBOSE_MODE = 0L; // 0 == off, 1 == on
40 const long CLOSE_CONNECTION_ON_ERROR = 1L; // 0 == off, 1 == on
41 const long EXCLUDE_HEADER = 0L;
42 const long INCLUDE_HEADER = 1L;
43 const long INCLUDE_BODY = 0L;
44 const long EXCLUDE_BODY = 1L;
47 * Curl library environment. Direct initialize ensures it's constructed before adaptor
48 * or application creates any threads.
50 static Dali::TizenPlatform::Network::CurlEnvironment gCurlEnvironment;
52 // Without a write function or a buffer (file descriptor) to write to, curl will pump out
53 // header/body contents to stdout
54 size_t __cdecl DummyWrite(char* ptr, size_t size, size_t nmemb, void* userdata)
61 std::vector<uint8_t> data;
64 size_t __cdecl ChunkLoader(char* ptr, size_t size, size_t nmemb, void* userdata)
66 std::vector<ChunkData>* chunks = static_cast<std::vector<ChunkData>*>(userdata);
67 int numBytes = size * nmemb;
70 chunks->push_back(ChunkData());
71 ChunkData& chunkData = (*chunks)[chunks->size() - 1];
72 chunkData.data.reserve(numBytes);
73 memcpy(chunkData.data.data(), ptr, numBytes);
78 static size_t __cdecl WriteFunction(void* input, size_t uSize, size_t uCount, void* avg)
80 fwrite((const char*)input, uSize, uCount, (FILE*)avg);
81 return uSize * uCount;
84 void InitWriteFunction(void* curlHandle)
86 curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, WriteFunction);
89 CURLcode DownloadFileDataWithSize(CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t dataSize)
91 CURLcode result(CURLE_OK);
94 Dali::Internal::Platform::FileWriter fileWriter(dataBuffer, dataSize);
95 FILE* dataBufferFilePointer = fileWriter.GetFile();
96 if(nullptr != dataBufferFilePointer)
98 // we only want the body which contains the file data
99 curl_easy_setopt(curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER);
100 curl_easy_setopt(curlHandle, CURLOPT_NOBODY, INCLUDE_BODY);
102 // disable the write callback, and get curl to write directly into our data buffer
103 InitWriteFunction(curlHandle);
105 curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, dataBufferFilePointer);
107 // synchronous request of the body data
108 result = curl_easy_perform(curlHandle);
113 CURLcode DownloadFileDataByChunk(CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t& dataSize)
116 std::vector<ChunkData> chunks;
118 // we only want the body which contains the file data
119 curl_easy_setopt(curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER);
120 curl_easy_setopt(curlHandle, CURLOPT_NOBODY, INCLUDE_BODY);
122 // Enable the write callback.
123 curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, ChunkLoader);
124 curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, &chunks);
126 // synchronous request of the body data
127 CURLcode result = curl_easy_perform(curlHandle);
129 // chunks should now contain all of the chunked data. Reassemble into a single vector
131 for(size_t i = 0; i < chunks.size(); ++i)
133 dataSize += chunks[i].data.capacity();
135 dataBuffer.Resize(dataSize);
138 for(size_t i = 0; i < chunks.size(); ++i)
140 memcpy(&dataBuffer[offset], chunks[i].data.data(), chunks[i].data.capacity());
141 offset += chunks[i].data.capacity();
147 void ConfigureCurlOptions(void* curlHandle, const std::string& url)
149 curl_easy_setopt(curlHandle, CURLOPT_URL, url.c_str());
150 //curl_easy_setopt( curlHandle, CURLOPT_VERBOSE, VERBOSE_MODE );
151 curl_easy_setopt(curlHandle, CURLOPT_PROXY, "109.123.100.31:3128");
153 // CURLOPT_FAILONERROR is not fail-safe especially when authentication is involved ( see manual )
154 // Removed CURLOPT_FAILONERROR option
155 curl_easy_setopt(curlHandle, CURLOPT_CONNECTTIMEOUT, CONNECTION_TIMEOUT_SECONDS);
156 curl_easy_setopt(curlHandle, CURLOPT_HEADER, INCLUDE_HEADER);
157 curl_easy_setopt(curlHandle, CURLOPT_NOBODY, EXCLUDE_BODY);
160 bool DownloadFile(CURL* curlHandle,
161 const std::string& url,
162 Dali::Vector<uint8_t>& dataBuffer,
164 size_t maximumAllowedSizeBytes)
166 CURLcode result(CURLE_OK);
169 // setup curl to download just the header so we can extract the content length
170 ConfigureCurlOptions(curlHandle, url);
172 curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, DummyWrite);
174 // perform the request to get the header
175 result = curl_easy_perform(curlHandle);
177 if(result != CURLE_OK)
179 DALI_LOG_ERROR("Failed to download http header for \"%s\" with error code %d\n", url.c_str(), result);
183 // get the content length, -1 == size is not known
184 curl_easy_getinfo(curlHandle, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &size);
186 if(size >= maximumAllowedSizeBytes)
188 DALI_LOG_ERROR("File content length %f > max allowed %zu \"%s\" \n", size, maximumAllowedSizeBytes, url.c_str());
193 // If we know the size up front, allocate once and avoid chunk copies.
194 dataSize = static_cast<size_t>(size);
195 result = DownloadFileDataWithSize(curlHandle, dataBuffer, dataSize);
199 result = DownloadFileDataByChunk(curlHandle, dataBuffer, dataSize);
202 if(result != CURLE_OK)
204 DALI_LOG_ERROR("Failed to download image file \"%s\" with error code %d\n", url.c_str(), result);
210 } // unnamed namespace
214 CurlEnvironment::CurlEnvironment()
216 // Must be called before we attempt any loads. e.g. by using curl_easy_init()
217 // and before we start any threads.
218 curl_global_init(CURL_GLOBAL_ALL);
221 CurlEnvironment::~CurlEnvironment()
223 curl_global_cleanup();
226 bool DownloadRemoteFileIntoMemory(const std::string& url,
227 Dali::Vector<uint8_t>& dataBuffer,
229 size_t maximumAllowedSizeBytes)
233 DALI_LOG_WARNING("empty url requested \n");
237 // start a libcurl easy session, this internally calls curl_global_init, if we ever have more than one download
238 // thread we need to explicity call curl_global_init() on startup from a single thread.
240 CURL* curlHandle = curl_easy_init();
242 bool result = DownloadFile(curlHandle, url, dataBuffer, dataSize, maximumAllowedSizeBytes);
245 curl_easy_cleanup(curlHandle);
250 } // namespace Network
252 } // namespace TizenPlatform