/*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
// EXTERNAL INCLUDES
#include <dali/integration-api/debug.h>
#include <pthread.h>
-#include <openssl/crypto.h>
#include <cstring>
#include <curl/curl.h>
+#include <../ExInclude/InternalFileOperation.h>
// INTERNAL INCLUDES
#include <dali/internal/system/common/file-writer.h>
namespace // unnamed namespace
{
+
+const int CONNECTION_TIMEOUT_SECONDS( 30L );
+const long VERBOSE_MODE = 0L; // 0 == off, 1 == on
+const long CLOSE_CONNECTION_ON_ERROR = 1L; // 0 == off, 1 == on
const long EXCLUDE_HEADER = 0L;
+const long INCLUDE_HEADER = 1L;
const long INCLUDE_BODY = 0L;
+const long EXCLUDE_BODY = 1L;
/**
* Curl library environment. Direct initialize ensures it's constructed before adaptor
// Without a write function or a buffer (file descriptor) to write to, curl will pump out
// header/body contents to stdout
-size_t DummyWrite(char *ptr, size_t size, size_t nmemb, void *userdata)
+size_t __cdecl DummyWrite(char *ptr, size_t size, size_t nmemb, void *userdata)
{
return size * nmemb;
}
std::vector< uint8_t > data;
};
-size_t ChunkLoader(char *ptr, size_t size, size_t nmemb, void *userdata)
+size_t __cdecl ChunkLoader(char *ptr, size_t size, size_t nmemb, void *userdata)
{
std::vector<ChunkData>* chunks = static_cast<std::vector<ChunkData>*>( userdata );
int numBytes = size*nmemb;
- chunks->push_back( ChunkData() );
- ChunkData& chunkData = (*chunks)[chunks->size()-1];
- chunkData.data.reserve( numBytes );
- memcpy( chunkData.data.data(), ptr, numBytes );
+ if( chunks != nullptr )
+ {
+ chunks->push_back( ChunkData() );
+ ChunkData& chunkData = (*chunks)[chunks->size()-1];
+ chunkData.data.reserve( numBytes );
+ memcpy( chunkData.data.data(), ptr, numBytes );
+ }
return numBytes;
}
+static size_t __cdecl WriteFunction( void *input, size_t uSize, size_t uCount, void *avg )
+{
+ fwrite( input, uSize, uCount, (FILE*)avg );
+ return uSize * uCount;
+}
+
+void InitWriteFunction( void* curlHandle )
+{
+ curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, WriteFunction );
+}
CURLcode DownloadFileDataWithSize( CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t dataSize )
{
// create
Dali::Internal::Platform::FileWriter fileWriter( dataBuffer, dataSize );
FILE* dataBufferFilePointer = fileWriter.GetFile();
- if( NULL != dataBufferFilePointer )
+ if( nullptr != dataBufferFilePointer )
{
// we only want the body which contains the file data
curl_easy_setopt( curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER );
curl_easy_setopt( curlHandle, CURLOPT_NOBODY, INCLUDE_BODY );
// disable the write callback, and get curl to write directly into our data buffer
- Network::CurlEnvironment::InitWriteFunction( curlHandle );
+ InitWriteFunction( curlHandle );
curl_easy_setopt( curlHandle, CURLOPT_WRITEDATA, dataBufferFilePointer );
return result;
}
+void ConfigureCurlOptions( void* curlHandle, const std::string& url )
+{
+ curl_easy_setopt( curlHandle, CURLOPT_URL, url.c_str() );
+ //curl_easy_setopt( curlHandle, CURLOPT_VERBOSE, VERBOSE_MODE );
+ curl_easy_setopt( curlHandle, CURLOPT_PROXY, "109.123.100.31:3128" );
+
+ // CURLOPT_FAILONERROR is not fail-safe especially when authentication is involved ( see manual )
+ // Removed CURLOPT_FAILONERROR option
+ curl_easy_setopt( curlHandle, CURLOPT_CONNECTTIMEOUT, CONNECTION_TIMEOUT_SECONDS );
+ curl_easy_setopt( curlHandle, CURLOPT_HEADER, INCLUDE_HEADER );
+ curl_easy_setopt( curlHandle, CURLOPT_NOBODY, EXCLUDE_BODY );
+}
+
bool DownloadFile( CURL* curlHandle,
const std::string& url,
Dali::Vector<uint8_t>& dataBuffer,
double size(0);
// setup curl to download just the header so we can extract the content length
- Network::CurlEnvironment::ConfigureCurlOptions( curlHandle, url );
+ ConfigureCurlOptions( curlHandle, url );
curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, DummyWrite);
namespace Network
{
-std::mutex* CurlEnvironment::mMutexs = NULL;
-
CurlEnvironment::CurlEnvironment()
{
// Must be called before we attempt any loads. e.g. by using curl_easy_init()
// and before we start any threads.
curl_global_init(CURL_GLOBAL_ALL);
-
- // libcurl with openssl needs locking_function and thread id for threadsafe
- // https://curl.haxx.se/libcurl/c/threadsafe.html
- // https://www.openssl.org/docs/man1.0.2/crypto/threads.html#DESCRIPTION
- // SetLockingFunction sets locking_function and get thread id by the guide.
- SetLockingFunction();
}
CurlEnvironment::~CurlEnvironment()
{
- UnsetLockingFunction();
-
curl_global_cleanup();
}
-// libcurl with openssl needs locking_function and thread id for threadsafe
-// https://curl.haxx.se/libcurl/c/threadsafe.html
-// https://www.openssl.org/docs/man1.0.2/crypto/threads.html#DESCRIPTION
-void CurlEnvironment::OnOpenSSLLocking( int mode, int n, const char* file, int line )
-{
- if( mode & CRYPTO_LOCK )
- {
- mMutexs[n].lock();
- }
- else
- {
- mMutexs[n].unlock();
- }
-}
-
-void CurlEnvironment::SetLockingFunction()
-{
- if( mMutexs != NULL )
- {
- return;
- }
-
- mMutexs = new std::mutex[ CRYPTO_num_locks() ];
-
- CRYPTO_set_id_callback( &CurlEnvironment::GetThreadId );
- CRYPTO_set_locking_callback( &CurlEnvironment::OnOpenSSLLocking );
-}
-
-void CurlEnvironment::UnsetLockingFunction()
-{
- if( mMutexs == NULL )
- {
- return;
- }
-
- CRYPTO_set_id_callback( NULL );
- CRYPTO_set_locking_callback( NULL );
-
- delete [] mMutexs;
- mMutexs = NULL;
-}
-
bool DownloadRemoteFileIntoMemory( const std::string& url,
Dali::Vector<uint8_t>& dataBuffer,
size_t& dataSize,