In today’s web world of Ajax (Asynchronous Javascript and XML), C++ programmers may feel out of place, but there’s not need for that - they just need to make use of that old favourite: CGI - the Common Gateway Interface.
Basic Requirements for C++ and CGI
The C++ programmer doesn’t really need that much - just a server that has:
- web server software (such as Apache2)
- a C++ code compiler (such as g++ - the GNU compiler)
Creating a Simple CGI C++ Program
A simple CGI program consists of C++ code that will produce an output. Something like the following will suffice:
This C++ code does three things:
- it obtains the HTML variable QUERY_STRING by making use of the getenv function
- it outputs the text "Content-Type: text/plain\n\n" (which is standard to all CGI scripts and programs)
- it outputs QUERY_STRING
It is then just a matter of compiling the C++ code, for example:
g++ -o response response.cpp
The compiled program must be placed in the CGI bin directory for the server. The developer may have to check the Apache2 configuration files to find out where that is, but once it's in the correct location then the C++ program can be accessed via the web browser:
http://<my server>/cgi-bin/response?input="A test"
Of course, where this gets interesting is when this standard CGI program is turned into a state of the art Ajax application.
Turning a C++ CGI Program into an Ajax Application
The key to Ajax is not some new technology - it is quite simple Javascript code:
<script type = "text/javascript">
var XMLHttp;
if(navigator.appName == "Microsoft Internet Explorer") {
XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
} else {
XMLHttp = new XMLHttpRequest();
}
function getresponse () {
XMLHttp.open
("GET", "/cgi-bin/response?"
+ "fname=" + document.getElementById('fname').value
+ "&sname=" + document.getElementById('sname').value
,true);
XMLHttp.onreadystatechange=function() {
document.getElementById('response_area').innerHTML = XMLHttp.responseText;
}
XMLHttp.send(null);
}
</script>
First Names(s)<input onkeyup = "javascript: getresponse ()" id=fname><br>
Surname<input onkeyup = "javascript: getresponse ()" id=sname>
<div id = "response_area">
</div>
This HTML and Javascript code does the following:
- creates an appropriate XML Http Request object (the core to the Ajax technique)
- sends requests to the server (two variables - fname and sname)
- handles any responses from the server - in this case it updates an area of the screen with the output from the 'response' C++ program
In this example the two variables are sent to the CGI program; however, the string that is returned contains all of the data as well as some control characters - further processing is required before that data is usable.
Processing Ajax Data in a C++ Program
At this point the C++ programmer could spend a few hours writing code to process the QUERY_STRING variable, however this is not necessary - there are any number of C++ libraries freely available, for instance the CGI libraries can be installed on a Debian Linux server by logging on as root and typing:
apt-get install libcgicc1
Then, the CGI library can be used in the following way:
#include <iostream>
#include "cgicc/Cgicc.h"
using namespace std;
using namespace cgicc;
int main() {
Cgicc cgi;
form_iterator fname = cgi.getElement("fname");
form_iterator sname = cgi.getElement("sname");
cout << "Content-Type: text/plain\n\n"
<< "You've input:"
<< " First names(s): " << **fname
<< " Surname: " << **sname << endl;
return 0;
}
This new file can be compiled (after ensuring that the complier knows which library to use and where it is) as follows:
g++ -o response -I/usr/include -lcgicc response2.c
With these few easy steps the C++ programmer, with the aid of just a little Javascript, has moved into the world of Ajax applications.