interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / browse / a_core.om

629 lines · 11111 bytes Raw Blame History
1note browse/a_core.om (ForgeBrowse): request-rest parsing, ref and path validation, ref
2note resolution against the allow-listed storage, URL emission and small helpers.
3note Names: functions br*, globals gb*, texts tb*. Contract: docs/CONTRACT.md v1.
4note No user text ever reaches git as a ref: a ref is either a name listed by for-each-ref
5note (longest match against the URL) or 7..40 lowercase hex naming a commit reachable from a
6note published head or tag (brreach, shared with dl). Paths are checked by brokp
7note (no NUL or control bytes, no empty, "." or ".." segment, no leading "-" or "/").
8global gbr uchar 4096
9global gbrl ulong 1
10global gbw uchar 64
11global gbwl ulong 1
12global gbref uchar 512
13global gbrefl ulong 1
14global gbfr uchar 1024
15global gbfrl ulong 1
16global gbsha uchar 40
17global gbshl ulong 1
18global gbsa uchar 40
19global gbp uchar 4096
20global gbpl ulong 1
21global gbn ulong 16
22text tbfer "for-each-ref"
23text tbfmtr "--format=%(refname)"
24text tbrh "refs/heads"
25text tbrt "refs/tags"
26text tbrhs "refs/heads/"
27text tbrts "refs/tags/"
28text tbns "refs/namespaces/"
29text tbnsrh "/refs/heads"
30text tbnsrt "/refs/tags"
31text tbrevp "rev-parse"
32text tbverif "--verify"
33text tbq "-q"
34text tbeoo "--end-of-options"
35text tbpcom "^{commit}"
36text tbmain "main"
37text tbah "<a href=\""
38text tbaq "\">"
39text tbae "</a>"
40text tbhex "0123456789ABCDEF"
41text tblhex "0123456789abcdef"
42const BRTMO long 15000000000
43const BRBIG ulong 33554432
44note ---- byte classes
45fn brdig c uchar to bool
46ret and ge c 48 le c 57
47end
48fn bralpha c uchar to bool
49if and ge c 97 le c 122
50ret true
51end
52ret and ge c 65 le c 90
53end
54fn bralnum c uchar to bool
55if brdig c
56ret true
57end
58ret bralpha c
59end
60fn brlhex c uchar to bool
61if brdig c
62ret true
63end
64ret and ge c 97 le c 102
65end
66fn brspc c uchar to bool
67ret or eq c 32 eq c 9
68end
69fn brlow c uchar to uchar
70if and ge c 65 le c 90
71ret uchar add c 32
72end
73ret c
74end
75fn brhexv c uchar to ulong
76if brdig c
77ret ulong sub c 48
78end
79if and ge c 97 le c 102
80ret ulong sub c 87
81end
82if and ge c 65 le c 70
83ret ulong sub c 55
84end
85ret 99
86end
87note ---- rest parsing: frest = "" | "/" | "/<word>[/<rest>]"; rest percent-decoded into gbr
88fn brisw t view uchar * to bool
89n = get gbwl 0
90if ne n len t
91ret false
92end
93for i ulong n
94if ne get gbw i get t i
95ret false
96end
97end
98ret true
99end
100fn brparse to bool
101n = get frestl 0
102put gbwl 0 0
103put gbrl 0 0
104if eq n 0
105ret true
106end
107i = ulong 1
108while lt i n bound BRBIG
109c = get frest i
110if eq c 47
111break
112end
113k = get gbwl 0
114if ge k 63
115ret false
116end
117put gbw k c
118put gbwl 0 add k 1
119i = add i 1
120end
121if lt i n
122i = add i 1
123end
124ret brdecr i n
125end
126note brdecr: percent-decode frest[a, e) into gbr; refuse NUL and control bytes (raw or decoded)
127fn brdecr a ulong e ulong to bool
128o = ulong 0
129i = a
130while lt i e bound BRBIG
131c = get frest i
132if eq c 37
133if lt add i 2 e
134h = brhexv get frest add i 1
135l = brhexv get frest add i 2
136if and lt h 16 lt l 16
137c = uchar add mul h 16 l
138i = add i 2
139end
140end
141end
142if or lt c 32 eq c 127
143ret false
144end
145if ge o 4096
146ret false
147end
148put gbr o c
149o = add o 1
150i = add i 1
151end
152put gbrl 0 o
153ret true
154end
155note ---- path checks on gbp[0, gbpl)
156fn brseg s ulong e ulong to bool
157if ge s e
158ret false
159end
160if eq get gbp s 46
161if eq e add s 1
162ret false
163end
164if eq e add s 2
165if eq get gbp add s 1 46
166ret false
167end
168end
169end
170ret true
171end
172fn brokp to bool
173n = get gbpl 0
174if eq n 0
175ret true
176end
177if or eq get gbp 0 45 eq get gbp 0 47
178ret false
179end
180s = ulong 0
181for i ulong n bound BRBIG
182c = get gbp i
183if or lt c 32 eq c 127
184ret false
185end
186if eq c 47
187if not brseg s i
188ret false
189end
190s = add i 1
191end
192end
193ret brseg s n
194end
195note ---- git argument helpers
196fn brkns t view uchar * to void
197fkopen
198for i ulong len tbns
199fkc get tbns i
200end
201for i ulong len tfnid
202fkc get tfnid i
203end
204for i ulong len t
205fkc get t i
206end
207end
208fn brkfr to void
209for i ulong get gbfrl 0
210fkc get gbfr i
211end
212end
213fn brksha to void
214for i ulong get gbshl 0
215fkc get gbsha i
216end
217end
218fn brkp to void
219for i ulong get gbpl 0
220fkc get gbp i
221end
222end
223note brkshp: one argument "<sha>:<path>"
224fn brkshp to void
225fkopen
226brksha
227fkc 58
228brkp
229end
230note ---- fxo helpers
231fn brxat a ulong e ulong t view uchar * to bool
232k = len t
233if gt add a k e
234ret false
235end
236for i ulong k
237if ne get fxo add a i get t i
238ret false
239end
240end
241ret true
242end
243fn brxfind a ulong e ulong c uchar to ulong
244i = a
245while lt i e bound BRBIG
246if eq get fxo i c
247ret i
248end
249i = add i 1
250end
251ret e
252end
253fn brxdec a ulong e ulong to ulong
254v = ulong 0
255i = a
256while lt i e bound BRBIG
257c = get fxo i
258if not brdig c
259ret v
260end
261v = add mul v 10 ulong sub c 48
262i = add i 1
263end
264ret v
265end
266note brpfx: length of the ref prefix of fxo[s, e) (refs/heads/, refs/tags/, or the delegate
267note namespace's), 0 when it is none of them
268fn brpfx s ulong e ulong to ulong
269if brxat s e tbrhs
270ret len tbrhs
271end
272if brxat s e tbrts
273ret len tbrts
274end
275if not brxat s e tbns
276ret 0
277end
278a = add s len tbns
279if not brxat a e tfnid
280ret 0
281end
282a = add a len tfnid
283if brxat a e tbnsrh
284if lt add a len tbnsrh e
285if eq get fxo add a len tbnsrh 47
286ret sub add a add len tbnsrh 1 s
287end
288end
289end
290if brxat a e tbnsrt
291if lt add a len tbnsrt e
292if eq get fxo add a len tbnsrt 47
293ret sub add a add len tbnsrt 1 s
294end
295end
296end
297ret 0
298end
299note brmatch: short name fxo[a, e) is a prefix of gbr ending at a "/" or the end
300fn brmatch a ulong e ulong to bool
301k = sub e a
302n = get gbrl 0
303if or eq k 0 gt k n
304ret false
305end
306for i ulong k
307if ne get fxo add a i get gbr i
308ret false
309end
310end
311if eq k n
312ret true
313end
314ret eq get gbr k 47
315end
316note brbest: longest listed ref matching the start of gbr; sets gbfr/gbref, returns its length. The
317note listing (fsopen by brres) is read one line at a time through fxo, so no size of the ref list cuts
318note it: a ref past the first 16 MiB of names is found like any other. 0 when no ref matches, or the
319note listing could not be read to its end (brres looks at fxotr and the exit status).
320fn brbest to ulong
321best = ulong 0
322for it ulong 1000000000
323e = fsnext 10
324if eq e FSEND
325break
326end
327if eq e FSFAIL
328ret 0
329end
330s = get fsrs 0
331pl = brpfx s e
332if gt pl 0
333if brmatch add s pl e
334k = sub e add s pl
335note a ref name longer than gbref (512 bytes) is not a match: the copy below would write past gbref,
336note which is trap 3 and ends the server (exit 103). The page is then a 404.
337if and gt k best le k len gbref
338best = k
339brsetfr s e
340end
341end
342end
343end
344if eq best 0
345ret 0
346end
347put gbrefl 0 best
348for i ulong best
349put gbref i get gbr i
350end
351ret best
352end
353fn brsetfr a ulong e ulong to void
354k = sub e a
355if gt k 1024
356k = 1024
357end
358for i ulong k
359put gbfr i get fxo add a i
360end
361put gbfrl 0 k
362end
363note brhexseg: gbr starts with 7..40 lowercase hex then "/" or the end; returns that length or 0
364fn brhexseg to ulong
365n = get gbrl 0
366k = ulong 0
367while lt k n bound BRBIG
368c = get gbr k
369if eq c 47
370break
371end
372if not brlhex c
373ret 0
374end
375k = add k 1
376end
377if or lt k 7 gt k 40
378ret 0
379end
380ret k
381end
382note brrevp: rev-parse gbfr^{commit} into gbsha; false when it does not name a commit
383fn brrevp to bool
384put gbshl 0 0
385fksource
386fka tbrevp
387fka tbverif
388fka tbq
389fka tbeoo
390fkopen
391brkfr
392for i ulong len tbpcom
393fkc get tbpcom i
394end
395st = fexec BRTMO
396if ne st 0
397ret false
398end
399if lt get fxol 0 40
400ret false
401end
402for i ulong 40
403c = get fxo i
404if not brlhex c
405ret false
406end
407put gbsha i c
408end
409put gbshl 0 40
410ret true
411end
412note brres: resolve gbr = "<ref>[/<path>]" (decoded URL rest). Sets gbref (as in the URL),
413note gbfr (full refname or hex), gbsha (40 hex) and gbp (path, trailing "/" dropped). False = 404.
414fn brres to bool
415put gbrefl 0 0
416put gbpl 0 0
417put gbshl 0 0
418put gbfrl 0 0
419if eq get gbrl 0 0
420ret false
421end
422fksource
423fka tbfer
424fka tbfmtr
425fka tbrh
426fka tbrt
427brkns tbnsrh
428brkns tbnsrt
429if not fsopen BRTMO
430ret false
431end
432k = brbest
433st = fsclose
434note one ref name longer than fxo cannot be read whole: the ref asked for may be that one
435if ne get fxotr 0 0
436fincut
437ret false
438end
439if ne st 0
440ret false
441end
442hx = false
443note A full commit id wins over a same-named ref, keeping pinned page links pinned.
444if eq brhexseg 40
445k = 0
446end
447if eq k 0
448k = brhexseg
449if eq k 0
450ret false
451end
452hx = true
453for i ulong k
454put gbref i get gbr i
455put gbfr i get gbr i
456end
457put gbrefl 0 k
458put gbfrl 0 k
459end
460if not brsetp k
461ret false
462end
463if not brrevp
464ret false
465end
466if hx
467ret brreach
468end
469ret true
470end
471note brreach: gbsha is reachable from a published head or tag (root or delegate namespace);
472note the check is dl's (dlrch0 / dlrch1), so a commit id other peers, cobs or rewritten
473note history hold is 404 in browse exactly as in downloads
474fn brreach to bool
475dlrch0
476fkopen
477brksha
478ret dlrch1
479end
480note brsetp: gbp = gbr after the ref (k bytes) and its "/", without trailing slashes; checked
481fn brsetp k ulong to bool
482n = get gbrl 0
483a = k
484if lt a n
485a = add a 1
486end
487e = n
488while gt e a bound BRBIG
489if ne get gbr sub e 1 47
490break
491end
492e = sub e 1
493end
494o = ulong 0
495for i ulong sub e a
496put gbp i get gbr add a i
497o = add o 1
498end
499put gbpl 0 o
500ret brokp
501end
502note brdefref: put the default branch name into gbr (HEAD's branch, nodes/head.om; "main" when none)
503fn brdefref to void
504put gbrl 0 0
505if nshead
506for i ulong get nshbl 0 bound 256
507put gbr i get nshb i
508end
509put gbrl 0 get nshbl 0
510end
511if eq get gbrl 0 0
512for i ulong len tbmain
513put gbr i get tbmain i
514end
515put gbrl 0 len tbmain
516end
517end
518note ---- URL emission into fob (percent-encoding; every byte not unreserved or "/" is %XX)
519fn bruc c uchar to void
520ok = bralnum c
521if or eq c 45 eq c 46
522ok = true
523end
524if or eq c 95 eq c 126
525ok = true
526end
527if eq c 47
528ok = true
529end
530if ok
531fputc c
532ret
533end
534fputc 37
535fputc get tbhex shr c 4
536fputc get tbhex and c 15
537end
538fn brurepo to void
539fputb
540for i ulong get fownl 0
541bruc get fown i
542end
543fputc 47
544for i ulong get frepol 0
545bruc get frepo i
546end
547end
548fn bruref to void
549for i ulong get gbrefl 0
550bruc get gbref i
551end
552end
553fn brup a ulong n ulong to void
554for i ulong n bound BRBIG
555bruc get gbp add a i
556end
557end
558note bruact: <base><owner>/<repo>/<t>/<ref>
559fn bruact t view uchar * to void
560brurepo
561fputc 47
562fput t
563fputc 47
564bruref
565end
566note bruactp: bruact then "/" and gbp[0, n) when n > 0
567fn bruactp t view uchar * n ulong to void
568bruact t
569if gt n 0
570fputc 47
571brup 0 n
572end
573end
574note brushx: url-encode fxo[a, a+n) (refs, paths and shas read from git output)
575fn bruxo a ulong n ulong to void
576for i ulong n bound BRBIG
577bruc get fxo add a i
578end
579end
580note ---- misc output
581fn brgesc a ulong n ulong to void
582for i ulong n bound BRBIG
583fescc get gbp add a i
584end
585end
586fn brrefesc to void
587for i ulong get gbrefl 0
588fescc get gbref i
589end
590end
591fn brshort to void
592for i ulong 7
593fputc get gbsha i
594end
595end
596fn brd2 v ulong to void
597fputc uchar add div v 10 48
598fputc uchar add rem v 10 48
599end
600note brdate: unix seconds as YYYY-MM-DD (UTC, proleptic Gregorian, days-from-civil inverse)
601fn brdate t ulong to void
602z = add div t 86400 719468
603era = div z 146097
604doe = sub z mul era 146097
605yoe = div sub add sub doe div doe 1460 div doe 36524 div doe 146096 365
606y = add yoe mul era 400
607doy = sub doe sub add mul 365 yoe div yoe 4 div yoe 100
608mp = div add mul 5 doy 2 153
609d = add sub doy div add mul 153 mp 2 5 1
610m = add mp 3
611if ge mp 10
612m = sub mp 9
613end
614if le m 2
615y = add y 1
616end
617fnum y
618fputc 45
619brd2 m
620fputc 45
621brd2 d
622end
623note brtime: the UTC time of day HH:MM
624fn brtime t ulong to void
625s = rem t 86400
626brd2 div s 3600
627fputc 58
628brd2 div rem s 3600 60
629end