| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| <html> |
| <head> |
| <title>Simple Web Server</title> |
| <link rel="stylesheet" href="style.css"> |
| </head> |
| |
| <body> |
| <? prevnext "<h1>Example: Mini Web Server</h1>" ?> |
| |
| |
| <PRE> |
| <B><FONT COLOR="#A020F0">proc</FONT></B> <B><FONT COLOR="#0000FF">Serve</FONT></B> {<FONT COLOR="#B8860B">chan addr port</FONT>} { |
| <B><FONT COLOR="#A020F0">fconfigure</FONT></B> $<FONT COLOR="#B8860B">chan</FONT> -translation {auto binary} |
| <B><FONT COLOR="#A020F0">set</FONT></B> line [<B><FONT COLOR="#A020F0">gets</FONT></B> $<FONT COLOR="#B8860B">chan</FONT>] |
| <B><FONT COLOR="#A020F0">if</FONT></B> { [<B><FONT COLOR="#A020F0">catch</FONT></B> { <B><FONT COLOR="#A020F0">set</FONT></B> fl [<B><FONT COLOR="#A020F0">open</FONT></B> <B><FONT COLOR="#BC8F8F">".[lindex $line 1]"</FONT></B> r] } err] } { |
| <B><FONT COLOR="#A020F0">puts</FONT></B> $<FONT COLOR="#B8860B">chan</FONT> <B><FONT COLOR="#BC8F8F">"HTTP/1.0 404 Not Found\r\n"</FONT></B> |
| } else { |
| <B><FONT COLOR="#A020F0">puts</FONT></B> $<FONT COLOR="#B8860B">chan</FONT> <B><FONT COLOR="#BC8F8F">"HTTP/1.0 200 OK\r |
| Content-Type: text/html\r\n\r\n[read $fl]"</FONT></B> |
| } |
| close $<FONT COLOR="#B8860B">chan</FONT> |
| } |
| |
| <B><FONT COLOR="#A020F0">set</FONT></B> sk [<B><FONT COLOR="#A020F0">socket</FONT></B> -server Serve 5151] |
| <B><FONT COLOR="#A020F0">fconfigure</FONT></B> $<FONT COLOR="#B8860B">sk</FONT> -buffering line |
| <B><FONT COLOR="#A020F0">vwait</FONT></B> forever |
| </PRE> |
| |
| </body> |
| </html> |