httpsocket Class Reference

HTTP client class. More...

#include <http.h>

Inheritance diagram for httpsocket:
Inheritance graph
[legend]
Collaboration diagram for httpsocket:
Collaboration graph
[legend]

List of all members.

Public Member Functions

stringpost (const string &url, const string &contenttype, const string &body, value *hdr=NULL)
stringpost (const string &url, const string &contenttype, const string &body, value &hdr)
stringpost (const string &url, const value &postvar, value *headers=NULL)
stringpost (const string &url, const value postvar, value &hdr)
stringget (const string &url, value *hdr=NULL)
stringget (const string &url, value &hdr)
void setproxy (const string &host, int port)
void setheader (const statstring &name, const string &value)
void keepalive (bool k=false)
void nokeepalive (void)
void authentication (const string &user, const string &pass)
tcpsocketsock (void)
int timeout (void)
void timeout (int to)

Public Attributes

value postheaders
string error
int errorcode
int status

Protected Member Functions

bool connectToHost (const string &hostname, int port)
bool getData (string &into, size_t bytes)
bool getChunked (string &into)
stringgetResult (value *hdr)

Protected Attributes

tcpsocket _sock
string _host
int _port
bool _keepalive
int _timeout
bool _useproxy
string _proxyhost
int _proxyport

Detailed Description

HTTP client class.

Implements the HTTP/1.1 standard POST and GET methods to submit data to a webserver and read the results. When posting, it can either be a properly serialized set of name/value pairs out of a value object, or some arbitrary data with a content-type.

Example usage (using a GET request):

#include <grace/application.h>
#include <grace/http.h>
#include <grace/str.h>

class leechApp : public application
{
public:
			 leechApp (void) : application ("tld.example.app.leech")
			 {
			 }
			~leechApp (void)
			 {
			 }
			 
	int		 main (void);
};

APPOBJECT(leechApp);

int leechApp::main (void)
{
	httpsocket hs;
	string returnData;
	string url;
	
	url = argv["*"][0];
	if (! url.strlen)
	{
		ferr.printf ("Usage: leech <url>\n"); return 1;
	}
	
	returnData = hs.get (url);
	fout.puts (returnData);
	return 0;
}

Member Function Documentation

void httpsocket::authentication ( const string user,
const string pass 
)

Set HTTP basic authentication credentials.

Parameters:
user Username.
pass Password.

References string::encode64(), postheaders, and string::strcat().

Here is the call graph for this function:

bool httpsocket::connectToHost ( const string hostname,
int  port 
) [protected]

Internal connect method.

Acts smart by reusing an open tcpsocket if the host did not change and keepalive is permitted.

References _host, _keepalive, _port, _sock, file::close(), tcpsocket::connect(), string::crop(), file::eof(), and string::strlen().

Referenced by get(), and post().

Here is the call graph for this function:

string* httpsocket::get ( const string url,
value hdr 
) [inline]

Perform a HTTP get.

Parameters:
url Full url of the resource to get.
hdr Object to store return headers
Returns:
Body data.
string * httpsocket::get ( const string url,
value hdr = NULL 
)

Perform a HTTP get.

Parameters:
url Full url of the resource to get.
hdr Object to store return headers (NULL if none)
Returns:
Body data.

References _host, _keepalive, _port, _proxyhost, _proxyport, _sock, _useproxy, file::close(), file::codec, connectToHost(), string::crop(), string::cutat(), string::cval(), error, errorcode, value::exists(), string::globcmp(), value::id, string::mid(), postheaders, file::puts(), strutil::regexp(), status, string::str(), string::strchr(), string::strlen(), and tcpsocket::uconnect().

Here is the call graph for this function:

string* httpsocket::post ( const string url,
const value  postvar,
value hdr 
) [inline]

Perform a HTTP post.

Posts variables using the x-www-urlencoding scheme.

Parameters:
url Full url of the resource to post to.
postvar The variables to post.
hdr Object to store return headers
Returns:
Body data.

References post().

Here is the call graph for this function:

string * httpsocket::post ( const string url,
const value postvar,
value headers = NULL 
)

Perform a HTTP post.

Posts variables using the x-www-urlencoding scheme.

Parameters:
url Full url of the resource to post to.
postvar The variables to post.
hdr Object to store return headers (NULL if none)
Returns:
Body data.

References post(), string::strcat(), and strutil::urlencode().

Here is the call graph for this function:

string* httpsocket::post ( const string url,
const string contenttype,
const string body,
value hdr 
) [inline]

Perform a HTTP post.

Parameters:
url Full url of the resource to post to.
contenttype Mime-type of post data.
body Post data.
hdr Object to store return headers.
Returns:
Body data.

References post().

Here is the call graph for this function:

string * httpsocket::post ( const string url,
const string contenttype,
const string body,
value hdr = NULL 
)

Perform a HTTP post.

Parameters:
url Full url of the resource to post to.
contenttype Mime-type of post data.
body Post data.
hdr Object to store return headers (NULL if none)
Returns:
Body data.

References _host, _keepalive, _port, _proxyhost, _proxyport, _sock, _useproxy, file::close(), file::codec, connectToHost(), string::crop(), string::cutat(), string::cval(), error, errorcode, value::exists(), string::globcmp(), value::id, string::mid(), postheaders, file::puts(), strutil::regexp(), value::rmval(), status, string::strchr(), string::strlen(), and tcpsocket::uconnect().

Referenced by post().

Here is the call graph for this function:

void httpsocket::setheader ( const statstring name,
const string value 
)

Set the value an output header.

Parameters:
name The header name.
value The header value.
void httpsocket::setproxy ( const string host,
int  port 
) [inline]

Make connections through a proxy server.

Parameters:
host Proxy server it's host name
port Proxy port to connect through

References _proxyhost, _proxyport, and _useproxy.

tcpsocket& httpsocket::sock ( void   )  [inline]

Access to the underlying tcpsocket.

Returns:
Reference to the socket.

References _sock.

void httpsocket::timeout ( int  to  )  [inline]

Set current timeout value.

Parameters:
to New timeout in ms, 0 disables.

References _timeout.


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

Generated on Sun Feb 28 10:36:05 2010 for GRACE by  doxygen 1.6.1