Traditional Interactive Web Pages:
Web Script and CGI




Web server can use programs to generate custoemrized responses. These programs are called gateways or scripts.

Script can connect the Web to other services (such as database)

Script usually collect information from the user through interactive forms

Script can construct custom documents dynamically at the time they are requested. (Scripts create "intelligent" documents that reflect the user's cirumstances)

A Web script that provides access to another service, such as an existing database, is commonly called gateway because it acts as a door between the Web and another online service.

A Web gateway script talks to a database service by translating from the Web language, HTTP, to the language the database service speaks.




The basic goal is for the browser program and the script program to communicate with each other

Standing from the scripts, the input comes from the web client and output goes back to the client. The httpd daemon is not in the picture

However, the httpd daemon must do the following:

  1. It must determine that the request is really supposed to be a program rather than a static page
  2. The httpd must locate the program (from the file system) and see if it is permitted to execute it
  3. The httpd must start the environment needed to run the script and then pass on input from the client
  4. The httpd must read the output from the script and pass it back to the client
  5. httpd must send an error code (message) back to the web client when there is an error
  6. Cleanup such as closing connection and de-initialize environment must also be done by httpd

Web server has "rules" to tell which file is a script (most of them are by location /cgi-bin or file extension



Script and CGI

Web server program and script are usually written by different programmers ... so making them work together might be a problem

A common standard is used - Common Gateway Interface (CGI), it defined how data is passed between the httpd server and the script

As long as script and httpd follow the CGI rules, things work

Web script are often called cgi-scripts, cgi-bin is used for CGI binaries or executable programs

CGI rules are operating system dependent (excluding Java) (Unix use process/environment var, NT use application process/temp file, Mac use Apple Events ...)

CGI is just one standard, Netscape used to use Netscape Server Application Program Interface (NSAPI), MS use OLE ?

All script interfaces define how the script will be started and how data will be passed to and from the script



An Script Example

Step 1: The httpd daemon waits for a request to arrive from a client on the Internet

Step 2: A request arrives from a client

s1.jpg (14182 bytes)

Step 3: The httpd daemon parses the request

s2.jpg (25570 bytes)

Step 4: Httpd read in other information such as browser type, accept what, etc

Step 5: Init and perform the method

s3.jpg (30711 bytes)

Step 5A: The Script is successfully executed

s4.jpg (23950 bytes)

And then,

s5.jpg (34895 bytes)Step 5B: If failed, the following error will be sent back:

HTTP/1.0 200 Document follows
Server: NCSA/2.0
Date: Thu, Jun 24 1999 12:00:00 GMT
Content-type: text/plain
Cannot find requested script program on this system

(Note that 200 is used, since we are passing some document back (a document with an error message))


Step 6: Free resource, close file, close network connection

Step 7: Loop back to Step 1



The Costs of Using Scripts

Running script means at least twice as many programs must run on the server

(Perl: perl, the script, and httpd
Java: JVM, program, httpd, etc)

The httpd server not only relays the information from the script to the client, it checks to make sure that the proper headers are sent

Some httpd scan the script result to see if the headers are presents or not slows the relay of the data

For this reason, we have "No Parse Header" or NPH script and is more efficient (trade off the checking)

When in error situation, web script must be robust and should reliably return information that will make sense to httpd and browser




Two way hand share

Request Form

User from the client side fillin the form

Httpd process the form
(GET /script/myinfoprog? HTTP/1.0)