LibUC
#include "uc_web.h"
LibUC provides set of routines for web and REST based programming. These include routines for basic cgi programming and for posting and retrieving UniversalContainers to and from web services.
The web_rpc_call and web_rpc_return functions are intended to work with the init_cgi to provide a complete, easy to use, remote call mechanism. The intended use pattern begins with the client, which creates a UniversalContainer containing the variables of the request. It uses this request object to call web_rpc_call. The server receives this request by calling init_cgi. The server can then check the "post" key to retrieve the request object. Once the server has constructed a response object it uses that object to call web_rpc_return. The client then receives this response as the return value of the initial web_rpc_call.
CGI Functions
UniversalContainer init_cgi(void)
Note: This method must be called before the cgi program reads any input from stdin. This function returns a dictionary describing the input and environment of a cgi program. It attempts to parse get and post variables into UniversalContainers if possible. It understands both traditional HTTP form posts, and data sent in json format. The table below describes the entries in the dictionary.
Key | Value |
---|---|
get | If get variables where sent to the cgi script, this value holds a dictionary of those variables, and will evaluate to true in a boolean cast. Otherwise it evaluates to false. |
post | If post variables where sent to the cgi script, this value holds a dictionary of those variables, and will evaluate to true in a boolean cast. Otherwise it evaluates to false. init_cgi understands both application/x-www-form-urlencoded and application/json formats for post data. |
cookies | If cookies where sent to the cgi script, this value holds a dictionary of those variables, and will evaluate to true in a boolean cast. Otherwise it evaluates to false. |
env | Contains a dictionary of the environmental variables passed to the cgi program. |
stdin_available | Boolean that indicates whether or not the stdin stream was consumed by init_cgi. Generally, this will be false when post data was read, and false when it was not available or in a format not understood by init_cgi. |
Web RPC Functions
UniversalContainer web_rpc_call(const char* url, UniversalContainer& request, const char* mime_type)
This method requires that LibUC be built with libcurl support.
This method formats the provided request object according to the provided mime_type, and then does an HTTP post of this formatted data to the given url. The response from the server is interpreted based on the returning mime type, and parsed to produce the UniversalContainer which is returned. The understood types are application/x-www-form-urlencoded and application/json, and if mime_type is NULL, mime_type will default to application/x-www-form-urlencoded.
void web_rpc_return(UniversalContainer& uc, const char* mime_type)
This method formats the passed UniversalContainer according to the provided mime_type, then prints the mime-type and container to stdout. It provides an easy way for servers to respond to clients that use web_rpc_call. Understood types are application/x-www-form-urlencoded and application/json, and if mime_type is NULL, mime_type will default to application/x-www-form-urlencoded.
UniversalContainer web_fetch(const char* url)
This method requires that LibUC be built with libcurl support.
Retrieves a set of name/value pairs from a given url, using HTTP get. This method checks the mime-type and uses that to determine how to parse the data. Understood types are application/x-www-form-urlencoded and application/json. Most web documents are not formatted in this fashion, but some software such as couchDB provides data in this fashion.