interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / server / main.om
| 1 | note forged server/main.om: listen on 127.0.0.1:18480, read each request into the contract |
| 2 | note globals, dispatch (route.om), write the response (layout for kind 0), log one access line. |
| 3 | note Reload: between connections forged re-reads the allow-list and the social log when the |
| 4 | note RIDS.md mtime changes or the flag file ~/forge-data/reload exists (run.sh turns SIGHUP into it). |
| 5 | note Restart: when the flag file ~/forge-data/restart exists, forged removes it and exits 0 after the |
| 6 | note connection it is serving; the supervisor, which holds the listener, then starts the next forged |
| 7 | note on the same socket. A flag left from an earlier run is removed at start. |
| 8 | text tfget "GET" |
| 9 | text tfheadm "HEAD" |
| 10 | text tfpost "POST" |
| 11 | text tfhxb "x-forge-base" |
| 12 | text tfhck "cookie" |
| 13 | text tfhrip "x-real-ip" |
| 14 | text tfhinm "if-none-match" |
| 15 | text tfhorg "origin" |
| 16 | text tfhua "user-agent" |
| 17 | text tfhrf "referer" |
| 18 | text tfhacc "accept" |
| 19 | text tfforge "/forge/" |
| 20 | text tflocal "127.0.0.1" |
| 21 | text tfrlflag "reload" |
| 22 | text tfrsflag "restart" |
| 23 | text tfrsreq "forged: restart requested by the supervisor; exiting between connections\n" |
| 24 | text tfnctype "Content-Type" |
| 25 | text tfnloc "Location" |
| 26 | text tfncd "Content-Disposition" |
| 27 | text tfnsck "Set-Cookie" |
| 28 | text tfnetag "ETag" |
| 29 | text tfncc "Cache-Control" |
| 30 | text tfncsp "Content-Security-Policy" |
| 31 | text tfnxcto "X-Content-Type-Options" |
| 32 | text tfnref "Referrer-Policy" |
| 33 | text tfcsp "default-src 'none'; style-src 'self'; img-src 'self'; script-src 'none'; form-action 'self'; frame-ancestors 'none'; base-uri 'none'" |
| 34 | text tfnosnf "nosniff" |
| 35 | text tfsameo "same-origin" |
| 36 | text tfnostor "no-store" |
| 37 | text tfnaxp "X-Accel-Expires" |
| 38 | text tfezero "0" |
| 39 | global fepnoc ulong 1 |
| 40 | text tfcspsb "sandbox; default-src 'none'; script-src 'none'; img-src 'self'; style-src 'self'; frame-ancestors 'none'; base-uri 'none'; form-action 'none'" |
| 41 | text tfnra "Retry-After" |
| 42 | text tfhtml "text/html; charset=utf-8" |
| 43 | text tfsplain "text/plain; charset=utf-8" |
| 44 | text tfoctet "application/octet-stream" |
| 45 | text tfstart "forged: listening on 127.0.0.1:18480, public repos " |
| 46 | text tfnolist "forged: cannot listen on 127.0.0.1:18480\n" |
| 47 | text tfinharg "--inherited-listener" |
| 48 | text tfinhok "forged: serving the listener inherited from the supervisor on descriptor 0 (checked: IPv4 stream, 127.0.0.1, compiled port)\n" |
| 49 | text tfinhbad "forged: refusing the inherited listener: descriptor 0 is not the IPv4 stream listener on 127.0.0.1 and the compiled port\n" |
| 50 | text tfrld "forged: reloaded, public repos " |
| 51 | text tfnl "\n" |
| 52 | note ---- request capture |
| 53 | fn fhcopy cs edit ulong 320 io edit uchar 131072 nm view uchar * w ulong to bool |
| 54 | i = hhfind cs io nm |
| 55 | if lt i 0 |
| 56 | ret false |
| 57 | end |
| 58 | o = hhvoff cs ulong i |
| 59 | l = hhvlen cs ulong i |
| 60 | for k ulong hmin l 4096 bound 4096 |
| 61 | fhput w k get io add o k |
| 62 | end |
| 63 | fhlen w l |
| 64 | ret true |
| 65 | end |
| 66 | note fhput / fhlen: w 0 base, 1 cookie, 2 ip, 3 if-none-match, 4 origin, 5 user agent, 6 referrer, 7 accept |
| 67 | note (silently capped; 5 and 6 at 200 bytes, they are only logged) |
| 68 | fn fhput w ulong k ulong c uchar to void |
| 69 | if and eq w 0 lt k 64 |
| 70 | put fbase k c |
| 71 | end |
| 72 | if and eq w 1 lt k 4096 |
| 73 | put fck k c |
| 74 | end |
| 75 | if and eq w 2 lt k 64 |
| 76 | put fip k c |
| 77 | end |
| 78 | if and eq w 3 lt k 256 |
| 79 | put finm k c |
| 80 | end |
| 81 | if and eq w 4 lt k 256 |
| 82 | put forig k c |
| 83 | end |
| 84 | if and eq w 5 lt k 200 |
| 85 | put fag k c |
| 86 | end |
| 87 | if and eq w 6 lt k 200 |
| 88 | put frf k c |
| 89 | end |
| 90 | if and eq w 7 lt k 512 |
| 91 | put facc k c |
| 92 | end |
| 93 | end |
| 94 | fn fhlen w ulong l ulong to void |
| 95 | if eq w 0 |
| 96 | put fbasel 0 hmin l 64 |
| 97 | end |
| 98 | if eq w 1 |
| 99 | put fckl 0 hmin l 4096 |
| 100 | end |
| 101 | if eq w 2 |
| 102 | put fipl 0 hmin l 64 |
| 103 | end |
| 104 | if eq w 3 |
| 105 | put finml 0 hmin l 256 |
| 106 | end |
| 107 | if eq w 4 |
| 108 | put forigl 0 hmin l 256 |
| 109 | end |
| 110 | if eq w 5 |
| 111 | put fagl 0 hmin l 200 |
| 112 | end |
| 113 | if eq w 6 |
| 114 | put frfl 0 hmin l 200 |
| 115 | end |
| 116 | if eq w 7 |
| 117 | put faccl 0 hmin l 512 |
| 118 | end |
| 119 | end |
| 120 | note fbasefix: only "/" and "/forge/" are accepted; anything else becomes "/" |
| 121 | fn fbasefix to void |
| 122 | ok = false |
| 123 | if eq get fbasel 0 7 |
| 124 | ok = true |
| 125 | for i ulong 7 |
| 126 | if ne get fbase i get tfforge i |
| 127 | ok = false |
| 128 | end |
| 129 | end |
| 130 | end |
| 131 | if not ok |
| 132 | put fbase 0 47 |
| 133 | put fbasel 0 1 |
| 134 | end |
| 135 | end |
| 136 | fn freqhdr cs edit ulong 320 io edit uchar 131072 to void |
| 137 | put fbasel 0 0 |
| 138 | put fckl 0 0 |
| 139 | put fipl 0 0 |
| 140 | put finml 0 0 |
| 141 | put forigl 0 0 |
| 142 | put fagl 0 0 |
| 143 | put frfl 0 0 |
| 144 | put faccl 0 0 |
| 145 | x = fhcopy cs io tfhxb 0 |
| 146 | x = fhcopy cs io tfhck 1 |
| 147 | x = fhcopy cs io tfhrip 2 |
| 148 | x = fhcopy cs io tfhinm 3 |
| 149 | x = fhcopy cs io tfhorg 4 |
| 150 | x = fhcopy cs io tfhua 5 |
| 151 | x = fhcopy cs io tfhrf 6 |
| 152 | x = fhcopy cs io tfhacc 7 |
| 153 | fbasefix |
| 154 | if eq get fipl 0 0 |
| 155 | for i ulong len tflocal |
| 156 | put fip i get tflocal i |
| 157 | end |
| 158 | put fipl 0 len tflocal |
| 159 | end |
| 160 | if gt get fckl 0 4095 |
| 161 | put fckl 0 0 |
| 162 | end |
| 163 | end |
| 164 | note freqtgt: split the request target into fpath and fqs; false when it is not origin-form or too long |
| 165 | fn freqtgt cs edit ulong 320 io edit uchar 131072 to bool |
| 166 | o = get cs HTOFF |
| 167 | l = get cs HTLEN |
| 168 | put fpathl 0 0 |
| 169 | put fqsl 0 0 |
| 170 | if or eq l 0 gt l 8000 |
| 171 | ret false |
| 172 | end |
| 173 | if ne get io o 47 |
| 174 | ret false |
| 175 | end |
| 176 | q = l |
| 177 | for i ulong l bound 8192 |
| 178 | if eq get io add o i 63 |
| 179 | q = i |
| 180 | break |
| 181 | end |
| 182 | end |
| 183 | if gt q 4095 |
| 184 | ret false |
| 185 | end |
| 186 | for i ulong q bound 4096 |
| 187 | put fpath i get io add o i |
| 188 | end |
| 189 | put fpathl 0 q |
| 190 | if lt q l |
| 191 | r = sub sub l q 1 |
| 192 | if gt r 4095 |
| 193 | ret false |
| 194 | end |
| 195 | for i ulong r bound 4096 |
| 196 | put fqs i get io add add o q add 1 i |
| 197 | end |
| 198 | put fqsl 0 r |
| 199 | end |
| 200 | ret true |
| 201 | end |
| 202 | fn freqmeth cs edit ulong 320 io edit uchar 131072 to void |
| 203 | put fmeth 0 0 |
| 204 | if or hismeth cs io tfget hismeth cs io tfheadm |
| 205 | put fmeth 0 1 |
| 206 | end |
| 207 | if hismeth cs io tfpost |
| 208 | put fmeth 0 3 |
| 209 | end |
| 210 | end |
| 211 | note freqbody: read a POST body (cap 64 KiB) into fbody; returns 0 or the status to answer. The one |
| 212 | note path with a larger body, .../patches/new, goes through freqbig into fbig (cap 3 MiB). |
| 213 | fn freqbody cs edit ulong 320 io edit uchar 131072 bd edit uchar 65536 to ulong |
| 214 | put fbodyl 0 0 |
| 215 | put fbigl 0 0 |
| 216 | if ne get fmeth 0 3 |
| 217 | ret 0 |
| 218 | end |
| 219 | if fbigrt |
| 220 | ret freqbig cs io bd |
| 221 | end |
| 222 | put cs HBCAP 65536 |
| 223 | n = hbodyall cs io bd |
| 224 | if lt n 0 |
| 225 | if eq n HEBIG |
| 226 | ret 413 |
| 227 | end |
| 228 | ret 400 |
| 229 | end |
| 230 | for i ulong ulong n bound 65536 |
| 231 | put fbody i get bd i |
| 232 | end |
| 233 | put fbodyl 0 ulong n |
| 234 | ret 0 |
| 235 | end |
| 236 | text tfpnew "/patches/new" |
| 237 | note fbigrt: reserve the larger body only for /<owner>/<repo>/patches/new |
| 238 | fn fbigrt to bool |
| 239 | n = get fpathl 0 |
| 240 | m = len tfpnew |
| 241 | if or lt n add m 4 ne get fpath 0 47 |
| 242 | ret false |
| 243 | end |
| 244 | start = sub n m |
| 245 | e1 = fsegend 1 |
| 246 | rstart = add e1 1 |
| 247 | if or le e1 1 ge rstart start |
| 248 | ret false |
| 249 | end |
| 250 | if ne fsegend rstart start |
| 251 | ret false |
| 252 | end |
| 253 | for i ulong m bound 16 |
| 254 | if ne get fpath add start i get tfpnew i |
| 255 | ret false |
| 256 | end |
| 257 | end |
| 258 | ret true |
| 259 | end |
| 260 | note freqbig: the body into fbig through the 64 KiB chunk bd; 413 when it passes 3 MiB |
| 261 | fn freqbig cs edit ulong 320 io edit uchar 131072 bd edit uchar 65536 to ulong |
| 262 | put cs HBCAP 3145728 |
| 263 | got = ulong 0 |
| 264 | for it ulong 4194304 |
| 265 | r = hbodyrd cs io bd |
| 266 | if lt r 0 |
| 267 | if eq r HEBIG |
| 268 | ret 413 |
| 269 | end |
| 270 | ret 400 |
| 271 | end |
| 272 | if eq r 0 |
| 273 | put fbigl 0 got |
| 274 | ret 0 |
| 275 | end |
| 276 | if gt add got ulong r len fbig |
| 277 | put cs HKEEP 0 |
| 278 | ret 413 |
| 279 | end |
| 280 | for i ulong ulong r bound 65536 |
| 281 | put fbig add got i get bd i |
| 282 | end |
| 283 | got = add got ulong r |
| 284 | end |
| 285 | put cs HKEEP 0 |
| 286 | ret 413 |
| 287 | end |
| 288 | note Opt mutable application pages out of the shared edge cache. |
| 289 | fn fsetepno to void |
| 290 | put fepnoc 0 1 |
| 291 | end |
| 292 | fn fresetr to void |
| 293 | put fobl 0 0 |
| 294 | put fobtr 0 0 |
| 295 | put ftxmore 0 0 |
| 296 | put fovf 0 0 |
| 297 | put ficut 0 0 |
| 298 | put fkind 0 0 |
| 299 | put fctl 0 0 |
| 300 | put fcdl 0 0 |
| 301 | put fsckl 0 0 |
| 302 | put flocl 0 0 |
| 303 | put ftil 0 0 |
| 304 | put ffilel 0 0 |
| 305 | put fetagl 0 0 |
| 306 | put fccl 0 0 |
| 307 | put fuserl 0 0 |
| 308 | put fcsrfl 0 0 |
| 309 | put fql 0 0 |
| 310 | put fridx 0 9999 |
| 311 | put fridl 0 0 |
| 312 | put fownl 0 0 |
| 313 | put frepol 0 0 |
| 314 | put frestl 0 0 |
| 315 | put fretry 0 0 |
| 316 | put fcspsb 0 0 |
| 317 | put fepnoc 0 0 |
| 318 | put fmvng 0 0 |
| 319 | put fjalt 0 0 |
| 320 | end |
| 321 | note ---- response writing |
| 322 | note fgv: copy response header global w into d; returns the length. |
| 323 | note w 0 fct, 1 floc, 2 fcd, 3 fsck, 4 fetag, 5 fcc |
| 324 | fn fgvb w ulong k ulong to uchar |
| 325 | if eq w 0 |
| 326 | ret get fct k |
| 327 | end |
| 328 | if eq w 1 |
| 329 | ret get floc k |
| 330 | end |
| 331 | if eq w 2 |
| 332 | ret get fcd k |
| 333 | end |
| 334 | if eq w 3 |
| 335 | ret get fsck k |
| 336 | end |
| 337 | if eq w 4 |
| 338 | ret get fetag k |
| 339 | end |
| 340 | ret get fcc k |
| 341 | end |
| 342 | fn fgvl w ulong to ulong |
| 343 | if eq w 0 |
| 344 | ret get fctl 0 |
| 345 | end |
| 346 | if eq w 1 |
| 347 | ret get flocl 0 |
| 348 | end |
| 349 | if eq w 2 |
| 350 | ret get fcdl 0 |
| 351 | end |
| 352 | if eq w 3 |
| 353 | ret get fsckl 0 |
| 354 | end |
| 355 | if eq w 4 |
| 356 | ret get fetagl 0 |
| 357 | end |
| 358 | ret get fccl 0 |
| 359 | end |
| 360 | fn fhdrg cs edit ulong 320 io edit uchar 131072 nm view uchar * w ulong to long |
| 361 | n = fgvl w |
| 362 | if eq n 0 |
| 363 | ret 0 |
| 364 | end |
| 365 | d = new uchar 4096 0 |
| 366 | for k ulong n bound 4096 |
| 367 | put d k fgvb w k |
| 368 | end |
| 369 | r = hhname cs io nm |
| 370 | ret hfe r hhval cs io view part d 0 n |
| 371 | end |
| 372 | fn fhdrt cs edit ulong 320 io edit uchar 131072 nm view uchar * v view uchar * to long |
| 373 | r = hhname cs io nm |
| 374 | ret hfe r hhval cs io v |
| 375 | end |
| 376 | fn fsechdr cs edit ulong 320 io edit uchar 131072 to long |
| 377 | r = long 0 |
| 378 | if eq get fcspsb 0 0 |
| 379 | r = fhdrt cs io tfncsp tfcsp |
| 380 | else |
| 381 | r = fhdrt cs io tfncsp tfcspsb |
| 382 | end |
| 383 | r = hfe r fhdrt cs io tfnxcto tfnosnf |
| 384 | r = hfe r fhdrt cs io tfnref tfsameo |
| 385 | r = hfe r fhdrg cs io tfnsck 3 |
| 386 | if ne get fepnoc 0 0 |
| 387 | r = hfe r fhdrt cs io tfnaxp tfezero |
| 388 | end |
| 389 | if ne get fretry 0 0 |
| 390 | r = hfe r hhname cs io tfnra |
| 391 | r = hfe r hhnum cs io get fretry 0 |
| 392 | end |
| 393 | ret r |
| 394 | end |
| 395 | fn fcchdr cs edit ulong 320 io edit uchar 131072 to long |
| 396 | if eq get fccl 0 0 |
| 397 | ret fhdrt cs io tfncc tfnostor |
| 398 | end |
| 399 | ret fhdrg cs io tfncc 5 |
| 400 | end |
| 401 | fn fobcp ch edit uchar 32768 b ulong m ulong to void |
| 402 | for k ulong m bound 32768 |
| 403 | put ch k get fob add b k |
| 404 | end |
| 405 | end |
| 406 | note fsendr: send fob[a, a+n) through a 32 KiB chunk |
| 407 | fn fsendr cs edit ulong 320 io edit uchar 131072 a ulong n ulong to long |
| 408 | ch = new uchar 32768 0 |
| 409 | p = ulong 0 |
| 410 | r = long 0 |
| 411 | for it ulong 1000 |
| 412 | if ge p n |
| 413 | break |
| 414 | end |
| 415 | m = hmin sub n p 32768 |
| 416 | fobcp ch add a p m |
| 417 | r = hfe r hput cs io view part ch 0 m |
| 418 | if lt r 0 |
| 419 | ret r |
| 420 | end |
| 421 | p = add p m |
| 422 | end |
| 423 | ret r |
| 424 | end |
| 425 | note kind 0: the fragment is fob[0,L); flayout (server/pages.om, called by fsend) appended fhead and |
| 426 | note ffoot after it; they are sent around it. Each part goes out through ftrrun (server/tr.om), which |
| 427 | note adds the translation hints; a first pass counts the bytes for Content-Length. With the query |
| 428 | note format=text the page's fragment is sent as plain text instead (ftxsend, server/tr.om). |
| 429 | fn fsend0 cs edit ulong 320 io edit uchar 131072 st ulong to long |
| 430 | l = get flay 0 |
| 431 | lh = add l get flay 1 |
| 432 | lf = add lh get flay 2 |
| 433 | if ftxwant |
| 434 | ret ftxsend cs io st l |
| 435 | end |
| 436 | n = ulong ftrrun cs io l lh 0 |
| 437 | n = add n ulong ftrrun cs io 0 l 0 |
| 438 | n = add n ulong ftrrun cs io lh lf 0 |
| 439 | r = hrspst cs io st |
| 440 | r = hfe r fhdrt cs io tfnctype tfhtml |
| 441 | r = hfe r fsechdr cs io |
| 442 | r = hfe r fhdrt cs io tfncc tfnostor |
| 443 | r = hfe r hhend cs io 0 n |
| 444 | r = hfe r ftrrun cs io l lh 1 |
| 445 | r = hfe r ftrrun cs io 0 l 1 |
| 446 | r = hfe r ftrrun cs io lh lf 1 |
| 447 | ret hfe r hwfin cs io |
| 448 | end |
| 449 | fn fsend1 cs edit ulong 320 io edit uchar 131072 st ulong to long |
| 450 | r = hrspst cs io st |
| 451 | if ne st 304 |
| 452 | if eq get fctl 0 0 |
| 453 | fsetct tfoctet |
| 454 | end |
| 455 | r = hfe r fhdrg cs io tfnctype 0 |
| 456 | end |
| 457 | r = hfe r fsechdr cs io |
| 458 | r = hfe r fhdrg cs io tfncd 2 |
| 459 | r = hfe r fhdrg cs io tfnetag 4 |
| 460 | r = hfe r fcchdr cs io |
| 461 | if eq st 304 |
| 462 | r = hfe r hhend cs io 0 0 |
| 463 | ret hfe r hwfin cs io |
| 464 | end |
| 465 | r = hfe r hhend cs io 0 get fobl 0 |
| 466 | r = hfe r fsendr cs io 0 get fobl 0 |
| 467 | ret hfe r hwfin cs io |
| 468 | end |
| 469 | fn fsend2 cs edit ulong 320 io edit uchar 131072 st ulong to long |
| 470 | r = hrspst cs io st |
| 471 | r = hfe r fhdrg cs io tfnloc 1 |
| 472 | r = hfe r fsechdr cs io |
| 473 | r = hfe r fhdrt cs io tfncc tfnostor |
| 474 | r = hfe r hhend cs io 0 0 |
| 475 | ret hfe r hwfin cs io |
| 476 | end |
| 477 | note kind 3: stream ffile (cap 512 MiB); a missing file is 404 |
| 478 | fn fsend3 cs edit ulong 320 io edit uchar 131072 st ulong to long |
| 479 | fd = fopen ffile 0 get ffilel 0 524288 0 |
| 480 | if lt fd 0 |
| 481 | put fkind 0 0 |
| 482 | put fobl 0 0 |
| 483 | ret fsend cs io 404 |
| 484 | end |
| 485 | r = fstat int fd fstb 0 128 |
| 486 | sz = ulong 0 |
| 487 | for i ulong 8 |
| 488 | sz = or sz shl ulong get fstb add 48 i mul i 8 |
| 489 | end |
| 490 | if or lt r 0 gt sz 536870912 |
| 491 | x = fclose int fd |
| 492 | put fkind 0 0 |
| 493 | put fobl 0 0 |
| 494 | ret fsend cs io 500 |
| 495 | end |
| 496 | r = fsend3h cs io st sz |
| 497 | r = hfe r fsend3b cs io int fd sz |
| 498 | x = fclose int fd |
| 499 | ret hfe r hwfin cs io |
| 500 | end |
| 501 | fn fsend3h cs edit ulong 320 io edit uchar 131072 st ulong sz ulong to long |
| 502 | r = hrspst cs io st |
| 503 | if eq get fctl 0 0 |
| 504 | fsetct tfoctet |
| 505 | end |
| 506 | r = hfe r fhdrg cs io tfnctype 0 |
| 507 | r = hfe r fsechdr cs io |
| 508 | r = hfe r fhdrg cs io tfncd 2 |
| 509 | r = hfe r fhdrg cs io tfnetag 4 |
| 510 | r = hfe r fcchdr cs io |
| 511 | ret hfe r hhend cs io 0 sz |
| 512 | end |
| 513 | note fsend3b: the response deadline (60 s, armed by fsend) moves out by 1 s per 128 KiB sent, so a |
| 514 | note client reading slower than 128 KiB/s on average is cut off (P2-1: one connection at a time) |
| 515 | fn fsendl cs edit ulong 320 t0 ulong p ulong to void |
| 516 | s = mul div p 131072 1000000000 |
| 517 | put cs HDL add t0 add 60000000000 s |
| 518 | end |
| 519 | fn fsend3b cs edit ulong 320 io edit uchar 131072 fd int sz ulong to long |
| 520 | ch = new uchar 65536 0 |
| 521 | p = ulong 0 |
| 522 | t0 = ulong monons |
| 523 | for it ulong 100000 |
| 524 | if ge p sz |
| 525 | ret 0 |
| 526 | end |
| 527 | n = read fd ch 0 65536 |
| 528 | if le n 0 |
| 529 | put cs HKEEP 0 |
| 530 | ret -5 |
| 531 | end |
| 532 | m = hmin ulong n sub sz p |
| 533 | r = hput cs io view part ch 0 m |
| 534 | if lt r 0 |
| 535 | ret r |
| 536 | end |
| 537 | p = add p m |
| 538 | fsendl cs t0 p |
| 539 | end |
| 540 | ret 0 |
| 541 | end |
| 542 | note fsend4: kind 4, a body too large for the capture: run the argv the handler left in fpk and send |
| 543 | note its stdout as a chunked body (git output above fxo's 16 MiB, e.g. the .diff of a huge commit). |
| 544 | note The handler ran it once already (status known); a failure after the head closes the connection |
| 545 | note without the last chunk, so the client sees a cut body, never a silently short one. |
| 546 | fn fsend4 cs edit ulong 320 io edit uchar 131072 st ulong to long |
| 547 | r = pipefds fpfd 0 8 524288 |
| 548 | if lt r 0 |
| 549 | put fkind 0 0 |
| 550 | ret fsend cs io 500 |
| 551 | end |
| 552 | rfd = fpfdv 0 |
| 553 | wfd = fpfdv 1 |
| 554 | pid = fork |
| 555 | if lt pid 0 |
| 556 | x = fclose rfd |
| 557 | x = fclose wfd |
| 558 | put fkind 0 0 |
| 559 | ret fsend cs io 500 |
| 560 | end |
| 561 | if eq pid 0 |
| 562 | x = fclose rfd |
| 563 | fchild wfd |
| 564 | exit 127 |
| 565 | end |
| 566 | x = fclose wfd |
| 567 | dl = add long monons 120000000000 |
| 568 | r = hrspst cs io st |
| 569 | r = hfe r fhdrg cs io tfnctype 0 |
| 570 | r = hfe r fsechdr cs io |
| 571 | r = hfe r fcchdr cs io |
| 572 | r = hfe r hhend cs io 1 0 |
| 573 | if ge r 0 |
| 574 | r = fsend4b cs io rfd dl |
| 575 | end |
| 576 | x = fclose rfd |
| 577 | if lt r 0 |
| 578 | x = killpid int pid 9 |
| 579 | end |
| 580 | s = freap int pid dl |
| 581 | if and ge r 0 ne s 0 |
| 582 | r = -5 |
| 583 | end |
| 584 | if lt r 0 |
| 585 | put cs HKEEP 0 |
| 586 | ret r |
| 587 | end |
| 588 | ret hwfin cs io |
| 589 | end |
| 590 | fn fsend4b cs edit ulong 320 io edit uchar 131072 fd int dl long to long |
| 591 | ch = new uchar 65536 0 |
| 592 | p = ulong 0 |
| 593 | t0 = ulong monons |
| 594 | for it ulong 100000000 |
| 595 | left = sub dl long monons |
| 596 | if le left 0 |
| 597 | ret -5 |
| 598 | end |
| 599 | w = waitio fd 1 left |
| 600 | if lt w 0 |
| 601 | if ne w -4 |
| 602 | ret -5 |
| 603 | end |
| 604 | continue |
| 605 | end |
| 606 | if eq w 0 |
| 607 | continue |
| 608 | end |
| 609 | n = read fd ch 0 65536 |
| 610 | if eq n 0 |
| 611 | ret 0 |
| 612 | end |
| 613 | if lt n 0 |
| 614 | if or eq n -4 eq n -11 |
| 615 | continue |
| 616 | end |
| 617 | ret -5 |
| 618 | end |
| 619 | r = hput cs io view part ch 0 ulong n |
| 620 | if lt r 0 |
| 621 | ret r |
| 622 | end |
| 623 | p = add p ulong n |
| 624 | fsendl cs t0 p |
| 625 | end |
| 626 | ret -5 |
| 627 | end |
| 628 | note kind 5: a generated shelf, streamed in bounded batches from ohbody (open/route.om). |
| 629 | note Neither HTML nor JSON has a whole-response buffer or length limit. hhend mode 1 asks the |
| 630 | note HTTP writer for chunked framing; hwfin writes the final zero chunk only after a clean EOF. |
| 631 | note On a later read/send error close without that final chunk: a client must see an incomplete |
| 632 | note response, never a successfully shorter shelf. Translation runs on each complete HTML batch. |
| 633 | fn fsend5 cs edit ulong 320 io edit uchar 131072 st ulong to long |
| 634 | more = ohbody |
| 635 | if ne get fobtr 0 0 |
| 636 | ohbodyend |
| 637 | put fobl 0 0 |
| 638 | put fobtr 0 0 |
| 639 | put fkind 0 0 |
| 640 | ret fsend cs io 500 |
| 641 | end |
| 642 | r = hrspst cs io st |
| 643 | if and eq get gobody 1 0 ftxwant |
| 644 | r = hfe r fhdrt cs io tfnctype tfsplain |
| 645 | else |
| 646 | r = hfe r fhdrg cs io tfnctype 0 |
| 647 | end |
| 648 | r = hfe r fsechdr cs io |
| 649 | r = hfe r fhdrt cs io tfncc tfnostor |
| 650 | r = hfe r hhend cs io 1 0 |
| 651 | put ftxmore 0 0 |
| 652 | p = ulong 0 |
| 653 | t0 = ulong monons |
| 654 | while and more ge r 0 bound 9223372036854775807 |
| 655 | if ne get gobody 1 0 |
| 656 | r = fsendr cs io 0 get fobl 0 |
| 657 | else |
| 658 | if ftxwant |
| 659 | r = ftxrun cs io get fobl 0 1 |
| 660 | put ftxmore 0 1 |
| 661 | else |
| 662 | r = ftrrun cs io 0 get fobl 0 1 |
| 663 | end |
| 664 | end |
| 665 | p = add p get fobl 0 |
| 666 | fsendl cs t0 p |
| 667 | if lt r 0 |
| 668 | break |
| 669 | end |
| 670 | more = ohbody |
| 671 | if ne get fobtr 0 0 |
| 672 | r = -5 |
| 673 | break |
| 674 | end |
| 675 | end |
| 676 | ohbodyend |
| 677 | put ftxmore 0 0 |
| 678 | if lt r 0 |
| 679 | put cs HKEEP 0 |
| 680 | ret r |
| 681 | end |
| 682 | ret hwfin cs io |
| 683 | end |
| 684 | fn fsend cs edit ulong 320 io edit uchar 131072 st ulong to long |
| 685 | hsetdl cs 60000000000 |
| 686 | s = st |
| 687 | k = get fkind 0 |
| 688 | if and ge s 400 and eq k 0 eq get fobl 0 0 |
| 689 | ferrpage s |
| 690 | k = get fkind 0 |
| 691 | end |
| 692 | note Kinds 0, 1 and 2 are sent from buffers a handler filled (fob, floc). Before the first header: |
| 693 | note when a byte was dropped (fobtr), the cut response is thrown away and a whole 500 is sent in its |
| 694 | note place (fovfl). Kinds 3 and 4 stream a file or a command; kind 5 generates bounded batches in |
| 695 | note fob and streams them, so it does not build a whole response here. |
| 696 | if eq k 0 |
| 697 | flayout |
| 698 | end |
| 699 | if le k 2 |
| 700 | if fovfany |
| 701 | fovfl |
| 702 | s = 500 |
| 703 | k = get fkind 0 |
| 704 | note the notice itself is about 1,400 bytes as a page and under 300 as JSON, so it fits the 8 MiB |
| 705 | note fob; should a build with a smaller fob not hold it, the 500 goes out with an empty body |
| 706 | if ne get fobtr 0 0 |
| 707 | put fobtr 0 0 |
| 708 | put fobl 0 0 |
| 709 | put fctl 0 0 |
| 710 | put fkind 0 1 |
| 711 | k = 1 |
| 712 | end |
| 713 | end |
| 714 | end |
| 715 | if eq k 1 |
| 716 | ret fsend1 cs io s |
| 717 | end |
| 718 | if eq k 2 |
| 719 | ret fsend2 cs io s |
| 720 | end |
| 721 | if eq k 3 |
| 722 | ret fsend3 cs io s |
| 723 | end |
| 724 | if eq k 4 |
| 725 | ret fsend4 cs io s |
| 726 | end |
| 727 | if eq k 5 |
| 728 | ret fsend5 cs io s |
| 729 | end |
| 730 | ret fsend0 cs io s |
| 731 | end |
| 732 | note ---- access log: "<epoch> <ip> <method> <path> <status> <bytes> <ms> <agent> <referrer>" |
| 733 | note agent and referrer are the request's User-Agent and Referer, at most 200 bytes each, "-" when |
| 734 | note absent; a space is written as "+", so a line always has nine fields |
| 735 | global flg uchar 8192 |
| 736 | global flgl ulong 1 |
| 737 | fn flgc c uchar to void |
| 738 | n = get flgl 0 |
| 739 | if lt n 8191 |
| 740 | put flg n c |
| 741 | put flgl 0 add n 1 |
| 742 | end |
| 743 | end |
| 744 | fn flgn v ulong to void |
| 745 | d = new uchar 20 0 |
| 746 | i = ulong 20 |
| 747 | x = v |
| 748 | for it ulong 20 |
| 749 | i = sub i 1 |
| 750 | put d i uchar add rem x 10 48 |
| 751 | x = div x 10 |
| 752 | if eq x 0 |
| 753 | break |
| 754 | end |
| 755 | end |
| 756 | for k ulong sub 20 i bound 20 |
| 757 | flgc get d add i k |
| 758 | end |
| 759 | end |
| 760 | fn flgs c uchar to void |
| 761 | if or lt c 33 gt c 126 |
| 762 | flgc 63 |
| 763 | ret |
| 764 | end |
| 765 | flgc c |
| 766 | end |
| 767 | note flga: one byte of an agent or referrer; a space becomes "+", the rest goes through flgs |
| 768 | fn flga c uchar to void |
| 769 | if eq c 32 |
| 770 | flgc 43 |
| 771 | ret |
| 772 | end |
| 773 | flgs c |
| 774 | end |
| 775 | fn flogreq st ulong tz long to void |
| 776 | put flgl 0 0 |
| 777 | flgn div ulong realns 1000000000 |
| 778 | flgc 32 |
| 779 | for i ulong get fipl 0 bound 64 |
| 780 | flgs get fip i |
| 781 | end |
| 782 | flgc 32 |
| 783 | flgn get fmeth 0 |
| 784 | flgc 32 |
| 785 | for i ulong hmin get fpathl 0 512 bound 512 |
| 786 | flgs get fpath i |
| 787 | end |
| 788 | flgc 32 |
| 789 | flgn st |
| 790 | flgc 32 |
| 791 | flgn get fobl 0 |
| 792 | flgc 32 |
| 793 | flgn div ulong sub long monons tz 1000000 |
| 794 | flgc 32 |
| 795 | if eq get fagl 0 0 |
| 796 | flgc 45 |
| 797 | end |
| 798 | for i ulong get fagl 0 bound 200 |
| 799 | flga get fag i |
| 800 | end |
| 801 | flgc 32 |
| 802 | if eq get frfl 0 0 |
| 803 | flgc 45 |
| 804 | end |
| 805 | for i ulong get frfl 0 bound 200 |
| 806 | flga get frf i |
| 807 | end |
| 808 | flgc 10 |
| 809 | w = write 2 flg 0 get flgl 0 |
| 810 | end |
| 811 | note ---- one request |
| 812 | fn fhandle cs edit ulong 320 io edit uchar 131072 bd edit uchar 65536 to long |
| 813 | tz = long monons |
| 814 | fresetr |
| 815 | freqmeth cs io |
| 816 | freqhdr cs io |
| 817 | st = ulong 0 |
| 818 | if not freqtgt cs io |
| 819 | st = 400 |
| 820 | end |
| 821 | if eq st 0 |
| 822 | st = freqbody cs io bd |
| 823 | end |
| 824 | if eq st 0 |
| 825 | if eq get fmeth 0 0 |
| 826 | st = 405 |
| 827 | end |
| 828 | end |
| 829 | if eq st 0 |
| 830 | x = sosess |
| 831 | st = ulong froute |
| 832 | end |
| 833 | r = fsend cs io st |
| 834 | note the access log names the status that was sent |
| 835 | if ne get fovf 0 0 |
| 836 | st = 500 |
| 837 | end |
| 838 | flogreq st tz |
| 839 | ret r |
| 840 | end |
| 841 | fn fserve c int cs edit ulong 320 io edit uchar 131072 bd edit uchar 65536 to void |
| 842 | r = hinit cs io c 1 |
| 843 | if lt r 0 |
| 844 | x = fclose c |
| 845 | ret |
| 846 | end |
| 847 | hsettmo cs 15000000000 15000000000 |
| 848 | hsetbdl cs 60000000000 |
| 849 | note http.om refuses a Content-Length above HBCAP when it reads the head (413), before the path is |
| 850 | note known here: the head-time cap is the patch intake's 3 MiB, and freqbody sets 64 KiB for every |
| 851 | note other POST before it reads the body |
| 852 | put cs HBCAP 3145728 |
| 853 | for k ulong 100 |
| 854 | r = hreqrd cs io |
| 855 | if le r 0 |
| 856 | if lt r 0 |
| 857 | x = hreject cs io r |
| 858 | end |
| 859 | break |
| 860 | end |
| 861 | r = fhandle cs io bd |
| 862 | put cs HBCAP 3145728 |
| 863 | if lt r 0 |
| 864 | break |
| 865 | end |
| 866 | if eq hnext cs io 0 |
| 867 | break |
| 868 | end |
| 869 | end |
| 870 | r = hclose cs |
| 871 | end |
| 872 | note ---- reload: allow-list + social log. fload gives a negative count when the source could not be |
| 873 | note read or was refused (fan is 0, every repository URL is 404); the log line then says 0. Before |
| 874 | note 2026-11-13 the negative count went through `ulong n`, which traps 5 (exit 105), so an unreadable |
| 875 | note RIDS.md stopped forged instead of serving nothing. |
| 876 | fn freload to void |
| 877 | n = fload |
| 878 | fzload |
| 879 | x = soload |
| 880 | flog tfrld |
| 881 | put flgl 0 0 |
| 882 | c = ulong 0 |
| 883 | if gt n 0 |
| 884 | c = ulong n |
| 885 | end |
| 886 | flgn c |
| 887 | flgc 10 |
| 888 | w = write 2 flg 0 get flgl 0 |
| 889 | end |
| 890 | fn fflagp to void |
| 891 | fpb |
| 892 | fpt tfdata |
| 893 | fpt tfrlflag |
| 894 | end |
| 895 | fn fchkrl to void |
| 896 | fflagp |
| 897 | r = stat fpth 0 get fpthl 0 fstb 0 128 |
| 898 | if ge r 0 |
| 899 | x = unlink fpth 0 get fpthl 0 |
| 900 | freload |
| 901 | ret |
| 902 | end |
| 903 | if fsrcchg |
| 904 | freload |
| 905 | end |
| 906 | end |
| 907 | note frsreq: true when the supervisor has asked for a restart (the flag file exists); removes it. |
| 908 | fn frsreq to bool |
| 909 | fpb |
| 910 | fpt tfdata |
| 911 | fpt tfrsflag |
| 912 | r = stat fpth 0 get fpthl 0 fstb 0 128 |
| 913 | if lt r 0 |
| 914 | ret false |
| 915 | end |
| 916 | x = unlink fpth 0 get fpthl 0 |
| 917 | ret true |
| 918 | end |
| 919 | note finharg: argv is exactly forged --inherited-listener |
| 920 | fn finharg to bool |
| 921 | if ne argcnt 2 |
| 922 | ret false |
| 923 | end |
| 924 | b = new uchar 32 0 |
| 925 | n = argread 1 b 0 len b |
| 926 | if ne n long len tfinharg |
| 927 | ret false |
| 928 | end |
| 929 | for i ulong len tfinharg bound 32 |
| 930 | if ne get b i get tfinharg i |
| 931 | ret false |
| 932 | end |
| 933 | end |
| 934 | ret true |
| 935 | end |
| 936 | note finherit: the supervisor's listener (infra/spark/forged-listen.om) arrives as descriptor 0. It |
| 937 | note must be bound to exactly the address in a (AF_INET, compiled port, 127.0.0.1) and accept |
| 938 | note listen (a stream socket). It moves to a close-on-exec descriptor and /dev/null takes 0, so no |
| 939 | note child process can inherit it. Returns the listener or -1. |
| 940 | fn finherit a edit uchar * to long |
| 941 | s = new uchar 16 0 |
| 942 | k = sockname 0 s 0 16 |
| 943 | if ne k 16 |
| 944 | ret -1 |
| 945 | end |
| 946 | for i ulong 8 |
| 947 | if ne get s i get a i |
| 948 | ret -1 |
| 949 | end |
| 950 | end |
| 951 | r = listen 0 64 |
| 952 | if ne r 0 |
| 953 | ret -1 |
| 954 | end |
| 955 | n1 = fopen tfdevnul 0 len tfdevnul 524288 0 |
| 956 | if lt n1 0 |
| 957 | ret -1 |
| 958 | end |
| 959 | l = dupfd 0 int n1 524288 |
| 960 | if ne l n1 |
| 961 | ret -1 |
| 962 | end |
| 963 | n2 = fopen tfdevnul 0 len tfdevnul 2 0 |
| 964 | if lt n2 0 |
| 965 | ret -1 |
| 966 | end |
| 967 | z = dupfd int n2 0 0 |
| 968 | x = fclose int n2 |
| 969 | if ne z 0 |
| 970 | ret -1 |
| 971 | end |
| 972 | ret l |
| 973 | end |
| 974 | fn main to int |
| 975 | cs = new ulong 320 0 |
| 976 | io = new uchar 131072 0 |
| 977 | bd = new uchar 65536 0 |
| 978 | a = new uchar 16 0 |
| 979 | mode = fzmode |
| 980 | if ne mode -1 |
| 981 | ret int mode |
| 982 | end |
| 983 | r = haddr4 a 2130706433 18480 |
| 984 | lf = long -1 |
| 985 | if finharg |
| 986 | lf = finherit a |
| 987 | if lt lf 0 |
| 988 | flog tfinhbad |
| 989 | ret 2 |
| 990 | end |
| 991 | flog tfinhok |
| 992 | else |
| 993 | lf = hlisten a 64 |
| 994 | if lt lf 0 |
| 995 | flog tfnolist |
| 996 | ret 2 |
| 997 | end |
| 998 | end |
| 999 | x = frsreq |
| 1000 | freload |
| 1001 | for it ulong 4000000000 |
| 1002 | c = haccept int lf 1000000000 |
| 1003 | if ge c 0 |
| 1004 | fserve int c cs io bd |
| 1005 | end |
| 1006 | fchkrl |
| 1007 | if frsreq |
| 1008 | flog tfrsreq |
| 1009 | ret 0 |
| 1010 | end |
| 1011 | end |
| 1012 | ret 0 |
| 1013 | end |