cgi.tcl

cgi.tcl , by Don Libes, is great for rapid development of CGI-based dynamic web sites. It is in wide use, and generally regarded as quite stable.

Documentation

http://expect.sourceforge.net/cgi.tcl/ref.txt

Description

cgi.tcl primarily processes CGI input and produces HTML output.

See Also

ncgi
a branch of cgi.tcl that beaks the code into namespaces and 3 modules
Don Libes' cgi.tcl run in a Slave Interpreter with TclHttpd
WebSubmit
BOOK Web TCL Complete ,by Steve Ball
Using Tcl to write CGI applications
Mel's Internet Toolkit
a derivative of cgi.tcl

An Upload Example

WJR: Here's an example script that could be used to upload files. If anything it demonstrates how easy cgi.tcl makes such as interface. There are no security mechanisms in this script, so make sure to control for unauthorized uploads! This particular example is from a Windows/Apache setup but I'm sure only minimal changes would be required for other platforms.

#!d:/tcl/bin/tclsh.exe

package require cgi

# Upload dir
set updir [file join {d:\Apache2\htdocs\my-app\uploads\in}]

cgi_eval {

    # Read all CGI input and decode it
    cgi_input
   
    # Name of file on server, "the_file" represents the form field
    # used to upload the file
    set server [cgi_import_file -server the_file]

    # If file size is 0, delete the CGI temp file and redirect
    # client to the upload error page
    if {[file size $server] == 0} {
        file delete $server
        cgi_redirect /my-app/upload-error.html
        exit
    }

    # Filename sent by the client (tail only, IE sends the entire
    # path of the client file)
    set client [file tail [cgi_import_file -client the_file]]
   
    # Make sure the uploaded file has an acceptable filename
    regsub -all {([^\w.-])} $client - safe_file
    regsub {^[-.]+} $safe_file "" safe_file
   
    # Move (and rename) the file to the uploads/in directory
    # Existing files will be overwritten
    file rename -force $server $updir/$safe_file
    #warning: ensure your Tcl version is not subject to bug 2015723, or the rename may intermittently & silently fail to do anything.
       
    # Redirect client
    cgi_redirect /my-app/upload.html
}