#!/usr/local/bin/tcl

proc accept { file addr port } {
    set sp [split [gets $file]]
    if { [string toupper [lindex $sp 0]] == "GET" } {
    	if { [lindex $sp 1] == "/" } { 
    		set sp [lreplace $sp 1 1 "/index.html"]
    	}
    	set sp [lreplace $sp 1 1 .[lindex $sp 1]]
    	set f ""
    	catch { set f [open [lindex $sp 1] r] }
    	if { $f != "" } {
    		puts -nonewline $file [read $f]
    		close $f
    	} else {
    		puts $file "404 / File not found"
    	}
    } else {
    	puts $file "501 / I don't know what you're asking! "
    }
    close $file
}

set n [socket -server accept 8080]

while { 1 } { vwait foo }