Mercurial > repos > rhope
comparison webserver.rhope @ 0:76568becd6d6
Rhope Alpha 2a source import
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Tue, 28 Apr 2009 23:06:07 +0000 |
parents | |
children | 6202b866d72c |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:76568becd6d6 |
---|---|
1 Import extendlib.rhope | |
2 | |
3 Get Content Type[path:out] | |
4 { | |
5 parts <- [path]Split["."] | |
6 ext <- [parts]Index[ [[parts]Length] - [1] ] | |
7 If[ [ext] = ["html"] ] | |
8 { | |
9 out <- "text/html; charset=ISO-8859-1" | |
10 }{ | |
11 If[ [ext] = ["jpg"] ] | |
12 { | |
13 out <- "image/jpeg" | |
14 }{ | |
15 If[ [ext] = ["gif"] ] | |
16 { | |
17 out <- "image/gif" | |
18 }{ | |
19 If[ [ext] = ["css"] ] | |
20 { | |
21 out <- "text/css" | |
22 }{ | |
23 out <- "application/octet-stream" | |
24 } | |
25 } | |
26 } | |
27 } | |
28 } | |
29 | |
30 HTTP OK[client,type,content length,headers:out] | |
31 { | |
32 out <- HTTP Response[client, type, content length, headers, "200 OK"] | |
33 } | |
34 | |
35 HTTP Response[client,type,content length,headers,code:out] | |
36 { | |
37 start headers <- [New@Dictionary[]]Set["Content-Type", type] | |
38 If[[content length] < [0]] | |
39 { | |
40 default headers <- [start headers]Set["Transfer-Encoding", "Chunked"] | |
41 }{ | |
42 default headers <- [start headers]Set["Content-Length", content length] | |
43 } | |
44 | |
45 out <- [client]Put String@Net Client[ | |
46 [ | |
47 [ | |
48 [ | |
49 ["HTTP/1.1 "]Append[code] | |
50 ]Append["\r\n"] | |
51 ]Append[ | |
52 [ | |
53 Combine[headers, default headers] | |
54 ]Key Value Join[": ", "\r\n"] | |
55 ] | |
56 ]Append["\r\n\r\n"]] | |
57 } | |
58 | |
59 HTTP Not Found[client] | |
60 { | |
61 string <- "<html><head><title>Document Not Found</title></head><body>The document you requested is not available on this server.</body></html>" | |
62 HTTP Response[client, Get Content Type[".html"], [string]Length, New@Dictionary[], "404 Not Found"] | |
63 { | |
64 [~]Put String[string] | |
65 } | |
66 } | |
67 | |
68 Handle Request[client,type,query,headers,handler] | |
69 { | |
70 parts <- [query]Split["?"] | |
71 path <- [parts]Index[0] | |
72 [[path]Split["/"]]Index[1] | |
73 { | |
74 handlerpath <- ["/"]Append[~] | |
75 }{ | |
76 handlerpath <- "/" | |
77 } | |
78 [handler]Index[handlerpath] | |
79 { | |
80 If[[[parts]Length] > [1]] | |
81 { | |
82 queryvars <- Dict Split[[parts]Index[1], "=", "&"] | |
83 }{ | |
84 queryvars <- New@Dictionary[] | |
85 } | |
86 [~]Do@Worker[ [[[[[New@List[]]Append[client]]Append[path]]Append[type]]Append[queryvars]]Append[headers] ] | |
87 }{ | |
88 | |
89 ,newpath <- [path]Slice[1] | |
90 file <- <String@File[newpath] | |
91 content length <- Length[file] | |
92 If[[content length] > [0]] | |
93 { | |
94 junk,data <- [file]Get FString[content length] | |
95 [HTTP OK[client, Get Content Type[path], content length, New@Dictionary[]] | |
96 ]Put String@Net Client[data] | |
97 }{ | |
98 HTTP Not Found[client] | |
99 } | |
100 } | |
101 | |
102 } | |
103 | |
104 Connection Start[con,handlers] | |
105 { | |
106 client, request <- [con]Get DString@Net Client["\r\n"] | |
107 parts <- [request]Split@String[" "] | |
108 Print[request] | |
109 [client]Get DString@Net Client["\r\n\r\n"] | |
110 { | |
111 Handle Request[~, [parts]Index@List[0], [parts]Index@List[1], headers, handlers] | |
112 }{ | |
113 headers <- Map[Dict Split[~, ":", "\r\n"], ["Trim"]Set Input[1, " \t"]] | |
114 } | |
115 } | |
116 |