Integrating TclHttpd with your Application
The bulk of this chapter describes the various ways you can extend the server and integrate it into your application. TclHttpd is interesting because, as a Tcl script, it is easy to add to your application. Suddenly your application has an interface that is accessible to Web browsers in your company's intranet or the global Internet. The Web server provides several ways you can connect it to your application:
As a "normal" web server, you can serve static documents that describe your application.
You can arrange for all URL requests in a section of your Web site to be handled by your application. This is a very general interface where you interpret what the URL means and what sort of pages to return to each request. For example, http://www.scriptics.com/resource is implemented this way. The URL past /resource selects an index in a simple database, and the server returns a page describing the pages under that index.
This is a domain handler that maps URLs onto Tcl procedures. The form query data that is part of the HTTP GET or POST request is automatically mapped onto the parameters of the application-direct procedure. The procedure simply computes the page as its return value. This is an elegant and efficient alternative to the CGI interface. For example, in TclHttpd the URLs under /status report various statistics about the web server's operation.
You can define a Tcl procedure that handles all files of a particular type. For example, the server has a handler for CGI scripts, HTML files, image maps, and HTML+Tcl template files.
These are web pages that mix Tcl and HTML markup. The server replaces the Tcl using the subst command and returns the result. The server can cache the result in a regular HTML file to avoid the overhead of template processing on future requests. Templates are a great way to maintain common look and feel to a family of web pages, as well as to implement more advanced dynamic HTML features like self-checking forms.
Figure 18-1 shows the basic components of the server. At the core is the Httpd module (httpd.tcl), which implements the server side of the HTTP protocol. The "d" in Httpd stands for daemon, which is the name given to system servers on UNIX. This module manages network requests, dispatches them to the Url module, and provides routines used to return the results to requests.
Figure 18-1. The dotted box represents one application that embeds TclHttpd. Document templates and Application Direct URLs provide direct connections from an HTTP request to your application.
The Url module (url.tcl) divides the Web site into domains, which are subtrees of the URL hierarchy provided by the server. The idea is that different domains may have completely different implementations. For example, the Document domain (doc.tcl) maps its URLs into files and directories on your hard disk, while the Application-Direct domain (direct.tcl) maps URLs into Tcl procedure calls within your application. The CGI domain (cgi.tcl) maps URLs onto other programs that compute web pages.