2 * Copyright (c) 2023 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>
30 #include <dali/devel-api/adaptor-framework/environment-variable.h>
31 #include <dali/internal/system/common/environment-variables.h>
32 #include <dali/internal/system/common/file-writer.h>
33 #include <dali/public-api/common/dali-common.h>
35 using namespace Dali::Integration;
39 namespace TizenPlatform
41 namespace // unnamed namespace
43 inline void LogCurlResult(CURLcode result, char* errorBuffer, std::string url, std::string prefix)
45 if(result != CURLE_OK)
47 if(errorBuffer != nullptr)
49 DALI_LOG_ERROR("%s \"%s\" with error code %d\n", prefix.c_str(), url.c_str(), result);
53 DALI_LOG_ERROR("%s \"%s\" with error code %d (%s)\n", prefix.c_str(), url.c_str(), result, errorBuffer);
58 std::string ConvertDataReadable(uint8_t* data, const size_t size, const size_t width)
60 std::ostringstream oss;
62 for(size_t i = 0u; i < size; ++i)
64 if(i > 0u && (i % width) == 0u)
68 oss << ((data[i] >= 0x20 && data[i] < 0x80) ? static_cast<char>(data[i]) : '.');
74 int CurloptVerboseLogTrace(CURL* handle, curl_infotype type, char* data, size_t size, void* clientp)
76 std::ostringstream oss;
77 (void)handle; /* prevent compiler warning */
84 oss << "== Info: " << std::string(data, size) << "\n";
88 default: /* in case a new one is introduced to shock us */
93 case CURLINFO_HEADER_OUT:
95 oss << "=> Send header\n";
98 case CURLINFO_DATA_OUT:
100 oss << "=> Send data\n";
103 case CURLINFO_SSL_DATA_OUT:
105 oss << "=> Send SSL data\n";
108 case CURLINFO_HEADER_IN:
110 oss << "<= Recv header\n";
113 case CURLINFO_DATA_IN:
115 oss << "<= Recv data\n";
118 case CURLINFO_SSL_DATA_IN:
120 oss << "<= Recv SSL data\n";
125 oss << "data size : " << size << " bytes\n";
127 oss << ConvertDataReadable(reinterpret_cast<uint8_t*>(data), size, 0x40);
129 DALI_LOG_DEBUG_INFO("Verbose curl log : %s", oss.str().c_str());
134 const int CONNECTION_TIMEOUT_SECONDS(30L);
135 const int TIMEOUT_SECONDS(120L);
136 const long CLOSE_CONNECTION_ON_ERROR = 1L; // 0 == off, 1 == on
137 const long EXCLUDE_HEADER = 0L;
138 const long INCLUDE_HEADER = 1L;
139 const long INCLUDE_BODY = 0L;
140 const long EXCLUDE_BODY = 1L;
143 * @brief Get the Curlopt Verbose Mode value from environment.
145 * @return 0 if verbose mode off. 1 if verbose mode on.
147 long GetCurloptVerboseMode()
149 static long verboseMode = 0;
150 static bool verboseModeSetted = false;
151 if(DALI_UNLIKELY(!verboseModeSetted))
153 auto verboseModeString = EnvironmentVariable::GetEnvironmentVariable(DALI_ENV_CURLOPT_VERBOSE_MODE);
154 verboseMode = verboseModeString ? (std::strtol(verboseModeString, nullptr, 10) > 0 ? 1 : 0) : 0;
161 * Curl library environment. Direct initialize ensures it's constructed before adaptor
162 * or application creates any threads.
164 static Dali::TizenPlatform::Network::CurlEnvironment gCurlEnvironment;
166 void ConfigureCurlOptions(CURL* curlHandle, const std::string& url)
168 auto verboseMode = GetCurloptVerboseMode(); // 0 : off, 1 : on
170 curl_easy_setopt(curlHandle, CURLOPT_URL, url.c_str());
171 curl_easy_setopt(curlHandle, CURLOPT_VERBOSE, verboseMode);
173 // CURLOPT_FAILONERROR is not fail-safe especially when authentication is involved ( see manual )
174 // Removed CURLOPT_FAILONERROR option
175 curl_easy_setopt(curlHandle, CURLOPT_CONNECTTIMEOUT, CONNECTION_TIMEOUT_SECONDS);
176 curl_easy_setopt(curlHandle, CURLOPT_TIMEOUT, TIMEOUT_SECONDS);
177 curl_easy_setopt(curlHandle, CURLOPT_HEADER, INCLUDE_HEADER);
178 curl_easy_setopt(curlHandle, CURLOPT_NOBODY, EXCLUDE_BODY);
179 curl_easy_setopt(curlHandle, CURLOPT_NOSIGNAL, 1L);
180 curl_easy_setopt(curlHandle, CURLOPT_FOLLOWLOCATION, 1L);
181 curl_easy_setopt(curlHandle, CURLOPT_MAXREDIRS, 5L);
185 curl_easy_setopt(curlHandle, CURLOPT_DEBUGFUNCTION, CurloptVerboseLogTrace);
188 // If the proxy variable is set, ensure it's also used.
189 // In theory, this variable should be used by the curl library; however, something
191 char* proxy = std::getenv("http_proxy");
194 curl_easy_setopt(curlHandle, CURLOPT_PROXY, proxy);
198 // Without a write function or a buffer (file descriptor) to write to, curl will pump out
199 // header/body contents to stdout
200 size_t DummyWrite(char* ptr, size_t size, size_t nmemb, void* userdata)
207 std::vector<uint8_t> data;
210 size_t ChunkLoader(char* ptr, size_t size, size_t nmemb, void* userdata)
212 std::vector<ChunkData>* chunks = static_cast<std::vector<ChunkData>*>(userdata);
213 int numBytes = size * nmemb;
214 chunks->push_back(ChunkData());
215 ChunkData& chunkData = (*chunks)[chunks->size() - 1];
216 chunkData.data.reserve(numBytes);
217 memcpy(&chunkData.data[0], ptr, numBytes);
221 CURLcode DownloadFileDataWithSize(CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t dataSize)
223 CURLcode result(CURLE_OK);
226 Dali::Internal::Platform::FileWriter fileWriter(dataBuffer, dataSize);
227 FILE* dataBufferFilePointer = fileWriter.GetFile();
229 if(NULL != dataBufferFilePointer)
231 setbuf(dataBufferFilePointer, NULL); // Turn buffering off
233 // we only want the body which contains the file data
234 curl_easy_setopt(curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER);
235 curl_easy_setopt(curlHandle, CURLOPT_NOBODY, INCLUDE_BODY);
237 // disable the write callback, and get curl to write directly into our data buffer
238 curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, NULL);
239 curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, dataBufferFilePointer);
241 // synchronous request of the body data
242 result = curl_easy_perform(curlHandle);
246 DALI_LOG_ERROR("Fail to open buffer writter with size : %zu!\n", dataSize);
247 // @todo : Need to check that is it correct error code?
248 result = CURLE_READ_ERROR;
253 CURLcode DownloadFileDataByChunk(CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t& dataSize)
256 std::vector<ChunkData> chunks;
258 // we only want the body which contains the file data
259 curl_easy_setopt(curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER);
260 curl_easy_setopt(curlHandle, CURLOPT_NOBODY, INCLUDE_BODY);
262 // Enable the write callback.
263 curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, ChunkLoader);
264 curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, &chunks);
266 // synchronous request of the body data
267 CURLcode result = curl_easy_perform(curlHandle);
269 // chunks should now contain all of the chunked data. Reassemble into a single vector
271 for(size_t i = 0; i < chunks.size(); ++i)
273 dataSize += chunks[i].data.capacity();
275 dataBuffer.ResizeUninitialized(dataSize);
277 if(DALI_LIKELY(dataSize > 0))
279 std::uint8_t* dataBufferPtr = dataBuffer.Begin();
280 for(size_t i = 0; i < chunks.size(); ++i)
282 memcpy(dataBufferPtr, &chunks[i].data[0], chunks[i].data.capacity());
283 dataBufferPtr += chunks[i].data.capacity();
290 bool DownloadFile(CURL* curlHandle,
291 const std::string& url,
292 Dali::Vector<uint8_t>& dataBuffer,
294 size_t maximumAllowedSizeBytes,
297 CURLcode result(CURLE_OK);
299 // setup curl to download just the header so we can extract the content length
300 ConfigureCurlOptions(curlHandle, url);
301 curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, DummyWrite);
302 if(errorBuffer != nullptr)
307 // perform the request to get the header
308 result = curl_easy_perform(curlHandle);
310 if(result != CURLE_OK)
312 if(errorBuffer != nullptr)
314 DALI_LOG_ERROR("Failed to download http header for \"%s\" with error code %d (%s)\n", url.c_str(), result, &errorBuffer[0]);
318 DALI_LOG_ERROR("Failed to download http header for \"%s\" with error code %d\n", url.c_str(), result);
323 // get the content length, -1 == size is not known
325 curl_easy_getinfo(curlHandle, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &size);
329 result = DownloadFileDataByChunk(curlHandle, dataBuffer, dataSize);
331 else if(size >= static_cast<curl_off_t>(maximumAllowedSizeBytes))
333 DALI_LOG_ERROR("File content length %" CURL_FORMAT_CURL_OFF_T " > max allowed %zu \"%s\" \n", size, maximumAllowedSizeBytes, url.c_str());
338 // If we know the size up front, allocate once and avoid chunk copies.
339 dataSize = static_cast<size_t>(size);
340 result = DownloadFileDataWithSize(curlHandle, dataBuffer, dataSize);
341 if(result != CURLE_OK)
343 LogCurlResult(result, errorBuffer, url, "Failed to download file, trying to load by chunk");
344 // In the case where the size is wrong (e.g. on a proxy server that rewrites data),
345 // the data buffer will be corrupt. In this case, try again using the chunk writer.
346 result = DownloadFileDataByChunk(curlHandle, dataBuffer, dataSize);
350 LogCurlResult(result, errorBuffer, url, "Failed to download image file");
352 if(result != CURLE_OK)
356 else if(DALI_UNLIKELY(dataSize == 0u))
358 DALI_LOG_WARNING("Warning : Download data size is 0! url : %s\n", url.c_str());
363 } // unnamed namespace
367 CurlEnvironment::CurlEnvironment()
369 // Must be called before we attempt any loads. e.g. by using curl_easy_init()
370 // and before we start any threads.
371 curl_global_init(CURL_GLOBAL_ALL);
374 CurlEnvironment::~CurlEnvironment()
376 curl_global_cleanup();
379 bool DownloadRemoteFileIntoMemory(const std::string& url,
380 Dali::Vector<uint8_t>& dataBuffer,
382 size_t maximumAllowedSizeBytes)
388 DALI_LOG_WARNING("empty url requested \n");
392 // start a libcurl easy session, this internally calls curl_global_init, if we ever have more than one download
393 // thread we need to explicity call curl_global_init() on startup from a single thread.
395 CURL* curlHandle = curl_easy_init();
398 std::vector<char> errorBuffer(CURL_ERROR_SIZE);
399 curl_easy_setopt(curlHandle, CURLOPT_ERRORBUFFER, &errorBuffer[0]);
400 result = DownloadFile(curlHandle, url, dataBuffer, dataSize, maximumAllowedSizeBytes, &errorBuffer[0]);
403 curl_easy_cleanup(curlHandle);
408 } // namespace Network
410 } // namespace TizenPlatform