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