4. CGI Protocol
CERN HTTPd Server NCSA HTTPd Server
Both provided mechanisms to invoke external
programs and scripts to create dynamic content but incompatible
The Solution
The CGI 1.1 specification
5. CGI Operation
● Apache receives a request and determines that it
needs to be served by the CGI program
● Apache starts an instance of the CGI program
● Apache passes information about the request to the
CGI
● Apache receives the response from the CGI, optionally
processes its headers and contents, and sends it to the
client
● The CGI program finishes and all resources associated
with it are recalled by the operating system
6. CGI Environment Variables
Variable Name Variable Description
SERVER_NAME Hostname or IP address of the server
REQUEST_METHOD HTTP request method: HEAD, GET,
POST, and so on
REMOTE_ADDR Client IP address
CONTENT_TYPE MIME type of any client data being
passed by a POST or PUT request
CONTENT_LENGTH Size of the client data
7. CGI Response
● Location: Instructs Apache that the CGI is not going
to answer the request and that the client should be
redirected to the specified URL.
● Status: This is not a valid HTTP header and it is not
transmitted back to the client, but it indicates the
HTTP status code for the request to Apache.
● Content-Type: Specifies the type of data returned in
the request. For example, if you are returning a Web
page, the header value should be text/html.
8. Advantages and Disadvantages of
CGI Scripts
● Portability ● Performance
● Simplicity ● Code and
● Existing Code Presentation
● Source Hiding
● Memory Leaks
11. Configuring Apache
Fine-Grained Control
SetHandler Directive
# Any files accessed thru the /cgi-bin/ url will execute as CGI scripts.
<Location “/cgi-bin/”>
Options +ExecCGI
SetHandler cgi-script
<Location>
Associating CGI Processing with File Extensions
# Any files ending in .pl will be executed as CGI scripts
<Files *.pl>
Options +ExecCGI
SetHandler cgi-script
</Files>
# Any files ending in .cgi in the /usr/local/apache2/htdocs/scripts
# will be executed as CGI scripts
AddHandler cgi-script .cgi
<Directory “/usr/local/apache2/htdocs/scripts”>
Options +ExecCGI
</Directory>
12. Configuring Apache
Action and Script
Action image/gif /cgi-bin/process.cgi
Script PUT /cgi-bin/upload.cgi