8-Line Server

jdp - An 8 line server. Serves text files out of, or below, the current directory. As no headers are sent, it depends on the browser what he does with it - IE6 seems to expect HTML 0.9 (i.e. linebreaks are ignored, etc.)

proc s {c a p} {
        set o [lindex [split [gets $c] " "] 1]
        puts $c [read [set f [open ".$o"]]]
        close $f
        close $c
}
set s [socket -server s 3210]
vwait forever

jdp - After spending time in the chatroom, with the help of stu and suchenwi, it became a 1-line, 140-char server. (Interactive mode only, though.)

vw [soc -server {appl {{c a p} {puts $c [read [set f [open .[regsub {\.\.+} [lindex [split [gets $c] \ ] 1] x]]]][close $f];close $c}}} 80]

RS 2009-05-29: The fcopy version is still a few bytes shorter:

vw [soc -server {appl {{c a p} {fcopy [set f [open .[regsub {\.\.+} [lindex [split [gets $c] \ ] 1] x]]] $c;close $f;close $c}}} 80]

In fact, I'm pretty sure the split isn't needed, as {} etc can't occur in URLs. Down to 121 characters:

vw [soc -server {appl {{c a p} {fcopy [set f [open .[regsub {\.\.+} [lindex [gets $c] 1] x]]] $c;close $f;close $c}}} 80]

jdp 2009-06-01: Perhaps the 'x' should be replaced with some character that is illegal in filenames, to avoid conflict with files and directories named 'x'. Also, if we set the read/write modes to be binary, then we can actually serve any file (not just text files). Unfortunately, that enhancement costs us 37 characters, making it 158 characters.

vw [soc -server {appl {{c a p} {fconfigure $c -translation binary;fcopy [set f [open .[regsub {\.\.+} [lindex [gets $c] 1] /] rb]] $c;close $f;close $c}}} 80]

cjl 2009-06-01 : Silly mod, but following the readability-reducing example set earlier, it can be reduced to 131 chars.

vw [soc -server {appl {{c a p} {fcon $c -t binary;fcop [set f [op .[regs {\.\.+} [lind [ge $c] 1] /] rb]] $c;clos $f;clos $c}}} 80]

RS 2009-06-04 Have you tested that? On my 8.5.1, apply does no command completion, so I get

 invalid command name "fcon"

The binary keyword to fconfigure can't be abbreviated either. cjl : I actually only tested in isolation each of the reductions I made to vw's version to find the shortest unique form for each command, and had already discovered that "binary" can't be truncated. I didn't spot the significance of apply, otherwise I would have tested the end result too.

CEW In the spirit of the one line server, I created a one line client to match:

set ex socket 127.0.0.1 80;puts $ex “get /SendMyData”;set MyData gets $ex;close $ex

While the industry forges ahead with millions of lines of code, it is important to realize the benefit of succinct writing: it is much easier to understand code when the entire application is in a single field of view.