changeset 146:1f39e69446f9

Finished porting webserver
author Mike Pavone <pavone@retrodev.com>
date Sun, 21 Nov 2010 22:47:14 -0500
parents 357f4ce3ca6d
children f3686f60985d
files basicweb.rhope webserver.rhope
diffstat 2 files changed, 73 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- 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[] }
 }
--- 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 <- "<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, 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]
 	}
 }