Fix segfault with remote image download and tidy up
[platform/core/uifw/dali-adaptor.git] / platform-abstractions / tizen / resource-loader / network / file-download.cpp
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
22 #include <dali/integration-api/debug.h>
23 #include <curl/curl.h>
24
25 // INTERNAL INCLUDES
26 #include "portable/file-closer.h"
27
28
29 using namespace Dali::Integration;
30
31 namespace Dali
32 {
33
34 namespace TizenPlatform
35 {
36
37 namespace // unnamed namespace
38 {
39
40 const int CONNECTION_TIMEOUT_SECONDS( 30L );
41 const long VERBOSE_MODE = 0L;                // 0 == off, 1 == on
42 const long CLOSE_CONNECTION_ON_ERROR = 1L;   // 0 == off, 1 == on
43 const long EXCLUDE_HEADER = 0L;
44 const long INCLUDE_HEADER = 1L;
45 const long INCLUDE_BODY = 0L;
46 const long EXCLUDE_BODY = 1L;
47
48 void ConfigureCurlOptions( CURL* curl_handle, const std::string& url )
49 {
50   curl_easy_setopt( curl_handle, CURLOPT_URL, url.c_str() );
51   curl_easy_setopt( curl_handle, CURLOPT_VERBOSE, VERBOSE_MODE );
52
53   // CURLOPT_FAILONERROR is not fail-safe especially when authentication is involved ( see manual )
54   curl_easy_setopt( curl_handle, CURLOPT_FAILONERROR, CLOSE_CONNECTION_ON_ERROR );
55   curl_easy_setopt( curl_handle, CURLOPT_CONNECTTIMEOUT, CONNECTION_TIMEOUT_SECONDS );
56   curl_easy_setopt( curl_handle, CURLOPT_HEADER, INCLUDE_HEADER );
57   curl_easy_setopt( curl_handle, CURLOPT_NOBODY, EXCLUDE_BODY );
58 }
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 DummyWrite(char *ptr, size_t size, size_t nmemb, void *userdata)
63 {
64   return size * nmemb;
65 }
66
67
68 bool DownloadFile( CURL* curl_handle,
69                    const std::string& url,
70                    Dali::Vector<uint8_t>& dataBuffer,
71                    size_t& dataSize,
72                    size_t maximumAllowedSizeBytes )
73 {
74   CURLcode res( CURLE_OK );
75   double size(0);
76
77   // setup curl to download just the header so we can extract the content length
78   ConfigureCurlOptions( curl_handle, url );
79
80   curl_easy_setopt( curl_handle, CURLOPT_WRITEFUNCTION, DummyWrite);
81
82   // perform the request to get the header
83   res = curl_easy_perform( curl_handle );
84
85   if( res != CURLE_OK)
86   {
87     DALI_LOG_WARNING( "Failed to download http header for \"%s\" with error code %d\n", url.c_str(), res );
88     return false;
89   }
90
91   // get the content length, -1 == size is not known
92   curl_easy_getinfo( curl_handle,CURLINFO_CONTENT_LENGTH_DOWNLOAD , &size );
93
94   if( size < 1 )
95   {
96     DALI_LOG_WARNING( "Header missing content length \"%s\" \n", url.c_str() );
97     return false;
98   }
99   if( size >= maximumAllowedSizeBytes )
100   {
101     DALI_LOG_WARNING( "File content length %f > max allowed %zu \"%s\" \n", size, maximumAllowedSizeBytes, url.c_str() );
102     return false;
103   }
104
105   dataSize = static_cast<size_t>( size );
106
107   dataBuffer.Resize( dataSize );
108
109   // create
110   Dali::Internal::Platform::FileCloser fileCloser( static_cast<void*>(&dataBuffer[0]), dataSize, "wb" );
111   FILE* dataBufferFilePointer = fileCloser.GetFile();
112   if( NULL != dataBufferFilePointer )
113   {
114     // we only want the body which contains the file data
115     curl_easy_setopt( curl_handle, CURLOPT_HEADER, EXCLUDE_HEADER );
116     curl_easy_setopt( curl_handle, CURLOPT_NOBODY, INCLUDE_BODY );
117
118     // disable the write callback, and get curl to write directly into our data buffer
119     curl_easy_setopt( curl_handle, CURLOPT_WRITEFUNCTION, NULL );
120     curl_easy_setopt( curl_handle, CURLOPT_WRITEDATA, dataBufferFilePointer );
121
122     // synchronous request of the body data
123     res = curl_easy_perform( curl_handle );
124
125     if( CURLE_OK != res )
126     {
127       DALI_LOG_WARNING( "Failed to download image file \"%s\" with error code %d\n", url.c_str(), res );
128       return false;
129     }
130   }
131   return true;
132 }
133 } // unnamed namespace
134
135
136
137 bool Network::DownloadRemoteFileIntoMemory( const std::string& url,
138                                             Dali::Vector<uint8_t>& dataBuffer,
139                                             size_t& dataSize,
140                                             size_t maximumAllowedSizeBytes )
141 {
142   if( url.empty() )
143   {
144     DALI_LOG_WARNING("empty url requested \n");
145     return false;
146   }
147
148   // start a libcurl easy session, this internally calls curl_global_init, if we ever have more than one download
149   // thread we need to explicity call curl_global_init() on startup from a single thread.
150
151   CURL* curl_handle = curl_easy_init();
152
153   bool result = DownloadFile( curl_handle, url, dataBuffer,  dataSize, maximumAllowedSizeBytes);
154
155   // clean up session
156   curl_easy_cleanup( curl_handle );
157
158   return result;
159 }
160
161
162 } // namespace TizenPlatform
163
164 } // namespace Dali