4932669-cgi
TRANSCRIPT
-
8/8/2019 4932669-Cgi
1/14
1.INTRODUCTION:
The Common Gateway Interface (CGI) is a standard for
interfacing external applications with information servers, such as HTTP orWeb servers. A plain HTML document that the web daemon retrieves is
static, which means it exits in a constant state: a text file that doesnt change.
A CGI program, on the other hand, is executed in real-time, so that it can
output dynamic information.
For example, you have used a fill-out or an image map on Web,
you have probably used a gateway script without even knowing it. With CGI
Scripts, everything seems to happen automatically. You enter data and click
a mouse button, and a moment later, a result is displayed. What happens
between the click of the mouse and the display of the result is what CGI
does.
Steps
Client sends request to the server
Server does not have the capacity to process the client request
So, it inturn contacts some server side program like CGI
Now CGI process the client request and sends response back to client
Then Server sends the response back to client in the form of html page
CGI 1
CLIENT CGI
S
E
RV
E
R
2
43
-
8/8/2019 4932669-Cgi
2/14
2.WHAT ARE CGI SCRIPTS?
CGI scripts are external programs that run on the Web server. CGI
scripts are used to create highly interactive Web publications. The standard
that defines how external programs are used on Web servers and how they
interact with other applications is the Common Gateway Interface. The three
keywords that comprise the name of the standard---common, gateway and
interface---describe how the standard works.
Common: By specifying a common way for scripts to be
accessed, CGI enables anyone, regardless of platform, to pass information to
a CGI script.
Gateway: By defining the link (gateway) between the script,
the server, and other applications, CGI makes it possible for external
program to accept generalized input and pass information to other
applications.
Interface: By describing the external programs can be accessed
by users (the interface), CGI reduces the complex process of interfacing with
external programs to a few basic procedures.
3.CHOOSING A PROGRAMMING LANGUAGE FOR
SCRIPTS:
CGI scripts are also called gateway scripts. One can writegateway scripts in almost any computer languages that produce an
executable file. The most common language for scripts is the following:
CGI 2
-
8/8/2019 4932669-Cgi
3/14
Bourne Shell
C Shell
C/C++ Shell
PERL
Python
Tcl
Visual Basic
Scripts located on server called server-side scripts (CGI). In the
contrast, some scripts, called client-side scripts, are embedded in HTML
documents and executed by browser. JavaScript and VB Script are two up
and coming client-side scripting languages.
The most programming language to write script is one
that works with Web server and meet needs. Preferably,
the language should already be available on the Web
server and user should be proficient in it (or at least have
some knowledge). The most user input in the form of text
that most manipulated in some way, which makes
supports for text strings and their manipulations critical.
PERL, C/C++, and UNIX shell are the most popularlanguages for scripts. PERL is rapidly becoming the
widely used language for CGI scripts.
4.WHY CGI SCRIPTS?
The primary reason to use CGI scripts is to automate what
would otherwise be a manual and probably time consuming process. UsingCGI scripts benefits both developer and user. The user gets simplicity,
automated response to input, easy ways to make submissions, and fast ways
to conduct searches. Gateway scripts enable to automatically process orders,
queries, and much more. CGI programs are commonly used to do the
following tasks:
CGI 3
-
8/8/2019 4932669-Cgi
4/14
*Process input, typically search strings, and output a document
containing the results of the search.
*Validate user identification and password information and
grant readers access to restricted areas of the Web site.
*Process input from image maps and direct the reader to
associated documents.
*Add the readers feedback or survey responses to a database or
index.
*Track visitors to Web pages and post continually updated
numbers to the Web pages as it is accessed.
*Generate documents based on the type of browser the reader is
using.
*Perform post-submission processing and output results for the
reader.
5.HOW CGI SCRIPTS WORK?
Gateway scripts are used to process input submitted by readers
of Web publications. The input usually consists of environment variables
that the Web server passes to the gateway scripts. Environment variables
describe the information being passed, such as the version of CGI used on
the server, the type data, the size of the data, and other importantinformation. Gateway scripts can also receive command-line arguments and
standard input. To execute a CGI script, the script must exist on the server
that referencing. One must also have a server that is both capable of
executing gateway scripts and configured to handle the type of script one to
use.
Readers pass information to a CGI script by activating a link
containing a reference to the script. The gateway script processes the input
and formats the results as output that the Web server can use. The Web
server takes the results and passes them back to the readers browser. The
browser displays the output for the reader.
The output from a gateway script begins with header containing
a directive to the server. Currently, the three valid server directives are
Content-type, Location, and Status. The header can consist of a directive in
CGI 4
-
8/8/2019 4932669-Cgi
5/14
the format of an HTTP header followed by a blank line. The blank line
separates the header from the data passing back to the browser. Output
containing Location and Status directives usually consists of a single line
because the directive contained on the Location or Status line is all that
server, and when there is no subsequent date, no need to insert a blank line.
The server interprets the output, sets environment variables, and passes the
output to the client.
Any transaction between a client and server has many parts.
These parts can be broken down into following steps:
1.Browser shows the HTML document containing the form.
2.User enters data into the form as need then click the Submit button.
3.Optionally, a script in the browser performs client-side validation of
the forms contents.
4.The browser decodes the URL and contacts the server.
5.Browser requests the document file from the server.
6.The server translates the URL into a path and filename.
7.The server realizes that the URL points to a program instead of a
Static file.
8.The server prepares the environment and launches script.
9.The script executes and reads the environment variables and
STDIN.
10.The script sends the proper MIME headers to STDOUT for the
Forthcoming content.
11.The script sends the rest of its output to STDOUT and terminates.
12.The server notices that the script has finished and closes the
Connection to browser.
13.Browser displays the output from the script.
CGI 5
-
8/8/2019 4932669-Cgi
6/14
6.INPUT TO CGI SCRIPTS:
When a user activates a link to gateway script, input is sent to
the server. The server formats this data into environment variables and
checks to see whether additional data was submitted via standard input
stream.
Environment variables:
Input to CGI scripts usually is in the form of environment
variables. The environment variables passed to gateway scripts are
associated with the browser requesting information from the server, theserver processing the request, and the data passed in the request.
Environment variables are case-sensitive. Although some environment
variables are system-specific, many environment variables are standard.
Environment variables are set automatically whenever reader input is passed
to a server.
Standardenvironmentvariables:
Variable Purpose
AUTH_TYPE Specifies the authentication method and is
Used to validate a users access.
CONTENT_LENGTH Used to provide a way of tracking the length
Of the data string as a numeric value.
CONTENT_TYPE Indicates the MIME type of data.
GATEWAY_INTERFACE Indicates which version of the CGI standard
The server is using.
CGI 6
-
8/8/2019 4932669-Cgi
7/14
HTTP_ACCEPT Indicates the MIME type content types the
browser will accept, as passed to the
gateway script via the server.
HTTP_USER_AGENT Indicates the type of browser used to send
the request, as passed to the gateway script
Via the server.
PATH_INFO Identifies the extra information included in
the URL after the identification of the
CGI script.
PATH_TRANSLATED Set by server based on the PATH_INFOvariable. The server translates the
PATH_INFO variable into this variable.
QUERY_STRING Set to the query string (if the URL contains a
query string).
REMOTE_ADDR Identifies the Internet Protocol address of
the remote computer making the request.
REMOTE_HOST Identifies the name of the machine making
the request.
REMOTE_IDENT Identifies the machine-making request.
REMOTE_USER Identifies the user name as authenticated by
the user.
REQUEST_METHOD Identifies the method by which request was
made.
SCRIPT_NAME Identifies the virtual path to the script being
executed.
CGI 7
-
8/8/2019 4932669-Cgi
8/14
SERVER_PORT Identifies the port number the server
received the request on.
SERVER_PROTOCOL Identifies the protocol of the request sent to
the server.
SERVER_SOFTWARE Identifies the Web server software.
CGI Standard Input:
Most users input sent to a Web server is used to sent environment
Variables, yet not all input fits neatly into an environment variable. When a
user submits data to be processed by a gateway script, this data is received
as an URL-encoded search string or through the standard input stream.
Sending data as standard input is the most direct way to send data.
The server opens the standard input stream and reads the specified amount
of data.
CGIINPUT:
Under HTTP 1.0, a two-submission method for forms exists. The
HTTP GET method uses URL-encoded search strings. When a server
receives an URL-encoded search string, the server assign the value of the
Search string to the QUERY_STRING environment variable.
The HTTP POST method uses the standard input streams. When
Receives data by the standard input stream, the server assigns the value with
the length of the input stream to the CONTENT_LENGTH variable.
CGI 8
-
8/8/2019 4932669-Cgi
9/14
7.CGI SCRIPT STRUCTURE:
When the server invokes script, the server passes information to
the script via environment variables and, in the case of POST via STDIN.
The basic structure of CGI application is straightforward
initialization, processing, output and termination ideally a script follows
these steps in this order.
1.The program begins.
2.The program calls do-initialize.
3.The program calls do-process.
4.The program calls do-output.
5.The program ends.
INITIALIZATION:
The first thing script must do when it starts is its input,
environment and state. Basic operating system environment information canbe obtained the usual way (e.g. from the system registry in the windows)
State information will come from the input rather than the
operating environment or static variables. Each time CGI scripts are
invoked, its as if theyve never been invoked before. Every thing must be
initialized from scratch.
PROCESSING:
The processing, however, is the heart of your script. Processing
data means, generally, transforming it some how, in classical data processing
terminology, this is called the transform step because in batch-oriented
processing, the program reads a record, applies some rule to it (transforming
it), and then writes it back out.
CGI 9
-
8/8/2019 4932669-Cgi
10/14
OUTPUT:
In simple the output is usually a header and some HTML. More
complex scripts might output graphics, graphic mixed with text, or all the
information necessary to call the script again with some additional
information.
TERMINATION:
Termination is nothing more than cleaning up after your self
and quitting. If youve locked any files, you must release them letting the
program end. If youve allocated memory, semaphores, or others, you must
free them.
8.OUTPUT FROM CGI SCRIPT
After the script finishes processing the input, the script should
return output to the server. The server will then return the output to the
client. Generally, a blank line and a body in the form of HTTP response that
includes a header follow this output. Although the CGI header output is
strictly formatted, the body of the output is formatted in the manner youspecify in the header. For example the body can contain an HTML document
for the client display.
CGI HEADERS:
CGI headers contain directives. Your CGI script outputs these
directives are valid
1.content-type
2.location
3.status
CGI 10
-
8/8/2019 4932669-Cgi
11/14
A single header can contain one or all of the server directives,
your CGI script outputs these directives to the server. Although the header is
followed by blank line that separates the header from the body, the output
does not have to contain a body.
CONTENT TYPES USED IN CGI HEADERS:
The content-type field in a CGI header identifies the MIME
type of the data you are sending back to the client. Usually the data output
from a script is a fully formatted document, such as an HTML document.
Content-type: text/html.
LOCATIONS USED IN CGI HEADERS:
The output of your script doesnt have to be a document createdwithin the script. You can reference any document on the web using the
location field by its URL. Server process location references wither directly
or indirectly depending on the location of the file. If the server can find the
file locally, it passes the file to specify a location in a script as follows:
Location: http://www.typress.com/
STATUS USED IN THE CGI HEADERS:
The status field passes a status line to the server for forwarding
to the client. The status code shows the general status as follows:
1. Not yet allocated
2. Success
3. Redirection
4. Client error
5. Server error
Although servers use many status codes, the status codes you pass to a
client via your CGI script are usually client error codes.
Basic hello world CGI script in C, with basic HTML output added
CGI 11
http://www.typress.com/http://www.typress.com/ -
8/8/2019 4932669-Cgi
12/14
int main (int arc, char *arc[])
{
printf (Content-type: text/html\n\n);
printf ();
printf ();
printf (Hello, world!\n);
printf ();
printf (\n);
printf(
-
8/8/2019 4932669-Cgi
13/14
maintenance, and the capability to perform the chosen task should also be
considered.
If one of your mandates is the capability to move code among
platforms with a minimum of functions from the ANSI C libraries and avoid
making other operating system calls. Unfortunately, following this will limit
scripts basic functionality if you wrap your platform-dependent code in self-
contained routines; however, you minimize the work needed to port from
one platform to the next. A properly designed program can have any module
replaced in its entirely without affecting the rest of the program. Using these
guidelines, you may have to replace a subroutine or two, and youll certainly
have to recompile step is used. Learning to write Perl is easy than C
Server Independence:
Server independence is fairly easy to achieve, but for some
reason to be stumbling block to beginning scriptwriters. To be server
independence, script must run without modification on any server using the
same operating system. Only server-independence programs can be useful as
shareware or freeware a doubt, server-independence is a requirement for
commercial software.
The following are some rules of server-independence:
1.Dont assume your environment.
2.Dont assume privileges.
3.Dont assume consistency of CGI variables.
4.Dont assume version-specific information.
5.Dont assume LAN or WAN configuration.
6.Dont assume the availability of system objects.
CGI 13
-
8/8/2019 4932669-Cgi
14/14
10.CONCLUSION
The Common Gateway Interface opens the door for adding
advanced features to your Web publications. This workhorse running
quietly in the background less you use fill-out forms, database queries,and index searches and create documents on the fly. You use CGI
whenever you want to open a two-way communication channel with
the reader. CGI enhancement to Web means extra for Web publishes.
Still, the payoff associated with CGI makes the extra effort truly
worthwhile.
CGI 14