2014-01-08 23:57:58 -05:00
|
|
|
// SuperTuxKart - a fun racing game with go-kart
|
2015-03-29 20:31:42 -04:00
|
|
|
// Copyright (C) 2013-2015 Glenn De Jonghe
|
2014-01-08 23:57:58 -05:00
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation; either version 3
|
|
|
|
// of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
#include "online/http_request.hpp"
|
|
|
|
|
|
|
|
#include "config/user_config.hpp"
|
|
|
|
#include "online/request_manager.hpp"
|
|
|
|
#include "utils/constants.hpp"
|
|
|
|
#include "utils/translation.hpp"
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
# include <winsock2.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <curl/curl.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2014-01-12 06:20:32 -05:00
|
|
|
namespace Online
|
|
|
|
{
|
2014-08-04 14:55:56 -04:00
|
|
|
const std::string API::USER_PATH = "user/";
|
|
|
|
const std::string API::SERVER_PATH = "server/";
|
|
|
|
|
2014-03-29 06:33:43 -04:00
|
|
|
/** Creates a HTTP(S) request that will have a raw string as result. (Can
|
2014-01-08 23:57:58 -05:00
|
|
|
* of course be used if the result doesn't matter.)
|
2014-01-12 06:20:32 -05:00
|
|
|
* \param manage_memory whether or not the RequestManager should take care of
|
2014-01-08 23:57:58 -05:00
|
|
|
* deleting the object after all callbacks have been done.
|
2014-03-29 06:33:43 -04:00
|
|
|
* \param priority by what priority should the RequestManager take care of
|
2014-01-08 23:57:58 -05:00
|
|
|
* this request.
|
|
|
|
*/
|
|
|
|
HTTPRequest::HTTPRequest(bool manage_memory, int priority)
|
|
|
|
: Request(manage_memory, priority, 0)
|
|
|
|
{
|
2014-01-12 06:20:32 -05:00
|
|
|
init();
|
2014-01-08 23:57:58 -05:00
|
|
|
} // HTTPRequest
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
2014-01-12 06:20:32 -05:00
|
|
|
/** This constructor configures this request to save the data in a flie.
|
|
|
|
* \param filename Name of the file to save the data to.
|
|
|
|
* \param manage_memory whether or not the RequestManager should take care of
|
|
|
|
* deleting the object after all callbacks have been done.
|
2014-03-29 06:33:43 -04:00
|
|
|
* \param priority by what priority should the RequestManager take care of
|
2014-01-12 06:20:32 -05:00
|
|
|
* this request.
|
|
|
|
*/
|
2014-03-29 06:33:43 -04:00
|
|
|
HTTPRequest::HTTPRequest(const std::string &filename, bool manage_memory,
|
2014-01-12 06:20:32 -05:00
|
|
|
int priority)
|
|
|
|
: Request(manage_memory, priority, 0)
|
|
|
|
{
|
2014-01-13 06:35:12 -05:00
|
|
|
// A http request should not even be created when internet is disabled
|
2014-03-29 06:33:43 -04:00
|
|
|
assert(UserConfigParams::m_internet_status ==
|
2014-01-13 06:35:12 -05:00
|
|
|
RequestManager::IPERM_ALLOWED);
|
2014-08-04 11:41:47 -04:00
|
|
|
assert(filename.size() > 0);
|
|
|
|
|
2014-01-12 06:20:32 -05:00
|
|
|
init();
|
|
|
|
m_filename = file_manager->getAddonsFile(filename);
|
|
|
|
} // HTTPRequest(filename ...)
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
/** Char * needs a separate constructor, otherwise it will be considered
|
|
|
|
* to be the no-filename constructor (char* -> bool).
|
|
|
|
*/
|
2014-03-29 06:33:43 -04:00
|
|
|
HTTPRequest::HTTPRequest(const char* const filename, bool manage_memory,
|
2014-01-12 06:20:32 -05:00
|
|
|
int priority)
|
|
|
|
: Request(manage_memory, priority, 0)
|
2014-01-08 23:57:58 -05:00
|
|
|
{
|
2014-01-13 06:35:12 -05:00
|
|
|
// A http request should not even be created when internet is disabled
|
2014-03-29 06:33:43 -04:00
|
|
|
assert(UserConfigParams::m_internet_status ==
|
2014-01-13 06:35:12 -05:00
|
|
|
RequestManager::IPERM_ALLOWED);
|
2014-08-04 11:41:47 -04:00
|
|
|
|
2014-01-12 06:20:32 -05:00
|
|
|
init();
|
|
|
|
m_filename = file_manager->getAddonsFile(filename);
|
|
|
|
} // HTTPRequest(filename ...)
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
/** Initialises all member variables.
|
|
|
|
*/
|
|
|
|
void HTTPRequest::init()
|
|
|
|
{
|
|
|
|
m_url = "";
|
|
|
|
m_string_buffer = "";
|
|
|
|
m_filename = "";
|
|
|
|
m_parameters = "";
|
|
|
|
m_curl_code = CURLE_OK;
|
|
|
|
m_progress.setAtomic(0);
|
|
|
|
} // init
|
2014-01-08 23:57:58 -05:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
2014-03-29 06:33:43 -04:00
|
|
|
/** A handy shortcut that appends the given path to the URL of the
|
2014-08-11 00:10:01 -04:00
|
|
|
* mutiplayer server. It also supports the old (version 1) api,
|
|
|
|
* where a 'action' parameter was sent to 'client-user.php'.
|
2014-08-04 14:55:56 -04:00
|
|
|
* \param path The path to add to the server.(see API::USER_*)
|
|
|
|
* \param action The action to perform. eg: connect, pool
|
2014-01-08 23:57:58 -05:00
|
|
|
*/
|
2014-09-01 21:57:48 -04:00
|
|
|
void HTTPRequest::setApiURL(const std::string& path,
|
2014-08-11 00:10:01 -04:00
|
|
|
const std::string &action)
|
2014-01-08 23:57:58 -05:00
|
|
|
{
|
2014-08-11 00:10:01 -04:00
|
|
|
// Old (0.8.1) API: send to client-user.php, and add action as a parameter
|
|
|
|
if(UserConfigParams::m_server_version==1)
|
|
|
|
{
|
2014-09-01 21:57:48 -04:00
|
|
|
setURL( (std::string)UserConfigParams::m_server_multiplayer +
|
2014-08-11 00:10:01 -04:00
|
|
|
"client-user.php" );
|
2014-08-11 07:56:13 -04:00
|
|
|
if(action=="change-password")
|
|
|
|
addParameter("action", "change_password");
|
|
|
|
else if(action=="recover")
|
|
|
|
addParameter("action", "recovery");
|
|
|
|
else
|
|
|
|
addParameter("action", action);
|
2014-08-11 00:10:01 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setURL(
|
|
|
|
(std::string)UserConfigParams::m_server_multiplayer +
|
2014-08-11 07:56:13 -04:00
|
|
|
+"v"+StringUtils::toString(UserConfigParams::m_server_version)
|
|
|
|
+ "/" + path + // eg: /user/, /server/
|
2014-08-11 00:10:01 -04:00
|
|
|
action + "/" // eg: connect/, pool/, get-server-list/
|
|
|
|
);
|
|
|
|
}
|
2014-01-08 23:57:58 -05:00
|
|
|
} // setServerURL
|
|
|
|
|
2014-01-12 06:20:32 -05:00
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
/** A handy shortcut that appends the given path to the URL of the addons
|
|
|
|
* server.
|
2014-08-04 11:41:47 -04:00
|
|
|
* \param path The path to add to the server, without the trailing slash
|
2014-01-12 06:20:32 -05:00
|
|
|
*/
|
|
|
|
void HTTPRequest::setAddonsURL(const std::string& path)
|
|
|
|
{
|
2014-08-04 11:41:47 -04:00
|
|
|
setURL((std::string)UserConfigParams::m_server_addons + "/" + path);
|
2014-01-12 06:20:32 -05:00
|
|
|
} // set AddonsURL
|
2014-08-04 11:41:47 -04:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
2014-01-08 23:57:58 -05:00
|
|
|
/** Checks the request if it has enough (correct) information to be
|
|
|
|
* executed (and thus allowed to add to the queue).
|
|
|
|
*/
|
2014-01-29 19:08:17 -05:00
|
|
|
bool HTTPRequest::isAllowedToAdd() const
|
2014-01-08 23:57:58 -05:00
|
|
|
{
|
|
|
|
return Request::isAllowedToAdd() && m_url.substr(0, 5) == "http:";
|
|
|
|
} // isAllowedToAdd
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
/** Sets up the curl data structures.
|
|
|
|
*/
|
|
|
|
void HTTPRequest::prepareOperation()
|
|
|
|
{
|
|
|
|
m_curl_session = curl_easy_init();
|
2014-08-04 11:41:47 -04:00
|
|
|
if (!m_curl_session)
|
2014-01-08 23:57:58 -05:00
|
|
|
{
|
|
|
|
Log::error("HTTPRequest::prepareOperation",
|
|
|
|
"LibCurl session not initialized.");
|
|
|
|
return;
|
|
|
|
}
|
2014-01-12 06:20:32 -05:00
|
|
|
|
2014-01-08 23:57:58 -05:00
|
|
|
curl_easy_setopt(m_curl_session, CURLOPT_URL, m_url.c_str());
|
|
|
|
curl_easy_setopt(m_curl_session, CURLOPT_FOLLOWLOCATION, 1);
|
|
|
|
curl_easy_setopt(m_curl_session, CURLOPT_NOPROGRESS, 0);
|
|
|
|
curl_easy_setopt(m_curl_session, CURLOPT_PROGRESSDATA, this);
|
|
|
|
curl_easy_setopt(m_curl_session, CURLOPT_PROGRESSFUNCTION,
|
|
|
|
&HTTPRequest::progressDownload);
|
|
|
|
curl_easy_setopt(m_curl_session, CURLOPT_CONNECTTIMEOUT, 20);
|
|
|
|
curl_easy_setopt(m_curl_session, CURLOPT_LOW_SPEED_LIMIT, 10);
|
|
|
|
curl_easy_setopt(m_curl_session, CURLOPT_LOW_SPEED_TIME, 20);
|
2017-01-25 04:28:48 -05:00
|
|
|
curl_easy_setopt(m_curl_session, CURLOPT_NOSIGNAL, 1);
|
2014-01-20 16:12:08 -05:00
|
|
|
//curl_easy_setopt(m_curl_session, CURLOPT_VERBOSE, 1L);
|
2015-03-03 00:03:38 -05:00
|
|
|
if (m_url.substr(0, 8) == "https://")
|
2014-01-12 06:20:32 -05:00
|
|
|
{
|
2014-08-04 11:41:47 -04:00
|
|
|
// https, load certificate info
|
2014-01-12 06:20:32 -05:00
|
|
|
struct curl_slist *chunk = NULL;
|
2014-12-10 07:53:47 -05:00
|
|
|
chunk = curl_slist_append(chunk, "Host: addons.supertuxkart.net");
|
2014-01-12 06:20:32 -05:00
|
|
|
curl_easy_setopt(m_curl_session, CURLOPT_HTTPHEADER, chunk);
|
2014-12-03 19:39:56 -05:00
|
|
|
CURLcode error = curl_easy_setopt(m_curl_session, CURLOPT_CAINFO,
|
2014-12-10 07:53:47 -05:00
|
|
|
file_manager->getAsset("addons.supertuxkart.net.pem").c_str());
|
2014-12-03 19:39:56 -05:00
|
|
|
if (error != CURLE_OK)
|
|
|
|
{
|
|
|
|
Log::error("HTTPRequest", "Error setting CAINFO to '%s'",
|
2014-12-10 07:53:47 -05:00
|
|
|
file_manager->getAsset("addons.supertuxkart.net.pem").c_str());
|
2014-12-03 19:39:56 -05:00
|
|
|
Log::error("HTTPRequest", "Error %d: '%s'.", error,
|
|
|
|
curl_easy_strerror(error));
|
|
|
|
}
|
2014-12-09 20:55:32 -05:00
|
|
|
curl_easy_setopt(m_curl_session, CURLOPT_SSL_VERIFYPEER, 1L);
|
2014-12-09 20:23:35 -05:00
|
|
|
#ifdef __APPLE__
|
|
|
|
curl_easy_setopt(m_curl_session, CURLOPT_SSL_VERIFYHOST, 0L);
|
|
|
|
#else
|
2014-12-04 21:57:27 -05:00
|
|
|
curl_easy_setopt(m_curl_session, CURLOPT_SSL_VERIFYHOST, 1L);
|
2014-12-09 20:23:35 -05:00
|
|
|
#endif
|
2014-01-12 06:20:32 -05:00
|
|
|
}
|
2014-01-08 23:57:58 -05:00
|
|
|
} // prepareOperation
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
/** The actual curl download happens here.
|
|
|
|
*/
|
|
|
|
void HTTPRequest::operation()
|
|
|
|
{
|
2014-08-04 11:41:47 -04:00
|
|
|
if (!m_curl_session)
|
2014-01-08 23:57:58 -05:00
|
|
|
return;
|
2014-01-12 06:20:32 -05:00
|
|
|
|
|
|
|
FILE *fout = NULL;
|
2014-08-04 11:41:47 -04:00
|
|
|
if (m_filename.size() > 0)
|
2014-01-08 23:57:58 -05:00
|
|
|
{
|
2014-01-12 06:20:32 -05:00
|
|
|
fout = fopen((m_filename+".part").c_str(), "wb");
|
|
|
|
|
2014-08-04 11:41:47 -04:00
|
|
|
if (!fout)
|
2014-01-12 06:20:32 -05:00
|
|
|
{
|
|
|
|
Log::error("HTTPRequest",
|
|
|
|
"Can't open '%s' for writing, ignored.",
|
|
|
|
(m_filename+".part").c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
curl_easy_setopt(m_curl_session, CURLOPT_WRITEDATA, fout );
|
|
|
|
curl_easy_setopt(m_curl_session, CURLOPT_WRITEFUNCTION, fwrite);
|
2014-01-08 23:57:58 -05:00
|
|
|
}
|
2014-01-12 06:20:32 -05:00
|
|
|
else
|
|
|
|
{
|
2014-03-29 06:33:43 -04:00
|
|
|
curl_easy_setopt(m_curl_session, CURLOPT_WRITEDATA,
|
2014-01-12 06:20:32 -05:00
|
|
|
&m_string_buffer);
|
2014-03-29 06:33:43 -04:00
|
|
|
curl_easy_setopt(m_curl_session, CURLOPT_WRITEFUNCTION,
|
2014-01-12 06:20:32 -05:00
|
|
|
&HTTPRequest::writeCallback);
|
|
|
|
}
|
|
|
|
|
2014-03-29 06:33:43 -04:00
|
|
|
// All parameters added have a '&' added
|
2014-08-04 11:41:47 -04:00
|
|
|
if (m_parameters.size() > 0)
|
2014-01-12 17:43:45 -05:00
|
|
|
{
|
|
|
|
m_parameters.erase(m_parameters.size()-1);
|
|
|
|
}
|
2014-01-12 06:20:32 -05:00
|
|
|
|
2014-08-04 11:41:47 -04:00
|
|
|
if (m_parameters.size() == 0)
|
|
|
|
{
|
2014-01-13 06:35:12 -05:00
|
|
|
Log::info("HTTPRequest", "Downloading %s", m_url.c_str());
|
2014-08-04 11:41:47 -04:00
|
|
|
}
|
|
|
|
else if (Log::getLogLevel() <= Log::LL_INFO)
|
2014-03-04 18:34:28 -05:00
|
|
|
{
|
|
|
|
// Avoid printing the password or token, just replace them with *s
|
|
|
|
std::string param = m_parameters;
|
2014-08-04 11:41:47 -04:00
|
|
|
|
2014-04-06 18:13:33 -04:00
|
|
|
// List of strings whose values should not be printed. "" is the
|
|
|
|
// end indicator.
|
|
|
|
static std::string dont_print[] = { "&password=", "&token=", "¤t=",
|
2014-08-05 12:40:56 -04:00
|
|
|
"&new1=", "&new2=", "&password_confirm=", ""};
|
2014-04-06 18:13:33 -04:00
|
|
|
unsigned int j = 0;
|
2014-08-04 11:41:47 -04:00
|
|
|
while (dont_print[j].size() > 0)
|
2014-03-04 18:34:28 -05:00
|
|
|
{
|
2014-03-05 16:04:43 -05:00
|
|
|
// Get the string that should be replaced.
|
2014-04-06 18:13:33 -04:00
|
|
|
std::size_t pos = param.find(dont_print[j]);
|
2014-03-04 18:34:28 -05:00
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
2014-04-06 18:13:33 -04:00
|
|
|
pos += dont_print[j].size();
|
2014-03-04 18:34:28 -05:00
|
|
|
while (pos < param.size() && param[pos] != '&')
|
|
|
|
{
|
|
|
|
param[pos] = '*';
|
|
|
|
pos++;
|
|
|
|
} // while not end
|
|
|
|
} // if string found
|
2014-04-06 18:13:33 -04:00
|
|
|
j++;
|
|
|
|
} // while dont_print[j].size()>0
|
2014-08-04 11:41:47 -04:00
|
|
|
|
|
|
|
Log::info("HTTPRequest", "Sending %s to %s", param.c_str(), m_url.c_str());
|
|
|
|
} // end log http request
|
|
|
|
|
|
|
|
curl_easy_setopt(m_curl_session, CURLOPT_POSTFIELDS, m_parameters.c_str());
|
2014-01-08 23:57:58 -05:00
|
|
|
std::string uagent( std::string("SuperTuxKart/") + STK_VERSION );
|
|
|
|
#ifdef WIN32
|
|
|
|
uagent += (std::string)" (Windows)";
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
uagent += (std::string)" (Macintosh)";
|
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
uagent += (std::string)" (FreeBSD)";
|
2017-10-07 15:35:38 -04:00
|
|
|
#elif defined(ANDROID)
|
|
|
|
uagent += (std::string)" (Android)";
|
2014-01-08 23:57:58 -05:00
|
|
|
#elif defined(linux)
|
|
|
|
uagent += (std::string)" (Linux)";
|
|
|
|
#else
|
|
|
|
// Unknown system type
|
|
|
|
#endif
|
|
|
|
curl_easy_setopt(m_curl_session, CURLOPT_USERAGENT, uagent.c_str());
|
|
|
|
|
|
|
|
m_curl_code = curl_easy_perform(m_curl_session);
|
|
|
|
Request::operation();
|
2014-01-12 06:20:32 -05:00
|
|
|
|
2014-08-04 11:41:47 -04:00
|
|
|
if (fout)
|
2014-01-12 06:20:32 -05:00
|
|
|
{
|
|
|
|
fclose(fout);
|
2014-08-04 11:41:47 -04:00
|
|
|
if (m_curl_code == CURLE_OK)
|
2014-01-12 06:20:32 -05:00
|
|
|
{
|
|
|
|
if(UserConfigParams::logAddons())
|
|
|
|
Log::info("HTTPRequest", "Download successful.");
|
2014-08-04 11:41:47 -04:00
|
|
|
|
2014-01-12 06:20:32 -05:00
|
|
|
// The behaviour of rename is unspecified if the target
|
|
|
|
// file should already exist - so remove it.
|
2014-01-15 23:29:47 -05:00
|
|
|
bool ok = file_manager->removeFile(m_filename);
|
2014-08-04 11:41:47 -04:00
|
|
|
if (!ok)
|
2014-01-15 23:29:47 -05:00
|
|
|
{
|
|
|
|
Log::error("addons",
|
|
|
|
"Could not removed existing addons.xml file.");
|
|
|
|
m_curl_code = CURLE_WRITE_ERROR;
|
|
|
|
}
|
2014-03-29 06:33:43 -04:00
|
|
|
int ret = rename((m_filename+".part").c_str(),
|
2014-01-12 06:20:32 -05:00
|
|
|
m_filename.c_str() );
|
2014-08-04 11:41:47 -04:00
|
|
|
|
2014-01-12 06:20:32 -05:00
|
|
|
// In case of an error, set the status to indicate this
|
2014-08-04 11:41:47 -04:00
|
|
|
if (ret != 0)
|
2014-01-12 06:20:32 -05:00
|
|
|
{
|
2014-01-15 23:29:47 -05:00
|
|
|
Log::error("addons",
|
|
|
|
"Could not rename downloaded addons.xml file!");
|
2014-01-12 06:20:32 -05:00
|
|
|
m_curl_code = CURLE_WRITE_ERROR;
|
|
|
|
}
|
|
|
|
} // m_curl_code ==CURLE_OK
|
|
|
|
} // if fout
|
2014-01-08 23:57:58 -05:00
|
|
|
} // operation
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
2014-03-29 06:33:43 -04:00
|
|
|
/** Cleanup once the download is finished. The value of progress is
|
2014-01-08 23:57:58 -05:00
|
|
|
* guaranteed to be >=0 and <1 while the download is in progress, and
|
|
|
|
* will only be set to 1 on a successfull finish here.
|
|
|
|
*/
|
|
|
|
void HTTPRequest::afterOperation()
|
|
|
|
{
|
2014-08-04 11:41:47 -04:00
|
|
|
if (m_curl_code == CURLE_OK)
|
2014-01-08 23:57:58 -05:00
|
|
|
setProgress(1.0f);
|
|
|
|
else
|
|
|
|
setProgress(-1.0f);
|
2014-08-04 11:41:47 -04:00
|
|
|
|
2014-01-08 23:57:58 -05:00
|
|
|
Request::afterOperation();
|
|
|
|
curl_easy_cleanup(m_curl_session);
|
|
|
|
} // afterOperation
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
/** Callback from curl. This stores the data received by curl in the
|
|
|
|
* buffer of this request.
|
|
|
|
* \param content Pointer to the data received by curl.
|
|
|
|
* \param size Size of one block.
|
|
|
|
* \param nmemb Number of blocks received.
|
|
|
|
* \param userp Pointer to the user buffer.
|
|
|
|
*/
|
|
|
|
size_t HTTPRequest::writeCallback(void *contents, size_t size,
|
|
|
|
size_t nmemb, void *userp)
|
|
|
|
{
|
|
|
|
((std::string*)userp)->append((char*)contents, size * nmemb);
|
|
|
|
return size * nmemb;
|
|
|
|
} // writeCallback
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Callback function from curl: inform about progress. It makes sure that
|
|
|
|
* the value reported by getProgress () is <1 while the download is still
|
|
|
|
* in progress.
|
|
|
|
* \param clientp
|
|
|
|
* \param download_total Total size of data to download.
|
|
|
|
* \param download_now How much has been downloaded so far.
|
|
|
|
* \param upload_total Total amount of upload.
|
|
|
|
* \param upload_now How muc has been uploaded so far.
|
|
|
|
*/
|
|
|
|
int HTTPRequest::progressDownload(void *clientp,
|
|
|
|
double download_total, double download_now,
|
|
|
|
double upload_total, double upload_now)
|
|
|
|
{
|
|
|
|
HTTPRequest *request = (HTTPRequest *)clientp;
|
|
|
|
|
|
|
|
// Check if we are asked to abort the download. If so, signal this
|
|
|
|
// back to libcurl by returning a non-zero status.
|
2015-01-22 10:56:30 -05:00
|
|
|
if ((RequestManager::get()->getAbort() || request->isCancelled()) &&
|
2014-06-04 03:05:35 -04:00
|
|
|
request->isAbortable() )
|
2014-01-08 23:57:58 -05:00
|
|
|
{
|
|
|
|
// Indicates to abort the current download, which means that this
|
|
|
|
// thread will go back to the mainloop and handle the next request.
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
float f;
|
2014-08-04 11:41:47 -04:00
|
|
|
if (download_now < download_total)
|
2014-01-08 23:57:58 -05:00
|
|
|
{
|
|
|
|
f = (float)download_now / (float)download_total;
|
2014-08-04 11:41:47 -04:00
|
|
|
|
2014-01-08 23:57:58 -05:00
|
|
|
// In case of floating point rouding errors make sure that
|
|
|
|
// 1.0 is only reached when downloadFileInternal is finished
|
2014-08-04 11:41:47 -04:00
|
|
|
if (f >= 1.0f)
|
|
|
|
f = 0.99f;
|
2014-01-08 23:57:58 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Don't set progress to 1.0f; this is done in afterOperation()
|
|
|
|
// after checking curls return code!
|
2014-08-04 11:41:47 -04:00
|
|
|
f = (download_total == 0) ? 0 : 0.99f;
|
2014-01-08 23:57:58 -05:00
|
|
|
}
|
|
|
|
request->setProgress(f);
|
2014-08-04 11:41:47 -04:00
|
|
|
|
2014-01-08 23:57:58 -05:00
|
|
|
return 0;
|
|
|
|
} // progressDownload
|
|
|
|
|
|
|
|
} // namespace Online
|