Mercurial > repos > rhope
annotate 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 |
rev | line source |
---|---|
0 | 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 { | |
47
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
37 start headers <- [Dictionary[]]Set["Content-Type", type] |
0 | 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>" | |
47
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
62 HTTP Response[client, Get Content Type[".html"], [string]Length, Dictionary[], "404 Not Found"] |
0 | 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 } | |
47
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
78 host <- [headers]Index["Host"] {} |
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
79 { |
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
80 host <- "" |
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
81 } |
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
82 |
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
83 the handler <- [handler]Index[[host]Append[handlerpath]] {} |
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
84 { |
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
85 the handler <- [handler]Index[handlerpath] {} |
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
86 { |
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
87 ,newpath <- [path]Slice[1] |
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
88 If[[newpath] = ["default.css"]] |
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
89 { |
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
90 file <- <String@File[newpath] |
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
91 content length <- Length[file] |
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
92 If[[content length] > [0]] |
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
93 { |
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
94 junk,data <- [file]Get FString[content length] |
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
95 [HTTP OK[client, Get Content Type[path], content length, Dictionary[]] |
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
96 ]Put String@Net Client[data] |
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
97 }{ |
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
98 HTTP Not Found[client] |
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
99 } |
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
100 }{ |
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
101 HTTP Not Found[client] |
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
102 } |
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
103 } |
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
104 } |
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
105 |
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
106 Val[the handler] |
0 | 107 { |
108 If[[[parts]Length] > [1]] | |
109 { | |
110 queryvars <- Dict Split[[parts]Index[1], "=", "&"] | |
111 }{ | |
47
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
112 queryvars <- Dictionary[] |
0 | 113 } |
47
6202b866d72c
Cleaned up constructor names and merged some other changes in to support the Rhope website
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
114 [~]Do@Worker[ [[[[[List[]]Append[client]]Append[path]]Append[type]]Append[queryvars]]Append[headers] ] |
0 | 115 } |
116 | |
117 } | |
118 | |
119 Connection Start[con,handlers] | |
120 { | |
121 client, request <- [con]Get DString@Net Client["\r\n"] | |
122 parts <- [request]Split@String[" "] | |
123 Print[request] | |
124 [client]Get DString@Net Client["\r\n\r\n"] | |
125 { | |
126 Handle Request[~, [parts]Index@List[0], [parts]Index@List[1], headers, handlers] | |
127 }{ | |
128 headers <- Map[Dict Split[~, ":", "\r\n"], ["Trim"]Set Input[1, " \t"]] | |
129 } | |
130 } | |
131 |