Dali 3D User Interface Engine
file-download.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 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 "file-download.h"
20 
21 // EXTERNAL INCLUDES
23 #include <curl/curl.h>
24 
25 // INTERNAL INCLUDES
26 #include "portable/file-closer.h"
27 
28 #ifdef TPK_CURL_ENABLED
29 #include <tpkp_curl.h>
30 #endif // TPK_CURL_ENABLED
31 
32 using namespace Dali::Integration;
33 
34 namespace Dali
35 {
36 
37 namespace TizenPlatform
38 {
39 
40 namespace // unnamed namespace
41 {
42 
43 const int CONNECTION_TIMEOUT_SECONDS( 30L );
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 void ConfigureCurlOptions( CURL* curl_handle, const std::string& url )
52 {
53  curl_easy_setopt( curl_handle, CURLOPT_URL, url.c_str() );
54  curl_easy_setopt( curl_handle, CURLOPT_VERBOSE, VERBOSE_MODE );
55 
56  // CURLOPT_FAILONERROR is not fail-safe especially when authentication is involved ( see manual )
57  curl_easy_setopt( curl_handle, CURLOPT_FAILONERROR, CLOSE_CONNECTION_ON_ERROR );
58  curl_easy_setopt( curl_handle, CURLOPT_CONNECTTIMEOUT, CONNECTION_TIMEOUT_SECONDS );
59  curl_easy_setopt( curl_handle, CURLOPT_HEADER, INCLUDE_HEADER );
60  curl_easy_setopt( curl_handle, CURLOPT_NOBODY, EXCLUDE_BODY );
61 
62 #ifdef TPK_CURL_ENABLED
63  // Apply certificate pinning on Tizen
64  curl_easy_setopt( curl_handle, CURLOPT_SSL_CTX_FUNCTION, tpkp_curl_ssl_ctx_callback );
65 #endif // TPK_CURL_ENABLED
66 }
67 
68 // Without a write function or a buffer (file descriptor) to write to, curl will pump out
69 // header/body contents to stdout
70 size_t DummyWrite(char *ptr, size_t size, size_t nmemb, void *userdata)
71 {
72  return size * nmemb;
73 }
74 
75 
76 bool DownloadFile( CURL* curl_handle,
77  const std::string& url,
78  Dali::Vector<uint8_t>& dataBuffer,
79  size_t& dataSize,
80  size_t maximumAllowedSizeBytes )
81 {
82  CURLcode res( CURLE_OK );
83  double size(0);
84 
85  // setup curl to download just the header so we can extract the content length
86  ConfigureCurlOptions( curl_handle, url );
87 
88  curl_easy_setopt( curl_handle, CURLOPT_WRITEFUNCTION, DummyWrite);
89 
90  // perform the request to get the header
91  res = curl_easy_perform( curl_handle );
92 
93  if( res != CURLE_OK)
94  {
95  DALI_LOG_WARNING( "Failed to download http header for \"%s\" with error code %d\n", url.c_str(), res );
96  return false;
97  }
98 
99  // get the content length, -1 == size is not known
100  curl_easy_getinfo( curl_handle,CURLINFO_CONTENT_LENGTH_DOWNLOAD , &size );
101 
102  if( size < 1 )
103  {
104  DALI_LOG_WARNING( "Header missing content length \"%s\" \n", url.c_str() );
105  return false;
106  }
107  if( size >= maximumAllowedSizeBytes )
108  {
109  DALI_LOG_WARNING( "File content length %f > max allowed %zu \"%s\" \n", size, maximumAllowedSizeBytes, url.c_str() );
110  return false;
111  }
112 
113  dataSize = static_cast<size_t>( size );
114 
115  dataBuffer.Resize( dataSize );
116 
117  // create
118  Dali::Internal::Platform::FileCloser fileCloser( static_cast<void*>(&dataBuffer[0]), dataSize, "wb" );
119  FILE* dataBufferFilePointer = fileCloser.GetFile();
120  if( NULL != dataBufferFilePointer )
121  {
122  // we only want the body which contains the file data
123  curl_easy_setopt( curl_handle, CURLOPT_HEADER, EXCLUDE_HEADER );
124  curl_easy_setopt( curl_handle, CURLOPT_NOBODY, INCLUDE_BODY );
125 
126  // disable the write callback, and get curl to write directly into our data buffer
127  curl_easy_setopt( curl_handle, CURLOPT_WRITEFUNCTION, NULL );
128  curl_easy_setopt( curl_handle, CURLOPT_WRITEDATA, dataBufferFilePointer );
129 
130  // synchronous request of the body data
131  res = curl_easy_perform( curl_handle );
132 
133  if( CURLE_OK != res )
134  {
135  DALI_LOG_WARNING( "Failed to download image file \"%s\" with error code %d\n", url.c_str(), res );
136  return false;
137  }
138  }
139  return true;
140 }
141 } // unnamed namespace
142 
143 
144 
145 bool Network::DownloadRemoteFileIntoMemory( const std::string& url,
146  Dali::Vector<uint8_t>& dataBuffer,
147  size_t& dataSize,
148  size_t maximumAllowedSizeBytes )
149 {
150  if( url.empty() )
151  {
152  DALI_LOG_WARNING("empty url requested \n");
153  return false;
154  }
155 
156  // start a libcurl easy session, this internally calls curl_global_init, if we ever have more than one download
157  // thread we need to explicity call curl_global_init() on startup from a single thread.
158 
159  CURL* curl_handle = curl_easy_init();
160 
161  bool result = DownloadFile( curl_handle, url, dataBuffer, dataSize, maximumAllowedSizeBytes);
162 
163  // clean up session
164  curl_easy_cleanup( curl_handle );
165 
166 #ifdef TPK_CURL_ENABLED
167  // Clean up tpkp(the module for certificate pinning) resources on Tizen
168  tpkp_curl_cleanup();
169 #endif // TPK_CURL_ENABLED
170 
171  return result;
172 }
173 
174 
175 } // namespace TizenPlatform
176 
177 } // namespace Dali
Dali Docs Home
Read more about Dali