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