GClasses
GClasses::GHttpClient Class Reference

Detailed Description

This class allows you to get files using the HTTP protocol.

#include <GHttp.h>

Public Types

Public Member Functions

 GHttpClient ()
 
virtual ~GHttpClient ()
 
void abort ()
 The client may call this method to abort the download. More...
 
unsigned char * data (size_t *pnSize)
 Don't call this until the status is "Done". It returns a pointer to the file that was downloaded. The buffer will be deleted when this object is deleted, so if you want to retain the buffer, call releaseData instead. More...
 
unsigned char * get (const char *url, size_t *pOutSize, unsigned int sleepMiliSecs=200, unsigned int timeoutSecs=30)
 Gets a file from the specified URL. You are responsible to delete[] the data this returns. The size of the data is returned at *pOutSize. An exception is thrown if any error occurs. sleepMiliSecs specifies how long to sleep while waiting for more data. If no progress is made for timeoutSecs seconds, an exception is thrown. (This is a high-level method that calls the other low-level methods of this class. Generally, you will either use just this method, or all of the other methods.) More...
 
void onLoseConnection ()
 This is called when the connection is lost. More...
 
unsigned char * releaseData (size_t *pnSize)
 Just like getData except it forgets about the buffer so you'll have to delete it yourself. More...
 
bool sendGetRequest (const char *szUrl, bool headersOnly=false)
 Send a request to get a file. Returns immediately (before the file is downloaded). If headersOnly is true, then only the headers will be requested, and not the file. Returns true if the request was sent successfully. Returns false if it could not connect to the specified URL. More...
 
void setClientName (const char *szClientName)
 Specify the name that the client uses to identify itself when requesting a file. The default is "GHttpClient/1.0". More...
 
Status status (float *pfProgress)
 See what the status of the download is. If everything is going okay, it will return "Downloading" while downloading and "Done" when the file is available. pfProgress is an optional parameter. If it is non-NULL, it will return a number between 0 and 1 that indicates the ratio of content (not including header data) already downloaded. More...
 

Protected Member Functions

void gimmeWhatYouGot ()
 
virtual void onReceiveData (const unsigned char *pData, size_t nLen)
 This method is called whenever a chunk of data is received. More...
 
void processBody (const unsigned char *szData, size_t nSize)
 
void processChunkBody (const unsigned char *szData, size_t nSize)
 
void processHeader (const unsigned char *szData, size_t nSize)
 

Protected Attributes

bool m_aborted
 
bool m_bAmCurrentlyDoingJustHeaders
 
bool m_bChunked
 
bool m_bPastHeader
 
std::ostringstream m_chunkBuf
 
double m_dLastReceiveTime
 
size_t m_nContentSize
 
size_t m_nDataPos
 
size_t m_nHeaderPos
 
unsigned char * m_pData
 
char * m_pReceiveBuf
 
GHttpClientSocket * m_pSocket
 
Status m_status
 
char m_szClientName [32]
 
char m_szHeaderBuf [258]
 
char * m_szRedirect
 
char m_szServer [256]
 

Member Enumeration Documentation

Enumerator
Downloading 
Error 
NotFound 
Done 
Aborted 

Constructor & Destructor Documentation

GClasses::GHttpClient::GHttpClient ( )
virtual GClasses::GHttpClient::~GHttpClient ( )
virtual

Member Function Documentation

void GClasses::GHttpClient::abort ( )

The client may call this method to abort the download.

unsigned char* GClasses::GHttpClient::data ( size_t *  pnSize)

Don't call this until the status is "Done". It returns a pointer to the file that was downloaded. The buffer will be deleted when this object is deleted, so if you want to retain the buffer, call releaseData instead.

unsigned char* GClasses::GHttpClient::get ( const char *  url,
size_t *  pOutSize,
unsigned int  sleepMiliSecs = 200,
unsigned int  timeoutSecs = 30 
)

Gets a file from the specified URL. You are responsible to delete[] the data this returns. The size of the data is returned at *pOutSize. An exception is thrown if any error occurs. sleepMiliSecs specifies how long to sleep while waiting for more data. If no progress is made for timeoutSecs seconds, an exception is thrown. (This is a high-level method that calls the other low-level methods of this class. Generally, you will either use just this method, or all of the other methods.)

void GClasses::GHttpClient::gimmeWhatYouGot ( )
protected
void GClasses::GHttpClient::onLoseConnection ( )

This is called when the connection is lost.

virtual void GClasses::GHttpClient::onReceiveData ( const unsigned char *  pData,
size_t  nLen 
)
inlineprotectedvirtual

This method is called whenever a chunk of data is received.

void GClasses::GHttpClient::processBody ( const unsigned char *  szData,
size_t  nSize 
)
protected
void GClasses::GHttpClient::processChunkBody ( const unsigned char *  szData,
size_t  nSize 
)
protected
void GClasses::GHttpClient::processHeader ( const unsigned char *  szData,
size_t  nSize 
)
protected
unsigned char* GClasses::GHttpClient::releaseData ( size_t *  pnSize)

Just like getData except it forgets about the buffer so you'll have to delete it yourself.

bool GClasses::GHttpClient::sendGetRequest ( const char *  szUrl,
bool  headersOnly = false 
)

Send a request to get a file. Returns immediately (before the file is downloaded). If headersOnly is true, then only the headers will be requested, and not the file. Returns true if the request was sent successfully. Returns false if it could not connect to the specified URL.

void GClasses::GHttpClient::setClientName ( const char *  szClientName)

Specify the name that the client uses to identify itself when requesting a file. The default is "GHttpClient/1.0".

Status GClasses::GHttpClient::status ( float *  pfProgress)

See what the status of the download is. If everything is going okay, it will return "Downloading" while downloading and "Done" when the file is available. pfProgress is an optional parameter. If it is non-NULL, it will return a number between 0 and 1 that indicates the ratio of content (not including header data) already downloaded.

Member Data Documentation

bool GClasses::GHttpClient::m_aborted
protected
bool GClasses::GHttpClient::m_bAmCurrentlyDoingJustHeaders
protected
bool GClasses::GHttpClient::m_bChunked
protected
bool GClasses::GHttpClient::m_bPastHeader
protected
std::ostringstream GClasses::GHttpClient::m_chunkBuf
protected
double GClasses::GHttpClient::m_dLastReceiveTime
protected
size_t GClasses::GHttpClient::m_nContentSize
protected
size_t GClasses::GHttpClient::m_nDataPos
protected
size_t GClasses::GHttpClient::m_nHeaderPos
protected
unsigned char* GClasses::GHttpClient::m_pData
protected
char* GClasses::GHttpClient::m_pReceiveBuf
protected
GHttpClientSocket* GClasses::GHttpClient::m_pSocket
protected
Status GClasses::GHttpClient::m_status
protected
char GClasses::GHttpClient::m_szClientName[32]
protected
char GClasses::GHttpClient::m_szHeaderBuf[258]
protected
char* GClasses::GHttpClient::m_szRedirect
protected
char GClasses::GHttpClient::m_szServer[256]
protected