Version 9 of 8-Line Server

Updated 2009-06-01 16:15:18 by jdp

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]

Category Example