2 * Copyright (c) 2022 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>
29 #include <dali/internal/system/common/file-writer.h>
31 using namespace Dali::Integration;
35 namespace TizenPlatform
37 namespace // unnamed namespace
39 inline void LogCurlResult(CURLcode result, char* errorBuffer, std::string url, std::string prefix)
41 if(result != CURLE_OK)
43 if(errorBuffer != nullptr)
45 DALI_LOG_ERROR("%s \"%s\" with error code %d\n", prefix.c_str(), url.c_str(), result);
49 DALI_LOG_ERROR("$s \"%s\" with error code %d (%s)\n", prefix.c_str(), url.c_str(), result, errorBuffer);
54 const int CONNECTION_TIMEOUT_SECONDS(30L);
55 const int TIMEOUT_SECONDS(120L);
56 const long VERBOSE_MODE = 0L; // 0 == off, 1 == on
57 const long CLOSE_CONNECTION_ON_ERROR = 1L; // 0 == off, 1 == on
58 const long EXCLUDE_HEADER = 0L;
59 const long INCLUDE_HEADER = 1L;
60 const long INCLUDE_BODY = 0L;
61 const long EXCLUDE_BODY = 1L;
64 * Curl library environment. Direct initialize ensures it's constructed before adaptor
65 * or application creates any threads.
67 static Dali::TizenPlatform::Network::CurlEnvironment gCurlEnvironment;
69 void ConfigureCurlOptions(CURL* curlHandle, const std::string& url)
71 curl_easy_setopt(curlHandle, CURLOPT_URL, url.c_str());
72 curl_easy_setopt(curlHandle, CURLOPT_VERBOSE, VERBOSE_MODE);
74 // CURLOPT_FAILONERROR is not fail-safe especially when authentication is involved ( see manual )
75 // Removed CURLOPT_FAILONERROR option
76 curl_easy_setopt(curlHandle, CURLOPT_CONNECTTIMEOUT, CONNECTION_TIMEOUT_SECONDS);
77 curl_easy_setopt(curlHandle, CURLOPT_TIMEOUT, TIMEOUT_SECONDS);
78 curl_easy_setopt(curlHandle, CURLOPT_HEADER, INCLUDE_HEADER);
79 curl_easy_setopt(curlHandle, CURLOPT_NOBODY, EXCLUDE_BODY);
80 curl_easy_setopt(curlHandle, CURLOPT_NOSIGNAL, 1L);
81 curl_easy_setopt(curlHandle, CURLOPT_FOLLOWLOCATION, 1L);
82 curl_easy_setopt(curlHandle, CURLOPT_MAXREDIRS, 5L);
84 // If the proxy variable is set, ensure it's also used.
85 // In theory, this variable should be used by the curl library; however, something
87 char* proxy = std::getenv("http_proxy");
90 curl_easy_setopt(curlHandle, CURLOPT_PROXY, proxy);
94 // Without a write function or a buffer (file descriptor) to write to, curl will pump out
95 // header/body contents to stdout
96 size_t DummyWrite(char* ptr, size_t size, size_t nmemb, void* userdata)
103 std::vector<uint8_t> data;
106 size_t ChunkLoader(char* ptr, size_t size, size_t nmemb, void* userdata)
108 std::vector<ChunkData>* chunks = static_cast<std::vector<ChunkData>*>(userdata);
109 int numBytes = size * nmemb;
110 chunks->push_back(ChunkData());
111 ChunkData& chunkData = (*chunks)[chunks->size() - 1];
112 chunkData.data.reserve(numBytes);
113 memcpy(&chunkData.data[0], ptr, numBytes);
117 CURLcode DownloadFileDataWithSize(CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t dataSize)
119 CURLcode result(CURLE_OK);
122 Dali::Internal::Platform::FileWriter fileWriter(dataBuffer, dataSize);
123 FILE* dataBufferFilePointer = fileWriter.GetFile();
125 if(NULL != dataBufferFilePointer)
127 setbuf(dataBufferFilePointer, NULL); // Turn buffering off
129 // we only want the body which contains the file data
130 curl_easy_setopt(curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER);
131 curl_easy_setopt(curlHandle, CURLOPT_NOBODY, INCLUDE_BODY);
133 // disable the write callback, and get curl to write directly into our data buffer
134 curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, NULL);
135 curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, dataBufferFilePointer);
137 // synchronous request of the body data
138 result = curl_easy_perform(curlHandle);
142 DALI_LOG_ERROR("Fail to open buffer writter with size : %zu!\n", dataSize);
143 // @todo : Need to check that is it correct error code?
144 result = CURLE_READ_ERROR;
149 CURLcode DownloadFileDataByChunk(CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t& dataSize)
152 std::vector<ChunkData> chunks;
154 // we only want the body which contains the file data
155 curl_easy_setopt(curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER);
156 curl_easy_setopt(curlHandle, CURLOPT_NOBODY, INCLUDE_BODY);
158 // Enable the write callback.
159 curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, ChunkLoader);
160 curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, &chunks);
162 // synchronous request of the body data
163 CURLcode result = curl_easy_perform(curlHandle);
165 // chunks should now contain all of the chunked data. Reassemble into a single vector
167 for(size_t i = 0; i < chunks.size(); ++i)
169 dataSize += chunks[i].data.capacity();
171 dataBuffer.ResizeUninitialized(dataSize);
173 if(DALI_LIKELY(dataSize > 0))
175 std::uint8_t* dataBufferPtr = dataBuffer.Begin();
176 for(size_t i = 0; i < chunks.size(); ++i)
178 memcpy(dataBufferPtr, &chunks[i].data[0], chunks[i].data.capacity());
179 dataBufferPtr += chunks[i].data.capacity();
186 bool DownloadFile(CURL* curlHandle,
187 const std::string& url,
188 Dali::Vector<uint8_t>& dataBuffer,
190 size_t maximumAllowedSizeBytes,
193 CURLcode result(CURLE_OK);
195 // setup curl to download just the header so we can extract the content length
196 ConfigureCurlOptions(curlHandle, url);
197 curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, DummyWrite);
198 if(errorBuffer != nullptr)
203 // perform the request to get the header
204 result = curl_easy_perform(curlHandle);
206 if(result != CURLE_OK)
208 if(errorBuffer != nullptr)
210 DALI_LOG_ERROR("Failed to download http header for \"%s\" with error code %d (%s)\n", url.c_str(), result, &errorBuffer[0]);
214 DALI_LOG_ERROR("Failed to download http header for \"%s\" with error code %d\n", url.c_str(), result);
219 // get the content length, -1 == size is not known
221 curl_easy_getinfo(curlHandle, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &size);
225 result = DownloadFileDataByChunk(curlHandle, dataBuffer, dataSize);
227 else if(size >= static_cast<curl_off_t>(maximumAllowedSizeBytes))
229 DALI_LOG_ERROR("File content length %" CURL_FORMAT_CURL_OFF_T " > max allowed %zu \"%s\" \n", size, maximumAllowedSizeBytes, url.c_str());
234 // If we know the size up front, allocate once and avoid chunk copies.
235 dataSize = static_cast<size_t>(size);
236 result = DownloadFileDataWithSize(curlHandle, dataBuffer, dataSize);
237 if(result != CURLE_OK)
239 LogCurlResult(result, errorBuffer, url, "Failed to download file, trying to load by chunk");
240 // In the case where the size is wrong (e.g. on a proxy server that rewrites data),
241 // the data buffer will be corrupt. In this case, try again using the chunk writer.
242 result = DownloadFileDataByChunk(curlHandle, dataBuffer, dataSize);
246 LogCurlResult(result, errorBuffer, url, "Failed to download image file");
248 if(result != CURLE_OK)
255 } // unnamed namespace
259 CurlEnvironment::CurlEnvironment()
261 // Must be called before we attempt any loads. e.g. by using curl_easy_init()
262 // and before we start any threads.
263 curl_global_init(CURL_GLOBAL_ALL);
266 CurlEnvironment::~CurlEnvironment()
268 curl_global_cleanup();
271 bool DownloadRemoteFileIntoMemory(const std::string& url,
272 Dali::Vector<uint8_t>& dataBuffer,
274 size_t maximumAllowedSizeBytes)
280 DALI_LOG_WARNING("empty url requested \n");
284 // start a libcurl easy session, this internally calls curl_global_init, if we ever have more than one download
285 // thread we need to explicity call curl_global_init() on startup from a single thread.
287 CURL* curlHandle = curl_easy_init();
290 std::vector<char> errorBuffer(CURL_ERROR_SIZE);
291 curl_easy_setopt(curlHandle, CURLOPT_ERRORBUFFER, &errorBuffer[0]);
292 result = DownloadFile(curlHandle, url, dataBuffer, dataSize, maximumAllowedSizeBytes, &errorBuffer[0]);
295 curl_easy_cleanup(curlHandle);
300 } // namespace Network
302 } // namespace TizenPlatform