# HG changeset patch # User Mike Pavone # Date 1290397634 18000 # Node ID 1f39e69446f95aa3736b8894a21dbf0f1ae5d7e7 # Parent 357f4ce3ca6dca8f03ce6672b0217766d659402f Finished porting webserver diff -r 357f4ce3ca6d -r 1f39e69446f9 basicweb.rhope --- a/basicweb.rhope Sun Nov 21 22:08:17 2010 -0500 +++ b/basicweb.rhope Sun Nov 21 22:47:14 2010 -0500 @@ -5,12 +5,15 @@ //Import the library that does all the hard work Import webserver.rhope -Main[] +Main[args] { + [args]Index[1] + { port <- Int32[~] } + { port <- 80 } Print["Starting webserver"] //Since we're just serving static files we don't need to setup any handlers handlers <- Dictionary[] - //Start listening on port 80 - Listen on Port[80,["Connection Start"]Set Input[1, handlers]] - Wait Forever[] + //Start listening on the desired port + Listen on Port[port,Connection Start[?, ?, handlers]] + { Wait Forever[] } } diff -r 357f4ce3ca6d -r 1f39e69446f9 webserver.rhope --- a/webserver.rhope Sun Nov 21 22:08:17 2010 -0500 +++ b/webserver.rhope Sun Nov 21 22:47:14 2010 -0500 @@ -1,3 +1,37 @@ + +Import net.rhope + +_Dict Split[dict,entry,index,keydelim:out] +{ + parts <- [entry]Split[keydelim] + out <- [dict]Set[[parts]Index[0],[parts]Index[1]] +} + +Dict Split[string,keydelim,entrydelim:out] +{ + out <- Fold[_Dict Split[?, ?, ?, keydelim], Dictionary[], [string]Split[entrydelim]] +} + +_Key Value Join[dict,key,key sep,val sep,string:out] +{ + new string <- [[[string]Append[String[key]]]Append[key sep]]Append[ String[[dict]Index[key]] ] + [dict]Next[key] + { + out <- _Key Value Join[dict, ~, key sep, val sep, [new string]Append[val sep]] + }{ + out <- Val[new string] + } +} + +Key Value Join[dict,key sep,val sep:out] +{ + [dict]First + { + out <- _Key Value Join[dict, ~, key sep, val sep, ""] + }{ + out <- "" + } +} Get Content Type[path:out] { @@ -41,7 +75,7 @@ default headers <- [start headers]Set["Content-Length", content length] } - out <- [client]Put String[ + out <- [ [ [ [ @@ -52,7 +86,8 @@ Combine[headers, default headers] ]Key Value Join[": ", "\r\n"] ] - ]Append["\r\n\r\n"]] + ]Append["\r\n\r\n"] + ]Write to File[client] } HTTP Not Found[client] @@ -60,10 +95,18 @@ string <- "Document Not FoundThe document you requested is not available on this server." HTTP Response[client, Get Content Type[".html"], [string]Length, Dictionary[], "404 Not Found"] { - [~]Put String[string] + [string]Write to File[~] } } +Good Path?[path:yep,nope] +{ + nope <- If[[path]Starts With["."]] {} + { nope <- If[[path]Starts With["/"]] {} + { nope <- If[[path]Contains[".."]] {} + { nope,yep <- If[[path]Contains["//"]] }}} +} + Handle Request[client,type,query,headers,handler] { parts <- [query]Split["?"] @@ -84,19 +127,19 @@ the handler <- [handler]Index[handlerpath] {} { ,newpath <- [path]Slice[1] - If[[newpath] = ["default.css"]] - { - file <- Open[File[newpath],"r"] - content length <- Length[file] - If[[content length] > [0]] + Good Path?[newpath] { - data <- [file]Read[content length] - [HTTP OK[client, Get Content Type[path], content length, Dictionary[]] - ]Write[data] - { Close[file] } - }{ - HTTP Not Found[client] - } + file <- Open[File[newpath],"r"] + content length <- Length[file] + If[[content length] > [0]] + { + data <- [file]Read[content length] + [HTTP OK[client, Get Content Type[path], content length, Dictionary[]] + ]Write[data] + { Close[file] } + }{ + HTTP Not Found[client] + } }{ HTTP Not Found[client] } @@ -111,21 +154,22 @@ }{ queryvars <- Dictionary[] } - [~]Do@Worker[ [[[[[List[]]Append[client]]Append[path]]Append[type]]Append[queryvars]]Append[headers] ] + [~]Call[client,path,type,queryvars,headers] } } -Connection Start[con,handlers] +Connection Start[con,address,handlers] { - client, request <- [con]Get DString["\r\n"] + ,client <- [con]Read Delim[["\r\n"]Buffer >>] + { request <- String[~] } parts <- [request]Split[" "] - Print[request] - [client]Get DString["\r\n\r\n"] + Print[[[request]Append[" "]]Append[address]] + [client]Read Delim[["\r\n\r\n"]Buffer >>] { - Handle Request[~, [parts]Index[0], [parts]Index[1], headers, handlers] + headers <- Map[Dict Split[String[~], ":", "\r\n"], Trim[?, " \t"]] }{ - headers <- Map[Dict Split[~, ":", "\r\n"], ["Trim"]Set Input[1, " \t"]] + Handle Request[~, [parts]Index[0], [parts]Index[1], headers, handlers] } }