- Internet & the Web
How CGI-BIN works | Working of CGI-BIN |Steps in CGI | Process of CGI Scripts
Working of CGI
CGI | CGI-BIN
CGI stands for Common Gateway Interface. CGI is a set of standards by which Web Servers can communicate with external program to handle the data from the form. This is why web servers are not programmed to process the forms. CGI doesn’t communicate directly with the browser. CGI-BIN acts as an interface for the CGI. CGI can be created using the programming languages C,Perl,Java or Visual Basic. CGI normally resides in a directory and this directory is known as CGI-BIN.
Working of CGI present inside CGI-BIN
· A CGI is a program which is generally called scripts because they were written in UNIX and shellscript and Pearl.
· Pearl is an interpreted language in which instruction are interpreted and immediately compiled to machine instructions.
· C language is also used to create CGI-Bin but it is not called as a script.
· After looking at CGI program server access information from browser. Before launching the scripts it prepares a no of variables called environment variables which will represent te current state of a server. If one sever is busy with various scripts, CGI executes its scripts at once each with its own environment.
How CGI works | Steps in CGI process with CGI-BIN
1. Browser will first show the document containing the form.
2. User will enter data into the form and click submit button.
3. Script in browser will validated the data entered in the form (client-side validation).
4. Browser will decode the URL and contacts the server.
5. Browser will request the document file from the server.
6. Server translates the URL into a path and a file name.
7. Server realizes that the URL points to a program.
8. Servers prepare its environments variables and launch the scripts.
9. CGI Script executes and read the environment variables and STDIN.
10. Script sends appropriate MIME header to STDOUT for content.
11. Script sends rest of its output to STDOUT and terminates.
12. Server will notify the termination of the script and closes the connection to browser.
13. Browser displays the output from the CGI script.
STDIN: It is a mnemonic for standard input. The input is redirected to a program from browser via server when POST method is used.
STDOUT: It is a mnemonic for standard output. It writes the output back to the browser.
· Requests for data and is used to obtain static documents.
· The requested information will appended onto the end of the URL as a query string.
Eg : cgi_bin/myprog.cgi?name=tom&id=32109
Query strings here used are: name=tom and id-32109
· If slash is used instead of question mark the server passes info to script using PATH_INFO variable instead of QUERY_STRING.
· When browser send data from a form using STDIN this method is used.
· The server passes one variable called CONTENT_LENGTH to indicate the size of the incoming data in bytes.
These 2 methods GET and POST are passed as parameter to the server by a variable REQUEST_METHOD.
Steps in CGI process
There are 3 steps in CGI processing
· CGI Script determines the input, environment and the state.
· Each time it acts like as it was invoked for the first time.
· The method used to invoke the script can be determined using REQUEST_METHOD variable.
· The input can be retrieved using the following variables
· If request method is GET, QUERY_STRING variable can be used to determine the input.
· If request method is POST, STDIN and CONTENT_LENGTH variable can be used to determine te input.
Processing phase is divided into 2 phases
1. Processing input data
Transforms the data which differentiates it from all the CGI programs.
2. Out put the results
Output is usually a header and some HTML in case of simple CGI scripts.
· It refers to cleaning up resources after processing and quitting.
· All files, memory allocated must be freed or released for other useful purpose. Failure to do this will report an error.
· An error exit routine can be used to check whether the scripts resources are cleaned up as thoroughly as possible.
Buy Complete CGI programming books
CGI Environment Variables
Environment Variables represent the current state of the server. Each server has its own environment variables and most commonly used are:
AUTH_TYPE provides type of authentication supported by the server.
CONTENT_LENGTH gives the length of data supplied by STDIN and POST method.
CONTENT_TYPE is type of data just as MIME header.
HTTP_ACCEPT is a common delimited list of MIME types accepted by browser.
PATH_INFO is extra path information supplied at the end of URL.
GATEWAY_INTERFACE is the version number of CGI ex:CGI/1.1
QUERY_STRING is extra information at the end of the URL after ‘?’
REQUEST_METHOD is method by which CGI script is invoked.
Eg: POST and GET
SCRIPT_NAME is the name of the script being invoked.
REMOTE_HOST is the host name of client making the request.
REMOTE_USER is the user name provided by the user if any AUTH_TYPE is supported.
SERVER_PORT is the port number for the connection.
SERVER_PROTOCOL is a name/ version of protocol used by request.
Normally when you are logging to your email account watch the URL tab of the browser we can see the request is going to the cgi_bin. CGI program or CGI script resides in this cgi_bin directory. CGI-BIN acts as the interface for CGI programs. Using of CGI reduced the complexity for building servers as well as browsers. The server should not do the input processing it will call the CGI program present inside the CGI-BIN.