cxxtools::http::Client Class Reference

This class implements a http client. More...

#include <cxxtools/http/client.h>

Public Member Functions

 Client ()
 default constructor.
 Client (SelectorBase &selector)
 Client (const net::Uri &uri)
 constructor with cxxtools::net::Uri.
 Client (const Client &other)
 copy and assignment.
Clientoperator= (const Client &other)
 ~Client ()
void prepareConnect (const net::AddrInfo &addrinfo, bool ssl=false)
 The prepareConnect methods set the host and port of the server for this http client.
void prepareConnect (const std::string &host, unsigned short int port, bool ssl=false)
void prepareConnect (const net::Uri &uri)
void connect ()
 Connects to the client specified by prepareConnect or passed by teh constructor.
void close ()
 Closes the network connection if connected.
void connect (const net::AddrInfo &addrinfo)
 Sets the network parameters and connects the socket.
void connect (const std::string &host, unsigned short int port)
void connect (const net::Uri &uri)
const ReplyHeaderexecute (const Request &request, Milliseconds timeout=Selectable::WaitInfinite, Milliseconds connectTimeout=Selectable::WaitInfinite)
 Sends the passed request to the server and parses the headers.
const ReplyreadBody ()
 Reads the http body after header read with execute.
const Replyreply () const
 Returns the reply of the last executed request.
Replyreply ()
const ReplyHeaderheader () const
 Returns the headers of the last executed reply.
std::string body () const
 Returns the body of the last executed request.
const Replyget (const std::string &url, const QueryParams &qparams, Milliseconds timeout=Selectable::WaitInfinite, Milliseconds connectTimeout=Selectable::WaitInfinite)
 Executes a GET request with query parameters.
const Replyget (const std::string &url, Milliseconds timeout=Selectable::WaitInfinite, Milliseconds connectTimeout=Selectable::WaitInfinite)
 Executes a GET request.
const Replypost (const std::string &url, const QueryParams &qparams, Milliseconds timeout=Selectable::WaitInfinite, Milliseconds connectTimeout=Selectable::WaitInfinite)
 Executes a POST request with query parameters.
const Replypost (const std::string &url, Milliseconds timeout=Selectable::WaitInfinite, Milliseconds connectTimeout=Selectable::WaitInfinite)
 Executes a POST request.
void beginExecute (const Request &request)
 Starts a new request.
void endExecute ()
 Finishes the request.
void setSelector (SelectorBase &selector)
 Sets the selector for asyncronous event processing.
SelectorBaseselector ()
 Returns the selector for asyncronous event processing.
bool wait (Milliseconds msecs)
 Executes the underlying selector until a event occurs or the specified timeout is reached.
std::istream & in ()
 Returns the underlying stream, where the reply is to be read from.
const std::string & host () const
unsigned short int port () const
void auth (const std::string &username, const std::string &password)
 Sets the username and password for all subsequent requests.
void clearAuth ()
 Clears the username and password for all subsequent requests.
void cancel ()
 Client (const net::AddrInfo &addr, bool ssl=false)
 Client (const std::string &host, unsigned short int port, bool ssl=false)
 Client (SelectorBase &selector, const std::string &host, unsigned short int port, bool ssl=false)
 Client (SelectorBase &selector, const net::AddrInfo &addrinfo, bool ssl=false)
 Client (SelectorBase &selector, const net::Uri &uri)

Public Attributes

Signal< Client & > requestSent
 Signals that the request is sent to the server.
Signal< Client & > headerReceived
 Signals that the header is received.
cxxtools::Delegate
< std::size_t, Client & > 
bodyAvailable
 This delegate is called, when data is arrived while reading the body.
Signal< Client & > replyFinished
 Signals that the reply is completely processed.

Detailed Description

This class implements a http client.

The class supports syncronous and asyncronous requests.

For connection handling see Handling connections in cxxtools.

For asyncronous I/O see Asyncronous I/O with cxxtools.

Example for a syncronous call:

cxxtools::http::Client client("www.tntnet.org", 80);
std::string indexPage = client.get("/");

Constructor & Destructor Documentation

cxxtools::http::Client::Client ( )
inline

default constructor.

cxxtools::http::Client::Client ( const net::AddrInfo addr,
bool  ssl = false 
)
explicit

constructors which set the connection parameters.

cxxtools::http::Client::Client ( const std::string &  host,
unsigned short int  port,
bool  ssl = false 
)

constructors which set the connection parameters.

cxxtools::http::Client::Client ( SelectorBase selector)
inlineexplicit
cxxtools::http::Client::Client ( const net::Uri uri)
explicit

constructor with cxxtools::net::Uri.

Note that the Uri class has a non explicit constructor from std::string. The protocol of the uri must be http. The url part of the uri is ignored.

This makes using uris easy.

example:

cxxtools::http::Client client("http://localhost:8000/");
cxxtools::http::Client::Client ( SelectorBase selector,
const std::string &  host,
unsigned short int  port,
bool  ssl = false 
)

constructors which set the selector for asyncronous request processing and the connection parameters.

cxxtools::http::Client::Client ( SelectorBase selector,
const net::AddrInfo addrinfo,
bool  ssl = false 
)

constructors which set the selector for asyncronous request processing and the connection parameters.

cxxtools::http::Client::Client ( SelectorBase selector,
const net::Uri uri 
)

constructors which set the selector for asyncronous request processing and the connection parameters.

cxxtools::http::Client::Client ( const Client other)

copy and assignment.

When the class is copied, the copy points to the same implementation. Hence it is not a real copy.

cxxtools::http::Client::~Client ( )

