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