|
#define | HTTP_NO_RESPONSE (0) |
|
#define | HTTP_CODE_CONTINUE (100) |
|
#define | HTTP_CODE_OK (200) |
|
#define | HTTP_CODE_CREATED (201) |
|
#define | HTTP_CODE_NO_CONTENT (204) |
|
#define | HTTP_CODE_MULTI_STATUS (207) |
|
#define | HTTP_CODE_MOVED_TEMPORARILY (302) |
|
#define | HTTP_CODE_NOT_MODIFIED (304) |
|
#define | HTTP_CODE_BAD_REQUEST (400) |
|
#define | HTTP_CODE_FORBIDDEN (403) |
|
#define | HTTP_CODE_NOT_FOUND (404) |
|
#define | HTTP_CODE_METHOD_NOT_ALLOWED (405) |
|
#define | HTTP_CODE_REQUEST_TIME_OUT (408) |
|
#define | HTTP_CODE_REQUEST_URI_TOO_LONG (414) |
|
#define | HTTP_CODE_INTERNAL_SERVER_ERROR (500) |
|
#define | HTTP_CODE_NOT_IMPLEMENTED (501) |
|
#define | HTTP_CODE_SERVICE_UNAVAILABLE (503) |
|
#define | HTTP_PROTOCOL_11 "HTTP/1.1" |
|
#define | SET_TCP_LINGER_TIMEOUT (15) /*< linger seconds, 0 for disable */ |
|
#define | SET_TCP_NODELAY (1) /*< 0 for disable */ |
|
#define | MAX_SHUTDOWN_WAIT (100) /*< maximum shutdown wait, unit is ms */ |
|
#define | MAX_ATOMIC_DATA_SIZE (32 * 1024) /*< maximum sending bytes */ |
|
|
qhttpclient_t * | qhttpclient (const char *destname, int port) |
| Initialize & create new HTTP client. More...
|
|
static bool | setssl (qhttpclient_t *client) |
| qhttpclient->setssl(): Sets connection to HTTPS connection More...
|
|
static void | settimeout (qhttpclient_t *client, int timeoutms) |
| qhttpclient->settimeout(): Sets connection wait timeout. More...
|
|
static void | setkeepalive (qhttpclient_t *client, bool keepalive) |
| qhttpclient->setkeepalive(): Sets KEEP-ALIVE feature on/off. More...
|
|
static void | setuseragent (qhttpclient_t *client, const char *useragent) |
| qhttpclient->setuseragent(): Sets user-agent string. More...
|
|
static bool | open_ (qhttpclient_t *client) |
| qhttpclient->open(): Opens a connection to the remote host. More...
|
|
static bool | head (qhttpclient_t *client, const char *uri, int *rescode, qlisttbl_t *reqheaders, qlisttbl_t *resheaders) |
| qhttpclient->head(): Sends a HEAD request. More...
|
|
static bool | get (qhttpclient_t *client, const char *uri, int fd, off_t *savesize, int *rescode, qlisttbl_t *reqheaders, qlisttbl_t *resheaders, bool(*callback)(void *userdata, off_t recvbytes), void *userdata) |
| qhttpclient->get(): Downloads a file from the remote host using GET method. More...
|
|
static bool | put (qhttpclient_t *client, const char *uri, int fd, off_t length, int *rescode, qlisttbl_t *reqheaders, qlisttbl_t *resheaders, bool(*callback)(void *userdata, off_t sentbytes), void *userdata) |
| qhttpclient->put(): Uploads a file to the remote host using PUT method. More...
|
|
static void * | cmd (qhttpclient_t *client, const char *method, const char *uri, void *data, size_t size, int *rescode, size_t *contentslength, qlisttbl_t *reqheaders, qlisttbl_t *resheaders) |
| qhttpclient->cmd(): Sends a custom request(method) to the remote host and reads it's response. More...
|
|
static bool | sendrequest (qhttpclient_t *client, const char *method, const char *uri, qlisttbl_t *reqheaders) |
| qhttpclient->sendrequest(): Sends a HTTP request to the remote host. More...
|
|
static int | readresponse (qhttpclient_t *client, qlisttbl_t *resheaders, off_t *contentlength) |
| qhttpclient->readresponse(): Reads HTTP response header from the remote host. More...
|
|
static ssize_t | gets_ (qhttpclient_t *client, char *buf, size_t bufsize) |
| qhttpclient->gets(): Reads a text line from a HTTP/HTTPS stream. More...
|
|
static ssize_t | read_ (qhttpclient_t *client, void *buf, size_t nbytes) |
| qhttpclient->read(): Reads data from a HTTP/HTTPS stream. More...
|
|
static ssize_t | write_ (qhttpclient_t *client, const void *buf, size_t nbytes) |
| qhttpclient->write(): Writes data to a HTTP/HTTPS stream. More...
|
|
static off_t | recvfile (qhttpclient_t *client, int fd, off_t nbytes) |
| qhttpclient->recvfile(): Reads data from a HTTP/HTTPS stream and save into a file descriptor. More...
|
|
static off_t | sendfile_ (qhttpclient_t *client, int fd, off_t nbytes) |
| qhttpclient->sendfile(): Send file data to a HTTP/HTTPS stream. More...
|
|
static bool | _close (qhttpclient_t *client) |
| qhttpclient->close(): Closes the connection. More...
|
|
static void | _free (qhttpclient_t *client) |
| qhttpclient->free(): Free object. More...
|
|
HTTP client object.
qhttpclient implements HTTP client.
Example code for simple HTTP GET operation.
#define REMOTE_URL "/robots.txt"
#define SAVEFILE "/tmp/robots.txt"
int main(void) {
qhttpclient_t *httpclient =
qhttpclient(
"https://secure.qdecoder.org", 0);
if(httpclient == NULL) return -1;
int nFd = open(SAVEFILE, O_CREAT | O_TRUNC | O_WRONLY, 0644);
if(nFd < 0) {
httpclient->free(httpclient);
return -1;
}
qlisttbl_t *resheaders =
qlisttbl(QLISTTBL_UNIQUE | QLISTTBL_CASEINSENSITIVE);
off_t nSavesize = 0;
int nRescode = 0;
bool bRet = httpclient->get(httpclient, REMOTE_URL, nFd, &nSavesize,
&nRescode, NULL, resheaders, NULL, NULL);
close(nFd);
printf("%s %d, %d bytes saved\n", (bRet?"Success":"Failed"), nRescode,
(int)nSavesize);
resheaders->debug(resheaders, stdout);
httpclient->free(httpclient);
return (bRet ? 0 : -1);
}
[Output]
Success 200, 30 bytes saved
Date=Fri, 11 Feb 2011 23:40:50 GMT? (30)
Server=Apache? (7)
Last-Modified=Sun, 15 Mar 2009 11:43:07 GMT? (30)
ETag="2e5c9d-1e-46526d665c8c0"? (26)
Accept-Ranges=bytes? (6)
Content-Length=30? (3)
Cache-Control=max-age=604800? (15)
Expires=Fri, 18 Feb 2011 23:40:50 GMT? (30)
Connection=close? (6)
Content-Type=text/plain? (11)
Example code for multiple PUT operation using same keep-alive connection.
qhttpclient_t *httpclient =
qhttpclient(
"www.qdecoder.org", 80);
if(httpclient == NULL) return;
httpclient->setkeepalive(httpclient, true);
if(httpclient->open(httpclient) == false) return;
httpclient->put(httpclient, ...);
httpclient->put(httpclient, ...);
httpclient->close(httpclient);
httpclient->free(httpclient);
Definition in file qhttpclient.c.