Title: Introduction to the Common Gateway Interface
1Introduction to the Common Gateway Interface
- CSC 492 Topics in Perl
- Joe Reynoldson
- 2/7/04
2Static Web Resources
- Definition resources which are written by web
developers and served via a web server unchanged - Static web resources can include HyperText Markup
Language (HTML) files, images, or multimedia
files such as MPeg4 video or Flash animations - Static resources are stored on a server file
system, and must be accessible by the web server
3Static Web Resources (continued)
- Advantages
- Serving files is a simple process which requires
little work for the server - Resources which rarely change require less work
to maintain - Authors do not have to be programmers
- Disadvantages
- Information which changes regularly falls out of
date quickly - Does not allow for interactivity with the client
- Cant be customized for individual clients
4Dynamic Web Resources
- Definition Resources which are generated
dynamically as a result of a web server
processing a request from a web browser - Servers can dynamically generate pages based on a
variety of conditions - Contents of an input form
- Location of the client
- Identity of the client
- Date or time
- Dynamic data source (database)
5Dynamic Web Resources (continued)
- Advantages
- A single program can serve a variety of clients
- Resources which constantly change are always up
to date - Increases the interactivity of a web site
- Disadvantages
- This process requires more work for the server
which can degrade response time - Increases complexity of an application
- Often requires programmers in addition to web
developers
6The Common Gateway Interface (CGI)
- Definition An application programming interface
(API) which defines the minimum requirements for
an external program to generate dynamic web
resources - CGI programs can be written in many languages,
but Perl is well suited and most often used - CGI programs can do many things
- Search online databases
- Online purchasing
- Sending email
- Posting comments
- Generate graphics
7CGI (continued)
- Advantages
- Can be written in almost any language
- Widely available and well documented
- Simple API
- Disadvantages
- Slower than embedded server side alternatives
- Requires web developer with programming knowledge
8Embedded Server Side Programming Techniques
- Definition Modules which embed a programming
interface within the web server - Examples
- Mod_perl
- An Apache (web server) module which embeds a perl
interpreter - Perl CGI applications can be executed internally
- Java Server Pages (JSP)
- Also embeds simplified Java code within HTML
files - Compiled and served by a JSP server (such as
Tomcat from http//jakarta.apache.org) - HyperText PreProcessor (PHP)
- Can embed commands to be processed within HTML
- A free module available at http//www.php.net/
and used mostly with the Apache web server
9Embedded Server Side Programming (continued)
- Advantages
- Faster
- Writing the application logic doesnt require web
design skills - Writing the interface doesnt require programming
skills - Logic and interface can be maintained separately
- Disadvantages
- Requires support for a particular module
- More complicated overall design
10CGI Request Overview
Client (Web Browser)
Web Server
CGI Program
2) Call CGI
1) HTTP Request
3) HTML Results
4) HTTP Response
11A Simple Example
- A user enters a HyperText Transfer Protocol
(HTTP) request for a file named hello.pl from a
server named www.usd.edu - The file is stored in a subfolder named cgi-bin
of the home directory for a user named jreynold - We'll cover details of HTTP later
- http//www.usd.edu/jreynold/cgi-bin/hello.pl
- The server receives the request, and translates
it to the appropriate program stored on the
server - /home/j/jreynold/www/cgi-bin/hello.pl
12A Simple Example (continued)
13A Simple Example (continued)
- The web browser receives and marks up the web
page appropriately
14A Simple Example (continued)
- The first thing to check when a CGI program
doesn't work is permissions - sunlight ls -l hello.pl
- -rwx------ 1 jreynold other 544 Feb 9
1316 hello.pl - sunlight chmod 755 hello.pl
- sunlight
- Be sure to verify the folder permissions too
(including jreynold, www, and cgi-bin in this
case) - The program must be executable by the web server
- On our server, this means the permissions must be
rwxr-xr-x for the file - All of the parent folders must be executable as
follows rwx--x--x - Now then, as I was saying...
15A Simple Example (continued)
- The web browser receives and marks up the web
page appropriately
16A Simple Example (continued)
- Uh oh, that wasn't the fix. Yes it was, stay calm
... stay calm. - Right, so the first line might be a problem since
Komodo defaults to CRLF for end of line. - What's the fix? Add the wT flags.
17A Simple Example (continued)
18A Simple Example (continued)
- The web browser receives and marks up the web
page appropriately
19A Simple Example (continued)
- The client may be unaware that a CGI program was
ever utilized
20A Short To Do List
- Learn HTTP and HTML
- Learn a little UNIX
- Learn some Perl (?)
- Learn CGI