Introduction to the Common Gateway Interface

thickbugSoftware and s/w Development

Oct 28, 2013 (3 years and 7 months ago)

63 views

Introduction to the Common
Gateway Interface

CSC 492 Topics in Perl

Joe Reynoldson

2/7/04

Static 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

Static 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


Can’t be customized
for individual clients

Dynamic 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)

Dynamic 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

The 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


CGI (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


Embedded 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

Embedded Server Side Programming
(continued)


Advantages


Faster


Writing the application
logic doesn’t require
web design skills


Writing the interface
doesn’t require
programming skills


Logic and interface
can be maintained
separately


Disadvantages


Requires support for a
particular module


More complicated
overall design

CGI Request Overview

Client

(Web Browser)

1) HTTP Request

Web Server

CGI Program

4) HTTP Response

2) Call CGI

3) HTML Results

A 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



A Simple Example (continued)

A Simple Example (continued)


The web browser receives and marks up the
web page appropriately


A 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 13:16 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...

A Simple Example (continued)


The web browser receives and marks up the
web page appropriately


A 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.

A Simple Example (continued)

A Simple Example (continued)


The web browser receives and marks up the
web page appropriately

A Simple Example (continued)


The client may be unaware that a CGI
program was ever utilized

A Short To Do List


Learn HTTP and HTML


Learn a little UNIX


Learn some Perl (

)


Learn CGI