[Top] [Prev] [Next] [Bottom]

4 Developing FastCGI
Applications in Tcl

This chapter explains how to code FastCGI applications in Tcl. Prior to creating a FastCGI application, you must have a FastCGI-savvy version of the Tcl interpreter. Open Market develops Tcl binaries for popular platforms and makes them available with our developer's kit.

The FastCGI-savvy binaries are extensions of standard Tcl, and are intended to replace your existing Tcl installation. There is no need to maintain two versions of Tcl: the version that we supply will work fine when invoked from a shell or a CGI program. There are also directions in the developer's kit for how to make your own FastCGI-savvy Tcl, if you need a version for some platform that we don't supply.

In many cases, you can convert a Tcl script from CGI to FastCGI by adding a few lines of code to an existing script. For more complex scripts, you may also need to rearrange some existing code.



Getting Started

The first line of any Tcl script typically specifies the pathname of the Tcl interpreter itself. You must specify the pathname of a FastCGI-savvy Tcl.

Then, you have to divide FastCGI scripts into the following two sections:



A response loop typically has the following format:



while {[FCGI_Accept] >= 0 } {

# body of response loop

}

The FCGI_Accept call returns 0 whenever a client requests the FastCGI script. Otherwise, the FCGI_Accept call returns -1.



Example: TinyFastCGI

Here is a simple example of a FastCGI application written in Tcl:




#!fcgi-savvy-tcl

set count 0

# Response Loop
while {[FCGI_Accept] >= 0 } {
        incr count
        puts -nonewline "Content-type: text/html\r\n\r\n"
        puts "<title>FastCGI Hello! (Tcl)</title>"
        puts "<h1>FastCGI Hello! (Tcl)</h1>"
        puts "Request number $count running on host  <i>$env(SERVER_NAME)</i>"
}



[Top] [Prev] [Next] [Bottom]