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
\n" append html "Password: $password
\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" ]
Username
Password
2002-01-29 [Acacio Cruz] ---- '''Wanted examples''' * Session management * embedded tclhttpd use (skeleton, not bloated startup) see: [Starting Tclhttpd in a slave interpreter] 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 "

Fortune Page

\n

An automatically generated fortune \n

[exec /cygwin/bin/fortune]
" } 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 humble 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 [http://www.freewebs.com/headsup/formkitse.htm] ---- [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]) ---- [ES]: Here is an init script for running Tclhttpd as a RedHat 9 service. Copy to ''/etc/init.d/tclhttpd'' and run '''chkconfig --add tclhttpd''' then '''chkconfig --level 2345 tclhttpd on'''. For Linux distributions that use a similar init.d structure as RedHat, you can try adapting this script. Otherwise, use it as a guide for adapting an already-installed Apache init script (which is what I did). Note that this script assumes that the Tclhttpd directory is /opt/tclhttpd/ (seemed logical). You can of course change that to whatever you want. If you're running Gentoo Linux, I recommend you look here: [http://www.etoyoc.com/gentoo/tclhttpd.html] #!/bin/sh # chkconfig: 235 99 10 # description: Start or stop the Tclhttpd server # ### BEGIN INIT INFO # Provides: tclhttpd # Required-Start: $network # Required-Stop: $network # Default-Start: 2 3 5 # Default-Stop: 0 1 6 # Description: Start or stop the Tclhttpd server ### END INIT INFO # Edwin A. Suominen: Hacked from Apache httpd init script, licensed likewise. # Source function library. . /etc/rc.d/init.d/functions binDir=/opt/tclhttpd/bin interp=/usr/bin/tclhttpd lockfile=/var/lock/subsys/tclhttpd pidFile=/var/run/tclhttpd.pid name='tclhttpd' RETVAL=0 setup () { # Ensure we have proper /etc/tclhttpd.conf setup local confFile1=$binDir/tclhttpd.rc local confFile2=/etc/tclhttpd.conf if [ ! -h $confFile1 ] then if [ ! -f $confFile2 ] && [ -f $confFile1 ] then mv $confFile1 $confFile2 fi ln -s $confFile2 $confFile1 fi # Ensure we have a script for starting the TCL interpreter # with httpd.tcl running if [ ! -x $interp ] then echo '#!/usr/bin/tclsh if { [file exists /var/lock/subsystem/tclhttpd] } { exit 1 } set fh [open /var/run/tclhttpd.pid w] puts $fh [pid] close $fh after 2000 {source /opt/tclhttpd/bin/httpd.tcl} vwait forever' > $interp chmod u+x $interp fi } start () { echo -n $"Starting $name: " $interp & RETVAL=$? echo [ $RETVAL = 0 ] && touch $lockfile } stop () { echo -n $"Stopping $name: " killproc $interp RETVAL=$? echo [ $RETVAL = 0 ] && rm -f $lockfile $pidfile } case "$1" in 'start') setup start ;; 'stop') stop ;; 'status') status $interp RETVAL=$? ;; 'restart') stop start ;; *) echo "Usage: $0 { start | stop | status | restart }" RETVAL=1 ;; esac exit $RETVAL ---- [Category Internet]