view webserver.rhope @ 142:7bbdc034e347

Fix some bugs. Get basic network code working (epoll listener + accept connections). Start porting webserver.
author Mike Pavone <pavone@retrodev.com>
date Sun, 21 Nov 2010 16:33:17 -0500
parents 6202b866d72c
children 1f39e69446f9
line wrap: on
line source


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

	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["\r\n"]
	parts <- [request]Split[" "]
	Print[request]
	[client]Get DString["\r\n\r\n"]
	{
		Handle Request[~, [parts]Index[0], [parts]Index[1], headers, handlers]
	}{
		headers <- Map[Dict Split[~, ":", "\r\n"], ["Trim"]Set Input[1, " \t"]]
	}
}