interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / server / main.om

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