CGI : Understanding & Implementing

 

Understanding CGI

 

Dynamic Documents

A dynamic document is created by a Web server whenever a browser requests the document. When a request arrives, the Web server runs an application program or a script that creates the dynamic documents. The server returns the output of the program or script as a response to the browser that requested the document. Because a fresh document is created for each request, the contents of a dynamic document can vary from one request to another. A very simple example of dynamic document is the retrieval of the time and data from a server. The client can ask the server to run a program such as the date program in UNIX and send the result of the program to the client.

 

Common Gateway Interface (CGI)

The Common gateway Interface is a technology that creates and handles dynamic documents. CGI is a set of standards that defines how a dynamic document is written, how data are input to the program and how output result is used. CGI is a not a new language, instead, it allows programmers to use any of several languages such as C, C++, Perl etc. The only thing that CGI defines is a set of rules and terms that the programmer must follow. (It is a specification for transferring information between a World Wide web server and a CGI program.)
The term 'common' in CGI indicates that the standard defines a set of rules that is common to any language or platform. The term 'gateway' here means that a CGI program can be used to access other resources such as databases, graphical packages etc. The term 'interface' means that there is a set of predefined terms, variables, calls and so on that can be used in any CGi program. A CGI program in its simplest form is a code written in one the languages, supporting the format of CGI. CGI is used to display the output of a program on browser.

 

INPUT

In traditional programming, when a program is executed, parameters can be passed to the program. Parameter passing allows the programmer to write a generic program that can be used in different situations. In CGI, the input from the browser to a server is sent by using a Form. If the information in a form is small ( such as a word), it can be appended to the URL after a question mark. For example, the following URL is carrying information (23, a value) :
http://www.abc/cgi-bin/prog.p1?23

When the server receives the URL, it uses the part of the URL before the question mark to access the program to be run, and it interprets the part after the question mark (23) as the input sent by the client. It stores this string in a variable. When the CGI program is executed, it can access this value.
If the input from the browser is too long to fit in the query string, the browser can ask the server to send a form. The browser can then fill the form with the input data and send it to the server. The information in the form can be used as the input to the CGI program.

 

OUTPUT

The whole idea of CGI is to execute a CGI program at the server site and send the output to the client (browser). The output is usually plain text or text with HTML structures; however the output can be variety of other things. It can be graphics or binary data, a status code or instructions to the server to send an existing document instead of the actual script.
To let the client know about the type of the document sent, a CGI program creates headers. As a matter of fact, the output of the CGI program always consists of two parts: A Header and A Body. The header is separated by a blank line from the body, this means any CGI program creates first the header, then a blank line, and then the body. Although the header and the blank line are not shown on the browser screen, the header is used by the browser to interpret the body.
There are 3 forms of Header Type :
Content-Type
Location
Status
Content-type is the most popular type. We now consider this further.

Content-Types
The following are the common formats/content-types:

Format/Content-Type
HTML- text/html
Text- text/plain
Gif- image/gif
JPEG- image/jpeg
Postscript- application/
MPEG- video/mpeg

To declare the Content-Type, the CGI script must output:

Content-Type: content-type specification

Typically the Content-Type will be declared to produce HTML. So, the first line of our CGI script will look like this:

Content-Type: text/html

Depending on the Content-Type defined, the data that follows the header declaration will vary.
If it is HTML that follows, then the CGI script must output the standard HTML syntax.
Thus to produce a web page that sends a simple line of text "What's the day today" to a browser, the CGI script must output :

Content-Type:text/html
<html>
<head>
<title>
Saloni </title>
</head>
<body>
<h1>What's the day today?</h1>
</body >
< /html >

So, The CGI program in C++ to display ( first.cpp) and write the above statements is as follows :

#include <iostream>
using namespace std;
int main()
{
cout<< "Content-type:text/html \n\n ";
cout<< " <html> <head> <title>";
cout<< " Saloni </title> </head> " ;
cout<< " <body> <h1>What's the day today?</h1> </body >" ;
cout<< " < /html > " ;
return 0;
}

Note : The first "\n" after the header is used to terminate the current line and the second "\n" is used to separate the header from the body since the CGI output must always be in such format. (as explained above) Things To Do : Change the permissions of the directory cgi-bin to 755 as : /usr/lib$ chmod 755 cgi-bin/ Place this program named first.cpp in /usr/lib/cgi-bin/

Compile the first.cpp file in /usr/lib/cgi-bin as :
g++ first.cpp -o f.cgi
// where f.cgi is the name of the executable; there can be any name; usually .cgi extension is used for cgi programs.
This would create f.cgi file.

And, You are done !
Now, Go to your browser and type url http://localhost/cgi-bin/f.cgi
And, The output appears there as...

What's the day today?

There's more to come about CGI in the upcoming blogs. So, do keep an eye here 😉
CHEERS !

5 thoughts on “CGI : Understanding & Implementing

  1. Nice post. I used to be checking constantly this weblog and I am inspired!
    Extremely useful info specially the ultimate part :
    ) I take care of such info a lot. I used to be looking for this
    particular info for a very long time. Thanks and best of luck.

  2. This design is spectacular! You obviously know how to keep a reader entertained.

    Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Wonderful job.
    I really enjoyed what you had to say, and more than that, how you presented it.
    Too cool!

  3. Greetings from Carolina! I’m bored to death at work so I decided to browse your site on my iphone
    during lunch break. I enjoy the information you provide here and
    can’t wait to take a look when I get home. I’m amazed at how quick your blog loaded on my phone ..
    I’m not even using WIFI, just 3G .. Anyways, superb
    site!

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: