SuperTuxKart
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Private Attributes | List of all members
Online::HTTPRequest Class Reference

A http request. More...

#include <http_request.hpp>

Inheritance diagram for Online::HTTPRequest:
Inheritance graph
[legend]

Public Member Functions

 HTTPRequest (int priority=1)
 Creates a HTTP(S) request that will have a raw string as result. More...
 
 HTTPRequest (const std::string &filename, int priority=1)
 This constructor configures this request to save the data in a flie. More...
 
 HTTPRequest (const char *const filename, int priority=1)
 Char * needs a separate constructor, otherwise it will be considered to be the no-filename constructor (char* -> bool).
 
virtual bool isAllowedToAdd () const OVERRIDE
 Checks the request if it has enough (correct) information to be executed (and thus allowed to add to the queue).
 
void setApiURL (const std::string &url, const std::string &action)
 A handy shortcut that appends the given path to the URL of the mutiplayer server. More...
 
void setAddonsURL (const std::string &path)
 A handy shortcut that appends the given path to the URL of the addons server. More...
 
bool hadDownloadError () const
 Returns true if there was an error downloading the file. More...
 
void setDownloadAssetsRequest (bool val)
 
const char * getDownloadErrorMessage () const
 Returns the curl error message if an error has occurred. More...
 
const std::string & getData () const
 Returns the downloaded string. More...
 
void addParameter (const std::string &name, const std::string &value)
 Sets a parameter to 'value' (std::string). More...
 
void addParameter (const std::string &name, const irr::core::stringw &value)
 Sets a parameter to 'value' (stringw). More...
 
template<typename T >
void addParameter (const std::string &name, const T &value)
 Sets a parameter to 'value' (arbitrary types). More...
 
float getProgress () const
 Returns the current progress. More...
 
void setProgress (float f)
 Sets the current progress. More...
 
const std::string & getURL () const
 
void setURL (const std::string &url)
 Sets the URL for this request. More...
 
const std::string & getFileName () const
 
double getTotalSize () const
 
void setTotalSize (double d)
 
- Public Member Functions inherited from Online::Request
 Request (int priority, int type)
 Creates a request that can be handled by the RequestManager. More...
 
void execute ()
 Executes the request. More...
 
void executeNow ()
 Executes the request now, i.e. More...
 
void queue ()
 Inserts this request into the RequestManager's queue for executing.
 
virtual void callback ()
 Executed when a request has finished. More...
 
int getType () const
 Returns the type of the request. More...
 
int getPriority () const
 Returns the priority of this request. More...
 
void cancel ()
 Signals that this request should be canceled. More...
 
bool isCancelled () const
 Returns if this request is to be canceled. More...
 
bool isAbortable () const
 Returns if this request can be aborted. More...
 
void setAbortable (bool b)
 Sets if this request is abortable or not. More...
 
void setBusy ()
 Sets the request state to busy. More...
 
void setExecuted ()
 Sets the request to be completed. More...
 
void setDone ()
 Should only be called by the manager.
 
bool isDone () const
 Returns if this request is done. More...
 
bool isPreparing () const
 Returns if this request is being prepared. More...
 
bool isBusy () const
 Returns if this request is busy. More...
 
