view webserver.rhope @ 75:0083b2f7b3c7

Partially working implementation of List. Modified build scripts to allow use of other compilers. Fixed some bugs involving method implementations on different types returning different numbers of outputs. Added Fold to the 'builtins' in the comipler.
author Mike Pavone <pavone@retrodev.com>
date Tue, 06 Jul 2010 07:52:59 -0400
parents 6202b866d72c
children 7bbdc034e347
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 <- [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, 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 <- "/"
	}
	host <- [headers]Index["Host"] {}
	{
		host <- ""
	}
	
	the handler <- [handler]Index[[host]Append[handlerpath]] {}
	{
		the handler <- [handler]Index[handlerpath] {}
		{
			,newpath <- [path]Slice[1]
			If[[newpath] = ["default.css"]]
			{
			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, Dictionary[]]
				]Put String@Net Client[data]
			}{
				HTTP Not Found[client]
			}
			}{
				HTTP Not Found[client]
			}
		}
	}

	Val[the handler]
	{
		If[[[parts]Length] > [1]]
		{
			queryvars <- Dict Split[[parts]Index[1], "=", "&"]
		}{
			queryvars <- Dictionary[]
		}
		[~]Do@Worker[ [[[[[List[]]Append[client]]Append[path]]Append[type]]Append[queryvars]]Append[headers] ]
	}
	
}

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"]]
	}
}