comparison webserver.vistxt @ 0:76568becd6d6

Rhope Alpha 2a source import
author Mike Pavone <pavone@retrodev.com>
date Tue, 28 Apr 2009 23:06:07 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:76568becd6d6
1 Import extendlib.vistxt
2
3 Get Content Type(1,1)
4 |:
5 parts <- [path(0)]Split["."]
6 ext <- [parts]Index[ [[parts]Length] - [1] ]
7 If[ [ext] = ["html"] ]
8 |:
9 out(0) <- "text/html; charset=ISO-8859-1"
10 :||:
11 If[ [ext] = ["jpg"] ]
12 |:
13 out(0) <- "image/jpeg"
14 :||:
15 If[ [ext] = ["gif"] ]
16 |:
17 out(0) <- "image/gif"
18 :||:
19 If[ [ext] = ["css"] ]
20 |:
21 out(0) <- "text/css"
22 :||:
23 out(0) <- "application/octet-stream"
24 :|
25 :|
26 :|
27 :|
28 :|
29
30 HTTP OK(4,1)
31 |:
32 out(0) <- HTTP Response[client(0), type(1), content length(2), headers(3), "200 OK"]
33 :|
34
35 HTTP Response(5,1)
36 |:
37 start headers <- [New@Dictionary[]]Set["Content-Type", type(1)]
38 If[[content length(2)] < [0]]
39 |:
40 default headers <- [start headers]Set["Transfer-Encoding", "Chunked"]
41 :||:
42 default headers <- [start headers]Set["Content-Length", content length(2)]
43 :|
44
45 out(0) <- [client(0)]Put String@Net Client[
46 [
47 [
48 [
49 ["HTTP/1.1 "]Append[code(4)]
50 ]Append["\r\n"]
51 ]Append[
52 [
53 Combine[headers(3), default headers]
54 ]Key Value Join[": ", "\r\n"]
55 ]
56 ]Append["\r\n\r\n"]]
57 :|
58
59 HTTP Not Found(1,0)
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(0), Get Content Type[".html"], [string]Length, New@Dictionary[], "404 Not Found"]
63 |:
64 [~]Put String[string]
65 :|
66 :|
67
68 Handle Request(5,0)
69 |:
70 parts <- [query(2)]Split["?"]
71 path <- [parts]Index[0]
72 [[path]Split["/"]]Index[1]
73 |:
74 handlerpath <- ["/"]Append[~]
75 :||:
76 handlerpath <- "/"
77 :|
78 [handler(4)]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(0)]]Append[path]]Append[type(1)]]Append[queryvars]]Append[headers(3)] ]
87 :||:
88
89 ,newpath <- [path]Slice@String[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(0), Get Content Type[path], content length, New@Dictionary[]]
96 ]Put String@Net Client[data]
97 :||:
98 HTTP Not Found[client(0)]
99 :|
100 :|
101
102 :|
103
104 Connection Start(2,0)
105 |:
106 client, request <- [con(0)]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(1)]
112 :||:
113 headers <- Map[Dict Split[~, ":", "\r\n"], ["Trim"]Set Input[1, " \t"]]
114 :|
115 :|
116