What Is CGI (Common Gateway Interface)?


Web Servers are an important part of the internet and the web. Web servers used to serve web sites, web pages, APIs, documents, video images, etc. Web servers are generally designed to be dynamic to respond to user requests in a proper fashion. CGI (Common Gateway Interface) is a technology designed to provide dynamic programming and response capabilities to the web servers.

What Is CGI (Common Gateway Interface)?

CGI is the short form of the Common Gateway Interface. CGI provides the programmatic interface to the webserver where the webserver will consume the CGI scripts and applications in order to respond to the user request. It is called common because it provides a generic interface where it is used as a gateway to the different scripts and applications. CGI scripts generally produce web pages that contain HTML, JavaScript, CSS, Image, API, ASCII text, etc.

CGI is first created as a best practice in NCSA (National Center for Supercomputing Applications) in order to call command-line executables from the web. With wide usage and popularity, the standard RFC 3875 is created in November 1997.

CGI Scripts

CGI scripts mainly created using C programming language because of its standard uses C programming for definitions. getenv() function of the C library is used to get environment variables from the requests. CGI scripts are stored in /usr/local/apache/htdocs/cgi-bin path in general but the path can be changed according to will. CGI scripts can be used in different programming and scripting languages like Bash, Perl, Python, etc. but for easiness and practical development and change scripting languages Bash and Perl are preferred.

LEARN MORE  Curl Post Data From Terminal with Examples

Example CGI Script

Below we can see an example CGI script which is developed in Perl scripting language.

#!/usr/bin/perl

=head1 DESCRIPTION

printenv — This CGI script will print environment variables

=cut
print "Content-Type: text/plain\n\n";

for my $var ( sort keys %ENV ) {
    printf "%s = \"%s\"\n", $var, $ENV{$var};
}
  • `#!/usr/bin/perl` line is used to set the given file interpreter or programming language. This line will set the interpreter as Perl which is located at `/usr/bin/perl`.
  • `=head1 DESCRIPTION` is a comment which simply put a header to the documentation and below the lines are also comment
  • `=cut` is the end of the comment.
  • `print “Content-Type: text/plain\n \n` will output the first line of the HTTP response for the request. This line will output the content type as plain text.
  • `for my $var ( sort keys %ENV) {` block will iterate over the environment variables and put the value into `$var` and the `$var` will be printed in every step.

When we call this Perl CGI script we will get an output or HTTP response as HTML format like below.

COMSPEC="C:\Windows\system32\cmd.exe"
DOCUMENT_ROOT="C:/Program Files (x86)/Apache Software Foundation/Apache2.2/htdocs"
GATEWAY_INTERFACE="CGI/1.1"
HOME="/home/SYSTEM"
HTTP_ACCEPT="text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
HTTP_ACCEPT_CHARSET="ISO-8859-1,utf-8;q=0.7,*;q=0.7"
HTTP_ACCEPT_ENCODING="gzip, deflate, br"
HTTP_ACCEPT_LANGUAGE="en-us,en;q=0.5"
HTTP_CONNECTION="keep-alive"
HTTP_HOST="example.com"
HTTP_USER_AGENT="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:67.0) Gecko/20100101 Firefox/66.0"
PATH="/home/SYSTEM/bin:/bin:/cygdrive/c/progra~2/php:/cygdrive/c/windows/system32:..."
PATHEXT=".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC"
PATH_INFO="/foo/bar"
PATH_TRANSLATED="C:\Program Files (x86)\Apache Software Foundation\Apache2.4\htdocs\foo\bar"
QUERY_STRING="var1=value1&var2=with%20percent%20encoding"
REMOTE_ADDR="127.0.0.1"
REMOTE_PORT="63555"
REQUEST_METHOD="GET"
REQUEST_URI="/cgi-bin/printenv.pl/foo/bar?var1=value1&var2=with%20percent%20encoding"
SCRIPT_FILENAME="C:/Program Files (x86)/Apache Software Foundation/Apache2.4/cgi-bin/printenv.pl"
SCRIPT_NAME="/cgi-bin/printenv.pl"
SERVER_ADDR="127.0.0.1"
SERVER_ADMIN="(server admin's email address)"
SERVER_NAME="127.0.0.1"
SERVER_PORT="80"
SERVER_PROTOCOL="HTTP/1.1"
SERVER_SIGNATURE=""
SERVER_SOFTWARE="Apache/2.2.39 (Win32) PHP/7.1.7"
SYSTEMROOT="C:\Windows"
TERM="cygwin"
WINDIR="C:\Windows"

Leave a Comment