Kyoto Tycoon
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes
kyototycoon::HTTPClient Class Reference

HTTP client. More...

#include <kthttp.h>

List of all members.

Public Types

enum  Method {
  MGET, MHEAD, MPOST, MPUT,
  MDELETE, MUNKNOWN
}
 Kinds of HTTP request methods. More...

Public Member Functions

 HTTPClient ()
 Default constructor.
 ~HTTPClient ()
 Destructor.
bool open (const std::string &host="", int32_t port=80, double timeout=-1)
 Open the connection.
bool close (bool grace=true)
 Close the connection.
int32_t fetch (const std::string &pathquery, Method method=MGET, std::string *resbody=NULL, std::map< std::string, std::string > *resheads=NULL, const std::string *reqbody=NULL, const std::map< std::string, std::string > *reqheads=NULL)
 Fetch a resource.
Socketreveal_core ()
 Reveal the internal TCP socket.

Static Public Member Functions

static int32_t fetch_once (const std::string &url, Method method=MGET, std::string *resbody=NULL, std::map< std::string, std::string > *resheads=NULL, const std::string *reqbody=NULL, const std::map< std::string, std::string > *reqheads=NULL, double timeout=-1)
 Fetch a resource at once.

Static Public Attributes

static const int32_t LINEBUFSIZ = 8192
 The size for a line buffer.
static const int32_t RECVMAXSIZ = 1 << 28
 The maximum size of received data.

Detailed Description

HTTP client.

Note:
Although all methods of this class are thread-safe, its instance does not have mutual exclusion mechanism. So, multiple threads must not share the same instance and they must use their own respective instances.

Member Enumeration Documentation

Kinds of HTTP request methods.

Enumerator:
MGET 

GET.

MHEAD 

HEAD.

MPOST 

POST.

MPUT 

PUT.

MDELETE 

DELETE.

MUNKNOWN 

unknown


Constructor & Destructor Documentation

Default constructor.

Destructor.


Member Function Documentation

bool kyototycoon::HTTPClient::open ( const std::string &  host = "",
int32_t  port = 80,
double  timeout = -1 
)

Open the connection.

Parameters:
hostthe name or the address of the server. If it is an empty string, the local host is specified.
portthe port numger of the server.
timeoutthe timeout of each operation in seconds. If it is not more than 0, no timeout is specified.
Returns:
true on success, or false on failure.
bool kyototycoon::HTTPClient::close ( bool  grace = true)

Close the connection.

Parameters:
gracetrue for graceful shutdown, or false for immediate disconnection.
Returns:
true on success, or false on failure.
int32_t kyototycoon::HTTPClient::fetch ( const std::string &  pathquery,
Method  method = MGET,
std::string *  resbody = NULL,
std::map< std::string, std::string > *  resheads = NULL,
const std::string *  reqbody = NULL,
const std::map< std::string, std::string > *  reqheads = NULL 
)

Fetch a resource.

Parameters:
pathquerythe path and the query string of the resource.
methodthe kind of the request methods.
resbodya string to contain the entity body of the response. If it is NULL, it is ignored.
resheadsa string map to contain the headers of the response. If it is NULL, it is ignored. Header names are converted into lower cases. The empty key means the request-line.
reqbodya string which contains the entity body of the request. If it is NULL, it is ignored.
reqheadsa string map which contains the headers of the request. If it is NULL, it is ignored.
Returns:
the status code of the response, or -1 on failure.

Reveal the internal TCP socket.

Returns:
the internal TCP socket.
static int32_t kyototycoon::HTTPClient::fetch_once ( const std::string &  url,
Method  method = MGET,
std::string *  resbody = NULL,
std::map< std::string, std::string > *  resheads = NULL,
const std::string *  reqbody = NULL,
const std::map< std::string, std::string > *  reqheads = NULL,
double  timeout = -1 
) [static]

Fetch a resource at once.

Parameters:
urlthe URL of the resource.
methodthe kind of the request methods.
resbodya string to contain the entity body of the response. If it is NULL, it is ignored.
resheadsa string map to contain the headers of the response. If it is NULL, it is ignored. Header names are converted into lower cases. The empty key means the request-line.
reqbodya string which contains the entity body of the request. If it is NULL, it is ignored.
reqheadsa string map which contains the headers of the request. If it is NULL, it is ignored.
timeoutthe timeout of each operation in seconds. If it is not more than 0, no timeout is specified.
Returns:
the status code of the response, or -1 on failure.

Member Data Documentation

const int32_t kyototycoon::HTTPClient::LINEBUFSIZ = 8192 [static]

The size for a line buffer.

const int32_t kyototycoon::HTTPClient::RECVMAXSIZ = 1 << 28 [static]

The maximum size of received data.