Common Gateway Interface (CGI) is middle man type interface thorough which web servers pass user’s requests to external databases/ information resources/ program applications and receive the processed data to sent it back to user. This post will discuss what is Common Gateway Interface (CGI), how it works, when & how it is used, its features, applications, advantages and disadvantages.
What is Common Gateway Interface (CGI)
It is a set of standards or rules defined by the World Wide Web Consortium (W3C) which any external gateway program uses to interface with information servers like HTTP servers. Any program which conforms to the its specification for accepting and returning data is called a CGI program or script.
Fig. 1 – Common Gateway Interface Process Diagram
In other words, it may also be defined as “A Common Gateway Interface is the set of standards or protocols used for transferring user’s request back and forth between the web server and external information sources”.
How does Common Gateway Interface (CGI) Script Work
For the Common Gateway Interface script to work, the web server needs to support it and should have been configured to process it. These scripts are mostly written in PERL/ C or it could be a shell script also.
To understand its concept let us take a look at what happens after there is a typical action of clicking a hyperlink on a web page:
- The browser at the client passes this information to the web server with a request for a filename, via the URL.
- The web server would parse the URL and search for the filename. If found, the file would be returned to the browser or an error message is sent back.
- The web browser would then either display the file or the error message to the user.
Fig. 2 – Form Interaction with Common Gateway Interface
Now in this scenario, if the web server is configured in a way that if the requested file is in a certain directory then rather than sending the file, a program is run and the output of that program is sent back to the user. This is where Common Gateway Interface comes into play.
All the Common Gateway Interface scripts or programs, which the web server executes, are kept in a pre-configured directory. This is the CGI directory and conventionally called /CGI-bin.
Features of Common Gateway Interface (CGI)
The features of CGI include:
- These applications run on the server.
- They are reusable pieces of code.
- It is a very well defined standard which is supported by most modern browsers.
- Since the interface is pretty consistent, these application can be written by a programmer in many different languages like C, C++, Java, and PERL.
- The person writing the Common Gateway Interface program can write it independently of the operating system which the server uses.
- It is a very simple and basic way of passing information about the user’s request from the web server to the application program, and getting the response back.
- By default, the CGI scripts run in the security context of the web server.
When Common Gateway Interface (CGI) is Used
These scripts are used for the below reasons:
- When there is a need for the webserver to dynamically interact with user, CGI scripts are one of the most common ways of doing it.
- These scripts are mostly used when there is data to be submitted by the user to the webserver by filling out a form. The CGI program would then receive data, process it and return the result back to the webserver and in turn to the user.
Fig. 3 – Common Gateway Interface Application
How Common Gateway Interface Scripts are Used
The web server using CGI application for its working allows the owner of the website to determine which URL should run which script. The steps involved in it can be broadly and simply stated as below:
- A directory is created within the webserver which contains the scripts. This directory is normally called CGI-bin. Let us say this directory is /user/local/CGI-bin.
- The user sends a request to the server in the form of URL http://mywebsite.com/cgi-bin/mycgiscript.pl.
- The server recognizes that the file being requested is a CGI script and instead of sending back the file, it runs the script and passes back the output of the script to the web client.
Let us take a closer look at a simple CGI program which prints out a line to the user and we will go through some of the headers and their descriptions. Below is a sample script written in PERL:
The output of this would appear on the web client as below:
Here the first line is an HTTP header which makes the browser understand that the content is a text string,
Content-type – This is the HTTP Field Name.
text – This is the HTTP Field Content.
There are a few other important HTTP headers used in CGI, as listed below:
While writing CGI programs, environment variables play an important role. All these programs have access to the following environment variables:
Applications of Common Gateway Interface (CGI)
CGI is used for simple applications that are interactive. Few examples where CGI can be used are:
- Guest books – allowing users to leave messages for everyone to see.
- Email forms, Feedback forms, Registration forms.
- Mailing list maintenance.
- Coloring book – allows user to color images.
Advantages of Common Gateway Interface (CGI)
Following are some of the advantages of using CGI scripts:
- They are language independent so it makes it easier for any programmer to choose their preferred language.
- They are very simple and can do advanced tasks much easier than other server-side programming languages.
- There is a huge existing CGI code library that can be reused.
- They are secure as they run on the web server.
- They are very lightweight as no specific library is needed to be present in order for the code to execute.
Disadvantages of Common Gateway Interface (CGI)
CGI programs come with their own set of disadvantages such as:
- Since the CGI program forks a new process for each HTTP request, it takes up a lot of server memory.
- It also needs to open a new database connection each time a request is made, which is again a slow and costly process.
- Data cannot be cached in memory between page loads, hence it is less efficient.