interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / core / hive-view / src / route.om

426 lines · 7938 bytes Raw Blame History
1note ==== route.om: POST /send from the HTML form, the route table and the per-connection loop
2note ---- POST /send: application/x-www-form-urlencoded fields s (session), k (form key), r (return
3note page: q = the overview, s = the session page), a (line, enter, escape) and t (the text). The
4note decoded fields are laid out as "NAME<LF>ACTION<LF>TEXT" for vwsendp (which checks the name,
5note the action, the text and the live session, types it and logs it); the browser is then sent
6note back to the page it came from with the result in the query (?m=CODE&a=ACTION&b=BYTES).
7global gfl ulong 8
8global gres ulong 3
9text tfns "s"
10text tfnk "k"
11text tfnr "r"
12text tfna "a"
13text tfnt "t"
14text tlocq "/?m="
15text tlocs "/s/"
16text tlocm "?m="
17text tloca "&a="
18text tlocb "&b="
19text tlocn "&s="
20note vwfdst / vwfcap: where field k is decoded inside buf (past the 16384-byte body) and its limit
21fn vwfdst k ulong to ulong
22if eq k 4
23ret 24576
24end
25ret add 20000 mul k 256
26end
27fn vwfcap k ulong to ulong
28if eq k 4
29ret 8192
30end
31if eq k 2
32ret 4
33end
34if eq k 3
35ret 8
36end
37ret 64
38end
39note vwfdec: percent-decode buf[p, q) ('+' is a space) to buf[d, ...); 1000000 when malformed or over cap
40fn vwfdec buf edit uchar 262144 p ulong q ulong d ulong cap ulong to ulong
41n = ulong 0
42i = p
43for it ulong 65536
44if ge i q
45ret n
46end
47if ge n cap
48ret 1000000
49end
50c = get buf i
51if eq c 43
52c = 32
53end
54if eq c 37
55if gt add i 3 q
56ret 1000000
57end
58h = hhexv get buf add i 1
59l = hhexv get buf add i 2
60if or gt h 15 gt l 15
61ret 1000000
62end
63c = uchar add mul h 16 l
64i = add i 2
65end
66put buf add d n c
67n = add n 1
68i = add i 1
69end
70ret 1000000
71end
72fn vwfidx buf edit uchar 262144 p ulong to ulong
73c = get buf p
74if eq c get tfns 0
75ret 0
76end
77if eq c get tfnk 0
78ret 1
79end
80if eq c get tfnr 0
81ret 2
82end
83if eq c get tfna 0
84ret 3
85end
86if eq c get tfnt 0
87ret 4
88end
89ret 9
90end
91note vwfparse: every field is one of s k r a t, present at most once, and decodes cleanly
92fn vwfparse buf edit uchar 262144 n ulong to bool
93for i ulong 8
94put gfl i 0
95end
96p = ulong 0
97for it ulong 16
98if ge p n
99ret true
100end
101q = n
102for i ulong sub n p
103if eq get buf add p i 38
104q = add p i
105break
106end
107end
108if or lt sub q p 2 ne get buf add p 1 61
109ret false
110end
111k = vwfidx buf p
112if gt k 4
113ret false
114end
115if ne get gfl k 0
116ret false
117end
118l = vwfdec buf add p 2 q vwfdst k vwfcap k
119if ge l 1000000
120ret false
121end
122put gfl k add l 1
123p = add q 1
124end
125ret false
126end
127note vwfkey: field k is exactly the form key (compared in time independent of the contents)
128fn vwfkey buf edit uchar 262144 to bool
129if ne get gfl 1 65
130ret false
131end
132vwcsrfk
133d = uchar 0
134b = vwfdst 1
135for i ulong 64
136d = or d xor get buf add b i get gcsrf i
137end
138ret eq d 0
139end
140fn vwfis buf edit uchar 262144 k ulong t view uchar * to bool
141if ne get gfl k add len t 1
142ret false
143end
144b = vwfdst k
145for i ulong len t
146if ne get buf add b i get t i
147ret false
148end
149end
150ret true
151end
152note vwfact: 0 line, 1 enter, 2 escape, 9 anything else
153fn vwfact buf edit uchar 262144 to ulong
154if vwfis buf 3 taline
155ret 0
156end
157if vwfis buf 3 taenter
158ret 1
159end
160if vwfis buf 3 taesc
161ret 2
162end
163ret 9
164end
165note vwfbuild: "NAME<LF>ACTION<LF>TEXT" at buf[0, ...) from the decoded fields; the text is kept
166note only for a line (the Enter and Escape buttons ignore whatever is in the box). Returns its length.
167fn vwfbuild buf edit uchar 262144 act ulong to ulong
168m = ulong 0
169ns = vwfdst 0
170nl = sub hmax get gfl 0 1 1
171for i ulong nl
172put buf m get buf add ns i
173m = add m 1
174end
175put buf m 10
176m = add m 1
177as = vwfdst 3
178al = sub hmax get gfl 3 1 1
179for i ulong al
180put buf m get buf add as i
181m = add m 1
182end
183put buf m 10
184m = add m 1
185if eq act 0
186ts = vwfdst 4
187tl = sub hmax get gfl 4 1 1
188for i ulong tl
189put buf m get buf add ts i
190m = add m 1
191end
192end
193ret m
194end
195fn vwnameb buf edit uchar 262144 to bool
196n = sub hmax get gfl 0 1 1
197if or eq n 0 gt n 63
198ret false
199end
200b = vwfdst 0
201for i ulong n
202if not vwnamec get buf add b i
203ret false
204end
205end
206ret true
207end
208fn vwlc2 loc edit uchar 256 n ulong t view uchar * to ulong
209k = n
210for i ulong len t
211if lt k 256
212put loc k get t i
213k = add k 1
214end
215end
216ret k
217end
218fn vwld2 loc edit uchar 256 n ulong v ulong to ulong
219d = new uchar 20 0
220k = ulong 0
221x = v
222for it ulong 20
223put d k uchar add rem x 10 48
224k = add k 1
225x = div x 10
226if eq x 0
227break
228end
229end
230m = n
231for i ulong k
232if lt m 256
233put loc m get d sub sub k 1 i
234m = add m 1
235end
236end
237ret m
238end
239fn vwlname loc edit uchar 256 n ulong buf edit uchar 262144 to ulong
240m = n
241b = vwfdst 0
242for i ulong sub hmax get gfl 0 1 1
243if lt m 256
244put loc m get buf add b i
245m = add m 1
246end
247end
248ret m
249end
250fn vwsendf cs edit ulong 320 io edit uchar 131072 buf edit uchar 262144 to long
251if not vworigin cs io
252ret vwtext cs io 403 tnoorig
253end
254n = hbodyall cs io buf
255if lt n 0
256if eq n HEBIG
257ret vwtext cs io 413 tbigbody
258end
259ret vwtext cs io 400 tbadbody
260end
261ok = vwfparse buf ulong n
262if and ok not vwfkey buf
263ret vwtext cs io 403 tbadkey
264end
265code = ulong 1
266act = ulong 9
267nb = ulong 0
268if ok
269act = vwfact buf
270if eq act 0
271nb = sub hmax get gfl 4 1 1
272end
273code = vwsendp buf vwfbuild buf act
274end
275put gres 0 code
276put gres 1 act
277put gres 2 nb
278ret vwredir cs io buf ok
279end
280note vwredir: 302 back to the page the form was on, with gres (code, action, bytes) in the query
281fn vwredir cs edit ulong 320 io edit uchar 131072 buf edit uchar 262144 ok bool to long
282okn = false
283tos = false
284if ok
285okn = vwnameb buf
286tos = vwfis buf 2 tfns
287end
288code = get gres 0
289act = get gres 1
290nb = get gres 2
291loc = new uchar 256 0
292k = ulong 0
293if and okn tos
294k = vwlc2 loc k tlocs
295k = vwlname loc k buf
296k = vwlc2 loc k tlocm
297else
298k = vwlc2 loc k tlocq
299end
300k = vwld2 loc k code
301if lt act 3
302k = vwlc2 loc k tloca
303k = vwld2 loc k act
304end
305k = vwlc2 loc k tlocb
306k = vwld2 loc k nb
307if okn
308k = vwlc2 loc k tlocn
309k = vwlname loc k buf
310end
311r = hrspst cs io 302
312r = hfe r vwsech cs io
313r = hfe r hhname cs io tloc
314r = hfe r hhval cs io view part loc 0 k
315r = hfe r hhend cs io 0 0
316ret hfe r hwfin cs io
317end
318note ---- routing
319note vwlogq: the request target is "/?t=..." (the one-time link), not a result query like "/?m=0"
320fn vwlogq cs edit ulong 320 io edit uchar 131072 to bool
321tg = get cs HTOFF
322tl = get cs HTLEN
323if lt tl 3
324ret false
325end
326ret and eq get io add tg 1 63 eq get io add tg 2 116
327end
328fn vwpanel cs edit ulong 320 io edit uchar 131072 buf edit uchar 262144 to long
329if hispath cs io tstatp
330ret vwpstat cs io buf
331end
332if hispath cs io tpane
333ret vwppane cs io buf
334end
335if hispath cs io ttiles
336ret vwptiles cs io buf
337end
338if hispath cs io thermp
339ret vwphermes cs io buf
340end
341if hispath cs io tmachp
342ret vwpmach cs io buf
343end
344if hispath cs io tboardp
345ret vwpboard cs io buf
346end
347if hispath cs io tentp
348ret vwpents cs io buf
349end
350if vwsname cs io
351ret vwpsess cs io buf
352end
353ret vwtext cs io 404 tnotfnd
354end
355fn vwroute cs edit ulong 320 io edit uchar 131072 buf edit uchar 262144 to long
356if not vwhostok cs io
357ret vwtext cs io 400 tbadhost
358end
359isget = hismeth cs io tget
360if hispath cs io tphealth
361ret vwtext cs io 200 tok200
362end
363if hispath cs io tpsend
364if not hismeth cs io tpost
365ret vwtext cs io 405 tmeth
366end
367if not vwauth cs io
368ret vwtext cs io 403 tnoauth
369end
370ret vwsendf cs io buf
371end
372if not isget
373ret vwtext cs io 405 tmeth
374end
375if hispath cs io tpcss
376ret vwcss cs io buf
377end
378if hispath cs io tpalp
379ret vwpal cs io buf
380end
381if hispath cs io troot
382if vwlogq cs io
383ret vwlogin cs io
384end
385if not vwauth cs io
386ret vwpsignout cs io buf
387end
388ret vwptop cs io buf
389end
390if not vwauth cs io
391ret vwpsignout cs io buf
392end
393ret vwpanel cs io buf
394end
395fn vwconn c long to void
396cs = new ulong 320 0
397io = new uchar 131072 0
398buf = new uchar 262144 0
399r = hinit cs io int c 1
400if lt r 0
401x = hclose cs
402ret
403end
404hsettmo cs 30000000000 10000000000
405hsetbdl cs 10000000000
406note the only request body hive-view accepts is the 16 KiB send form; a larger declared
407note Content-Length is refused with 413 as soon as the head is read
408put cs HBCAP 16384
409for k ulong 1000
410r = hreqrd cs io
411if le r 0
412if lt r 0
413x = hreject cs io r
414end
415break
416end
417r = vwroute cs io buf
418if lt r 0
419break
420end
421if eq hnext cs io 0
422break
423end
424end
425x = hclose cs
426end