dbp::cgi_application Class Reference

CGI application class. More...

#include <cgi_application.h>

Inheritance diagram for dbp::cgi_application:
Inheritance graph
[legend]
Collaboration diagram for dbp::cgi_application:
Collaboration graph
[legend]

List of all members.

Public Member Functions

virtual void on_handle_request (on_request_handler handler)
 Register a custom web request handler.
virtual void on_exception (on_exception_handler handler)
 Register a custom exception handler.
int run (int argc, char *argv[])
 Execute the application.

Protected Member Functions

virtual http_request get_request ()
virtual void send_response (const http_response &response)

Friends

class singleton< cgi_application >

Detailed Description

CGI application class.

This class implements the web application on the CGI protocol. The cgi_application supports CGI (Common Gateway Interface) version 1.1, as described at RFC 3875.

This is a most common web server extension format supported by all known extensible web servers. However, this protocol has many disadvantages including speed, memory usage, no pooling and limited session data.

CGI application example:

/*
 * Classic "Hello, world!" example CGI application.
 *
 * This example shows how to write "skeleton" of the application with
 * DCL framework using dbp::cgi_application class.
 *
 * As the result, the simple CGI application will be produced.
 */

#include <iostream>
#include <string>

// include all classes from dclbase library
#include <dcl/dclbase.h>
#include <dcl/cgi_application.h>

// use the DCL default namespace by default
using namespace dbp;

// declare our own application class
class hello_world_app {
public:
        // the constructor of the hello_world_app class initializes the
        // console application by obtaining a link to the dbp::application class
        // via its instance() method call.
        hello_world_app(): app(cgi_application::instance()) {
                // register the execute event handler
                app.on_handle_request(create_delegate(this,
                  &hello_world_app::process_request));
        }
private:
        // the reference to the console application class
        cgi_application &app;
        // execute event handler
        http_response process_request(const http_request &req) {
                // initialize the response
                http_response resp;
                // we are only support GET method in this demo
                if (req.get_method() == http_method::get) {
                        // initialize the response with greeting message
                        resp.set_content_type("text/plain");
                        resp.set_content("Hello, world!");
                } else {
                        // send the response with "405 Method Not Allowed" status code
                        resp.set_status(http_error::method_not_allowed);
                        resp.set_allow(http_method::get);
                }
                // send the response to the client
                return resp;
        }
};

// initialize and run the application via provided macros
IMPLEMENT_APP(hello_world_app().app);

Member Function Documentation

virtual void dbp::cgi_application::on_exception ( on_exception_handler  handler  )  [inline, virtual]

Register a custom exception handler.

By default, the web application returns the '500 Internal Server Error' HTTP error code on exceptions occured, but you can customize the default exceptions handling by registering your handler.

Parameters:
handler the exception handler delegate.

Implements dbp::web_application_int.

virtual void dbp::cgi_application::on_handle_request ( on_request_handler  handler  )  [inline, virtual]

Register a custom web request handler.

To make your application doing something useful, you should register your web request handler.

This handler is a web application entry point. When client is passing the request, it comes to a web server, processed with it and delivers to the application. You can obtain web request fields via http_request parameter.

After processing of the request you should to deliver the appropriate response to a client via web server by returning initialized http_response object.

The 'examples/hello_world/cgi_application' directory contains the source code of the web request handler implementation example.

Parameters:
handler the request handler delegate.

Implements dbp::web_application_int.


The documentation for this class was generated from the following file:

 
Support This Project
SourceForge.net Logo