Barracuda Application Server C/C++ Reference
NO
|
Modules | |
HttpUpload | |
HttpUpload is a collection of classes that makes it easy to implement a remote file manager. | |
Classes | |
struct | MultipartUpload |
The MultipartUpload, which is an abstract base class, implements the functionality as specified in RFC 1867. More... | |
struct | HttpRecData |
The HttpRecData class makes it easy to upload packets or large data chunks when using Barracuda in multithread mode. More... | |
struct | HttpAsynchReq |
Asynchronously receive data in a resource such as HttpDir or HttpPage. More... | |
struct | HttpAsynchReqResp |
Simultaneously receive and send a stream of data. More... | |
struct | HttpAsynchResp |
Send data asynchronously to a client. More... | |
Typedefs | |
typedef void(* | MultipartUpload_EndOfReq) (struct MultipartUpload *o) |
Executed by MultipartUpload when a multipart upload is completed. More... | |
typedef int(* | MultipartUpload_FormData) (struct MultipartUpload *o, const char *name, const char *value) |
Executed by MultipartUpload every time a form data element such as "text" is found in the multipart data stream. More... | |
typedef int(* | MultipartUpload_FileBegin) (struct MultipartUpload *o, const char *name, const char *fileName, const char *contentType, const char *contentTransferEncoding) |
Executed by MultipartUpload when a file is found in the multipart data stream. More... | |
typedef int(* | MultipartUpload_FileData) (struct MultipartUpload *o, const U8 *data, U16 len) |
Executed by MultipartUpload when a data chunk is available. More... | |
typedef void(* | MultipartUpload_Error) (struct MultipartUpload *o, MultipartUpload_ErrorType e) |
Executed by MultipartUpload if any errors are detected during parsing of the multipart data stream. More... | |
typedef struct MultipartUpload | MultipartUpload |
The MultipartUpload, which is an abstract base class, implements the functionality as specified in RFC 1867. More... | |
typedef struct HttpRecData | HttpRecData |
The HttpRecData class makes it easy to upload packets or large data chunks when using Barracuda in multithread mode. More... | |
typedef void(* | HttpAsynchReq_OnData) (struct HttpAsynchReq *super, void *data, S32 size) |
Prototype for the HttpAsynchReq OnRequest callback. More... | |
typedef struct HttpAsynchReq | HttpAsynchReq |
Asynchronously receive data in a resource such as HttpDir or HttpPage. More... | |
typedef HttpAsynchReqResp | HttpAsynchReqResp |
Simultaneously receive and send a stream of data. More... | |
typedef HttpAsynchResp | HttpAsynchResp |
Send data asynchronously to a client. More... | |
Enumerations | |
enum | MultipartUpload_ErrorType |
Error codes used by the callback function MultipartUpload_Error in interface class MultipartUpload. | |
Functions | |
MultipartUpload::MultipartUpload (HttpServer *server, MultipartUpload_EndOfReq onEndOfReq, MultipartUpload_FormData onFormData, MultipartUpload_FileBegin onFileBegin, MultipartUpload_FileData onFileData, MultipartUpload_Error onError, U32 bufferSize, AllocatorIntf *allocator=0) | |
MultipartUpload::~MultipartUpload () | |
Release buffers used during parsing the multipart stream. | |
int | MultipartUpload::start (HttpRequest *req) |
Start receiving data asynchronously. More... | |
HttpConnection * | MultipartUpload::getCon (MultipartUpload *o) |
Returns the internal active connection object. More... | |
HttpServer * | MultipartUpload::getServer () |
Get the HttpServer instance. | |
static SBaFileSize | HttpRecData::valid (HttpRequest *req) |
This method validates the client HTTP request. More... | |
HttpRecData::HttpRecData (HttpRequest *req) | |
The constructor is typically run by creating an object on the stack. More... | |
S32 | HttpRecData::read (void *buf, S32 bufSize) |
Read data from socket. More... | |
HttpRecData::~HttpRecData () | |
Destructor. | |
HttpAsynchReq::HttpAsynchReq (HttpServer *server, HttpAsynchReq_OnData data) | |
Create an instance of HttpAsynchReq. More... | |
HttpAsynchReq::~HttpAsynchReq () | |
Terminate a HttpAsynchReq object. | |
SBaFileSize | HttpAsynchReq::getPacketSizeLeft () |
Returns the remaining packet size if the packet size is known – i.e., if the initial HTTP header had a Content-Length header. More... | |
void * | HttpAsynchReq::getBuffer () |
Returns a pointer to the internal buffer where the received data is stored. More... | |
SBaFileSize | HttpAsynchReq::getBufferSize () |
Returns the lenght of the internal buffer. | |
static SBaFileSize | HttpAsynchReq::calcPacketSize (HttpRequest *req) |
Returns the packet size if the length is known. More... | |
int | HttpAsynchReq::start (HttpRequest *req, void *buffer, S32 bufferSize) |
Start receiving data asynchronously. More... | |
void | HttpAsynchReq::stop () |
Aborts the operation and terminates the active socket connection. | |
HttpConnection * | HttpAsynchReq::getCon (HttpAsynchReq *o) |
Returns the internal active connection object. More... | |
HttpAsynchReqResp::HttpAsynchReqResp (HttpServer *server, HttpAsynchReq_OnData onData) | |
Create a HttpAsynchReqResp instance. More... | |
int | HttpAsynchReqResp::start (HttpRequest *req, void *recBuf, S32 recBufSize, void *sendBuf, S32 sendBufSize) |
Start the HttpAsynchReqResp object. More... | |
int | HttpAsynchReqResp::startResp (HttpRequest *req, void *sendBuf, S32 sendBufSize) |
Start response if you know that all data was received by the web-server. More... | |
ThreadMutex * | HttpAsynchReqResp::getMutex () |
Get the dispatcher lock. | |
HttpServer * | HttpAsynchReqResp::getServer () |
Get the HttpServer object. | |
HttpAsynchResp * | HttpAsynchReqResp::getResponse () |
Get the response object. More... | |
HttpAsynchResp::HttpAsynchResp (char *buf, int size, HttpRequest *req) | |
Initiate a HttpAsynchResp from a HttpRequest object. More... | |
HttpAsynchResp::HttpAsynchResp (char *buf, int size, HttpConnection *con) | |
Initiate a HttpAsynchResp from a HttpConnection object. More... | |
bool | HttpAsynchResp::isValid () |
Returns true if the HttpConnection object is valid – i.e., if the socket connection is alive. | |
HttpAsynchResp::~HttpAsynchResp () | |
Calls method close. | |
void | HttpAsynchResp::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... | |
void | HttpAsynchResp::asynchThreadMode () |
Set in asynchronous thread mode so you can call the methods in this class without having to lock the dispatcher mutex. More... | |
ThreadMutex * | HttpAsynchResp::getMutex () |
Get the dispatcher lock. More... | |
int | HttpAsynchResp::setConClose () |
Close connection when the HttpAsynchResp object is done. More... | |
int | HttpAsynchResp::setLingeringClose () |
Used if a resource must close an active HttpAsynchReq and data is pending. More... | |
int | HttpAsynchResp::setStatus (int statusCode, const char *protocol=0) |
Set the response status code. More... | |
int | HttpAsynchResp::setHeader (const char *name, const char *value) |
Sets a HTTP response header with the given name and value. More... | |
int | HttpAsynchResp::sendData (const void *data, int pktSize, int chunkSize) |
Send data of known size to client. More... | |
int | HttpAsynchResp::sendNextChunk (const void *data, int chunkSize) |
Send next chunk if not all data was sent with sendData. More... | |
BufPrint * | HttpAsynchResp::getWriter () |
BufPrint is used when sending data of unknown length. More... | |
typedef struct HttpAsynchReq HttpAsynchReq |
Asynchronously receive data in a resource such as HttpDir or HttpPage.
An instance of the HttpAsynchReq class can be used as a method to receive any kind of data from a client as long as the client does not send the data as a multipart/form-data. A resource receiving multipart/form-data must use the MultipartUpload class.
You typically create an instance of this class in a resource when receiving a request. You can optionally keep instances of this class in a pool. The object is started by calling method start.
HttpAsynchReq is an abstract base class; thus, you must inherit and implement the socket event method. See constructor for how to install the event function.
The detached socket object is installed directly into the SoDisp object, and any socket event is directed to this object. The callback method specified in HttpAsynchReq::start is triggered when the buffer is full.
typedef void(* HttpAsynchReq_OnData) (struct HttpAsynchReq *super, void *data, S32 size) |
Prototype for the HttpAsynchReq OnRequest callback.
The callback is called when the internal buffer is full and at end of stream. End of stream is signaled by setting data = NULL and size=0.
If a HTTP client sends a 100-continue message, the function is initially called with a non NULL data pointer, but with size set to zero. The callback should create the resource and manage the error if creating the resource fails.
super | The object pointer, which you must upcast to your class implementation, i.e. MyAsynchReq* o = (MyAsynchReq*)super; |
data | pointer to received data. |
size | is the size of the received data. |
typedef HttpAsynchReqResp HttpAsynchReqResp |
Simultaneously receive and send a stream of data.
The HttpAsynchReqResp class makes it possible to asynchronously receive and send data at the same time. The HttpAsynchReqResp can also be used as a replacement for using a HttpAsynchReq and HttpAsynchResp object. An HttpAsynchReqResp instance is easier to use than first using a HttpAsynchReq object for receiving data and then later using a HttpAsynchResp for sending a response message.
typedef HttpAsynchResp HttpAsynchResp |
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
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.
typedef struct HttpRecData HttpRecData |
The HttpRecData class makes it easy to upload packets or large data chunks when using Barracuda in multithread mode.
This class gives similar functionality to HttpAsynchReq and makes it possible to receive any kind of data sent from a client in a CSP page or in a HttpDir/HttpPage.
CSP example
typedef struct MultipartUpload MultipartUpload |
The MultipartUpload, which is an abstract base class, implements the functionality as specified in RFC 1867.
A HTTP POST in multipart mode uses a rather complex scheme for transferring data, and we do not recommend using this class unless you use a browser for uploading data or need to send a stream of binary files and application/x-www-form-urlencoded data to the server. Transferring files from a client to the server is easier with HTTP PUT, but a browser can, unfortunately, not upload data using HTTP PUT.
You must subclass the MultipartUpload class and implement a number of 'onEvent' callback methods. The 'onEvent' methods are called as the MultipartUpload decodes the stream of data uploaded from the client.
The MultipartUpload class is designed such that any error detected in the 'onEvent' methods must make sure the MultipartUpload destructor or call MultipartUpload::getCon is called before returning a non-zero value. You may also have to release the memory associated with the MultipartUpload instance – i.e. with the instance of the class sub-classing MultipartUpload. The MultipartUpload class is designed such that it allows the callback to destroy the object itself; that is, the MultipartUpload logic makes sure no data, except for stack variables, are used when the callback returns an error code.
typedef void(* MultipartUpload_EndOfReq) (struct MultipartUpload *o) |
Executed by MultipartUpload when a multipart upload is completed.
typedef void(* MultipartUpload_Error) (struct MultipartUpload *o, MultipartUpload_ErrorType e) |
Executed by MultipartUpload if any errors are detected during parsing of the multipart data stream.
This method must call the MultipartUpload destructor or call MultipartUpload::getCon.
o | the object |
e | is the error code. |
typedef int(* MultipartUpload_FileBegin) (struct MultipartUpload *o, const char *name, const char *fileName, const char *contentType, const char *contentTransferEncoding) |
Executed by MultipartUpload when a file is found in the multipart data stream.
o | the object |
name | the name as specified in the HTML "input" type. |
fileName | is the name and possible path of the file entered by the user. The path separator is platform dependent. |
contentType | is the content mime type such as "text/plain". This parameter is NULL if not specified in the multipart data stream. |
contentTransferEncoding | is the transfer encoding such as "gzip". This parameter is NULL if not specified in the multipart data stream. |
typedef int(* MultipartUpload_FileData) (struct MultipartUpload *o, const U8 *data, U16 len) |
Executed by MultipartUpload when a data chunk is available.
The servlet first calls MultipartUpload_FileBegin and thereafter calls this method repeatedly until all data is received. End of file is signalled by setting the two input parameters to 0.
o | the object |
data | a pointer to the received data. |
len | length of the data chunk. |
typedef int(* MultipartUpload_FormData) (struct MultipartUpload *o, const char *name, const char *value) |
Executed by MultipartUpload every time a form data element such as "text" is found in the multipart data stream.
The browser bundles all data into the multipart stream sent to the server. The MultipartUpload servlet calls the formData callback function when it detects form data.
o | the object |
name | the name as specified in the HTML "input" type. |
value | the text entered by the user. |
void HttpAsynchResp::asynchThreadMode | ( | ) |
Set in asynchronous thread mode so you can call the methods in this class without having to lock the dispatcher mutex.
You must not call this method if you are calling the methods in this class from within a callback originating from the socket dispatcher SoDisp – i.e., if the mutex is already locked.
|
static |
Returns the packet size if the length is known.
This method is typically used before calling method HttpAsynchReq::start. You can calculate the length of the required buffer such that the entire data being received fits into the buffer. The OnData event method fires when the buffer is full. Thus, it is possible to make the OnData event method fire one time only if the buffer is large enough to contain all data being received.
void HttpAsynchResp::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.
A non-persistent HTTP connection such as a HTTP 1.0 connection is terminated.
void * HttpAsynchReq::getBuffer | ( | ) |
Returns a pointer to the internal buffer where the received data is stored.
The buffer is set in method HttpAsynchReq::start.
HttpConnection * HttpAsynchReq::getCon | ( | HttpAsynchReq * | o | ) |
Returns the internal active connection object.
This method is typically called from within the OnData event method when all data is received and you are moving the active connection object to an HttpAsynchResp object.
C code example:
HttpConnection * MultipartUpload::getCon | ( | MultipartUpload * | o | ) |
Returns the internal active connection object.
This method is typically called from within the onEndOfReq event method when all data is received and you are moving the active connection object to an HttpAsynchResp object.
C code example:
ThreadMutex * HttpAsynchResp::getMutex | ( | ) |
Get the dispatcher lock.
This is the only method in this class that can be called without locking the dispatcher.
SBaFileSize HttpAsynchReq::getPacketSizeLeft | ( | ) |
Returns the remaining packet size if the packet size is known – i.e., if the initial HTTP header had a Content-Length header.
This method is typically used in the OnData callback method. The method initially returns the same length as returned by HttpAsynchReq::calcPacketSize.
HttpAsynchResp * HttpAsynchReqResp::getResponse | ( | ) |
Get the response object.
The object used when sending response data.
BufPrint * HttpAsynchResp::getWriter | ( | ) |
BufPrint is used when sending data of unknown length.
For a HTTP/1.1. connection, the data is sent in chunk transfer mode.
This method returns NULL if HttpAsynchResp::sendData was called prior to this method.
HttpAsynchReq::HttpAsynchReq | ( | HttpServer * | server, |
HttpAsynchReq_OnData | data | ||
) |
Create an instance of HttpAsynchReq.
server | is a pointer to the HttpServer object. |
data | is a pointer to the socket event function. HttpAsynchReq is an abstract class and you must, therefore, implement the OnData socket event method. |
HttpAsynchReqResp::HttpAsynchReqResp | ( | HttpServer * | server, |
HttpAsynchReq_OnData | onData | ||
) |
Create a HttpAsynchReqResp instance.
server | the HttpServer instance. |
onData | the asynchronous receive callback method. HttpAsynchReqResp inherits from HttpAsynchReq and it is the HttpAsynchReq object which calls the callback method when the internal buffer is full. End of stream is signaled by setting the two parameters to the callback to null. |
HttpAsynchResp::HttpAsynchResp | ( | char * | buf, |
int | size, | ||
HttpConnection * | con | ||
) |
Initiate a HttpAsynchResp from a HttpConnection object.
buf | the output buffer. Minimum value is 255 bytes. |
size | the output buffer size. |
con | the active connection object is moved from HttpConnection to this object. |
HttpAsynchResp::HttpAsynchResp | ( | char * | buf, |
int | size, | ||
HttpRequest * | req | ||
) |
Initiate a HttpAsynchResp from a HttpRequest object.
buf | the output buffer. Minimum value is 255 bytes. |
size | the output buffer size. |
req | the active connection object is moved from HttpRequest to this object. |
HttpRecData::HttpRecData | ( | HttpRequest * | req | ) |
The constructor is typically run by creating an object on the stack.
req | is the page HttpRequest object. |
MultipartUpload::MultipartUpload | ( | HttpServer * | server, |
MultipartUpload_EndOfReq | onEndOfReq, | ||
MultipartUpload_FormData | onFormData, | ||
MultipartUpload_FileBegin | onFileBegin, | ||
MultipartUpload_FileData | onFileData, | ||
MultipartUpload_Error | onError, | ||
U32 | bufferSize, | ||
AllocatorIntf * | allocator = 0 |
||
) |
server | is the Web-Server object. |
onEndOfReq | is called when the multipart upload is completed. See MultipartUpload_EndOfReq. |
onFormData | the function where you process form data. See MultipartUpload_FormData. |
onFileBegin | is called when a file is found in the input stream. See MultipartUpload_FileBegin. |
onFileData | is repeatedly called until end of file. See MultipartUpload_FileData. |
onError | is called if an error is encountered when processing the input stream. See MultipartUpload_Error. |
bufferSize | is the size of the internal buffer used when parsing the received data stream. This size must be, at a minimum, the size of the largest form submitted. This size should probably be > 10K. |
allocator | An allocator used for various small string allocations needed when parsing the incomming data stream and for allocating the internal buffer. The size of the internal buffer is set with parameter 'bufferSize'. |
S32 HttpRecData::read | ( | void * | buf, |
S32 | bufSize | ||
) |
Read data from socket.
buf | is the buffer to where the data is saved. |
bufSize | is the size of the buffer. |
int HttpAsynchResp::sendData | ( | const void * | data, |
int | pktSize, | ||
int | chunkSize | ||
) |
Send data of known size to client.
data | the data to send. |
pktSize | The size of the data to send. |
chunkSize | the size of parameter 'data'. Set this parameter to pktSize if all data is sent in the sendData call. Otherwise, set chunkSize to the size of the chunk being sent, and then call method sendNextChunk. |
int HttpAsynchResp::sendNextChunk | ( | const void * | data, |
int | chunkSize | ||
) |
Send next chunk if not all data was sent with sendData.
data | the data to send. |
chunkSize | the size of parameter 'data'. |
int HttpAsynchResp::setConClose | ( | ) |
Close connection when the HttpAsynchResp object is done.
A persistent HTTP 1.1 connection is recycled and sent back to the connection pool in the web-server when the HttpAsynchResp object is done. Calling this method makes sure the connection closes when the HttpAsynchResp object is done. You should call this method if you stream data such as audio.
int HttpAsynchResp::setHeader | ( | const char * | name, |
const char * | value | ||
) |
Sets a HTTP response header with the given name and value.
name | the name of the header to set. |
value | the header value. |
int HttpAsynchResp::setLingeringClose | ( | ) |
Used if a resource must close an active HttpAsynchReq and data is pending.
If a server closes the input side of the connection while the client is sending data (or is planning to send data), then the server's TCP stack will signal an RST (reset) back to the client. This forces a disconnect before client can read response data.
Please note that Barracuda is not implementing this by using SO_LINGER, but is instead using a lingering close queue.
int HttpAsynchResp::setStatus | ( | int | statusCode, |
const char * | protocol = 0 |
||
) |
Set the response status code.
The default return code is 200.
statusCode | The HTTP status code |
protocol | set the protocol version. The protocol is set to "1.1" if this parameter is NULL. |
int MultipartUpload::start | ( | HttpRequest * | req | ) |
Start receiving data asynchronously.
The active socket object is detached from the web-server and moved into the MultipartUpload object. The MultipartUpload object is inserted into the SoDisp object, thus causing the socket dispatcher to fire receive events directly to the MultipartUpload object. The event methods are called: when the buffer is full, receiving form data, etc.
int HttpAsynchReq::start | ( | HttpRequest * | req, |
void * | buffer, | ||
S32 | bufferSize | ||
) |
Start receiving data asynchronously.
The active socket object is detached from the web-server and moved into the HttpAsynchReq object. The HttpAsynchReq object is inserted into the SoDisp object, thus causing the socket dispatcher to fire receive events directly to the HttpAsynchReq object. The event method OnData is called when the buffer is full. Thus, the OnData event method can be called a number of times if the buffer is not large enough to contain the data being received.
int HttpAsynchReqResp::start | ( | HttpRequest * | req, |
void * | recBuf, | ||
S32 | recBufSize, | ||
void * | sendBuf, | ||
S32 | sendBufSize | ||
) |
Start the HttpAsynchReqResp object.
The onData callback method is called directly if all data was received by the client; otherwise, the HttpAsynchReq object is installed into the SoDisp. The method returns a negative value if the HttpAsynchReq object failed to start. The method returns 0 if the object started successfully or if the destructor was called from within the onData callback.
req | the HttpRequest object. |
recBuf | the receive buffer. The onData callback is called when the buffer is full. |
recBufSize | size of recBuf. |
sendBuf | send buffer used by HttpAsynchResp. |
sendBufSize | the size of the send buffer. Min size is 256. |
int HttpAsynchReqResp::startResp | ( | HttpRequest * | req, |
void * | sendBuf, | ||
S32 | sendBufSize | ||
) |
Start response if you know that all data was received by the web-server.
This is a faster method than calling start if you know that the web-server received all data in its internal buffer before delegating the request to the requested service.
|
static |
This method validates the client HTTP request.
The data received from the client must have a content-length or be chunk encoded.