Version 36 of tclhttpd Examples

Updated 2003-10-22 14:01:29

This page will try to collect code examples to ease the beginners to fully exploit one of Tcl's pearls, TclHTTPD, a pure-Tcl HTTPD server.


Added to link to all Cookies related pages.


Setting a Cookie

To set a cookie, first use Doc_SetCookie. The following is an example of a function setting a cookie:

  Direct_Url /login login
  proc login { {username none} {password none} } {

    set expire [clock format [expr {[clock seconds] + 3600}] \
                -format "%A, %d-%b-%Y %H:%M:%S GMT" -gmt 1]

    Doc_SetCookie -name username -value $username -expires $expire \
                  -path {/} -domain {yourdomain.com}  

    # Just output parameters; for debugging
    set html [html::head "Login" ]
    append html "Username: $username <BR>\n"
    append html "Password: $password <BR>\n"
    return $html
  }

NOTE: Current problem is that the cookies added this way don't seem to last long, even without expiration set. If the browser is restarted, the cookie goes away. Seen in both IE & Netscape. Comments welcomed.

2002-01-29 Acacio Cruz


Retrieving a Cookie

To retrieve a saved cookie, use Doc_Cookie. The following is an example of simple .tml form that can be used as a login form:

  [ Doc_Dynamic
    html::head "Login form" ]

  <FORM action=/login method=post>
  <table>
    <tr>
      <td>Username</td>
      <td><input name=username value="[Doc_Cookie username]"></td>
    </tr>
    <tr>
      <td>Password</td>
      <td><input name=password></td>
    </tr>
    <tr>
      <td><input type=submit></td>
    </tr>
  </table>
  </FORM>
  </HTML>

2002-01-29 Acacio Cruz


Wanted examples

2002-01-29 Acacio Cruz


Doc_Cookie vs Httpd_Cookie

Doc_Cookie sets cookies which will be returned (via Httpd_Cookie) if, and only if, the procedure setting them is either in a Direct domain, or called via DocTemplate (ie: a .tml file.)

2002-07-31 CMcC


File Upload

This is a File Upload example.

2002-10-22 Michael Hankinson


TV I've got a tclhttpd with some additions and changes running now for half a year, with probably easily more then 95% or 24/7 uptime on a standard consumer connection, which works fine, as it has before, except not all is clear or asit probably is most desirable. Uninterupted uptime seems bounded by windows (XP) to a few weeks or so, depending on what I do, of course (want to run linux for instance..), but not because of leak or stack problems it seems, everything is fine, with say 10000 hits/week or so.

You'll need to be very aware that certain functions, like the cgi examples, expecially combined with the upload feature can be quite a potential security hazard for hackers who know the server.

Basically, remove all parts of the standard distribution with the errors and cgi's and dangerous scripts, and if you think it important, the reset button on the traffic page.

I'll make a page with a straightforward enough server application script based on the tclhttpd 3.4.1 standard distribution, I'll need to go through my files a bit to get that together for those who want a running server easily.

Anyone know why the file filter box on the document hits page doesn't seem to work right?

example Direct_Url:

 ###  Fortune page
 proc serve_fortune {} {
   return "<html><h2>Fortune Page</h2>\n<P>An automatically generated fortune \n<P><pre>[exec /cygwin/bin/fortune]</pre></html>"
 }
 Direct_Url /fortune serve_fortune

Use that in the httpd.tcl file before the last section somewhere, maybe source it in to get the equivalent of this page: http://195.241.128.75/fortune . You'll need cygwin.

(This in my not too huble opinion was not a remark without use, who thinks they can singlehandedly decide otherwise and break the reasonable code on these wikis and remove it?)


Starter Code for easy image deployment Image Server starter code for tclhttpd 2002-10-24 art morel


formkit uses tclhttpd templates (tml pages) and metakit to store and retrieve web form information.

June 26, 2003: a session enabled example of formkit is available at [L1 ]


Username/Password Database for Tclhttpd is an example using the session module in Tclhttpd. It requires Metakit.


Chapter "Static page access in Tclhttpd" in Wikit configuration shows how to use the static-page-approach from the wikit with the Tclhttpd-server (5/7/2003 Stefan Vogel).


I've put up some session demo code, http://sharedtech.dyndns.org/~colin/session.tgz which gives some brief and fairly minimal examples on the use of sessions in tclhttpd. Note, you will need MD5 for some of the examples, but it's not strictly necessary. These examples don't exploit sessions' ability to run in their own interpreters, but only the use of sessions to maintain persistent state. There's also code to give a larger and less forgeable session_id (which is all MD5 is used for.)


I've written a binding that lets you run OpenACS code from inside tclhttpd.

http://www.jsequeira.com/projects/portable.nsd/

The documentation could use some work, but it's a pretty powerful combination. The shim code provides examples of cookies and Direct URLs. (7oct2003 JJS)


You want to administrate some metakit-database-files with a web-interface in Tclhttpd? Simply have a look at Webadmin for Metakit with Tclhttpd.

A nearly complete webadministration-interface for metakits in only around 600 LoC (implemented as a Direct-URL). Tclhttpd is simply cool :-) (10/22/2003 Stefan Vogel)


Category Internet