Migrate to openssl 1.1
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / common / 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 <dali/internal/imaging/common/file-download.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <pthread.h>
24 #include <curl/curl.h>
25 #include <cstring>
26
27 // INTERNAL INCLUDES
28 #include <dali/internal/system/common/file-writer.h>
29
30 #ifdef TPK_CURL_ENABLED
31 #include <tpkp_curl.h>
32 #endif // TPK_CURL_ENABLED
33
34 using namespace Dali::Integration;
35
36 namespace Dali
37 {
38
39 namespace TizenPlatform
40 {
41
42 namespace // unnamed namespace
43 {
44
45 const int CONNECTION_TIMEOUT_SECONDS( 30L );
46 const int TIMEOUT_SECONDS( 120L );
47 const long VERBOSE_MODE = 0L;                // 0 == off, 1 == on
48 const long CLOSE_CONNECTION_ON_ERROR = 1L;   // 0 == off, 1 == on
49 const long EXCLUDE_HEADER = 0L;
50 const long INCLUDE_HEADER = 1L;
51 const long INCLUDE_BODY = 0L;
52 const long EXCLUDE_BODY = 1L;
53
54 /**
55  * Curl library environment. Direct initialize ensures it's constructed before adaptor
56  * or application creates any threads.
57  */
58 static Dali::TizenPlatform::Network::CurlEnvironment gCurlEnvironment;
59
60 void ConfigureCurlOptions( CURL* curlHandle, const std::string& url )
61 {
62   curl_easy_setopt( curlHandle, CURLOPT_URL, url.c_str() );
63   curl_easy_setopt( curlHandle, CURLOPT_VERBOSE, VERBOSE_MODE );
64
65   // CURLOPT_FAILONERROR is not fail-safe especially when authentication is involved ( see manual )
66   // Removed CURLOPT_FAILONERROR option
67   curl_easy_setopt( curlHandle, CURLOPT_CONNECTTIMEOUT, CONNECTION_TIMEOUT_SECONDS );
68   curl_easy_setopt( curlHandle, CURLOPT_TIMEOUT, TIMEOUT_SECONDS );
69   curl_easy_setopt( curlHandle, CURLOPT_HEADER, INCLUDE_HEADER );
70   curl_easy_setopt( curlHandle, CURLOPT_NOBODY, EXCLUDE_BODY );
71
72 #ifdef TPK_CURL_ENABLED
73   // Apply certificate pinning on Tizen
74   curl_easy_setopt( curlHandle, CURLOPT_SSL_CTX_FUNCTION, tpkp_curl_ssl_ctx_callback );
75 #endif // TPK_CURL_ENABLED
76 }
77
78 // Without a write function or a buffer (file descriptor) to write to, curl will pump out
79 // header/body contents to stdout
80 size_t DummyWrite(char *ptr, size_t size, size_t nmemb, void *userdata)
81 {
82   return size * nmemb;
83 }
84
85 struct ChunkData
86 {
87   std::vector< uint8_t > data;
88 };
89
90 size_t ChunkLoader(char *ptr, size_t size, size_t nmemb, void *userdata)
91 {
92   std::vector<ChunkData>* chunks = static_cast<std::vector<ChunkData>*>( userdata );
93   int numBytes = size*nmemb;
94   chunks->push_back( ChunkData() );
95   ChunkData& chunkData = (*chunks)[chunks->size()-1];
96   chunkData.data.reserve( numBytes );
97   memcpy( &chunkData.data[0], ptr, numBytes );
98   return numBytes;
99 }
100
101
102 CURLcode DownloadFileDataWithSize( CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t dataSize )
103 {
104   CURLcode result( CURLE_OK );
105
106   // create
107   Dali::Internal::Platform::FileWriter fileWriter( dataBuffer, dataSize );
108   FILE* dataBufferFilePointer = fileWriter.GetFile();
109   if( NULL != dataBufferFilePointer )
110   {
111     // we only want the body which contains the file data
112     curl_easy_setopt( curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER );
113     curl_easy_setopt( curlHandle, CURLOPT_NOBODY, INCLUDE_BODY );
114
115     // disable the write callback, and get curl to write directly into our data buffer
116     curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, NULL );
117     curl_easy_setopt( curlHandle, CURLOPT_WRITEDATA, dataBufferFilePointer );
118
119     // synchronous request of the body data
120     result = curl_easy_perform( curlHandle );
121   }
122   return result;
123 }
124
125 CURLcode DownloadFileDataByChunk( CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t& dataSize )
126 {
127   // create
128   std::vector< ChunkData > chunks;
129
130   // we only want the body which contains the file data
131   curl_easy_setopt( curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER );
132   curl_easy_setopt( curlHandle, CURLOPT_NOBODY, INCLUDE_BODY );
133
134   // Enable the write callback.
135   curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, ChunkLoader );
136   curl_easy_setopt( curlHandle, CURLOPT_WRITEDATA, &chunks );
137
138   // synchronous request of the body data
139   CURLcode result = curl_easy_perform( curlHandle );
140
141   // chunks should now contain all of the chunked data. Reassemble into a single vector
142   dataSize = 0;
143   for( size_t i=0; i<chunks.size() ; ++i )
144   {
145     dataSize += chunks[i].data.capacity();
146   }
147   dataBuffer.Resize(dataSize);
148
149   size_t offset = 0;
150   for( size_t i=0; i<chunks.size() ; ++i )
151   {
152     memcpy( &dataBuffer[offset], &chunks[i].data[0], chunks[i].data.capacity() );
153     offset += chunks[i].data.capacity();
154   }
155
156   return result;
157 }
158
159 bool DownloadFile( CURL* curlHandle,
160                    const std::string& url,
161                    Dali::Vector<uint8_t>& dataBuffer,
162                    size_t& dataSize,
163                    size_t maximumAllowedSizeBytes )
164 {
165   CURLcode result( CURLE_OK );
166   double size(0);
167
168   // setup curl to download just the header so we can extract the content length
169   ConfigureCurlOptions( curlHandle, url );
170
171   curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, DummyWrite);
172
173   // perform the request to get the header
174   result = curl_easy_perform( curlHandle );
175
176   if( result != CURLE_OK)
177   {
178     DALI_LOG_ERROR( "Failed to download http header for \"%s\" with error code %d\n", url.c_str(), result );
179     return false;
180   }
181
182   // get the content length, -1 == size is not known
183   curl_easy_getinfo( curlHandle,CURLINFO_CONTENT_LENGTH_DOWNLOAD , &size );
184
185
186   if( size >= maximumAllowedSizeBytes )
187   {
188     DALI_LOG_ERROR( "File content length %f > max allowed %zu \"%s\" \n", size, maximumAllowedSizeBytes, url.c_str() );
189     return false;
190   }
191   else if( size > 0 )
192   {
193     // If we know the size up front, allocate once and avoid chunk copies.
194     dataSize = static_cast<size_t>( size );
195     result = DownloadFileDataWithSize( curlHandle, dataBuffer, dataSize );
196   }
197   else
198   {
199     result = DownloadFileDataByChunk( curlHandle, dataBuffer, dataSize );
200   }
201
202   if( result != CURLE_OK )
203   {
204     DALI_LOG_ERROR( "Failed to download image file \"%s\" with error code %d\n", url.c_str(), result );
205     return false;
206   }
207   return true;
208 }
209
210
211 } // unnamed namespace
212
213
214 namespace Network
215 {
216
217 CurlEnvironment::CurlEnvironment()
218 {
219   // Must be called before we attempt any loads. e.g. by using curl_easy_init()
220   // and before we start any threads.
221   curl_global_init(CURL_GLOBAL_ALL);
222 }
223
224 CurlEnvironment::~CurlEnvironment()
225 {
226   curl_global_cleanup();
227 }
228
229 bool DownloadRemoteFileIntoMemory( const std::string& url,
230                                    Dali::Vector<uint8_t>& dataBuffer,
231                                    size_t& dataSize,
232                                    size_t maximumAllowedSizeBytes )
233 {
234   bool result = false;
235
236   if( url.empty() )
237   {
238     DALI_LOG_WARNING("empty url requested \n");
239     return false;
240   }
241
242   // start a libcurl easy session, this internally calls curl_global_init, if we ever have more than one download
243   // thread we need to explicity call curl_global_init() on startup from a single thread.
244
245   CURL* curlHandle = curl_easy_init();
246   if ( curlHandle )
247   {
248     result = DownloadFile( curlHandle, url, dataBuffer,  dataSize, maximumAllowedSizeBytes);
249
250     // clean up session
251     curl_easy_cleanup( curlHandle );
252
253 #ifdef TPK_CURL_ENABLED
254     // Clean up tpkp(the module for certificate pinning) resources on Tizen
255     tpkp_curl_cleanup();
256 #endif // TPK_CURL_ENABLED
257   }
258   return result;
259 }
260
261 } // namespace Network
262
263 } // namespace TizenPlatform
264
265 } // namespace Dali