qLibc
qhttpclient.c File Reference

HTTP client object. More...

Go to the source code of this file.

Macros

#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 */
 

Functions

qhttpclient_t * qhttpclient (const char *destname, int port)
 Initialize & create new HTTP client.
 
static bool setssl (qhttpclient_t *client)
 qhttpclient->setssl(): Sets connection to HTTPS connection
 
static void settimeout (qhttpclient_t *client, int timeoutms)
 qhttpclient->settimeout(): Sets connection wait timeout.
 
static void setkeepalive (qhttpclient_t *client, bool keepalive)
 qhttpclient->setkeepalive(): Sets KEEP-ALIVE feature on/off.
 
static void setuseragent (qhttpclient_t *client, const char *useragent)
 qhttpclient->setuseragent(): Sets user-agent string.
 
static bool open_ (qhttpclient_t *client)
 qhttpclient->open(): Opens a connection to the remote host.
 
static bool head (qhttpclient_t *client, const char *uri, int *rescode, qlisttbl_t *reqheaders, qlisttbl_t *resheaders)
 qhttpclient->head(): Sends a HEAD request.
 
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.
 
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.
 
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.
 
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.
 
static int readresponse (qhttpclient_t *client, qlisttbl_t *resheaders, off_t *contentlength)
 qhttpclient->readresponse(): Reads HTTP response header from the remote host.
 
static ssize_t gets_ (qhttpclient_t *client, char *buf, size_t bufsize)
 qhttpclient->gets(): Reads a text line from a HTTP/HTTPS stream.
 
static ssize_t read_ (qhttpclient_t *client, void *buf, size_t nbytes)
 qhttpclient->read(): Reads data from a HTTP/HTTPS stream.
 
static ssize_t write_ (qhttpclient_t *client, const void *buf, size_t nbytes)
 qhttpclient->write(): Writes data to a HTTP/HTTPS stream.
 
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.
 
static off_t sendfile_ (qhttpclient_t *client, int fd, off_t nbytes)
 qhttpclient->sendfile(): Send file data to a HTTP/HTTPS stream.
 
static bool _close (qhttpclient_t *client)
 qhttpclient->close(): Closes the connection.
 
static void _free (qhttpclient_t *client)
 qhttpclient->free(): Free object.
 

Detailed Description

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) {
// create new HTTP client
qhttpclient_t *httpclient = qhttpclient("https://secure.qdecoder.org", 0);
if(httpclient == NULL) return -1;
// open file for writing
int nFd = open(SAVEFILE, O_CREAT | O_TRUNC | O_WRONLY, 0644);
if(nFd < 0) {
httpclient->free(httpclient);
return -1;
}
// container for storing response headers for debugging purpose
qlisttbl_t *resheaders = qlisttbl(QLISTTBL_UNIQUE | QLISTTBL_CASEINSENSITIVE);
// download
off_t nSavesize = 0;
int nRescode = 0;
bool bRet = httpclient->get(httpclient, REMOTE_URL, nFd, &nSavesize,
&nRescode, NULL, resheaders, NULL, NULL);
// close file
close(nFd);
// print out debugging info
printf("%s %d, %d bytes saved\n", (bRet?"Success":"Failed"), nRescode,
(int)nSavesize);
resheaders->debug(resheaders, stdout);
// de-allocate HTTP client object
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)
qhttpclient_t * qhttpclient(const char *destname, int port)
Initialize & create new HTTP client.
qlisttbl_t * qlisttbl(int options)
Create a new Q_LIST linked-list container.
Definition qlisttbl.c:150

Example code for multiple PUT operation using same keep-alive connection.

// create new HTTP client
qhttpclient_t *httpclient = qhttpclient("www.qdecoder.org", 80);
if(httpclient == NULL) return;
// set options
httpclient->setkeepalive(httpclient, true);
// make a connection
if(httpclient->open(httpclient) == false) return;
// upload files
httpclient->put(httpclient, ...);
httpclient->put(httpclient, ...); // will be done within same connection.
// close connection - not necessary if we call free() just after this.
httpclient->close(httpclient);
// de-allocate HTTP client object
httpclient->free(httpclient);

Definition in file qhttpclient.c.

Macro Definition Documentation

◆ HTTP_NO_RESPONSE

