Barracuda Application Server C/C++ Reference
Native APIs, integration guides, and platform interfaces
HttpAsynchResp Struct Reference

Detailed Description

Send data asynchronously to a client.

Please see the HTTP protocol stack for an introduction to the HttpAsynchResp class.

You must lock the dispatcher prior to calling any methods in the HttpAsynchResp class unless the object is used from within a Barracuda event callback or you have called method HttpAsynchResp::asynchThreadMode. See using multiple threads in the Barracuda introduction for more information on protecting the Barracuda code.

There is one exception to the above. You can call method HttpAsynchResp::getMutex without locking the dispatcher first. You cannot lock the dispatcher unless you first have a reference to the dispatcher object.

example code

while(sendData)
{
ThreadLock lock(myAsynchResp->getMutex()); // Keep the lock until scope exit.
BufPrint* writer = myAsynchResp->getWriter();
if(!writer || writer->printf("Hi client") < 0)
break; // Stop producing output after a failure.
}
int printf(const char *fmt,...)
Format values using the compact BAS formatter.
Definition: BufPrint.h:391
int sendData(const void *data, int pktSize, int chunkSize)
Begin a fixed-length response body.
Definition: HttpAsynchResp.h:395
The BufPrint class, which implements a compact printf-style formatter, is a base class used by severa...
Definition: BufPrint.h:132
This class provides an easy and less error-prone way of locking and releasing a mutex.
Definition: ThreadLib.h:124

Certain restrictions apply to how you can use this object. This is, after all, a lightweight version of HttpResponse. The HttpAsynchResp object does not have large buffers for storing out data. Thus, the methods must be called in the following order:

Calling methods setConClose, setStatus, and setHeader are optional.

Calling method setHeader implicitly calls method setStatus with status code 200 unless you already called setStatus.

Sending data using method sendData or the methods in BufPrint flushes the HTTP header. Thus, you cannot call method setHeader after the HTTP header is flushed.

You must use one of BufPrint or ( HttpAsynchResp::sendData [ & HttpAsynchResp::sendNextChunk ] ) when sending response data.

#include <HttpAsynchResp.h>

Inheritance diagram for HttpAsynchResp:

Public Member Functions

 HttpAsynchResp ()
 Uninitialized storage; initialize before use or destruction. More...
 
 HttpAsynchResp (char *buf, int size, HttpRequest *req)
 Initiate a HttpAsynchResp from a HttpRequest object. More...
 
 HttpAsynchResp (char *buf, int size, HttpConnection *con)
 Initiate a HttpAsynchResp from a HttpConnection object. More...
 
bool isValid ()
 Check the installed buffer and connection. More...
 
void asynchThreadMode ()
 Set in asynchronous thread mode so you can call the methods in this class using its worker-thread send path. More...
 
 ~HttpAsynchResp ()
 Calls method close. More...
 
void close ()
 Flushes the response, if any, and checks the connection object: A valid and persistent HTTP 1.1 socket connection object is moved back into the Web-server's HTTP 1.1 HttpConnection pool such that the connection can be recycled. More...
 
ThreadMutex * getMutex ()
 Get the dispatcher lock. More...
 
int setConClose ()
 Close connection when the HttpAsynchResp object is done. More...
 
int setLingeringClose ()
 Request connection closure. More...
 
int setStatus (int statusCode, const char *protocol=0)
 Append the HTTP status line and standard Date/Server headers. More...
 
int setHeader (const char *name, const char *value)
 Append a response header before starting the body. More...
 
int sendData (const void *data, int pktSize, int chunkSize)
 Begin a fixed-length response body. More...
 
int sendNextChunk (const void *data, int chunkSize)
 Continue the fixed-length body begun with sendData. More...
 
BufPrint * getWriter ()
 BufPrint is used when sending data of unknown length. More...
 
- Public Member Functions inherited from HttpConnection
 HttpConnection ()
 Uninitialized storage; use HttpConnection_constructor before use. More...
 
struct HttpServer * getServer ()
 
void setKeepAlive ()
 Enable persistent connection reuse after a completed HTTP request. More...
 
void clearKeepAlive ()
 Disable persistent connection reuse after the request completes. More...
 
- Public Member Functions inherited from SoDispCon
 SoDispCon ()
 Uninitialized storage; call SoDispCon_constructor before use. More...
 
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 an initialized, empty connection to a remote endpoint. More...
 
bool isSecure ()
 Deprecated: Use getSharkSslCon(NULL). More...
 
bool getSharkSslCon (SharkSslCon **sc)
 Inspect a connected transport's TLS implementation. More...
 
bool isValid ()
 
bool isIP6 ()
 
int getPeerName (HttpSockaddr *addr, U16 *port=0)
 Read the remote peer endpoint. More...
 
int getSockName (HttpSockaddr *addr, U16 *port=0)
 Read the local socket endpoint. More...
 
char * addr2String (HttpSockaddr *addr, char *buf, int len)
 Format a numeric address without a port number. More...
 
bool cmpAddr (HttpSockaddr *addr2)
 Compare an address with the current peer address, ignoring port numbers. More...
 
void setTCPNoDelay (bool enable)
 Disable the TCP delay. More...
 
struct SoDisp * getDispatcher ()
 
bool hasMoreData ()
 
bool dispatcherHasCon ()
 
bool recEvActive ()
 
bool sendEvActive ()
 
void setDispSendEvent (SoDispCon_DispSendEv ev)
 Install a send-ready callback before enabling send events. More...
 
void setDispRecEvent (SoDispCon_DispRecEv ev)
 Install a receive callback before enabling receive events. More...
 
int readData (void *data, int len, bool relmutex=false)
 Read through the installed transport using its recorded readiness. More...
 
int setNonblocking ()
 Select nonblocking socket mode. More...
 
int setBlocking ()
 Select blocking socket mode. More...
 
int sendData (const void *data, int len)
 Send an entire buffer through the installed transport. More...
 
int asyncSend (int len)
 Begin or advance a send from the transport's asynchronous buffer. More...
 
int sendChunkData (const void *data, int len)
 Send one HTTP chunk, including its hexadecimal size and CRLF delimiters. More...
 
int asyncReady ()
 Advance pending output and inspect completion. More...
 
void * allocAsynchBuf (int *size)
 Obtain transport-owned writable storage for asynchronous sending. More...
 

Constructor & Destructor Documentation

◆ HttpAsynchResp()

HttpAsynchResp::HttpAsynchResp ( )

Uninitialized storage; initialize before use or destruction.