Document Types

   

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 18.  TclHttpd Web Server


The Document domain (doc.tcl) maps URLs onto files and directories. It provides more ways to extend the server by registering different document type handlers. This occurs in a two-step process. First, the type of a file is determined by its suffix. The mime.types file contains a map from suffixes to MIME types such as text/html or image/gif. This map is controlled by the Mtype module in mtype.tcl. Second, the server checks for a Tcl procedure with the appropriate name:

 Doc_mimetype 

The matching procedure, if any, is called to handle the URL request. The procedure should use routines in the Httpd module to return data for the request. If there is no matching Doc_mimetype procedure, then the default document handler uses Httpd_ReturnFile and specifies the Content Type based on the file extension:

 Httpd_ReturnFile $sock [Mtype $path] $path 

You can make up new types to support your application. Example 18-4 shows the pieces needed to create a handler for a fictitious document type application/myjunk that is invoked to handle files with the .junk suffix. You need to edit the mime.types file and add a document handler procedure to the server:

Example 18-4 A sample document type handler.
 # Add this line to mime.types application/myjunk     .junk # Define the document handler procedure #  path is the name of the file on disk #  suffix is part of the URL after the domain prefix #  sock is the handle on the client connection proc Doc_application/myjunk {path suffix sock} {    upvar #0 Httpd$sock data    # data(url) is more useful than the suffix parameter.    # Use the contents of file $path to compute a page    set contents [somefunc $path]    # Determine your content type    set type text/html    # Return the page    Httpd_ReturnData $sock $type $data } 

As another example, the HTML+Tcl templates use the .tml suffix that is mapped to the application/x-tcl-template type. The TclHttpd distribution also includes support for files with a .snmp extension that implements a template-based web interface to the Scotty SNMP Tcl extension.


       
    Top
     



    Practical Programming in Tcl and Tk
    Practical Programming in Tcl and Tk (4th Edition)
    ISBN: 0130385603
    EAN: 2147483647
    Year: 1999
    Pages: 478

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net