CGI


Common Gateway Interface (CGI)


An Example CGI Script

 #!/usr/bin/perl -w
 use strict;

 my $time = localtime;
 print "Content-Type: text/html\n\n";
 print "The current time on this server is: $time";

 The current time on this server is: Sat Jan 6 21:58:25 2001

Comments on the Time Server

 #!/usr/bin/perl -w

 use strict;

 print "Content-Type: text/html\n\n";


Invoking CGI Scripts

 http://www.host.com/cgi-bin/localtime.cgi

 > chmod 755 myscript.cgi


Special Instructions for the Lab

 ~<user>/course_html

 sigma10:8000/~<user>/localtime.cgi

 > cd
 > chmod 711 .
 > chmod 711 course_html

Some Alternatives to CGI Scripting


List of Alternatives


Sending Form Data


Request Parameters

 <input type="checkbox" name="send_email" value="yes">

Encoding of Special Chrarcters

 thanks+for+your+help%21

A Real Life Example


Environment Variables


Common Environment Variables

REQUEST_METHOD
Indicates the method by which the request was made
QUERY_STRING
Query information (a list of name-value pairs)
CONTENT_LENGTH
Length of the data passed to the CGI script through STDIN

More Environment Variables

SCRIPT_NAME
Path of the script being executed
PATH_INFO
Extra path information passed to a CGI script
REMOTE_HOST
Remote hostname of the client making the request (could be the address of a proxy between client and server)
REMOTE_ADDR
Remote IP address of the client making the request

Still More Environment Variables

HTTP_USER_AGENT
Name and version of client's browser
HTTP_ACCEPT
List of the content types the client can accept
HTTP_COOKIE
Name-value pair stored earlier at the client by the server

Output of a CGI Script

Content-Type
Specifying the type of content
Location
Specifying an URL to redirect the client to
Status
With a status code the server should include in the status line

Returning Content

 print "Content-Type: text/html\n\n"


Forwarding to Another URL

 print "Location: such_and_such_document.html\n\n";



Status Codes

 print "Status: 400 Bad Request\n\n";
200 OK
The request was processed successfully (added by web server)
302 Found
The URL has changed and browsers should direct all future requests to the URL provided in the Location header
400 Bad Request
Browser send an invalid request (this one will be detected by the web server)
401 Unauthorized
Requested resource is in a protected realm (invalid password)
403 Forbidden
Client is not allowed to access the requested resource (eg no permissions)
500 Internal Server Error
Something happened on the server that caused a failure (eg a syntax error)