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