view webserver.vistxt @ 166:1bfc19076f1b

Merge
author Mike Pavone <pavone@retrodev.com>
date Thu, 10 Mar 2011 04:16:38 +0000
parents 76568becd6d6
children
line wrap: on
line source

Import extendlib.vistxt

Get Content Type(1,1)
|:
	parts <- [path(0)]Split["."]
	ext <- [parts]Index[ [[parts]Length] - [1] ]
	If[ [ext] = ["html"] ]
	|:
		out(0) <- "text/html; charset=ISO-8859-1"
	:||: 
		If[ [ext] = ["jpg"] ]
		|:
			out(0) <- "image/jpeg"
		:||: 
			If[ [ext] = ["gif"] ]
			|:
				out(0) <- "image/gif"
			:||:
				If[ [ext] = ["css"] ]
				|:
					out(0) <- "text/css"
				:||:
					out(0) <- "application/octet-stream"
				:|
			:|
		:|
	:|
:|

HTTP OK(4,1)
|:
	out(0) <- HTTP Response[client(0), type(1), content length(2), headers(3), "200 OK"]
:|

HTTP Response(5,1)
|:
	start headers <- [New@Dictionary[]]Set["Content-Type", type(1)]
	If[[content length(2)] < [0]]
	|:
		default headers <- [start headers]Set["Transfer-Encoding", "Chunked"]
	:||:
		default headers <- [start headers]Set["Content-Length", content length(2)]
	:|
	
	out(0) <- [client(0)]Put String@Net Client[
				[
					[
						[
							["HTTP/1.1 "]Append[code(4)]
						]Append["\r\n"]
					]Append[
						[
							Combine[headers(3), default headers]
						]Key Value Join[": ", "\r\n"]
					]
				]Append["\r\n\r\n"]]
:|

HTTP Not Found(1,0)
|:
	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(0), Get Content Type[".html"], [string]Length, New@Dictionary[], "404 Not Found"]
	|:
		[~]Put String[string]
	:|
:|

Handle Request(5,0)
|:
	parts <- [query(2)]Split["?"]
	path <- [parts]Index[0]
	[[path]Split["/"]]Index[1]
	|:
		handlerpath <- ["/"]Append[~]
	:||:
		handlerpath <- "/"
	:|
	[handler(4)]Index[handlerpath]
	|:
		If[[[parts]Length] > [1]]
		|:
			queryvars <- Dict Split[[parts]Index[1], "=", "&"]
		:||:
			queryvars <- New@Dictionary[]
		:|
		[~]Do@Worker[ [[[[[New@List[]]Append[client(0)]]Append[path]]Append[type(1)]]Append[queryvars]]Append[headers(3)] ]
	:||:

		,newpath <- [path]Slice@String[1]
		file <- <String@File[newpath]
		content length <- Length[file]
		If[[content length] > [0]]
		|:
			junk,data <- [file]Get FString[content length]
			[HTTP OK[client(0), Get Content Type[path], content length, New@Dictionary[]]
			]Put String@Net Client[data]
		:||:
			HTTP Not Found[client(0)]
		:|
	:|
	
:|

Connection Start(2,0)
|:
	client, request <- [con(0)]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(1)]
	:||:
		headers <- Map[Dict Split[~, ":", "\r\n"], ["Trim"]Set Input[1, " \t"]]
	:|
:|