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 "file-download.h"
22 #include <dali/integration-api/debug.h>
23 #include <curl/curl.h>
27 #include "portable/file-closer.h"
29 #ifdef TPK_CURL_ENABLED
30 #include <tpkp_curl.h>
31 #endif // TPK_CURL_ENABLED
33 using namespace Dali::Integration;
38 namespace TizenPlatform
41 namespace // unnamed namespace
44 const int CONNECTION_TIMEOUT_SECONDS( 30L );
45 const long VERBOSE_MODE = 0L; // 0 == off, 1 == on
46 const long CLOSE_CONNECTION_ON_ERROR = 1L; // 0 == off, 1 == on
47 const long EXCLUDE_HEADER = 0L;
48 const long INCLUDE_HEADER = 1L;
49 const long INCLUDE_BODY = 0L;
50 const long EXCLUDE_BODY = 1L;
53 * Curl library environment. Direct initialize ensures it's constructed before adaptor
54 * or application creates any threads.
56 static Dali::TizenPlatform::Network::CurlEnvironment gCurlEnvironment;
59 void ConfigureCurlOptions( CURL* curlHandle, const std::string& url )
61 curl_easy_setopt( curlHandle, CURLOPT_URL, url.c_str() );
62 curl_easy_setopt( curlHandle, CURLOPT_VERBOSE, VERBOSE_MODE );
64 // CURLOPT_FAILONERROR is not fail-safe especially when authentication is involved ( see manual )
65 curl_easy_setopt( curlHandle, CURLOPT_FAILONERROR, CLOSE_CONNECTION_ON_ERROR );
66 curl_easy_setopt( curlHandle, CURLOPT_CONNECTTIMEOUT, CONNECTION_TIMEOUT_SECONDS );
67 curl_easy_setopt( curlHandle, CURLOPT_HEADER, INCLUDE_HEADER );
68 curl_easy_setopt( curlHandle, CURLOPT_NOBODY, EXCLUDE_BODY );
70 #ifdef TPK_CURL_ENABLED
71 // Apply certificate pinning on Tizen
72 curl_easy_setopt( curlHandle, CURLOPT_SSL_CTX_FUNCTION, tpkp_curl_ssl_ctx_callback );
73 #endif // TPK_CURL_ENABLED
76 // Without a write function or a buffer (file descriptor) to write to, curl will pump out
77 // header/body contents to stdout
78 size_t DummyWrite(char *ptr, size_t size, size_t nmemb, void *userdata)
85 std::vector< uint8_t > data;
88 size_t ChunkLoader(char *ptr, size_t size, size_t nmemb, void *userdata)
90 std::vector<ChunkData>* chunks = static_cast<std::vector<ChunkData>*>( userdata );
91 int numBytes = size*nmemb;
92 chunks->push_back( ChunkData() );
93 ChunkData& chunkData = (*chunks)[chunks->size()-1];
94 chunkData.data.reserve( numBytes );
95 memcpy( &chunkData.data[0], ptr, numBytes );
100 CURLcode DownloadFileDataWithSize( CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t dataSize )
102 CURLcode result( CURLE_OK );
103 dataBuffer.Resize( dataSize );
106 Dali::Internal::Platform::FileCloser fileCloser( static_cast<void*>(&dataBuffer[0]), dataSize, "wb" );
107 FILE* dataBufferFilePointer = fileCloser.GetFile();
108 if( NULL != dataBufferFilePointer )
110 // we only want the body which contains the file data
111 curl_easy_setopt( curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER );
112 curl_easy_setopt( curlHandle, CURLOPT_NOBODY, INCLUDE_BODY );
114 // disable the write callback, and get curl to write directly into our data buffer
115 curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, NULL );
116 curl_easy_setopt( curlHandle, CURLOPT_WRITEDATA, dataBufferFilePointer );
118 // synchronous request of the body data
119 result = curl_easy_perform( curlHandle );
124 CURLcode DownloadFileDataByChunk( CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t& dataSize )
127 std::vector< ChunkData > chunks;
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 // Enable the write callback.
134 curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, ChunkLoader );
135 curl_easy_setopt( curlHandle, CURLOPT_WRITEDATA, &chunks );
137 // synchronous request of the body data
138 CURLcode result = curl_easy_perform( curlHandle );
140 // chunks should now contain all of the chunked data. Reassemble into a single vector
142 for( size_t i=0; i<chunks.size() ; ++i )
144 dataSize += chunks[i].data.capacity();
146 dataBuffer.Resize(dataSize);
149 for( size_t i=0; i<chunks.size() ; ++i )
151 memcpy( &dataBuffer[offset], &chunks[i].data[0], chunks[i].data.capacity() );
152 offset += chunks[i].data.capacity();
158 bool DownloadFile( CURL* curlHandle,
159 const std::string& url,
160 Dali::Vector<uint8_t>& dataBuffer,
162 size_t maximumAllowedSizeBytes )
164 CURLcode result( CURLE_OK );
167 // setup curl to download just the header so we can extract the content length
168 ConfigureCurlOptions( curlHandle, url );
170 curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, DummyWrite);
172 // perform the request to get the header
173 result = curl_easy_perform( curlHandle );
175 if( result != CURLE_OK)
177 DALI_LOG_WARNING( "Failed to download http header for \"%s\" with error code %d\n", url.c_str(), result );
181 // get the content length, -1 == size is not known
182 curl_easy_getinfo( curlHandle,CURLINFO_CONTENT_LENGTH_DOWNLOAD , &size );
185 if( size >= maximumAllowedSizeBytes )
187 DALI_LOG_WARNING( "File content length %f > max allowed %zu \"%s\" \n", size, maximumAllowedSizeBytes, url.c_str() );
192 dataSize = static_cast<size_t>( size );
193 result = DownloadFileDataWithSize( curlHandle, dataBuffer, dataSize );
197 result = DownloadFileDataByChunk( curlHandle, dataBuffer, dataSize );
200 if( result != CURLE_OK )
202 DALI_LOG_WARNING( "Failed to download image file \"%s\" with error code %d\n", url.c_str(), result );
209 } // unnamed namespace
215 CurlEnvironment::CurlEnvironment()
217 // Must be called before we attempt any loads. e.g. by using curl_easy_init()
218 // and before we start any threads.
219 curl_global_init(CURL_GLOBAL_ALL);
222 CurlEnvironment::~CurlEnvironment()
224 curl_global_cleanup();
228 bool DownloadRemoteFileIntoMemory( const std::string& url,
229 Dali::Vector<uint8_t>& dataBuffer,
231 size_t maximumAllowedSizeBytes )
235 DALI_LOG_WARNING("empty url requested \n");
239 // start a libcurl easy session, this internally calls curl_global_init, if we ever have more than one download
240 // thread we need to explicity call curl_global_init() on startup from a single thread.
242 CURL* curlHandle = curl_easy_init();
244 bool result = DownloadFile( curlHandle, url, dataBuffer, dataSize, maximumAllowedSizeBytes);
247 curl_easy_cleanup( curlHandle );
249 #ifdef TPK_CURL_ENABLED
250 // Clean up tpkp(the module for certificate pinning) resources on Tizen
252 #endif // TPK_CURL_ENABLED
257 } // namespace Network
259 } // namespace TizenPlatform