Main Page | Class List | File List | Class Members | Related Pages

HTTP::GetRequest Class Reference

#include <httprequest.h>

List of all members.

Public Member Functions

 GetRequest ()
virtual ~GetRequest ()
void setUserAgent (const std::string &ua)
std::string getUserAgent () const
void addHeader (const std::string &key, const std::string &value)
void addHeader (const std::string &key, int value)
void clearHeaders ()
void reset ()
bool connect (const std::string &host, int port)
bool request (const std::string &page)
bool process ()
std::string getHeader () const
std::string getHeaderAsString (const std::string &key) const
int getHeaderAsInt (const std::string &key) const
unsigned int getHeaderSize () const
std::string getData () const
unsigned int getDataSize () const
std::string getFooter () const
unsigned int getFooterSize () const
std::string getRaw () const
unsigned int getRawSize () const
std::string getError () const
bool success () const

Protected Member Functions

void setError (std::string message)
bool splitData ()
bool postProcessData ()
unsigned int findSize (unsigned int n, const std::string &source) const
bool isChunkedTransfer () const
bool isTransferFinished () const
unsigned int getContentLength () const


Detailed Description

Use HTTP::GetRequest to send a HTTP/1.1 GET request and receive text or even binary data.
Initialize with connect(), send the request by calling request() and obtaion the data with getHeader() and getData(). You might want to use getError() if an error occures. For further information look at the following example.

Example:
 std::string data, header;
 HTTP::GetRequest gr;
 gr.setUserAgent("HTTPTool/1.1");
 gr.addHeader("Accept", "image/jpegn");
 gr.addHeader("header1", "value1");

 if ( gr.connect( "www.google.de", 80 ) ) {
   if ( gr.request( "index.html" ) ) {
     while ( gr.process() );
     // you may want to update a progress bar with gr.getRawSize();
   }
 }

 if ( gr.success() ) {
   header = gr.getHeader();
   data = gr.getData();
 } else {
   cout << gr.getError();
 }
 // now do something with data and header using getData() and getHeader()

Note:
getFooter() is not supported right now, because it is rarely used.
Author:
Dominik Haumann
Version:
0.4
Todo:
: LOCATION? -> imho, this should be done by the user :-)

Definition at line 79 of file httprequest.h.


Constructor & Destructor Documentation

HTTP::GetRequest::GetRequest  ) 
 

standard constructor

Definition at line 57 of file httprequest.cpp.

HTTP::GetRequest::~GetRequest  )  [virtual]
 

destructor

Definition at line 62 of file httprequest.cpp.


Member Function Documentation

void HTTP::GetRequest::addHeader const std::string &  key,
int  value
 

see above

Parameters:
key the key
value the value

Definition at line 79 of file httprequest.cpp.

References addHeader().

Here is the call graph for this function:

void HTTP::GetRequest::addHeader const std::string &  key,
const std::string &  value
 

add a header entry like "Content-Length" etc. This header will be sent to the server with the request.

Parameters:
key the key
value the value

Definition at line 74 of file httprequest.cpp.

Referenced by addHeader().

void HTTP::GetRequest::clearHeaders  ) 
 

remove all headers.

Definition at line 90 of file httprequest.cpp.

Referenced by reset().

bool HTTP::GetRequest::connect const std::string &  host,
int  port
 

connect to a server

Parameters:
host IP or DNS (eg 80.123.23.120 or www.google.de)
port port (usually port 80)
Returns:
true on success

Definition at line 101 of file httprequest.cpp.

References setError().

Here is the call graph for this function:

unsigned int HTTP::GetRequest::findSize unsigned int  n,
const std::string &  source
const [protected]
 

get size of one chunk (helper function for postProcessData)

Parameters:
n position to begin search
source string to search in
Returns:
size, or std::string::npos if invalid

Definition at line 425 of file httprequest.cpp.

Referenced by isTransferFinished(), and postProcessData().

unsigned int HTTP::GetRequest::getContentLength  )  const [protected]
 

the exact length is given by "Content-Length: "

Returns:
content length

Definition at line 340 of file httprequest.cpp.

Referenced by splitData().

std::string HTTP::GetRequest::getData  )  const
 

get data, this may be text or binary

Returns:
data as std::string

Definition at line 262 of file httprequest.cpp.

Referenced by WDS::CDlgUpdate::CheckDownload().

unsigned int HTTP::GetRequest::getDataSize  )  const
 

Returns:
get size of data

Definition at line 282 of file httprequest.cpp.

std::string HTTP::GetRequest::getError  )  const
 

get error message set by setError()

Returns:
error message

