Home | Libraries | People | FAQ | More |
TODO: Document using Requests
After a request has been accepted, it should also be 'loaded'. Before being loaded the request is in an undefined state and it is unsafe to access any data associated with the request - although you may still read/write with the request. For CGI, the request's constructor calls load implicitly (this is optional behaviour), in most other situations, one of the following functions are used:
Method Name |
Function signature |
Purpose |
---|---|---|
|
|
Loads the request meta-data according to the passed |
|
|
Loads the request meta-data according to the passed |
|
|
Asynchronously loads the request meta-data according to the passed
|
What the call does is acquire the request environment data as necessary
and parse the CGI GET
and
HTTP_COOKIE
variables.
Also reads and parses form data (ie. POST
data). If the form data include file uploads, these are saved to disk and
information about the file is stored in the request (see basic_request<>::uploads
).
#include <boost/cgi/cgi.hpp> namespace cgi = boost::cgi; int main() { // Delay loading the request data cgi::request request(cgi::parse_none); cgi::response response; // ... // Load the request now (including parsing stdin). // Passing an `error_code` argument stops exceptions being thrown. boost::system::error_code& ec; request.load(cgi::parse_all, ec); if (ec) { response<< "Failed to load response."; return cgi::commit(request, response, cgi::http::internal_server_error); } response<< "Loaded response OK."; return cgi::commit(request, response); }
#include <boost/cgi/fcgi.hpp> namespace fcgi = boost::fcgi; int handler(fcgi::request& request) { // It is undefined behaviour to use a request without loading it. request.load(fcgi::parse_env); // minimal parsing. // Create a Response fcgi::response response; response<< "Hello, world."; return fcgi::commit(request, response); } int main() { // Create a ProtocolService fcgi::service service; // Create a RequestAcceptor fcgi::acceptor acceptor(service); int status(0); while (!status) { status = acceptor.accept(&handler); } return status; }