How to do CGI using C++
Introduction
For this document, I will assume that you have a working knowledge of html
including html forms. There are two methods of doing cgi, "get" and "post"
and this document will concentrate solely on the post method. (So on your
html page make sure that the form tag contains 'method="post"')
Step 1: Getting the Data length
With the data passed to the cgi script, before you can read it in you need
to know how much of it there is. The length of the data is stored in an
environmental variable known as "CONTENT_LENGTH". To get the length, use
the function "getenv" in stdlib.h. This will return a string containing
that environmental variable. So, to get the length of the data and store
it in the variable called "data-len", requires the following lines
(NOTE: the below line will not compile. Replace the variable data-len with
your own variable defined previously as a string i.e. char X[?])
strcpy(data-len,getenv("CONTENT_LENGTH"));
Step 2: Converting the Data Length
So, now we've got the data length, we're set? Not quite. The data length as
you may have noticed from before, is a string. To make it usable, we need to
convert it to an int. There are a number of functions to do this (To see them
type man string and they will be in that list). The function I frequently use
is "atoi()" which takes the string as a parameter and returns the integer
equivalent.
Step 3: Reading in the Data
Finally now we can read in the data. We know the length of it from our atoi
function (you did store the result in a variable didn't you?). So all we need
is a basic for loop which runs length times and in the loop we use "cin.get(c)"
where c is a character (most likely a character in a string i.e. string[i])
for (int i=0; i< data-len; i++)
cin.get(str[i]);
Step 4: Data Parsing
Now we have the data passed from a form stored in a string variable. All
that remains is to use it. Sadly, there are still a few problems to be
overcome. If you have a form with two input fields, lets call them
field1 and field2, declared as follows
< INPUT TYPE=TEXT MAXLENGTH=30 NAME="field1">
< INPUT TYPE=TEXT MAXLENGTH=20 NAME="field2">
Lets say that into field1 the user types "hello word" and that into field2
he types "username@host.domain". What will then be read in by your
program is
field1=hello+world&field2=username%40host.domain
So as you can see, the data from the fields is not in a particularly
usable form. I shall leave it up to you, the programmer to work out the
details of how to extract the correct substrings from the data string, but
the following information is useful:
Data String Formatting
- The string consists of the name of the input followed by the value
that input takes.
- The field name is separated from the data value by an "="
- One set of field and data is separated from the next by an "&"
- Spaces in the input data are replaced by "+"
- Non letters and numbers are replaced by "%xx" where "xx" is the hex
value corresponding to that character.
All of the above can be seen in the sample data input given in the
previous section.
|
Bruce Richardson
1/12/99
|