Definition at line 297 of file httprequest.cpp.

std::string HTTP::GetRequest::getFooter  )  const
 

get footer

Returns:
footer of request

Definition at line 267 of file httprequest.cpp.

unsigned int HTTP::GetRequest::getFooterSize  )  const
 

Returns:
size of footer

Definition at line 287 of file httprequest.cpp.

std::string HTTP::GetRequest::getHeader  )  const
 

get header from the answer the server returned.

Attention:
Do not mistake it with addHeader which is used to send the request.
Returns:
header of request

Definition at line 225 of file httprequest.cpp.

int HTTP::GetRequest::getHeaderAsInt const std::string &  key  )  const
 

get a value out of the header

Parameters:
key key of header
Returns:
value of key (int)

Definition at line 247 of file httprequest.cpp.

References getHeaderAsString().

Referenced by WDS::CDlgUpdate::CheckDownload().

Here is the call graph for this function:

std::string HTTP::GetRequest::getHeaderAsString const std::string &  key  )  const
 

get a value out of the header

Parameters:
key key of header
Returns:
value of key (string) or "" if not found

Definition at line 230 of file httprequest.cpp.

Referenced by WDS::CDlgUpdate::CheckDownload(), and getHeaderAsInt().

unsigned int HTTP::GetRequest::getHeaderSize  )  const
 

Returns:
size of header

Definition at line 277 of file httprequest.cpp.

std::string HTTP::GetRequest::getRaw  )  const
 

get all data without any modifications

Returns:
raw data as std::string

Definition at line 272 of file httprequest.cpp.

unsigned int HTTP::GetRequest::getRawSize  )  const
 

during the process get the growing size with this function

Returns:
size of raw data

Definition at line 292 of file httprequest.cpp.

std::string HTTP::GetRequest::getUserAgent  )  const
 

get user agent (UA)

Definition at line 69 of file httprequest.cpp.

bool HTTP::GetRequest::isChunkedTransfer  )  const [protected]
 

check, whether this is a chunked transfer

Returns:
true, if chunked transfer

Definition at line 308 of file httprequest.cpp.

Referenced by isTransferFinished(), and splitData().

bool HTTP::GetRequest::isTransferFinished  )  const [protected]
 

If the transfer mode is 'chunked' and stepping through the sizes ends with the '0' we definitely have reached the end of the data transfer and may stop recieving more data.

Note:
this is necessary because otherwise we have a delay of ~5 seconds in which our app is not responding. So this is actually a HACK to make it work smoothly :)

Definition at line 313 of file httprequest.cpp.

References findSize(), and isChunkedTransfer().

Referenced by process().

Here is the call graph for this function:

bool HTTP::GetRequest::postProcessData  )  [protected]
 

if the transfer was chunked, remove the size infos

Returns:
true on success

Definition at line 350 of file httprequest.cpp.

References findSize().

Referenced by splitData().

Here is the call graph for this function:

bool HTTP::GetRequest::process  ) 
 

continue to get data

Returns:
true, if the transfer is not finished, otherwise false

Definition at line 193 of file httprequest.cpp.

References isTransferFinished(), and splitData().

Here is the call graph for this function:

bool HTTP::GetRequest::request const std::string &  page  ) 
 

initialize HTTP GET request

Parameters:
page rest of URL (eg. index.html)
Returns:
true on success

Definition at line 151 of file httprequest.cpp.

References setError().

Here is the call graph for this function:

void HTTP::GetRequest::reset  ) 
 

reset headers, user agent and setup the object for a new connection

Definition at line 95 of file httprequest.cpp.

References clearHeaders().

Here is the call graph for this function:

void HTTP::GetRequest::setError std::string  message  )  [protected]
 

set error message returned by getError()

Parameters:
message error message

Definition at line 303 of file httprequest.cpp.

Referenced by connect(), request(), and splitData().

void HTTP::GetRequest::setUserAgent const std::string &  ua  ) 
 

set the user agent (UA)

Definition at line 64 of file httprequest.cpp.

bool HTTP::GetRequest::splitData  )  [protected]
 

remove headers and footers

Returns:
true on success

Definition at line 452 of file httprequest.cpp.

References getContentLength(), isChunkedTransfer(), postProcessData(), and setError().

Referenced by process().

Here is the call graph for this function:

bool HTTP::GetRequest::success  )  const
 

get status of the transfer

Returns:
true, if transfer was successful

Definition at line 219 of file httprequest.cpp.


The documentation for this class was generated from the following files:
Generated on Sun Jan 16 18:20:27 2005 for WDSMap by  doxygen 1.3.9.1