Home  +  Forums  +  C++ and Sockets  +  C++ and SQL: MySQL, sqlite, ODBC  +  Miscellaneous Projects
Logo
~Sockets~
~Projects~
~Contact~
esta pagina en espanol

cgi++ HOWTO

Common Gateway Interface implemented using C++

Why use cgi as a method of implementing a dynamic website when there are so many powerful and easy-to-use tools such as PHP and ASP?

Using cgi will, at worst, only result in one executable stuffed away in some cgi-bin/ directory. Or maybe even worse, a lot of executables. To change the page layout you need to do a recompile and reinstall of the executable(s).

A cgi program can of course be written in some sort of script language where a recompile isn't necessary, making it "easier" to install changes.


Using C++

Using object oriented features in C++ such as inheritance makes developing a cgi program fast and reliable.

This document will try to describe the cgi++ method of developing a dynamic website.

What is cgi++?

It's a small collection of classes developed with the intention of making it easy to quickly develop a dynamic website.

Download cgi++-1.3.tar.gz 2007-11-29

Changelog

Source code documentation (generated by Doxygen). Select "File List" from the doxygen index page to see a list of all files in the example. Then select "[code]" to read the source code of that file. Files beginning with lowercase are usually the main() starting point, and files beginning with uppercase usually contains a class of the same name as the file itself.

Older versions

Download cgi++-1.2.2.tar.gz 2005-04-15

Download cgi++-1.1.tar.gz 2004-07-09

Download cgi++-1.0.tar.gz

Getting Your Hands Dirty

Only technical stuff beyond this point.

Cookies, POST, and stuff - the Web class

An implementation of a cgi++ application begins with creating a class which inherits the Web class. The Web class contains code to interpret information from the web server (apache / IIS) such as cookies and form posts (using the Cookies and Form classes).

Another base class to use is the MenuWeb class. It complements the Web class with automatic menu functions. The example below will use this as the base class.

The Web class is initialized with the Cookie domain and Cookie path, to be used in http 'Set-Cookie:' headers.

There are four methods that needs to be implemented in our inherited Web class:

These methods are all called, in above sequence, by another method named Execute ( ).

In short, they do as they are named. All methods have default implementations that could, and sometimes should, be used.

What about content? - the WebForm class

The above mentioned Web class only provides the framework for a cgi application. So where to put code for displaying pages with content? The actual pages are implemented by creating a class inherited from the WebForm class.

This is a very small class that only need to implement two methods to work:

The Process ( ) method are called upon to read form input from the page displayed (if any, if no form then this method is empty).

The Display ( ) method creates the actual html code displaying the page.

Once a WebForm class has been made, it is added as a member variable of the Web class mentioned above, and registered in the Web class constructor. It is registered once with the Web class method RegWebForm, to enable the Process ( ) method to be called automatically. If using MenuWeb as the parent class, the WebForm class is registered also with the MenuWeb class method AddMenuItem ( ) to make it appear in the automatic menu.

The Example

Code speaks better than a 1000 words, eh?

This example is an editor for the excellent Wolfshade MUD.

The MenuWeb class is used as the parent class for the Web class (named WolfshadeWeb). The WebForm class is first inherited by a BaseForm class which implements a lot of commonly used display elements. The BaseForm class is then inherited by the actual presentation classes.

The WolfshadeWeb class are using a MySQL database connection implemented using classes from the MySQL C API C++ Wrapper.

Example Documentation
and
The Example, Live

Class Diagram

Class Diagram
Page, code, and content Copyright (C) 2021 by Anders Hedström