#define HTTP_NO_RESPONSE   (0)

Definition at line 188 of file qhttpclient.c.

◆ HTTP_CODE_CONTINUE

#define HTTP_CODE_CONTINUE   (100)

Definition at line 189 of file qhttpclient.c.

◆ HTTP_CODE_OK

#define HTTP_CODE_OK   (200)

Definition at line 190 of file qhttpclient.c.

◆ HTTP_CODE_CREATED

#define HTTP_CODE_CREATED   (201)

Definition at line 191 of file qhttpclient.c.

◆ HTTP_CODE_NO_CONTENT

#define HTTP_CODE_NO_CONTENT   (204)

Definition at line 192 of file qhttpclient.c.

◆ HTTP_CODE_MULTI_STATUS

#define HTTP_CODE_MULTI_STATUS   (207)

Definition at line 193 of file qhttpclient.c.

◆ HTTP_CODE_MOVED_TEMPORARILY

#define HTTP_CODE_MOVED_TEMPORARILY   (302)

Definition at line 194 of file qhttpclient.c.

◆ HTTP_CODE_NOT_MODIFIED

#define HTTP_CODE_NOT_MODIFIED   (304)

Definition at line 195 of file qhttpclient.c.

◆ HTTP_CODE_BAD_REQUEST

#define HTTP_CODE_BAD_REQUEST   (400)

Definition at line 196 of file qhttpclient.c.

◆ HTTP_CODE_FORBIDDEN

#define HTTP_CODE_FORBIDDEN   (403)

Definition at line 197 of file qhttpclient.c.

◆ HTTP_CODE_NOT_FOUND

#define HTTP_CODE_NOT_FOUND   (404)

Definition at line 198 of file qhttpclient.c.

◆ HTTP_CODE_METHOD_NOT_ALLOWED

#define HTTP_CODE_METHOD_NOT_ALLOWED   (405)

Definition at line 199 of file qhttpclient.c.

◆ HTTP_CODE_REQUEST_TIME_OUT

#define HTTP_CODE_REQUEST_TIME_OUT   (408)

Definition at line 200 of file qhttpclient.c.

◆ HTTP_CODE_REQUEST_URI_TOO_LONG

#define HTTP_CODE_REQUEST_URI_TOO_LONG   (414)

Definition at line 201 of file qhttpclient.c.

◆ HTTP_CODE_INTERNAL_SERVER_ERROR

#define HTTP_CODE_INTERNAL_SERVER_ERROR   (500)

Definition at line 202 of file qhttpclient.c.

◆ HTTP_CODE_NOT_IMPLEMENTED

#define HTTP_CODE_NOT_IMPLEMENTED   (501)

Definition at line 203 of file qhttpclient.c.

◆ HTTP_CODE_SERVICE_UNAVAILABLE

#define HTTP_CODE_SERVICE_UNAVAILABLE   (503)

Definition at line 204 of file qhttpclient.c.

◆ HTTP_PROTOCOL_11

#define HTTP_PROTOCOL_11   "HTTP/1.1"

Definition at line 206 of file qhttpclient.c.

◆ SET_TCP_LINGER_TIMEOUT

#define SET_TCP_LINGER_TIMEOUT   (15) /*< linger seconds, 0 for disable */

Definition at line 211 of file qhttpclient.c.

◆ SET_TCP_NODELAY

#define SET_TCP_NODELAY   (1) /*< 0 for disable */

Definition at line 212 of file qhttpclient.c.

◆ MAX_SHUTDOWN_WAIT

#define MAX_SHUTDOWN_WAIT   (100) /*< maximum shutdown wait, unit is ms */

Definition at line 213 of file qhttpclient.c.

◆ MAX_ATOMIC_DATA_SIZE

#define MAX_ATOMIC_DATA_SIZE   (32 * 1024) /*< maximum sending bytes */

Definition at line 214 of file qhttpclient.c.

Function Documentation

◆ qhttpclient()

qhttpclient_t * qhttpclient ( const char *  destname,
int  port 
)

Initialize & create new HTTP client.

Parameters
destnameremote address, one of IP address, FQDN domain name and URI.
portremote port number. (can be 0 when destname is URI)
Returns
HTTP client object if succcessful, otherwise returns NULL.
qhttpclient_t *client = qhttpclient("1.2.3.4", 80);
qhttpclient_t *client = qhttpclient("www.qdecoder.org", 80);
qhttpclient_t *client = qhttpclient("http://www.qdecoder.org", 0);
qhttpclient_t *client = qhttpclient("http://www.qdecoder.org:80", 0);
qhttpclient_t *client = qhttpclient("https://www.qdecoder.org", 0);
qhttpclient_t *client = qhttpclient("https://www.qdecoder.org:443", 0);
Note
Keep-alive feature is turned off by default. Turn it on by calling setkeepalive(). If destname is URI string starting with "https://", setssl() will be called internally.

Definition at line 245 of file qhttpclient.c.

◆ setssl()

static bool setssl ( qhttpclient_t *  client)
static

qhttpclient->setssl(): Sets connection to HTTPS connection

Parameters
clientqhttpclient object pointer
httpclient->setssl(httpclient);

Definition at line 323 of file qhttpclient.c.

◆ settimeout()

static void settimeout ( qhttpclient_t *  client,
int  timeoutms 
)
static

qhttpclient->settimeout(): Sets connection wait timeout.

Parameters
clientqhttpclient object pointer
timeoutmstimeout mili-seconds. 0 for system defaults
httpclient->settimeout(httpclient, 0); // default
httpclient->settimeout(httpclient, 5000); // 5 seconds

Definition at line 363 of file qhttpclient.c.

◆ setkeepalive()

static void setkeepalive ( qhttpclient_t *  client,
bool  keepalive 
)
static

qhttpclient->setkeepalive(): Sets KEEP-ALIVE feature on/off.

Parameters
clientqhttpclient object pointer
keepalivetrue to set keep-alive on, false to set keep-alive off
httpclient->setkeepalive(httpclient, true); // keep-alive on
httpclient->setkeepalive(httpclient, false); // keep-alive off

Definition at line 380 of file qhttpclient.c.

◆ setuseragent()

static void setuseragent ( qhttpclient_t *  client,
const char *  useragent 
)
static

qhttpclient->setuseragent(): Sets user-agent string.

Parameters
clientqhttpclient object pointer
useragentuser-agent string
httpclient->setuseragent(httpclient, "MyAgent/1.0");

Definition at line 394 of file qhttpclient.c.

◆ open_()

static bool open_ ( qhttpclient_t *  client)
static

qhttpclient->open(): Opens a connection to the remote host.

Parameters
clientqhttpclient object pointer
Returns
true if successful, otherwise returns false
Note
Don't need to open a connection unless you definitely need to do this, because qhttpclient open a connection automatically when it's needed. This function also can be used to veryfy a connection failure with remote host.
if(httpclient->open(httpclient) == false) return;

Definition at line 417 of file qhttpclient.c.

◆ head()

static bool head ( qhttpclient_t *  client,
const char *  uri,
int *  rescode,
qlisttbl_t *  reqheaders,
qlisttbl_t *  resheaders 
)
static

qhttpclient->head(): Sends a HEAD request.

Parameters
clientqhttpclient object pointer.
uriURL encoded remote URI for downloading file. ("/path" or "http://.../path")
rescodeif not NULL, remote response code will be stored. (can be NULL)
reqheadersqlisttbl_t pointer which contains additional user request headers. (can be NULL)
resheadersqlisttbl_t pointer for storing response headers. (can be NULL)
Returns
true if successful(got 200 response), otherwise returns false
main() {
// create new HTTP client
qhttpclient_t *httpclient = qhttpclient("http://www.qdecoder.org", 0);
if(httpclient == NULL) return;
// set additional custom headers
qlisttbl_t *reqheaders = qlisttbl(QLISTTBL_UNIQUE | QLISTTBL_CASEINSENSITIVE);
qlisttbl_t *resheaders = qlisttbl(QLISTTBL_UNIQUE | QLISTTBL_CASEINSENSITIVE);
// send HEAD request
int nRescode = 0;
char *pszEncPath = qEncodeUrl("/img/qdecoder.png");
bool bRet = httpclient->head(httpclient, pszEncPath, &nRescode,
reqheaders, resheaders);
free(pszEncPath);
// to print out request, response headers
reqheaders->debug(reqheaders, stdout);
resheaders->debug(resheaders, stdout);
// check results
if(bRet == false) {
...(error occured)...
}
// free resources
httpclient->free(httpclient);
reqheaders->free(reqheaders);
resheaders->free(resheaders);
}

Definition at line 558 of file qhttpclient.c.

◆ get()

static bool get ( qhttpclient_t *  client,
const char *  uri,
int  fd,
off_t *  savesize,
int *  rescode,
qlisttbl_t *  reqheaders,
qlisttbl_t *  resheaders,
bool(*)(void *userdata, off_t recvbytes)  callback,
void *  userdata 
)
static

qhttpclient->get(): Downloads a file from the remote host using GET method.

Parameters
clientqhttpclient object pointer.
uriURL encoded remote URI for downloading file. ("/path" or "http://.../path")
fdopened file descriptor for writing.
savesizeif not NULL, the length of stored bytes will be stored. (can be NULL)
rescodeif not NULL, remote response code will be stored. (can be NULL)
reqheadersqlisttbl_t pointer which contains additional user request headers. (can be NULL)
resheadersqlisttbl_t pointer for storing response headers. (can be NULL)
callbackset user call-back function. (can be NULL)
userdataset user data for call-back. (can be NULL)
Returns
true if successful(200 OK), otherwise returns false
struct userdata {
...
};
static bool callback(void *userdata, off_t sentbytes) {
struct userdata *pMydata = (struct userdata*)userdata;
...(codes)...
if(need_to_cancel) return false; // stop file uploading immediately
return true;
}
main() {
// create new HTTP client
qhttpclient_t *httpclient = qhttpclient("http://www.qdecoder.org", 0);
if(httpclient == NULL) return;
// open file
int nFd = open("/tmp/test.data", O_WRONLY | O_CREAT, 0644);
// set additional custom headers
qlisttbl_t *reqheaders = qlisttbl(QLISTTBL_UNIQUE | QLISTTBL_CASEINSENSITIVE);
qlisttbl_t *resheaders = qlisttbl(QLISTTBL_UNIQUE | QLISTTBL_CASEINSENSITIVE);
// set userdata
struct userdata mydata;
...(codes)...
// send file
int nRescode = 0;
off_t nSavesize = 0;
char *pszEncPath = qEncodeUrl("/img/qdecoder.png");
bool bRet = httpclient->get(httpclient, pszEncPath, nFd, &nSavesize,
&nRescode,
reqheaders, resheaders,
callback, (void*)&mydata);
free(pszEncPath);
// to print out request, response headers
reqheaders->debug(reqheaders, stdout);
resheaders->debug(resheaders, stdout);
// check results
if(bRet == false) {
...(error occured)...
}
// free resources
httpclient->free(httpclient);
reqheaders->free(reqheaders);
resheaders->free(resheaders);
close(nFd);
}
Note
The call-back function will be called peridically whenever it send data as much as MAX_ATOMIC_DATA_SIZE. To stop uploading, return false in the call-back function, then PUT process will be stopped immediately. If a connection was not opened, it will open a connection automatically.
The "rescode" will be set if it received any response code from a remote server even though it returns false.

Definition at line 694 of file qhttpclient.c.

◆ put()

static bool put ( qhttpclient_t *  client,
const char *  uri,
int  fd,
off_t  length,
int *  rescode,
qlisttbl_t *  reqheaders,
qlisttbl_t *  resheaders,
bool(*)(void *userdata, off_t sentbytes)  callback,
void *  userdata 
)
static

qhttpclient->put(): Uploads a file to the remote host using PUT method.

Parameters
clientqhttpclient object pointer.
uriremote URL for uploading file. ("/path" or "http://.../path")
fdopened file descriptor for reading.
lengthsend size.
rescodeif not NULL, remote response code will be stored. (can be NULL)
reqheadersqlisttbl_t pointer which contains additional user request headers. (can be NULL)
resheadersqlisttbl_t pointer for storing response headers. (can be NULL)
callbackset user call-back function. (can be NULL)
userdataset user data for call-back. (can be NULL)
Returns
true if successful(201 Created), otherwise returns false
struct userdata {
...
};
static bool callback(void *userdata, off_t sentbytes) {
struct userdata *pMydata = (struct userdata*)userdata;
...(codes)...
if(need_to_cancel) return false; // stop file uploading immediately
return true;
}
main() {
// create new HTTP client
qhttpclient_t *httpclient = qhttpclient("http://www.qdecoder.org", 0);
if(httpclient == NULL) return;
// open file
int nFd = open(...);
off_t nFileSize = ...;
char *pFileMd5sum = ...;
time_t nFileDate = ...;
// set additional custom headers
qlisttbl_t *reqheaders = qlisttbl(QLISTTBL_UNIQUE | QLISTTBL_CASEINSENSITIVE);
reqheaders->putstr(reqheaders, "X-FILE-MD5SUM", pFileMd5sum);
reqheaders->putInt(reqheaders, "X-FILE-DATE", nFileDate);
// set userdata
struct userdata mydata;
...(codes)...
// send file
int nRescode = 0;
qlisttbl_t *resheaders = qlisttbl(QLISTTBL_UNIQUE | QLISTTBL_CASEINSENSITIVE);
bool bRet = httpclient->put(httpclient,
"/img/qdecoder.png", nFd, nFileSize,
&nRescode,
reqheaders, resheaders,
callback, (void*)&mydata);
// to print out request, response headers
reqheaders->debug(reqheaders, stdout);
resheaders->debug(resheaders, stdout);
// check results
if(bRet == false) {
...(error occured)...
}
// free resources
httpclient->free(httpclient);
reqheaders->free(reqheaders);
resheaders->free(resheaders);
close(nFd);
}
Note
The call-back function will be called peridically whenever it send data as much as MAX_ATOMIC_DATA_SIZE. To stop uploading, return false in the call-back function, then PUT process will be stopped immediately. If a connection was not opened, it will open a connection automatically.
The "rescode" will be set if it received any response code from a remote server even though it returns false.

Definition at line 926 of file qhttpclient.c.

◆ cmd()

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 
)
static

qhttpclient->cmd(): Sends a custom request(method) to the remote host and reads it's response.

Parameters
clientqhttpclient object pointer.
methodmethod name.
uriremote URL for uploading file. ("/path" or "http://.../path")
datadata to send. (can be NULL)
sizedata size.
rescodeif not NULL, remote response code will be stored. (can be NULL)
contentslengthif not NULL, the contents length will be stored. (can be NULL)
reqheadersqlisttbl_t pointer which contains additional user request headers. (can be NULL)
resheadersqlisttbl_t pointer for storing response headers. (can be NULL)
Returns
malloced content data if successful, otherwise returns NULL
int nResCode;
size_t nContentsLength;
void *contents = httpclient->cmd(httpclient, "DELETE" "/img/qdecoder.png",
NULL, 0
&nRescode, &nContentsLength
NULL, NULL);
if(contents == NULL) {
...(error occured)...
} else {
printf("Response code : %d\n", nResCode);
printf("Contents length : %zu\n", nContentsLength);
printf("Contents : %s\n", (char*)contents); // if contents is printable
free(contents); // de-allocate
}
Note
This store server's response into memory so if you expect server responses large amount of data, consider to use sendrequest() and readresponse() instead of using this. The returning malloced content will be allocated +1 byte than actual content size 'contentslength' and will be null terminated.

Definition at line 1099 of file qhttpclient.c.

◆ sendrequest()

static bool sendrequest ( qhttpclient_t *  client,
const char *  method,
const char *  uri,
qlisttbl_t *  reqheaders 
)
static

qhttpclient->sendrequest(): Sends a HTTP request to the remote host.

Parameters
clientqhttpclient object pointer
methodHTTP method name
uriURI string for the method. ("/path" or "http://.../path")
reqheadersqlisttbl_t pointer which contains additional user request headers. (can be NULL)
Returns
true if successful, otherwise returns false
Note
Default headers(Host, User-Agent, Connection) will be used if reqheaders does not have those headers in it.
qlisttbl_t *reqheaders = qlisttbl(QLISTTBL_UNIQUE | QLISTTBL_CASEINSENSITIVE);
reqheaders->putstr(reqheaders, "Date", qTimeGetGmtStaticStr(0), true);
httpclient->sendrequest(client,
"DELETE", "/img/qdecoder.png", reqheaders);

Definition at line 1194 of file qhttpclient.c.

◆ readresponse()

static int readresponse ( qhttpclient_t *  client,
qlisttbl_t *  resheaders,
off_t *  contentlength 
)
static

qhttpclient->readresponse(): Reads HTTP response header from the remote host.

Parameters
clientqhttpclient object pointer
resheadersqlisttbl_t pointer for storing response headers. (can be NULL)
contentlengthlength of content body(or -1 for chunked transfer encoding) will be stored. (can be NULL)
Returns
numeric HTTP response code if successful, otherwise returns 0.
// send request
httpclient->sendrequest(client, "DELETE", "/img/qdecoder.png", NULL);
// read response
qlisttbl_t *resheaders = qlisttbl(QLISTTBL_UNIQUE | QLISTTBL_CASEINSENSITIVE);
off_t clength;
int rescode = httpclient->readresponse(client, resheaders, &clength);
if(clength > 0) {
// read & throw out a content. don't need content
httpclient->read(client, NULL, clength);
}
Note
Data of content body must be read by a application side, if you want to use Keep-Alive session. Please refer qhttpclient->read().

Definition at line 1293 of file qhttpclient.c.

◆ gets_()

static ssize_t gets_ ( qhttpclient_t *  client,
char *  buf,
size_t  bufsize 
)
static

qhttpclient->gets(): Reads a text line from a HTTP/HTTPS stream.

Parameters
clientqhttpclient object pointer
bufdata buffer pointer
bufsizebuffer size
Returns
the number of bytes read from file descriptor if successful, otherwise returns -1.
Note
Be sure the return value does not mean the length of actual stored data. It means how many bytes are read from the file descriptor, so the new-line characters will be counted, but not stored.

Definition at line 1372 of file qhttpclient.c.

◆ read_()

static ssize_t read_ ( qhttpclient_t *  client,
void *  buf,
size_t  nbytes 
)
static

qhttpclient->read(): Reads data from a HTTP/HTTPS stream.

Parameters
clientqhttpclient object pointer.
bufa buffer pointer for storing content. (can be NULL, then read & throw out content)
lengthcontent size to read.
Returns
number of bytes readed
off_t clength = 0;
int resno = client->readresponse(client, NULL, &clength);
if(clength > 0) {
void *buf = malloc(clength);
client->read(client, buf, clength);
}

Definition at line 1436 of file qhttpclient.c.

◆ write_()

static ssize_t write_ ( qhttpclient_t *  client,
const void *  buf,
size_t  nbytes 
)
static

qhttpclient->write(): Writes data to a HTTP/HTTPS stream.

Parameters
clientqhttpclient object pointer.
bufa data pointer.
lengthcontent size to write.
Returns
number of bytes written.

Definition at line 1490 of file qhttpclient.c.

◆ recvfile()

static off_t recvfile ( qhttpclient_t *  client,
int  fd,
off_t  nbytes 
)
static

qhttpclient->recvfile(): Reads data from a HTTP/HTTPS stream and save into a file descriptor.

Parameters
clientqhttpclient object pointer.
fdoutput file descriptor
nbytesthe number of bytes to read and save.
Returns
the number of bytes written if successful, otherwise returns -1.

Definition at line 1534 of file qhttpclient.c.

◆ sendfile_()

static off_t sendfile_ ( qhttpclient_t *  client,
int  fd,
off_t  nbytes 
)
static

qhttpclient->sendfile(): Send file data to a HTTP/HTTPS stream.

Parameters
clientqhttpclient object pointer.
fdinput file descriptor
nbytesthe number of bytes to read and send.
Returns
the number of bytes sent if successful, otherwise returns -1.

Definition at line 1580 of file qhttpclient.c.

◆ _close()

static bool _close ( qhttpclient_t *  client)
static

qhttpclient->close(): Closes the connection.

Parameters
qhttpclient_tHTTP object pointer
Returns
true if successful, otherwise returns false
httpclient->close(httpclient);

Definition at line 1628 of file qhttpclient.c.

◆ _free()

static void _free ( qhttpclient_t *  client)
static

qhttpclient->free(): Free object.

Parameters
qhttpclient_tHTTP object pointer
Note
If the connection was not closed, it will close the connection first prior to de-allocate object.
httpclient->free(httpclient);

Definition at line 1678 of file qhttpclient.c.