-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.fs
302 lines (300 loc) · 15.3 KB
/
Program.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#nowarn "9"
open System.Runtime.InteropServices
open Microsoft.FSharp.NativeInterop
open FSharp.Data
open CommonLib
open Timetable
let tweakables =
{
daysBetween = 2
timeOff = [||]
minimumFraction = 5
maximumFraction = 4
acceptableOffset = t 3 0
}
let buffer = Array.zeroCreate<char> 128
let user_db = "/home/harlan/website_ssl/user.db"
let users =
try
System.Collections.Generic.Dictionary(
System.IO.File.ReadAllLines(user_db)|>Seq.map(fun (i) -> let j = i.Split(',') in j.[0],j.[1])
|> dict
)
with
| :? System.IndexOutOfRangeException -> System.Collections.Generic.Dictionary()
|e -> failwithf "Could not open the user database:\n%A" e
let conversions : list<string*(unit->string)> =
[
"%",(fun() -> "%")
"time_generated",System.DateTime.Now.ToLongTimeString
]
let sessions = System.Collections.Generic.Dictionary<string,string*System.DateTime>()
let dynamic (vars:(string*(unit->string)) list) session (page:string) =
let (a:string),(b:string option) =
if page="./HTML/dynamic/index.html" then
//If this goes wrong, BAD USER!!!
try
let vars = Map.ofList vars
//We have a user login
let user = vars.["username"]()
let pswd = users.[user]
let pswd'= vars.["password"]()
if pswd=pswd' then
let session = System.Guid.NewGuid().ToByteArray()|>System.Convert.ToBase64String
sessions.Add(session,(user,System.DateTime.Now+System.TimeSpan(0,30,0)))
async{while System.DateTime.Now < snd(sessions.[session]) do do! Async.Sleep 3600}
|> Async.Catch
|> Async.Ignore
|> Async.Start
"<meta http-equiv='refresh' content='0; url=/dynamic/MySchedule.html'>", Some(session)
else
"<meta http-equiv='refresh' content='0; url=/Login_bad.html'>",None
with |_ -> "<meta http-equiv='refresh' content='0; url=/Login_bad.html'>",None
elif page="./HTML/dynamic/create/index.html" then
try
let vars = Map.ofList vars
//We have a user login
let user = vars.["username"]()
let pswd' = vars.["password"]()
let pswd''= vars.["password2"]()
let good =
lock users (fun () ->
if pswd'<>pswd'' || users.ContainsKey(user) then false else
System.IO.File.AppendAllText(user_db,user+","+pswd')
users.Add(user,pswd')
true
)
if good then
let session = System.Guid.NewGuid().ToByteArray()|>System.Convert.ToBase64String
sessions.Add(session,(user,System.DateTime.Now+System.TimeSpan(0,30,0)))
async{while System.DateTime.Now < snd(sessions.[session]) do do! Async.Sleep 3600}
|> Async.Catch
|> Async.Ignore
|> Async.Start
"<meta http-equiv='refresh' content='0; url=/dynamic/MySchedule.html'>", Some(session)
else "<meta http-equiv='refresh' content='0; url=/SignUp_bad.html'>",None
with |_ -> "<meta http-equiv='refresh' content='0; url=/SignUp_bad.html'>",None
elif page="./HTML/dynamic/generate_timetable_form/index.html" then
try
let vars = Map.ofList vars
let parts = vars.["parts"]() |> int
let days = vars.["days"]() |> int
let subjects = vars.["subjects"]()|>int
let form =
[|
for i = 1 to days do
yield
[|for j = 1 to parts do yield sprintf "<td><input type='checkbox' value='timeoff' name='%i-%i'/></td>" i j|]
|> String.concat " "
|> sprintf "<tr><th scope='row'>Day %i</th>%s</tr>" i
|]
|> Array.append [|for i = 1 to parts do yield sprintf "<th>Part %i</th>" i|]
|> String.concat " "
|> sprintf "<table><th></th>%s</table>"
"<html>"+
"<head>" +
"<meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0' name='viewport' />" +
"<meta name='viewport' content='width=device-width' />" +
"<link href='/resources/assets/css/login.css' rel='stylesheet' />" +
"</head>" +
"<body class='content'>"+
"<div class='title'>Timetable generator</div>" +
"Select the days you have off:"+
"<form method='post' action='/dynamic/generate_timetable/'>"+
form +
(sprintf "<input type='hidden' name='parts' value='%i'/>" parts) +
(sprintf "<input type='hidden' name='days' value='%i'/>" days) +
(sprintf "<input type='hidden' name='subjects' value='%i'/>" subjects) +
"<br><br>"+
"Select the length of each part:" +
(
[|for i = 1 to parts do yield sprintf "<td><input type='number' value='timeoff' name='length-%i'/></td>" i|]
|> String.concat " "
|> sprintf "<table>%s<tr>%s</tr></table>" ([|for i = 1 to parts do yield sprintf "<th>Part %i</th>" i|]|>String.concat " ")
) +
"<br><br>"+
"Select the daily breaks you have (i.e. meals):" +
(
[|for i = 1 to parts do yield sprintf "<td><input type='checkbox' value='timeoff' name='%i'/></td>" i|]
|> String.concat " "
|> sprintf "<table>%s<tr>%s</tr></table>" ([|for i = 1 to parts do yield sprintf "<th>Part %i</th>" i|]|>String.concat " ")
) +
"<br><br>"+
"Type in your subjects, in order of difficulty/importance" +
(
[|for i = 1 to subjects do yield sprintf "<input type='textbox' placeholder='Subject %i' name='subject-%i'/>" i i|]
|> String.concat " "
) +
"<button type='submit'>Go!</button>" +
"</form>"+
"</body>" +
"</html>",None
with |_ -> "<meta http-equiv='refresh' content='0; url=/GenerateTimetableForm.html'>",None
elif page = "./HTML/dynamic/generate_timetable/index.html" then
try
let vars = Map.ofList vars
let days = vars.["days"]()|>int
let parts = vars.["parts"]()|>int
let n_subjects = vars.["subjects"]()|>int
let days_off =
[|
for day = 1 to days do
for part = 1 to parts do
let v = sprintf "%i-%i" day part
match vars.TryFind v with |Some(v) -> yield v |_ -> ()
|]
let routine =
[|
for part = 1 to parts do yield match string part |> vars.TryFind with |Some(v) -> Routine.Break|_ -> Routine.Work
|]
let subjects = [|for i = 1 to n_subjects do yield vars.[sprintf "subject-%i" i]()|]
let lengths = [|for i = 1 to parts do yield vars.[sprintf "length-%i" i]()|>int|]
let request =
{
days = days
subjects = subjects |> Array.mapi (fun i s -> {subject=s; difficultyPos=i})
tweakables = tweakables
routine = Array.zip routine lengths
}
let timetable = createTimetable request |> Option.get
[|
for (i,day) in Seq.indexed timetable do
for (part,j) in day do
yield
match part with
|Choice1Of2(subject) -> subject
|Choice2Of2(Routine.Break) -> "<i>Break</i>"
|Choice2Of2(Routine.Work) -> "<i>Free choice</i>"
|Choice2Of2(Routine.TimeOff) -> "<i>Time off</i>"
|_ -> raise (System.NotImplementedException())
|> Array.map(sprintf "<td>%s</td>")
|]
timetable
with |e -> sprintf "Something went wrong with generating the timetable. If you are trying to automate this, please check your code!<br>%A" (e.GetType()),None
else
let t =
if System.IO.File.Exists(page) then System.IO.File.ReadAllText(page)
else sprintf "Could not find dynamic page %s<br>Generated at %%time_generated%%" page
let d = ref false
let vars' =
vars
@
[
"verified_username", (fun () -> match session with |None -> "!no_user!" |Some(s) -> sessions.[s]|>fst)
]
List.fold(fun (acc:string) (key,elem:unit->string) ->
let key = "%"+key+"%"
if acc.Contains(key) then
let v = elem()
acc.Replace(key,v)
else acc
) t vars', None
PayloadVerb.GotPage,(System.Text.ASCIIEncoding.ASCII.GetBytes a),b
//let p = HtmlDocument.Load(page)
//for i in p.Descendants "dynamic" do
//for j in i.InnerText().Split(' ') do
//match j.ToLower() with
//|"date_generated" ->
[<EntryPoint>]
let main argv =
let c =
Array.zip
<| Array.map enum<Daemon> [|0..7|]
<| [|
for i = 0 to 7 do
let p = new System.IO.Pipes.NamedPipeServerStream("/tmp/" + Daemon.GetName(typeof<Daemon>,enum<Daemon> i) + "_Copelands_2016")
p.WaitForConnection()
yield p
|]
|> Map.ofArray
let p = c.[Daemon.Page]
flush p
while true do
let packet = read(p)
//Start the thread
//async{
try
let b = Array.zeroCreate MAX_PACKET_SIZE
let requestedpage',vars =
let payload = packet.payload
let m =
conversions
@
[
"payload_size",(fun() -> payload.Length|>string)
]
if packet.payloadverb = PayloadVerb.GotPost then
let s = Array.findIndex(function |0uy -> true |_ -> false) payload
let path = payload.[..s-1]
let a = payload.[s+1..]
let rec getall acc i j =
Array.tryFindIndex((=) 0uy) i
|> function
|None -> List.rev acc
|Some(s) -> let a,b = Array.splitAt s i in getall (a::acc) (b.[1..]) (j+s)
let a' = getall [] a 0 |> List.map (System.Text.ASCIIEncoding.ASCII.GetString) :> seq<_>
let e = a'.GetEnumerator()
System.Text.ASCIIEncoding.ASCII.GetString(path), m @
[while e.MoveNext() do
yield e.Current,(e.MoveNext()|>ignore;let v = e.Current in (fun () -> v))
]
else
//printfn "%A" payload
let p = System.Text.ASCIIEncoding.ASCII.GetString(payload)
let v = p.[..p.Length-2]
v, m
let requestedpage =
match Seq.tryFindIndex((=) '?') requestedpage' with
|Some(i) -> requestedpage'.[..i-1]
|_ -> requestedpage'
|> (fun v -> if v.EndsWith "/" then v+"index.html" else v)
//printfn "%A" (System.IO.Path.GetFullPath("./HTML/pages"+requestedpage))//System.IO.File.Exists("./pages"+requestedpage))
printfn "%s[%A]" requestedpage requestedpage.Length
let session = packet.session
let verb, response, setsession =
if requestedpage = "/" then
PayloadVerb.GotPage,(using(System.IO.File.OpenRead("./HTML/pages/index.html")) HtmlDocument.Load
|> string
|> System.Text.ASCIIEncoding.ASCII.GetBytes),None
elif requestedpage = "/favicon.ico" then
printfn "ICON"
PayloadVerb.GotFile,System.IO.File.ReadAllBytes("./HTML/resources/favicon.ico"), None
elif requestedpage.StartsWith("/command/") then
match requestedpage.Substring("/command/".Length).ToLower() with
|"reload" ->
//let p = System.Diagnostics.Process.Start("bash", "-c \"git -C ~/RevisionHub/Page/bin/Debug/HTML pull https://github.com/RevisionHub/HTML.git master\"");
PayloadVerb.GotFile, System.Text.ASCIIEncoding.ASCII.GetBytes "<html>Sorry Victor. I have locked the pages to preserve some vital edits I made. Send me an email if you need it!</html>", None
|_ -> PayloadVerb.Res404,[||], None
elif requestedpage.StartsWith("/dynamic/") then "./HTML"+requestedpage|> dynamic vars session
elif requestedpage.StartsWith("/resources/") then
if System.IO.File.Exists("./HTML/"+requestedpage) then
//printf "Getting..."
let v = PayloadVerb.GotFile,System.IO.File.ReadAllBytes("./HTML/"+requestedpage), None
//printfn " Got"
v
else
printfn "Res404: %A" requestedpage
PayloadVerb.Res404,[||], None
elif not(System.IO.File.Exists("./HTML/pages/"+requestedpage)) then
PayloadVerb.GotPage, (using(System.IO.File.OpenRead("./HTML/pages/404.html")) HtmlDocument.Load
|> string
|> System.Text.ASCIIEncoding.ASCII.GetBytes), None
else
PayloadVerb.GotPage,(using(System.IO.File.OpenRead("./HTML/pages/"+requestedpage)) HtmlDocument.Load
|> string
|> System.Text.ASCIIEncoding.ASCII.GetBytes), None
let r =
{
payloadsize = response.Length
payloadverb = verb
session = if setsession.IsSome then setsession else session
payload = response
}
write c.[Daemon.Backend] r
with
|e ->
let p = sprintf "Well done! You crashed the Page daemon!<br>%A" (e.GetType())|> System.Text.ASCIIEncoding.ASCII.GetBytes
write c.[Daemon.Backend] {payloadsize = p.Length; payloadverb = PayloadVerb.GotPage; session = None; payload = p}
//}
//|> Async.Start
0 // return an integer exit code