Mercurial > repos > rhope
view webserver.rhope @ 57:2174878a6e4b
Avoid unnecessary nested switch
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Thu, 08 Oct 2009 02:12:25 -0400 |
parents | 76568becd6d6 |
children | 6202b866d72c |
line wrap: on
line source
Import extendlib.rhope Get Content Type[path:out] { parts <- [path]Split["."] ext <- [parts]Index[ [[parts]Length] - [1] ] If[ [ext] = ["html"] ] { out <- "text/html; charset=ISO-8859-1" }{ If[ [ext] = ["jpg"] ] { out <- "image/jpeg" }{ If[ [ext] = ["gif"] ] { out <- "image/gif" }{ If[ [ext] = ["css"] ] { out <- "text/css" }{ out <- "application/octet-stream" } } } } } HTTP OK[client,type,content length,headers:out] { out <- HTTP Response[client, type, content length, headers, "200 OK"] } HTTP Response[client,type,content length,headers,code:out] { start headers <- [New@Dictionary[]]Set["Content-Type", type] If[[content length] < [0]] { default headers <- [start headers]Set["Transfer-Encoding", "Chunked"] }{ default headers <- [start headers]Set["Content-Length", content length] } out <- [client]Put String@Net Client[ [ [ [ ["HTTP/1.1 "]Append[code] ]Append["\r\n"] ]Append[ [ Combine[headers, default headers] ]Key Value Join[": ", "\r\n"] ] ]Append["\r\n\r\n"]] } HTTP Not Found[client] { string <- "<html><head><title>Document Not Found</title></head><body>The document you requested is not available on this server.</body></html>" HTTP Response[client, Get Content Type[".html"], [string]Length, New@Dictionary[], "404 Not Found"] { [~]Put String[string] } } Handle Request[client,type,query,headers,handler] { parts <- [query]Split["?"] path <- [parts]Index[0] [[path]Split["/"]]Index[1] { handlerpath <- ["/"]Append[~] }{ handlerpath <- "/" } [handler]Index[handlerpath] { If[[[parts]Length] > [1]] { queryvars <- Dict Split[[parts]Index[1], "=", "&"] }{ queryvars <- New@Dictionary[] } [~]Do@Worker[ [[[[[New@List[]]Append[client]]Append[path]]Append[type]]Append[queryvars]]Append[headers] ] }{ ,newpath <- [path]Slice[1] file <- <String@File[newpath] content length <- Length[file] If[[content length] > [0]] { junk,data <- [file]Get FString[content length] [HTTP OK[client, Get Content Type[path], content length, New@Dictionary[]] ]Put String@Net Client[data] }{ HTTP Not Found[client] } } } Connection Start[con,handlers] { client, request <- [con]Get DString@Net Client["\r\n"] parts <- [request]Split@String[" "] Print[request] [client]Get DString@Net Client["\r\n\r\n"] { Handle Request[~, [parts]Index@List[0], [parts]Index@List[1], headers, handlers] }{ headers <- Map[Dict Split[~, ":", "\r\n"], ["Trim"]Set Input[1, " \t"]] } }