HTTP client class. More...
#include <http.h>


Public Member Functions | |
| string * | post (const string &url, const string &contenttype, const string &body, value *hdr=NULL) |
| string * | post (const string &url, const string &contenttype, const string &body, value &hdr) |
| string * | post (const string &url, const value &postvar, value *headers=NULL) |
| string * | post (const string &url, const value postvar, value &hdr) |
| string * | get (const string &url, value *hdr=NULL) |
| string * | get (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) |
| tcpsocket & | sock (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) |
| string * | getResult (value *hdr) |
Protected Attributes | |
| tcpsocket | _sock |
| string | _host |
| int | _port |
| bool | _keepalive |
| int | _timeout |
| bool | _useproxy |
| string | _proxyhost |
| int | _proxyport |
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;
}
Set HTTP basic authentication credentials.
| user | Username. | |
| pass | Password. |
References string::encode64(), postheaders, and string::strcat().

| 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().

Perform a HTTP get.
| url | Full url of the resource to get. | |
| hdr | Object to store return headers |
Perform a HTTP get.
| url | Full url of the resource to get. | |
| hdr | Object to store return headers (NULL if none) |
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().

Perform a HTTP post.
Posts variables using the x-www-urlencoding scheme.
| url | Full url of the resource to post to. | |
| postvar | The variables to post. | |
| hdr | Object to store return headers |
References post().

Perform a HTTP post.
Posts variables using the x-www-urlencoding scheme.
| url | Full url of the resource to post to. | |
| postvar | The variables to post. | |
| hdr | Object to store return headers (NULL if none) |
References post(), string::strcat(), and strutil::urlencode().

| string* httpsocket::post | ( | const string & | url, | |
| const string & | contenttype, | |||
| const string & | body, | |||
| value & | hdr | |||
| ) | [inline] |
Perform a HTTP post.
| url | Full url of the resource to post to. | |
| contenttype | Mime-type of post data. | |
| body | Post data. | |
| hdr | Object to store return headers. |
References post().

| string * httpsocket::post | ( | const string & | url, | |
| const string & | contenttype, | |||
| const string & | body, | |||
| value * | hdr = NULL | |||
| ) |
Perform a HTTP post.
| 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) |
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().

| void httpsocket::setheader | ( | const statstring & | name, | |
| const string & | value | |||
| ) |
| void httpsocket::setproxy | ( | const string & | host, | |
| int | port | |||
| ) | [inline] |
Make connections through a proxy server.
| host | Proxy server it's host name | |
| port | Proxy port to connect through |
References _proxyhost, _proxyport, and _useproxy.
| tcpsocket& httpsocket::sock | ( | void | ) | [inline] |
| void httpsocket::timeout | ( | int | to | ) | [inline] |
1.6.1