Member Function Documentation

void cxxtools::http::Client::auth ( const std::string &  username,
const std::string &  password 
)

Sets the username and password for all subsequent requests.

Basic authorization is used always.

void cxxtools::http::Client::beginExecute ( const Request request)

Starts a new request.

This method does not block. To actually process the request, the event loop must be executed. The state of the request is signaled with the corresponding signals and delegates. The delegate "bodyAvailable" must be connected, if a body is received.

std::string cxxtools::http::Client::body ( ) const
inline

Returns the body of the last executed request.

The readBody() method must be called before the body is available.

void cxxtools::http::Client::cancel ( )
void cxxtools::http::Client::clearAuth ( )

Clears the username and password for all subsequent requests.

void cxxtools::http::Client::close ( )

Closes the network connection if connected.

The network connection is automaticalle initiated on the next request. The method do not discard the current connection parameters.

void cxxtools::http::Client::connect ( )

Connects to the client specified by prepareConnect or passed by teh constructor.

This happens actually automatically when needed. It just helps checking whether the server is reachable, since setting the connection paramters do not check it.

void cxxtools::http::Client::connect ( const net::AddrInfo addrinfo)
inline

Sets the network parameters and connects the socket.

void cxxtools::http::Client::connect ( const std::string &  host,
unsigned short int  port 
)
inline
void cxxtools::http::Client::connect ( const net::Uri uri)
inline
void cxxtools::http::Client::endExecute ( )

Finishes the request.

When the signal replyFinished is issued, the user has to call endExecute. This may throw an exception when something went wrong.

const ReplyHeader& cxxtools::http::Client::execute ( const Request request,
Milliseconds  timeout = Selectable::WaitInfinite,
Milliseconds  connectTimeout = Selectable::WaitInfinite 
)

Sends the passed request to the server and parses the headers.

The body must be read with readBody. This method blocks or times out until the body is parsed. When connectTimeout is set to WaitInfinite but timeout is set to something else, the connectTimeout is set to timeout as well.

const Reply& cxxtools::http::Client::get ( const std::string &  url,
const QueryParams qparams,
Milliseconds  timeout = Selectable::WaitInfinite,
Milliseconds  connectTimeout = Selectable::WaitInfinite 
)

Executes a GET request with query parameters.

This method blocks until the reply is recieved. When connectTimeout is set to WaitInfinite but timeout is set to something else, the connectTimeout is set to timeout as well.

const Reply& cxxtools::http::Client::get ( const std::string &  url,
Milliseconds  timeout = Selectable::WaitInfinite,
Milliseconds  connectTimeout = Selectable::WaitInfinite 
)
inline

Executes a GET request.

This method blocks until the reply is recieved. When connectTimeout is set to WaitInfinite but timeout is set to something else, the connectTimeout is set to timeout as well.

const ReplyHeader& cxxtools::http::Client::header ( ) const
inline

Returns the headers of the last executed reply.

The headers can be read after calling execute().

const std::string& cxxtools::http::Client::host ( ) const
std::istream& cxxtools::http::Client::in ( )

Returns the underlying stream, where the reply is to be read from.

Client& cxxtools::http::Client::operator= ( const Client other)
unsigned short int cxxtools::http::Client::port ( ) const
const Reply& cxxtools::http::Client::post ( const std::string &  url,
const QueryParams qparams,
Milliseconds  timeout = Selectable::WaitInfinite,
Milliseconds  connectTimeout = Selectable::WaitInfinite 
)

Executes a POST request with query parameters.

This method blocks until the reply is recieved. When connectTimeout is set to WaitInfinite but timeout is set to something else, the connectTimeout is set to timeout as well.

const Reply& cxxtools::http::Client::post ( const std::string &  url,
Milliseconds  timeout = Selectable::WaitInfinite,
Milliseconds  connectTimeout = Selectable::WaitInfinite 
)
inline

Executes a POST request.

This method blocks until the reply is recieved. When connectTimeout is set to WaitInfinite but timeout is set to something else, the connectTimeout is set to timeout as well.

void cxxtools::http::Client::prepareConnect ( const net::AddrInfo addrinfo,
bool  ssl = false 
)

The prepareConnect methods set the host and port of the server for this http client.

No actual network connect is done.

See Also
Handling connections in cxxtools
void cxxtools::http::Client::prepareConnect ( const std::string &  host,
unsigned short int  port,
bool  ssl = false 
)
void cxxtools::http::Client::prepareConnect ( const net::Uri uri)
const Reply& cxxtools::http::Client::readBody ( )

Reads the http body after header read with execute.

This method blocks until the body is received.

const Reply& cxxtools::http::Client::reply ( ) const

Returns the reply of the last executed request.

Reply& cxxtools::http::Client::reply ( )
SelectorBase* cxxtools::http::Client::selector ( )

Returns the selector for asyncronous event processing.

void cxxtools::http::Client::setSelector ( SelectorBase selector)

Sets the selector for asyncronous event processing.

bool cxxtools::http::Client::wait ( Milliseconds  msecs)

Executes the underlying selector until a event occurs or the specified timeout is reached.

Member Data Documentation

cxxtools::Delegate<std::size_t, Client&> cxxtools::http::Client::bodyAvailable

This delegate is called, when data is arrived while reading the body.

The connected functor must return the number of bytes read.

Signal<Client&> cxxtools::http::Client::headerReceived

Signals that the header is received.

Signal<Client&> cxxtools::http::Client::replyFinished

Signals that the reply is completely processed.

Signal<Client&> cxxtools::http::Client::requestSent

Signals that the request is sent to the server.


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