C application on web server using CGI

 What is CGI?

The Common Gateway Interface, or CGI, is a standard for external gateway programs to interface with information servers such as HTTP servers.
A plain HTML document that the Web daemon retrieves is static, which means it exists in a constant state: a text file that doesn’t change. A CGI program, on the other hand, is executed in real-time, so that it can output dynamic information.

CGI (Common Gateway Interface) programs are programs that exist, and are run on, a web server. They are normally run by a client computer by clicking a button in their browser. CGI programs usually perform some task like a search, or storing information on the server, and also normally generate a dynamic HTML page in response to the user’s request.

CGI programs are often written in PERL, but this document will address CGI programming using C.
CGI applications can be written in any language that can be executed on a Web platform such as: C, Perl, Tcl, Python, Shell Scripts(UNIX), Visual Basic and Applescript. Your choice depends on
what you have to do because different languages may be specialized for different purposes. Perl, for
instance, is great for string and file manipulation, while C is better for bigger, more complex programs. Perl and C are probably the most used languages for CGI programming.

We all know how CGI interacts with Perl. In Perl, we used $queryString = $ENV{‘QUERY_STRING’} to access the environmental variable. In C, you could call the library function “getenv()”, which is defined in standard library . Here is a simple example of how to call this function:

Sample source C code (search.c):

#include <stdio.h>
#include <stdlib.h>

int main(void)
char* query;

printf("<title>Search Result</title>n");
printf("<H3>Search Result</H3>n");

query = getenv("QUERY_STRING");

// Note 1: other functions to truncate the query string

// Note 2: other functions to search for the related documents

// Now, if you find the word and want to display on user's browser, do

printf("n<p> word %s is foundn", query);

Note 1: remember the string returned by getenv() is in a format: “key=science&send=search” (assume the user types in “science”. So you need to do some truncation work to obtain the actual query string.

Note 2: your code to search for the related docs for a specific term.
Then compile the C code as you normally do:

gcc -o search.cgi search.c
Be careful to name your C executable as *.cgi or it will not work on web.

In your form.html:

<FORM ACTION="localhost/cgi-bin/search.cgi"
<p> Enter your key word separated by blank:
<INPUT TYPE="text" NAME ="key" SIZE =60 MAXLENGTH=200>
<INPUT TYPE ="Submit" NAME ="send" VALUE="Search">
<INPUT TYPE="Reset" NAME ="clear" VALUE="Reset">

CGI output is quite simple. It consists of a Content type line with a MIME type/subtype that alerts the HTTPD of what type of data is coming for it to parse and 
return the proper data to the client so it can use it. However, on the CGI end ofthings, the type/subtype you output has to be adhered to quite strictly. This means that if you tell it youre going to send it text/plain youre sending plain text... much like text/html expects plaintext or HTML.

MIME types/subtypes are as follows:

There are many headers that are valid to CGI and they are as follows:

Content-type - A header used to tell the HTTPD what type of data to expect so it can parse it and output things properly.
Example: "Content-type: text/plain"

Location - A header used to refer the HTTPD to another location for the proper document, often used in things like Microsoft's pull down page referral lists etc.
Example: "Location: http://xyz.abc.com/sd/ert.html"

Content-length - A header used to tell the HTTPD the size of the data being sent to it.
Example: "Content-length: 1024"

Expires - used to tell the HTTPD only to show the data if it's earlier than a certain day of the week/month/day/year at a certain Hour/Minute/Second based at GMT in a 24 hour format.
Example: "Expires: Tuesday, 01-april-15 24:00:00 GMT"

Content-encoding - Used to specify the encoding of a document, valid values for this are x-gzip (.gz), x-compress (.z) and x-zip (.zip).
Example: "Content-encoding: x-gzip"


The difference between the GET and POST is how the information from the form is sent to the CGI program, from the server.

A GET will provide the the user’s input to the CGI program as an environment variable called QUERY_STRING. The CGI program would read this environment variable (using the C getenv() function) and parse it to get the user’s input. A GET method will also show the input data to the user in the URL area of the browser, showing a string like www.somewhere.com/CGIFavColor.CGI?FavColor=Black. The GET method is acceptable for small amounts of data. Also, GET is the default method, when a CGI program is run via a link

A POST will provide the user’s input to the CGI program, as if it were type at the keyboard, using the standard input device, or stdin. If POST is used, then an environment variable called CONTENT_LENGTH indicates how much data is being sent. You can read this data into a buffer, by doing something like:

This entry was posted in webdevelopment. Bookmark the permalink.