Merge "Fix svace issue - uninitialized class member" into devel/master
[platform/core/uifw/dali-adaptor.git] / platform-abstractions / tizen / resource-loader / network / file-download.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // HEADER
19 #include "file-download.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <curl/curl.h>
24
25 // INTERNAL INCLUDES
26 #include "portable/file-closer.h"
27
28 #ifdef TPK_CURL_ENABLED
29 #include <tpkp_curl.h>
30 #endif // TPK_CURL_ENABLED
31
32 using namespace Dali::Integration;
33
34 namespace Dali
35 {
36
37 namespace TizenPlatform
38 {
39
40 namespace // unnamed namespace
41 {
42
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;
50
51 /**
52  * Curl library environment. Direct initialize ensures it's constructed before adaptor
53  * or application creates any threads.
54  */
55 static Dali::TizenPlatform::Network::CurlEnvironment gCurlEnvironment;
56
57
58 void ConfigureCurlOptions( CURL* curl_handle, const std::string& url )
59 {
60   curl_easy_setopt( curl_handle, CURLOPT_URL, url.c_str() );
61   curl_easy_setopt( curl_handle, CURLOPT_VERBOSE, VERBOSE_MODE );
62
63   // CURLOPT_FAILONERROR is not fail-safe especially when authentication is involved ( see manual )
64   curl_easy_setopt( curl_handle, CURLOPT_FAILONERROR, CLOSE_CONNECTION_ON_ERROR );
65   curl_easy_setopt( curl_handle, CURLOPT_CONNECTTIMEOUT, CONNECTION_TIMEOUT_SECONDS );
66   curl_easy_setopt( curl_handle, CURLOPT_HEADER, INCLUDE_HEADER );
67   curl_easy_setopt( curl_handle, CURLOPT_NOBODY, EXCLUDE_BODY );
68
69 #ifdef TPK_CURL_ENABLED
70   // Apply certificate pinning on Tizen
71   curl_easy_setopt( curl_handle, CURLOPT_SSL_CTX_FUNCTION, tpkp_curl_ssl_ctx_callback );
72 #endif // TPK_CURL_ENABLED
73 }
74
75 // Without a write function or a buffer (file descriptor) to write to, curl will pump out
76 // header/body contents to stdout
77 size_t DummyWrite(char *ptr, size_t size, size_t nmemb, void *userdata)
78 {
79   return size * nmemb;
80 }
81
82
83 bool DownloadFile( CURL* curl_handle,
84                    const std::string& url,
85                    Dali::Vector<uint8_t>& dataBuffer,
86                    size_t& dataSize,
87                    size_t maximumAllowedSizeBytes )
88 {
89   CURLcode res( CURLE_OK );
90   double size(0);
91
92   // setup curl to download just the header so we can extract the content length
93   ConfigureCurlOptions( curl_handle, url );
94
95   curl_easy_setopt( curl_handle, CURLOPT_WRITEFUNCTION, DummyWrite);
96
97   // perform the request to get the header
98   res = curl_easy_perform( curl_handle );
99
100   if( res != CURLE_OK)
101   {
102     DALI_LOG_WARNING( "Failed to download http header for \"%s\" with error code %d\n", url.c_str(), res );
103     return false;
104   }
105
106   // get the content length, -1 == size is not known
107   curl_easy_getinfo( curl_handle,CURLINFO_CONTENT_LENGTH_DOWNLOAD , &size );
108
109   if( size < 1 )
110   {
111     DALI_LOG_WARNING( "Header missing content length \"%s\" \n", url.c_str() );
112     return false;
113   }
114   if( size >= maximumAllowedSizeBytes )
115   {
116     DALI_LOG_WARNING( "File content length %f > max allowed %zu \"%s\" \n", size, maximumAllowedSizeBytes, url.c_str() );
117     return false;
118   }
119
120   dataSize = static_cast<size_t>( size );
121
122   dataBuffer.Resize( dataSize );
123
124   // create
125   Dali::Internal::Platform::FileCloser fileCloser( static_cast<void*>(&dataBuffer[0]), dataSize, "wb" );
126   FILE* dataBufferFilePointer = fileCloser.GetFile();
127   if( NULL != dataBufferFilePointer )
128   {
129     // we only want the body which contains the file data
130     curl_easy_setopt( curl_handle, CURLOPT_HEADER, EXCLUDE_HEADER );
131     curl_easy_setopt( curl_handle, CURLOPT_NOBODY, INCLUDE_BODY );
132
133     // disable the write callback, and get curl to write directly into our data buffer
134     curl_easy_setopt( curl_handle, CURLOPT_WRITEFUNCTION, NULL );
135     curl_easy_setopt( curl_handle, CURLOPT_WRITEDATA, dataBufferFilePointer );
136
137     // synchronous request of the body data
138     res = curl_easy_perform( curl_handle );
139
140     if( CURLE_OK != res )
141     {
142       DALI_LOG_WARNING( "Failed to download image file \"%s\" with error code %d\n", url.c_str(), res );
143       return false;
144     }
145   }
146   return true;
147 }
148
149
150 } // unnamed namespace
151
152
153 namespace Network
154 {
155
156 CurlEnvironment::CurlEnvironment()
157 {
158   // Must be called before we attempt any loads. e.g. by using curl_easy_init()
159   // and before we start any threads.
160   curl_global_init(CURL_GLOBAL_ALL);
161 }
162
163 CurlEnvironment::~CurlEnvironment()
164 {
165   curl_global_cleanup();
166 }
167
168
169 bool DownloadRemoteFileIntoMemory( const std::string& url,
170                                    Dali::Vector<uint8_t>& dataBuffer,
171                                    size_t& dataSize,
172                                    size_t maximumAllowedSizeBytes )
173 {
174   if( url.empty() )
175   {
176     DALI_LOG_WARNING("empty url requested \n");
177     return false;
178   }
179
180   // start a libcurl easy session, this internally calls curl_global_init, if we ever have more than one download
181   // thread we need to explicity call curl_global_init() on startup from a single thread.
182
183   CURL* curl_handle = curl_easy_init();
184
185   bool result = DownloadFile( curl_handle, url, dataBuffer,  dataSize, maximumAllowedSizeBytes);
186
187   // clean up session
188   curl_easy_cleanup( curl_handle );
189
190 #ifdef TPK_CURL_ENABLED
191   // Clean up tpkp(the module for certificate pinning) resources on Tizen
192   tpkp_curl_cleanup();
193 #endif // TPK_CURL_ENABLED
194
195   return result;
196 }
197
198 } // namespace Network
199
200 } // namespace TizenPlatform
201
202 } // namespace Dali