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