2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "file-download.h"
22 #include <dali/integration-api/debug.h>
23 #include <curl/curl.h>
26 #include "portable/file-closer.h"
29 using namespace Dali::Integration;
34 namespace TizenPlatform
37 namespace // unnamed namespace
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;
48 void ConfigureCurlOptions( CURL* curl_handle, const std::string& url )
50 curl_easy_setopt( curl_handle, CURLOPT_URL, url.c_str() );
51 curl_easy_setopt( curl_handle, CURLOPT_VERBOSE, VERBOSE_MODE );
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 );
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)
68 bool DownloadFile( CURL* curl_handle,
69 const std::string& url,
70 Dali::Vector<uint8_t>& dataBuffer,
72 size_t maximumAllowedSizeBytes )
74 CURLcode res( CURLE_OK );
77 // setup curl to download just the header so we can extract the content length
78 ConfigureCurlOptions( curl_handle, url );
80 curl_easy_setopt( curl_handle, CURLOPT_WRITEFUNCTION, DummyWrite);
82 // perform the request to get the header
83 res = curl_easy_perform( curl_handle );
87 DALI_LOG_WARNING( "Failed to download http header for \"%s\" with error code %d\n", url.c_str(), res );
91 // get the content length, -1 == size is not known
92 curl_easy_getinfo( curl_handle,CURLINFO_CONTENT_LENGTH_DOWNLOAD , &size );
96 DALI_LOG_WARNING( "Header missing content length \"%s\" \n", url.c_str() );
99 if( size >= maximumAllowedSizeBytes )
101 DALI_LOG_WARNING( "File content length %f > max allowed %zu \"%s\" \n", size, maximumAllowedSizeBytes, url.c_str() );
105 dataSize = static_cast<size_t>( size );
107 dataBuffer.Resize( dataSize );
110 Dali::Internal::Platform::FileCloser fileCloser( static_cast<void*>(&dataBuffer[0]), dataSize, "wb" );
111 FILE* dataBufferFilePointer = fileCloser.GetFile();
112 if( NULL != dataBufferFilePointer )
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 );
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 );
122 // synchronous request of the body data
123 res = curl_easy_perform( curl_handle );
125 if( CURLE_OK != res )
127 DALI_LOG_WARNING( "Failed to download image file \"%s\" with error code %d\n", url.c_str(), res );
133 } // unnamed namespace
137 bool Network::DownloadRemoteFileIntoMemory( const std::string& url,
138 Dali::Vector<uint8_t>& dataBuffer,
140 size_t maximumAllowedSizeBytes )
144 DALI_LOG_WARNING("empty url requested \n");
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.
151 CURL* curl_handle = curl_easy_init();
153 bool result = DownloadFile( curl_handle, url, dataBuffer, dataSize, maximumAllowedSizeBytes);
156 curl_easy_cleanup( curl_handle );
162 } // namespace TizenPlatform