Mercurial > repos > rhope
comparison webserver.rhope @ 146:1f39e69446f9
Finished porting webserver
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Sun, 21 Nov 2010 22:47:14 -0500 |
parents | 7bbdc034e347 |
children | f3686f60985d |
comparison
equal
deleted
inserted
replaced
145:357f4ce3ca6d | 146:1f39e69446f9 |
---|---|
1 | |
2 Import net.rhope | |
3 | |
4 _Dict Split[dict,entry,index,keydelim:out] | |
5 { | |
6 parts <- [entry]Split[keydelim] | |
7 out <- [dict]Set[[parts]Index[0],[parts]Index[1]] | |
8 } | |
9 | |
10 Dict Split[string,keydelim,entrydelim:out] | |
11 { | |
12 out <- Fold[_Dict Split[?, ?, ?, keydelim], Dictionary[], [string]Split[entrydelim]] | |
13 } | |
14 | |
15 _Key Value Join[dict,key,key sep,val sep,string:out] | |
16 { | |
17 new string <- [[[string]Append[String[key]]]Append[key sep]]Append[ String[[dict]Index[key]] ] | |
18 [dict]Next[key] | |
19 { | |
20 out <- _Key Value Join[dict, ~, key sep, val sep, [new string]Append[val sep]] | |
21 }{ | |
22 out <- Val[new string] | |
23 } | |
24 } | |
25 | |
26 Key Value Join[dict,key sep,val sep:out] | |
27 { | |
28 [dict]First | |
29 { | |
30 out <- _Key Value Join[dict, ~, key sep, val sep, ""] | |
31 }{ | |
32 out <- "" | |
33 } | |
34 } | |
1 | 35 |
2 Get Content Type[path:out] | 36 Get Content Type[path:out] |
3 { | 37 { |
4 parts <- [path]Split["."] | 38 parts <- [path]Split["."] |
5 ext <- [parts]Index[ [[parts]Length] - [1] ] | 39 ext <- [parts]Index[ [[parts]Length] - [1] ] |
39 default headers <- [start headers]Set["Transfer-Encoding", "Chunked"] | 73 default headers <- [start headers]Set["Transfer-Encoding", "Chunked"] |
40 }{ | 74 }{ |
41 default headers <- [start headers]Set["Content-Length", content length] | 75 default headers <- [start headers]Set["Content-Length", content length] |
42 } | 76 } |
43 | 77 |
44 out <- [client]Put String[ | 78 out <- [ |
45 [ | 79 [ |
46 [ | 80 [ |
47 [ | 81 [ |
48 ["HTTP/1.1 "]Append[code] | 82 ["HTTP/1.1 "]Append[code] |
49 ]Append["\r\n"] | 83 ]Append["\r\n"] |
50 ]Append[ | 84 ]Append[ |
51 [ | 85 [ |
52 Combine[headers, default headers] | 86 Combine[headers, default headers] |
53 ]Key Value Join[": ", "\r\n"] | 87 ]Key Value Join[": ", "\r\n"] |
54 ] | 88 ] |
55 ]Append["\r\n\r\n"]] | 89 ]Append["\r\n\r\n"] |
90 ]Write to File[client] | |
56 } | 91 } |
57 | 92 |
58 HTTP Not Found[client] | 93 HTTP Not Found[client] |
59 { | 94 { |
60 string <- "<html><head><title>Document Not Found</title></head><body>The document you requested is not available on this server.</body></html>" | 95 string <- "<html><head><title>Document Not Found</title></head><body>The document you requested is not available on this server.</body></html>" |
61 HTTP Response[client, Get Content Type[".html"], [string]Length, Dictionary[], "404 Not Found"] | 96 HTTP Response[client, Get Content Type[".html"], [string]Length, Dictionary[], "404 Not Found"] |
62 { | 97 { |
63 [~]Put String[string] | 98 [string]Write to File[~] |
64 } | 99 } |
100 } | |
101 | |
102 Good Path?[path:yep,nope] | |
103 { | |
104 nope <- If[[path]Starts With["."]] {} | |
105 { nope <- If[[path]Starts With["/"]] {} | |
106 { nope <- If[[path]Contains[".."]] {} | |
107 { nope,yep <- If[[path]Contains["//"]] }}} | |
65 } | 108 } |
66 | 109 |
67 Handle Request[client,type,query,headers,handler] | 110 Handle Request[client,type,query,headers,handler] |
68 { | 111 { |
69 parts <- [query]Split["?"] | 112 parts <- [query]Split["?"] |
82 the handler <- [handler]Index[[host]Append[handlerpath]] {} | 125 the handler <- [handler]Index[[host]Append[handlerpath]] {} |
83 { | 126 { |
84 the handler <- [handler]Index[handlerpath] {} | 127 the handler <- [handler]Index[handlerpath] {} |
85 { | 128 { |
86 ,newpath <- [path]Slice[1] | 129 ,newpath <- [path]Slice[1] |
87 If[[newpath] = ["default.css"]] | 130 Good Path?[newpath] |
88 { | 131 { |
89 file <- Open[File[newpath],"r"] | 132 file <- Open[File[newpath],"r"] |
90 content length <- Length[file] | 133 content length <- Length[file] |
91 If[[content length] > [0]] | 134 If[[content length] > [0]] |
92 { | 135 { |
93 data <- [file]Read[content length] | 136 data <- [file]Read[content length] |
94 [HTTP OK[client, Get Content Type[path], content length, Dictionary[]] | 137 [HTTP OK[client, Get Content Type[path], content length, Dictionary[]] |
95 ]Write[data] | 138 ]Write[data] |
96 { Close[file] } | 139 { Close[file] } |
97 }{ | 140 }{ |
98 HTTP Not Found[client] | 141 HTTP Not Found[client] |
99 } | 142 } |
100 }{ | 143 }{ |
101 HTTP Not Found[client] | 144 HTTP Not Found[client] |
102 } | 145 } |
103 } | 146 } |
104 } | 147 } |
109 { | 152 { |
110 queryvars <- Dict Split[[parts]Index[1], "=", "&"] | 153 queryvars <- Dict Split[[parts]Index[1], "=", "&"] |
111 }{ | 154 }{ |
112 queryvars <- Dictionary[] | 155 queryvars <- Dictionary[] |
113 } | 156 } |
114 [~]Do@Worker[ [[[[[List[]]Append[client]]Append[path]]Append[type]]Append[queryvars]]Append[headers] ] | 157 [~]Call[client,path,type,queryvars,headers] |
115 } | 158 } |
116 | 159 |
117 } | 160 } |
118 | 161 |
119 Connection Start[con,handlers] | 162 Connection Start[con,address,handlers] |
120 { | 163 { |
121 client, request <- [con]Get DString["\r\n"] | 164 ,client <- [con]Read Delim[["\r\n"]Buffer >>] |
165 { request <- String[~] } | |
122 parts <- [request]Split[" "] | 166 parts <- [request]Split[" "] |
123 Print[request] | 167 Print[[[request]Append[" "]]Append[address]] |
124 [client]Get DString["\r\n\r\n"] | 168 [client]Read Delim[["\r\n\r\n"]Buffer >>] |
125 { | 169 { |
170 headers <- Map[Dict Split[String[~], ":", "\r\n"], Trim[?, " \t"]] | |
171 }{ | |
126 Handle Request[~, [parts]Index[0], [parts]Index[1], headers, handlers] | 172 Handle Request[~, [parts]Index[0], [parts]Index[1], headers, handlers] |
127 }{ | |
128 headers <- Map[Dict Split[~, ":", "\r\n"], ["Trim"]Set Input[1, " \t"]] | |
129 } | 173 } |
130 } | 174 } |
131 | 175 |