bool hasBeenExecuted () const
 Checks if the request has completed or done (i.e. More...
 

Protected Member Functions

virtual void prepareOperation () OVERRIDE
 Sets up the curl data structures.
 
virtual void operation () OVERRIDE
 The actual curl download happens here.
 
virtual void afterOperation () OVERRIDE
 Cleanup once the download is finished. More...
 
void init ()
 Initialises all member variables.
 

Static Protected Member Functions

static int progressDownload (void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
 Callback function from curl: inform about progress. More...
 
static size_t writeCallback (void *contents, size_t size, size_t nmemb, void *userp)
 Callback from curl. More...
 

Protected Attributes

std::string m_filename
 Contains a filename if the data should be saved into a file instead of being kept in in memory. More...
 
bool m_disable_sending_log
 
bool m_download_assets_request = false
 
- Protected Attributes inherited from Online::Request
Synchronised< bool > m_cancel
 Cancel this request if it is active. More...
 
Synchronised< bool > m_is_abortable
 If this request can be aborted (at the end of STK). More...
 
Synchronised< Statem_state
 Set to though if the reply of the request is in and callbacks are executed.
 

Private Attributes

std::atomic< float > m_progress
 The progress indicator. More...
 
std::atomic< double > m_total_size
 
std::string m_url
 The url to download. More...
 
std::string m_parameters
 The POST parameters that will be send with the request. More...
 
CURL * m_curl_session = NULL
 Pointer to the curl data structure for this request. More...
 
CURLcode m_curl_code
 curl return code. More...
 
std::string m_string_buffer
 String to store the received data in. More...
 
struct curl_slist * m_http_header = NULL
 

Additional Inherited Members

- Public Types inherited from Online::Request
enum  RequestType { RT_QUIT = 1 }
 

Detailed Description

A http request.

Constructor & Destructor Documentation

◆ HTTPRequest() [1/2]

Online::HTTPRequest::HTTPRequest ( int  priority = 1)

Creates a HTTP(S) request that will have a raw string as result.

(Can of course be used if the result doesn't matter.)

Parameters
priorityby what priority should the RequestManager take care of this request.

◆ HTTPRequest() [2/2]

Online::HTTPRequest::HTTPRequest ( const std::string &  filename,
int  priority = 1 
)

This constructor configures this request to save the data in a flie.

Parameters
filenameName of the file to save the data to.
priorityby what priority should the RequestManager take care of this request.

Member Function Documentation

◆ addParameter() [1/3]

void Online::HTTPRequest::addParameter ( const std::string &  name,
const std::string &  value 
)
inline

Sets a parameter to 'value' (std::string).

◆ addParameter() [2/3]

void Online::HTTPRequest::addParameter ( const std::string &  name,
const irr::core::stringw &  value 
)
inline

Sets a parameter to 'value' (stringw).

◆ addParameter() [3/3]

template<typename T >
void Online::HTTPRequest::addParameter ( const std::string &  name,
const T &  value 
)
inline

Sets a parameter to 'value' (arbitrary types).

◆ afterOperation()

void Online::HTTPRequest::afterOperation ( )
protectedvirtual

Cleanup once the download is finished.

The value of progress is guaranteed to be >=0 and <1 while the download is in progress, and will only be set to 1 on a successfull finish here.

Reimplemented from Online::Request.

Reimplemented in Online::XMLRequest, and AddonsPackRequest.

◆ getData()

const std::string& Online::HTTPRequest::getData ( ) const
inline

Returns the downloaded string.

Precondition
request has to be done
Returns
get the result string from the request reply

◆ getDownloadErrorMessage()

const char* Online::HTTPRequest::getDownloadErrorMessage ( ) const
inline

Returns the curl error message if an error has occurred.

Precondition
m_curl_code!=CURLE_OK

◆ getProgress()

float Online::HTTPRequest::getProgress ( ) const
inline

Returns the current progress.

◆ hadDownloadError()

bool Online::HTTPRequest::hadDownloadError ( ) const
inline

Returns true if there was an error downloading the file.

◆ progressDownload()

int Online::HTTPRequest::progressDownload ( void *  clientp,
double  download_total,
double  download_now,
double  upload_total,
double  upload_now 
)
staticprotected

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.

Parameters
clientp
download_totalTotal size of data to download.
download_nowHow much has been downloaded so far.
upload_totalTotal amount of upload.
upload_nowHow muc has been uploaded so far.

◆ setAddonsURL()

void Online::HTTPRequest::setAddonsURL ( const std::string &  path)

A handy shortcut that appends the given path to the URL of the addons server.

Parameters
pathThe path to add to the server, without the trailing slash

◆ setApiURL()

void Online::HTTPRequest::setApiURL ( const std::string &  path,
const std::string &  action 
)

A handy shortcut that appends the given path to the URL of the mutiplayer server.

It also supports the old (version 1) api, where a 'action' parameter was sent to 'client-user.php'.

Parameters
pathThe path to add to the server.(see API::USER_*)
actionThe action to perform. eg: connect, pool

◆ setProgress()

void Online::HTTPRequest::setProgress ( float  f)
inline

Sets the current progress.

◆ setURL()

void Online::HTTPRequest::setURL ( const std::string &  url)
inline

Sets the URL for this request.

◆ writeCallback()

size_t Online::HTTPRequest::writeCallback ( void *  contents,
size_t  size,
size_t  nmemb,
void *  userp 
)
staticprotected

Callback from curl.

This stores the data received by curl in the buffer of this request.

Parameters
contentPointer to the data received by curl.
sizeSize of one block.
nmembNumber of blocks received.
userpPointer to the user buffer.

Member Data Documentation

◆ m_curl_code

CURLcode Online::HTTPRequest::m_curl_code
private

curl return code.

◆ m_curl_session

CURL* Online::HTTPRequest::m_curl_session = NULL
private

Pointer to the curl data structure for this request.

◆ m_filename

std::string Online::HTTPRequest::m_filename
protected

Contains a filename if the data should be saved into a file instead of being kept in in memory.

Otherwise this is "".

◆ m_parameters

std::string Online::HTTPRequest::m_parameters
private

The POST parameters that will be send with the request.

◆ m_progress

std::atomic<float> Online::HTTPRequest::m_progress
private

The progress indicator.

0 untill it is started and the first packet is downloaded. Guaranteed to be <1 while the download is in progress, it will be set to either -1 (error) or 1 (everything ok) at the end.

◆ m_string_buffer

std::string Online::HTTPRequest::m_string_buffer
private

String to store the received data in.

◆ m_url

std::string Online::HTTPRequest::m_url
private

The url to download.


The documentation for this class was generated from the following files: