Barracuda Application Server C/C++ Reference
NO
HttpClient Struct Reference

Detailed Description

The HTTP(S) "C" client library implementation conforms to the HTTP/1.1 standard, RFC 2616.

The following example shows how to create a function that loads the response from a server that either returns static content with a known content length or from a server that returns dynamically created data using chunk encoding.

A server can send a content length or chunk encoding for dynamically generated data. We can trick the server into sending a content-length even for dynamically generated data by sending a head request. Knowing the content length makes it easier to allocate a storage buffer.

char* loadURL(HttpClient* http, const char* url, int* contentLen)
{
*contentLen = 0;
if( ! http->request(HttpMethod_Head, url) )
{
long int len;
const char* cl = http->getHeaderValue("Content-Length");
if(cl && http->getStatus() == 200)
{
len = strtol(cl, NULL, 10);
if(len > 0 && len < 30*1024 && //We do not want to alloc more than 30K
! http->request(HttpMethod_Get, url) && http->getStatus())
{
*contentLen = len;
char* buf = (char*)malloc(len+1); // +1 for strings (REF-S)
if(buf)
{
char* ptr=buf;
while(len)
{
int chunkLen = http->readData(ptr, len);
if(chunkLen <= 0)
break; //Failed: -1 on err. 0 not accepted
len -= chunkLen;
ptr += chunkLen;
assert(len >= 0);
}
if( ! len ) // if OK
{
// REF-S: Zero terminate.
// Can be used by caller if buf is a string.
*ptr=0;
return buf;
}
}
free(buf);
}
}
}
return NULL; // failed
}
int readData(void *buf, int bufSize)
Read HTTP response data.
Definition: HttpClient.h:490
int request(HttpMethod methodType, const char *url, const char *userPass=0, const HttpClientKeyVal *query=0, const HttpClientKeyVal *headers=0, BaFileSize size=0)
Definition: HttpClient.h:472
const char * getHeaderValue(const char *name)
Returns the value for the header 'name' or NULL if the header is not in the response.
Definition: HttpClient.h:494
int getStatus()
Returns the server's HTTP response code or a negative value on socket errors.
Definition: HttpClient.h:506
The HTTP(S) "C" client library implementation conforms to the HTTP/1.1 standard, RFC 2616.
Definition: HttpClient.h:190

#include <HttpClient.h>

Inheritance diagram for HttpClient:

Public Member Functions

 HttpClient (SoDisp *disp, U8 mode=HttpClient_Persistent)
 Create a HttpClient instance. More...
 
 ~HttpClient ()
 Terminate the HttpClient.
 
void setSSL (SharkSsl *ssl)
 Set the SharkSSL client object to enable https: URL's. More...
 
void setReadTmo (BaTime timeout)
 Set the read timeout (in milliseconds). More...
 
SharkSslConTrust trusted (void)
 Returns the peer's "trust" status. More...
 
void setAcceptTrusted (bool acceptTrusted)
 Force method HttpClient::request to accept only trusted connections when connecting to a server. More...
 
int request (HttpMethod methodType, const char *url, const char *userPass=0, const HttpClientKeyVal *query=0, const HttpClientKeyVal *headers=0, BaFileSize size=0)
 
int sendData (const void *data, int len)
 Send data to the server. More...
 
int readData (void *buf, int bufSize)
 Read HTTP response data. More...
 
const char * getHeaderValue (const char *name)
 Returns the value for the header 'name' or NULL if the header is not in the response. More...
 
HttpClientHeadergetHeaders (int *hlen)
 Returns all HTTP response headers. More...
 
void close ()
 Close a persisten HTTP 1.1 connection.
 
int getStatus ()
 Returns the server's HTTP response code or a negative value on socket errors.
 
int getError ()
 Returns the last socket error code if any. More...
 
SharkSslCon * getSharkSslCon ()
 Wrapper for SoDispCon:getSharkSslCon.
 
- Public Member Functions inherited from SoDispCon
int connect (const char *host, U16 port, const void *bindIntfName=0, U16 bindPort=0, U32 timeout=1500, BaBool dgram=false, BaBool ipv6=false, char **errinfo=0)
 Connect to host/IP address. More...
 
bool isSecure ()
 Deprecated: Use getSharkSslCon(NULL).
 
bool getSharkSslCon (SharkSslCon **sc)
 Returns true if this is an SSL connection. More...
 
bool isValid ()
 Returns true if the socket connection is valid.
 
bool isIP6 ()
 Returns true if this is an IP version 6 connection.
 
int getPeerName (HttpSockaddr *addr, U16 *port=0)
 Returns the 'peer' IP address. More...
 
int getSockName (HttpSockaddr *addr, U16 *port=0)
 Returns the 'sock' IP address. More...
 
char * addr2String (HttpSockaddr *addr, char *buf, int len)
 Converts IP address to string.
 
bool cmpAddr (HttpSockaddr *addr2)
 Compare addr with address returned by getPeerName.
 
void setTCPNoDelay (bool enable)
 Disable the TCP delay. More...
 
struct SoDispgetDispatcher ()
 Fetch the SoDisp object.
 
bool hasMoreData ()
 Returns true if more data on the socket.
 
bool dispatcherHasCon ()
 Returns true if connection is in the SoDisp object.
 
bool recEvActive ()
 Returns true if the connection is active in the SoDisp object.
 
bool sendEvActive ()
 Used for non blocking send.
 

Static Public Member Functions

static int isURL (const char *url)
 Returns true if the URL is valid.