Version 0 of mini demo webserver

Updated 2004-03-01 10:08:33

Super-mini web server that I use as a demonostration for presentations. Not correct or complete, but it does serve files...


 proc Serve {chan addr port} {
    fconfigure $chan -translation auto -buffering line
    set line [gets $chan]
    set path [file join . [string trimleft [lindex $line 1] /]]
    if { $path == "./reload" } {
        set reload 1
        source [info script]
        puts $chan "HTTP/1.0 200 OK"
        puts $chan "Content-Type: text/html"
        puts $chan ""
        puts $chan "Server reloaded"
    } else {
        if { [catch {
            set fl [open $path]
        } err] } {
            puts $chan "HTTP/1.0 404 Not Found"
        } else {
            puts $chan "HTTP/1.0 200 OK"
            puts $chan "Content-Type: text/html"
            puts $chan ""
            puts $chan [read $fl]
            close $fl
        }
    }
        close $chan
 }

 if { ! [info exists reload] } {
    set sk [socket -server Serve 5151]
    vwait forever
 } else {
    unset reload
 }