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();
124 setbuf(dataBufferFilePointer, NULL); // Turn buffering off
126 if(NULL != dataBufferFilePointer)
128 // we only want the body which contains the file data
129 curl_easy_setopt(curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER);
130 curl_easy_setopt(curlHandle, CURLOPT_NOBODY, INCLUDE_BODY);
132 // disable the write callback, and get curl to write directly into our data buffer
133 curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, NULL);
134 curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, dataBufferFilePointer);
136 // synchronous request of the body data
137 result = curl_easy_perform(curlHandle);
142 CURLcode DownloadFileDataByChunk(CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t& dataSize)
145 std::vector<ChunkData> chunks;
147 // we only want the body which contains the file data
148 curl_easy_setopt(curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER);
149 curl_easy_setopt(curlHandle, CURLOPT_NOBODY, INCLUDE_BODY);
151 // Enable the write callback.
152 curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, ChunkLoader);
153 curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, &chunks);
155 // synchronous request of the body data
156 CURLcode result = curl_easy_perform(curlHandle);
158 // chunks should now contain all of the chunked data. Reassemble into a single vector
160 for(size_t i = 0; i < chunks.size(); ++i)
162 dataSize += chunks[i].data.capacity();
164 dataBuffer.ResizeUninitialized(dataSize);
166 if(DALI_LIKELY(dataSize > 0))
168 std::uint8_t* dataBufferPtr = dataBuffer.Begin();
169 for(size_t i = 0; i < chunks.size(); ++i)
171 memcpy(dataBufferPtr, &chunks[i].data[0], chunks[i].data.capacity());
172 dataBufferPtr += chunks[i].data.capacity();
179 bool DownloadFile(CURL* curlHandle,
180 const std::string& url,
181 Dali::Vector<uint8_t>& dataBuffer,
183 size_t maximumAllowedSizeBytes,
186 CURLcode result(CURLE_OK);
188 // setup curl to download just the header so we can extract the content length
189 ConfigureCurlOptions(curlHandle, url);
190 curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, DummyWrite);
191 if(errorBuffer != nullptr)
196 // perform the request to get the header
197 result = curl_easy_perform(curlHandle);
199 if(result != CURLE_OK)
201 if(errorBuffer != nullptr)
203 DALI_LOG_ERROR("Failed to download http header for \"%s\" with error code %d (%s)\n", url.c_str(), result, &errorBuffer[0]);
207 DALI_LOG_ERROR("Failed to download http header for \"%s\" with error code %d\n", url.c_str(), result);
212 // get the content length, -1 == size is not known
214 curl_easy_getinfo(curlHandle, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &size);
218 result = DownloadFileDataByChunk(curlHandle, dataBuffer, dataSize);
220 else if(size >= static_cast<curl_off_t>(maximumAllowedSizeBytes))
222 DALI_LOG_ERROR("File content length %" CURL_FORMAT_CURL_OFF_T " > max allowed %zu \"%s\" \n", size, maximumAllowedSizeBytes, url.c_str());
227 // If we know the size up front, allocate once and avoid chunk copies.
228 dataSize = static_cast<size_t>(size);
229 result = DownloadFileDataWithSize(curlHandle, dataBuffer, dataSize);
230 if(result != CURLE_OK)
232 LogCurlResult(result, errorBuffer, url, "Failed to download file, trying to load by chunk");
233 // In the case where the size is wrong (e.g. on a proxy server that rewrites data),
234 // the data buffer will be corrupt. In this case, try again using the chunk writer.
235 result = DownloadFileDataByChunk(curlHandle, dataBuffer, dataSize);
239 LogCurlResult(result, errorBuffer, url, "Failed to download image file");
241 if(result != CURLE_OK)
248 } // unnamed namespace
252 CurlEnvironment::CurlEnvironment()
254 // Must be called before we attempt any loads. e.g. by using curl_easy_init()
255 // and before we start any threads.
256 curl_global_init(CURL_GLOBAL_ALL);
259 CurlEnvironment::~CurlEnvironment()
261 curl_global_cleanup();
264 bool DownloadRemoteFileIntoMemory(const std::string& url,
265 Dali::Vector<uint8_t>& dataBuffer,
267 size_t maximumAllowedSizeBytes)
273 DALI_LOG_WARNING("empty url requested \n");
277 // start a libcurl easy session, this internally calls curl_global_init, if we ever have more than one download
278 // thread we need to explicity call curl_global_init() on startup from a single thread.
280 CURL* curlHandle = curl_easy_init();
283 std::vector<char> errorBuffer(CURL_ERROR_SIZE);
284 curl_easy_setopt(curlHandle, CURLOPT_ERRORBUFFER, &errorBuffer[0]);
285 result = DownloadFile(curlHandle, url, dataBuffer, dataSize, maximumAllowedSizeBytes, &errorBuffer[0]);
288 curl_easy_cleanup(curlHandle);
293 } // namespace Network
295 } // namespace TizenPlatform