Wapp

README.md at [b60ef9d35e]
Login

File README.md artifact 88e94111a1 part of check-in b60ef9d35e


Wapp - A Web-Application Framework for TCL

1.0 Introduction

Wapp is a simple and lightweight framework that strives to simplify the construction of web application written in TCL. The same Wapp application can be launched in multiple ways:

All four methods of launching the application provide the same interface to the user.

1.0 Hello World!

Wapp is designed to be easy to use. A hello-world program is as follows:

package require wapp
proc wapp-default {req} {
   wapp "<h1>Hello, World!</h1>\n"
}
wapp-start $::argv

The application defines one or more procedures that accept HTTP requests. For an HTTP request where the initial portion of the URI is "abcde", the procedure named "wapp-page-abcde" will be invoked to construct the reply. If no such procedure exists, "wapp-default" is invoked instead.

The procedure generates a reply using one or more calls to the "wapp" command. Each "wapp" command appends new text to the reply.

The "wapp-start" command starts up the built-in web server.

To run this application, but the code above into a file named "main.tcl" and then run type "tclsh main.tcl" at the command-line. That should cause the "Hello, World!" page to appear in your web browser.

1.1 A Slightly Longer Example

Information about each HTTP request is encoded in the global ::wapp dict variable. The following sample program shows the information available in ::wapp.

package require wapp
proc wapp-default {} {
  global wapp
  wapp "<h1>Hello, World!</h1>\n"
  wapp-unsafe "<p>See the <a href='[dict get $wapp BASE_URL]/env'>Wapp "
  wapp "Environment</a></p>"
}
proc wapp-page-env {} {
  global wapp
  wapp "<h1>Wapp Environment</h1>\n"
  wapp "<pre>\n"
  foreach var [lsort [dict keys $wapp]] {
    if {[string index $var 0]=="."} continue
    wapp-escape-html "$var = [list [dict get $wapp $var]]\n"
  }
  wapp "</pre>"
}
wapp-start $::argv

In this application, the default "Hello, World!" page has been extended with a hyperlink to the /env page. The "wapp-unsafe" command works exactly the same as "wapp" (it appends its argument text to the web page under construction) except that the argument to "wapp-unsafe" is allowed to contain TCL variable and command expansions. The "wapp" command will generate a warning if its argument contains TCL variable or command expansions, as a defense against accidental XSS vulnerabilities.

The /env page is implemented by the "wapp-page-env" proc. This proc generates an HTML unordered list where each element of the list describes a single value in the global ::wapp dict. The "wapp-escape-html" command is like "wapp" and "wapp-unsafe" except that "wapp-escape-html" escapes HTML markup so that it displays correctly in the output.

1.2 The ::wapp Global Dict

To better understand how the ::wapp dict works, try running the previous sample program, but extend the /env URL with extra path elements and query parameters. For example: [http://localhost:8080/env/longer/path?q1=5&title=hello+world%21]

Notice how the query parameters in the input URL are decoded and become elements of the ::wapp dict. The same thing occurs with POST parameters and cookies - they are all converted into entries in the ::wapp dict variable so that the parameters are easily accessible to page generation procedures.

The ::wapp variable contains additional information about the request, roughly corresponding to CGI environment variables. To prevent environment information from overlapping and overwriting query parameters, all the environment information uses upper-case names and all query parameters are required to be lower case. If an input URL contains an upper-case query parameter (or POST parameter or cookie), that parameter is silently omitted from the ::wapp variable

The ::wapp variable contains the following environment values:

1.3 Additional Wapp Commands

The following utility commands are available for use by applications built on Wapp:

1.4 Design Rules

All global procs and variables used by Wapp begin with the four character prefix "wapp". Procs and variable intended for internal use begin with the seven character prefix "wappInt".