2 * Copyright (c) 2015 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>
26 #include "portable/file-closer.h"
28 #ifndef DALI_PROFILE_UBUNTU
29 #include <tpkp_curl.h>
30 #endif // DALI_PROFILE_UBUNTU
32 using namespace Dali::Integration;
37 namespace TizenPlatform
40 namespace // unnamed namespace
43 const int CONNECTION_TIMEOUT_SECONDS( 30L );
44 const long VERBOSE_MODE = 0L; // 0 == off, 1 == on
45 const long CLOSE_CONNECTION_ON_ERROR = 1L; // 0 == off, 1 == on
46 const long EXCLUDE_HEADER = 0L;
47 const long INCLUDE_HEADER = 1L;
48 const long INCLUDE_BODY = 0L;
49 const long EXCLUDE_BODY = 1L;
51 void ConfigureCurlOptions( CURL* curl_handle, const std::string& url )
53 curl_easy_setopt( curl_handle, CURLOPT_URL, url.c_str() );
54 curl_easy_setopt( curl_handle, CURLOPT_VERBOSE, VERBOSE_MODE );
56 // CURLOPT_FAILONERROR is not fail-safe especially when authentication is involved ( see manual )
57 curl_easy_setopt( curl_handle, CURLOPT_FAILONERROR, CLOSE_CONNECTION_ON_ERROR );
58 curl_easy_setopt( curl_handle, CURLOPT_CONNECTTIMEOUT, CONNECTION_TIMEOUT_SECONDS );
59 curl_easy_setopt( curl_handle, CURLOPT_HEADER, INCLUDE_HEADER );
60 curl_easy_setopt( curl_handle, CURLOPT_NOBODY, EXCLUDE_BODY );
62 #ifndef DALI_PROFILE_UBUNTU
63 // Apply certificate pinning on Tizen
64 curl_easy_setopt( curl_handle, CURLOPT_SSL_CTX_FUNCTION, tpkp_curl_ssl_ctx_callback );
65 #endif // DALI_PROFILE_UBUNTU
68 // Without a write function or a buffer (file descriptor) to write to, curl will pump out
69 // header/body contents to stdout
70 size_t DummyWrite(char *ptr, size_t size, size_t nmemb, void *userdata)
76 bool DownloadFile( CURL* curl_handle,
77 const std::string& url,
78 Dali::Vector<uint8_t>& dataBuffer,
80 size_t maximumAllowedSizeBytes )
82 CURLcode res( CURLE_OK );
85 // setup curl to download just the header so we can extract the content length
86 ConfigureCurlOptions( curl_handle, url );
88 curl_easy_setopt( curl_handle, CURLOPT_WRITEFUNCTION, DummyWrite);
90 // perform the request to get the header
91 res = curl_easy_perform( curl_handle );
95 DALI_LOG_WARNING( "Failed to download http header for \"%s\" with error code %d\n", url.c_str(), res );
99 // get the content length, -1 == size is not known
100 curl_easy_getinfo( curl_handle,CURLINFO_CONTENT_LENGTH_DOWNLOAD , &size );
104 DALI_LOG_WARNING( "Header missing content length \"%s\" \n", url.c_str() );
107 if( size >= maximumAllowedSizeBytes )
109 DALI_LOG_WARNING( "File content length %f > max allowed %zu \"%s\" \n", size, maximumAllowedSizeBytes, url.c_str() );
113 dataSize = static_cast<size_t>( size );
115 dataBuffer.Resize( dataSize );
118 Dali::Internal::Platform::FileCloser fileCloser( static_cast<void*>(&dataBuffer[0]), dataSize, "wb" );
119 FILE* dataBufferFilePointer = fileCloser.GetFile();
120 if( NULL != dataBufferFilePointer )
122 // we only want the body which contains the file data
123 curl_easy_setopt( curl_handle, CURLOPT_HEADER, EXCLUDE_HEADER );
124 curl_easy_setopt( curl_handle, CURLOPT_NOBODY, INCLUDE_BODY );
126 // disable the write callback, and get curl to write directly into our data buffer
127 curl_easy_setopt( curl_handle, CURLOPT_WRITEFUNCTION, NULL );
128 curl_easy_setopt( curl_handle, CURLOPT_WRITEDATA, dataBufferFilePointer );
130 // synchronous request of the body data
131 res = curl_easy_perform( curl_handle );
133 if( CURLE_OK != res )
135 DALI_LOG_WARNING( "Failed to download image file \"%s\" with error code %d\n", url.c_str(), res );
141 } // unnamed namespace
145 bool Network::DownloadRemoteFileIntoMemory( const std::string& url,
146 Dali::Vector<uint8_t>& dataBuffer,
148 size_t maximumAllowedSizeBytes )
152 DALI_LOG_WARNING("empty url requested \n");
156 // start a libcurl easy session, this internally calls curl_global_init, if we ever have more than one download
157 // thread we need to explicity call curl_global_init() on startup from a single thread.
159 CURL* curl_handle = curl_easy_init();
161 bool result = DownloadFile( curl_handle, url, dataBuffer, dataSize, maximumAllowedSizeBytes);
164 curl_easy_cleanup( curl_handle );
166 #ifndef DALI_PROFILE_UBUNTU
167 // Clean up tpkp(the module for certificate pinning) resources on Tizen
169 #endif // DALI_PROFILE_UBUNTU
175 } // namespace TizenPlatform