interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / browse / e1_json.om

916 lines · 14065 bytes Raw Blame History
1note browse/e1_json.om: machine form of /tree, /blob, /blame and /search (ForgeBrowse; docs/API.md,
2note docs/api-browse-files.md). Each handler runs the same validation and resolution as its HTML
3note page (brres, brotype, brokp through brsetp, brreach for hex refs) before it writes anything, so
4note a JSON request reads nothing the HTML page refuses and answers the same status. Helpers: bry*,
5note texts tby*, globals gby*, consts BRY*.
6note BRYCAP: stop writing long strings and lists when fob (8 MiB) has less than 64 KiB left, so the
7note document always closes as valid JSON; the cut is reported (content_truncated / truncated)
8const BRYCAP ulong 8323072
9global gbytr ulong 1
10global gbybin ulong 1
11global gbyxtr ulong 1
12global gbyln ulong 1
13global gbylv ulong 1
14text tbyst "forge/tree/1"
15text tbysb "forge/blob/1"
16text tbysl "forge/blame/1"
17text tbyss "forge/repo-search/1"
18text tbyrep "\\ufffd"
19text tbyplain "?plain=1"
20text tbyktext "text"
21text tbykimg "image"
22text tbykbinv "binary"
23text tbyklarg "large"
24text tbykref "ref"
25text tbykrk "ref_kind"
26text tbykcm "commit"
27text tbykpa "path"
28text tbykpar "parent"
29text tbykent "entries"
30text tbyktr "truncated"
31text tbykrd "readme"
32text tbyklc "last_commit"
33text tbykhi "history"
34text tbyknm "name"
35text tbykty "type"
36text tbyksh "sha"
37text tbyksz "size"
38text tbykht "html"
39text tbykraw "raw"
40text tbykbin "binary"
41text tbyklin "lines"
42text tbykkd "kind"
43text tbyklk "links"
44text tbykbl "blame"
45text tbykso "source"
46text tbykre "rendered"
47text tbykco "content"
48text tbykct "content_truncated"
49text tbykcms "commits"
50text tbykln "line"
51text tbyktx "text"
52text tbykq "query"
53text tbykhit "hits"
54text tbykau "author"
55text tbyktm "time"
56text tbyksu "subject"
57note ---- JSON strings straight from fxo (no 64 KiB fjt limit): well-formed UTF-8 kept, every other
58note byte U+FFFD, the escapes of fjasc; stops (gbytr = 1) when fob is nearly full
59fn bryul i ulong e ulong to ulong
60c = get fxo i
61if lt c 128
62ret 1
63end
64n = ulong 0
65if and ge c 194 le c 223
66n = 2
67end
68if and ge c 224 le c 239
69n = 3
70end
71if and ge c 240 le c 244
72n = 4
73end
74if eq n 0
75ret 0
76end
77if gt add i n e
78ret 0
79end
80for k ulong sub n 1 bound 3
81x = get fxo add add i k 1
82if or lt x 128 gt x 191
83ret 0
84end
85end
86d = get fxo add i 1
87if and eq c 224 lt d 160
88ret 0
89end
90if and eq c 237 gt d 159
91ret 0
92end
93if and eq c 240 lt d 144
94ret 0
95end
96if and eq c 244 gt d 143
97ret 0
98end
99ret n
100end
101fn bryxs a ulong n ulong to void
102fputc 34
103e = add a n
104i = a
105while lt i e bound BRBIG
106if gt get fobl 0 BRYCAP
107put gbytr 0 1
108break
109end
110l = bryul i e
111if eq l 0
112fput tbyrep
113i = add i 1
114continue
115end
116if eq l 1
117fjasc get fxo i
118i = add i 1
119continue
120end
121for k ulong l bound 4
122fputc get fxo add i k
123end
124i = add i l
125end
126fputc 34
127end
128note ---- small writers
129fn bryref to void
130fjtb
131for i ulong get gbrefl 0 bound 512
132fjtc get gbref i
133end
134fjstr
135end
136fn brypstr to void
137fjtb
138for i ulong get gbpl 0 bound 4096
139fjtc get gbp i
140end
141fjstr
142end
143fn brysha to void
144fputc 34
145for i ulong get gbshl 0 bound 40
146fputc get gbsha i
147end
148fputc 34
149end
150note bryl t n: "<repo>/<t>/<ref>[/<gbp[0, n)>]" as a JSON string (the page's href, bruactp)
151fn bryl t view uchar * n ulong to void
152fputc 34
153bruactp t n
154fputc 34
155end
156note bryhd0: ,"ref":..,"commit":..; bryhead t: the document head of a /tree, /blob or /blame page
157fn bryhd0 to void
158fyk tbykref
159bryref
160fyk tbykcm
161brysha
162end
163fn bryhead t view uchar * to void
164fyrhead t
165fyk tbykref
166bryref
167fyk tbykrk
168k = brrkind
169if eq k 1
170fysv tbkbr
171end
172if eq k 2
173fysv tbktag
174end
175if eq k 3
176fysv tbkcmt
177end
178fyk tbykcm
179brysha
180fyk tbykpa
181brypstr
182end
183note brysum: the commit summary {sha, author, time, subject, path}: sha fxo[o, o+40), author
184note fxo[a, a+al), time t (epoch), subject fxo[s, s+sl)
185fn brysum o ulong a ulong al ulong t ulong s ulong sl ulong to void
186fputc 123
187fyk0 tbyksh
188fputc 34
189fxput o 40
190fputc 34
191fyk tbykau
192bryxs a al
193fyk tbyktm
194fjiso t
195fyk tbyksu
196bryxs s sl
197fyk tbykpa
198fputc 34
199brucmt o 40
200fputc 34
201fputc 125
202end
203note ---- /tree/<ref>[/<path>] (brtreepg)
204fn brjtree to long
205if not brres
206ret brerr 404
207end
208k = brotype
209if eq k 0
210ret brerr 404
211end
212if eq k 2
213brloc tbblob
214ret 302
215end
216put gbytr 0 0
217bryhead tbyst
218fyk tbykpar
219brypar
220fyk tbykhi
221bryl tbcmts get gbpl 0
222fyk tbyklc
223brylast
224bryents
225fyk tbykrd
226bryrdme
227fyend
228ret 200
229end
230note brypar: the ".." row of the listing (brtup), null at the root
231fn brypar to void
232if eq get gbpl 0 0
233fynull
234ret
235end
236fputc 34
237brtup
238fputc 34
239end
240note brylast: the brlast box (newest commit touching gbp at gbsha) as a commit summary, or null
241fn brylast to void
242fksource
243fka tblog
244fka tbn1
245fka tbone
246fka tbfmtc
247fkopen
248brksha
249if gt get gbpl 0 0
250fka tbdd
251fkopen
252brkp
253end
254st = fexec BRTMO
255n = get fxol 0
256if or ne st 0 eq n 0
257fynull
258ret
259end
260e = brxfind 0 n 10
261k = brsplit 0 e 31
262if lt k 4
263fynull
264ret
265end
266o = brfa 0
267a = brfa 1
268al = brfl 1
269t = brxdec brfa 2 add brfa 2 brfl 2
270s = brfa 3
271sl = brfl 3
272brysum o a al t s sl
273end
274note bryents: ,"entries":[..],"truncated":b in brlist's order (pass 1 trees, pass 2 the rest, the
275note same BRMAXE cut); an ls-tree failure is the page's empty listing
276fn bryents to void
277put gbrml 0 0
278put gbrmk 0 0
279fyk tbykent
280fputc 91
281c = ulong 0
282if brlsx
283c = bryents2
284end
285fputc 93
286fyk tbyktr
287fybool or ge c BRMAXE ne get gbytr 0 0
288end
289fn bryents2 to ulong
290n = get fxol 0
291c = ulong 0
292for q ulong 2
293pass = add q 1
294s = ulong 0
295while lt s n bound BRBIG
296if gt get fobl 0 BRYCAP
297put gbytr 0 1
298break
299end
300e = brxfind s n 0
301if bryent s e pass c
302c = add c 1
303end
304if ge c BRMAXE
305break
306end
307s = add e 1
308end
309end
310ret c
311end
312note bryent: brtent's filter for one ls-tree -z -l record "<mode> <type> <sha> <size>\t<name>"
313fn bryent s ulong e ulong pass ulong c ulong to bool
314t = add s 7
315if ge t e
316ret false
317end
318tab = brxfind s e 9
319if ge tab e
320ret false
321end
322ch = get fxo t
323istree = eq ch 116
324want = eq pass 1
325if and istree not want
326ret false
327end
328if and not istree want
329ret false
330end
331if gt c 0
332fputc 44
333end
334bryent2 s t tab e
335if and not istree ne ch 99
336brreadme add tab 1 sub e add tab 1
337end
338ret true
339end
340fn bryent2 s ulong t ulong tab ulong e ulong to void
341na = add tab 1
342nl = sub e na
343s2 = brxfind t tab 32
344s3 = brxfind add s2 1 tab 32
345ch = get fxo t
346fputc 123
347fyk0 tbyknm
348bryxs na nl
349fyk tbykty
350fputc 34
351fxput t sub s2 t
352fputc 34
353fyk tbyksh
354fputc 34
355fxput add s2 1 sub s3 add s2 1
356fputc 34
357fyk tbyksz
358if or eq ch 116 eq ch 99
359fynull
360else
361bryszv add s3 1 tab
362end
363fyk tbykpa
364brypath na nl
365fyk tbykht
366bryhref ch na nl
367fputc 125
368end
369note bryszv: the decimal size field fxo[a, e) (leading spaces), null when it has no digits
370fn bryszv a ulong e ulong to void
371v = ulong 0
372d = ulong 0
373i = a
374while lt i e bound 64
375c = get fxo i
376if brdig c
377v = add mul v 10 ulong sub c 48
378d = add d 1
379end
380i = add i 1
381end
382if eq d 0
383fynull
384ret
385end
386fnum v
387end
388note brypath: the repo path gbp/name as a JSON string
389fn brypath na ulong nl ulong to void
390fjtb
391n = get gbpl 0
392for i ulong n bound 4096
393fjtc get gbp i
394end
395if gt n 0
396fjtc 47
397end
398note a tree entry name has no length limit in git: the bound is the buffer, not 4096 (fjtc cuts the string)
399for i ulong nl bound BRBIG
400fjtc get fxo add na i
401end
402fjstr
403end
404note bryhref: the row's link (brtdir / brtfile); a submodule row has none
405fn bryhref ch uchar na ulong nl ulong to void
406if eq ch 99
407fynull
408ret
409end
410fputc 34
411if eq ch 116
412bruactp tbtree get gbpl 0
413else
414bruactp tbblob get gbpl 0
415end
416fputc 47
417bruxo na nl
418fputc 34
419end
420note bryrdme: the README brrdshow renders (name kept by brreadme; it must exist at gbsha), or null
421fn bryrdme to void
422n = get gbrml 0
423if eq n 0
424fynull
425ret
426end
427if not bryrdok
428fynull
429ret
430end
431fputc 123
432fyk0 tbyknm
433fjtb
434for i ulong n bound 256
435fjtc get gbrm i
436end
437fjstr
438fyk tbykht
439bryrdl tbblob
440fyk tbykraw
441bryrdl tbraw
442fputc 125
443end
444fn bryrdl t view uchar * to void
445fputc 34
446bruactp t get gbpl 0
447fputc 47
448for i ulong get gbrml 0 bound 256
449bruc get gbrm i
450end
451fputc 34
452end
453note bryrdok: git cat-file -e <sha>:<gbp>/<readme> (brrdshow shows nothing when the blob is unreadable)
454fn bryrdok to bool
455fksource
456fka tbcatf
457fka tbge
458fkopen
459brksha
460fkc 58
461if gt get gbpl 0 0
462brkp
463fkc 47
464end
465for i ulong get gbrml 0 bound 256
466fkc get gbrm i
467end
468st = fexec BRTMO
469ret eq st 0
470end
471note ---- /blob/<ref>/<path> (brblobpg)
472fn brjblob to long
473if not brres
474ret brerr 404
475end
476if eq get gbpl 0 0
477ret brerr 404
478end
479k = brotype
480if eq k 0
481ret brerr 404
482end
483if eq k 1
484brloc tbtree
485ret 302
486end
487if not brcatb
488ret brerr 404
489end
490if not brybsz
491ret brerr 404
492end
493put gbytr 0 0
494bryhead tbysb
495fyk tbyknm
496brybname
497brjblob2
498fyend
499ret 200
500end
501note brybsz: binary flag, size and line count from the cat-file output in fxo. A blob larger than
502note fxo (fxotr) gets its size from cat-file -s and no line count (its text is not in memory)
503fn brybsz to bool
504tr = ne get fxotr 0 0
505put gbybin 0 0
506if brbin
507put gbybin 0 1
508end
509if not tr
510put gbyxtr 0 0
511put gbbsz 0 get fxol 0
512put gbyln 0 brlines
513put gbylv 0 1
514ret true
515end
516put gbyxtr 0 1
517put gbylv 0 0
518ret brbsz
519end
520fn brybname to void
521n = get gbpl 0
522k = n
523while gt k 0 bound BRBIG
524if eq get gbp sub k 1 47
525break
526end
527k = sub k 1
528end
529fjtb
530for i ulong sub n k bound 4096
531fjtc get gbp add k i
532end
533fjstr
534end
535fn brjblob2 to void
536fyk tbyksz
537fnum get gbbsz 0
538fyk tbyklin
539if eq get gbylv 0 0
540fynull
541else
542fnum get gbyln 0
543end
544fyk tbykbin
545fybool ne get gbybin 0 0
546k = brykind
547fyk tbykkd
548brykname k
549fyk tbyklk
550brylinks
551fyk tbykco
552if le k 2
553bryxs 0 get fxol 0
554else
555fynull
556end
557fyk tbykct
558fybool ne get gbytr 0 0
559end
560note brykind: brbbody's classes: 1 text (code table), 2 markdown (rendered), 3 image, 4 binary,
561note 5 large (too large to show; above BRRAWMX also without a raw link)
562fn brykind to ulong
563if gt get gbbsz 0 BRRAWMX
564ret 5
565end
566brext
567k = brimgk
568if and gt k 0 lt k 8
569ret 3
570end
571if or ne get gbybin 0 0 ne k 0
572ret 4
573end
574if or gt get fxol 0 BRSHOW ne get gbyxtr 0 0
575ret 5
576end
577if and brismd not brplain
578ret 2
579end
580ret 1
581end
582fn brykname k ulong to void
583if eq k 1
584fysv tbyktext
585end
586if eq k 2
587fysv tbxmkd
588end
589if eq k 3
590fysv tbykimg
591end
592if eq k 4
593fysv tbykbinv
594end
595if eq k 5
596fysv tbyklarg
597end
598end
599note brylinks: brbinfo's links: raw (none above BRRAWMX), blame, history, and the Markdown
600note source / rendered switch (only the one the page offers)
601fn brylinks to void
602n = get gbpl 0
603fputc 123
604fyk0 tbykraw
605if le get gbbsz 0 BRRAWMX
606bryl tbraw n
607else
608fynull
609end
610fyk tbykbl
611bryl tbblame n
612fyk tbykhi
613bryl tbcmts n
614md = brismd
615pl = brplain
616fyk tbykso
617if and md not pl
618fputc 34
619bruactp tbblob n
620fput tbyplain
621fputc 34
622else
623fynull
624end
625fyk tbykre
626if and md pl
627bryl tbblob n
628else
629fynull
630end
631fputc 125
632end
633note ---- /blame/<ref>/<path> (brblamepg)
634fn brjblame to long
635if not brres
636ret brerr 404
637end
638if eq get gbpl 0 0
639ret brerr 404
640end
641k = brotype
642if eq k 0
643ret brerr 404
644end
645if eq k 1
646brloc tbtree
647ret 302
648end
649fksource
650fka tbblame
651fka tbporc
652fkopen
653brksha
654fka tbdd
655fkopen
656brkp
657st = fexec BRTMO
658if ne st 0
659ret brerr 404
660end
661put gbytr 0 0
662bryhead tbysl
663fyk tbykhi
664bryl tbcmts get gbpl 0
665brybscan
666fyk tbykcms
667brycms
668fyk tbyklin
669brybl
670fyk tbyktr
671fybool or ne get fxotr 0 0 ne get gbytr 0 0
672fyend
673ret 200
674end
675note brybscan: brblame's walk without output: fills the commit table (gbbk, gbbo, gbba, gbbt, gbbs)
676fn brybscan to void
677put gbbn 0 0
678n = get fxol 0
679cur = ulong 0
680s = ulong 0
681while lt s n bound BRBIG
682e = brxfind s n 10
683c = uchar 10
684if lt s e
685c = get fxo s
686end
687if brbishdr s e
688cur = brbhdr s e
689else
690if ne c 9
691brbkv cur s e
692end
693end
694s = add e 1
695end
696end
697fn brycms to void
698fputc 91
699for i ulong get gbbn 0 bound 4096
700if gt i 0
701fputc 44
702end
703brycm1 i
704end
705fputc 93
706end
707fn brycm1 i ulong to void
708o = get gbbo i
709a = get gbba i
710al = get gbbal i
711t = get gbbt i
712s = get gbbs i
713sl = get gbbsl i
714brysum o a al t s sl
715end
716note brybl: the content rows in file order, each with the commit of its porcelain header
717fn brybl to void
718fputc 91
719n = get fxol 0
720ho = n
721ln = ulong 0
722cnt = ulong 0
723s = ulong 0
724while lt s n bound BRBIG
725e = brxfind s n 10
726if lt s e
727if eq get fxo s 9
728if gt get fobl 0 BRYCAP
729put gbytr 0 1
730break
731end
732if gt cnt 0
733fputc 44
734end
735brybl1 ln ho add s 1 e
736cnt = add cnt 1
737end
738end
739if brbishdr s e
740ho = s
741ln = brbfin s e
742end
743s = add e 1
744end
745fputc 93
746end
747fn brybl1 ln ulong ho ulong a ulong e ulong to void
748fputc 123
749fyk0 tbykln
750fnum ln
751fyk tbykcm
752if lt ho get fxol 0
753fputc 34
754fxput ho 40
755fputc 34
756else
757fynull
758end
759fyk tbyktx
760bryxs a sub e a
761fputc 125
762end
763note ---- /search?q= (brsrchpg)
764fn brjsrch to long
765if gt get gbrl 0 0
766ret brerr 404
767end
768q = fqget tbq1
769put gbql 0 0
770bad = false
771if gt q 256
772ret brerr 400
773end
774if gt q 0
775bad = not brsq ulong q
776end
777brdefref
778if not brres
779ret brerr 404
780end
781if bad
782ret brerr 400
783end
784put gbytr 0 0
785fyrhead tbyss
786fyk tbykq
787if gt get gbql 0 0
788fjtb
789for i ulong get gbql 0 bound 256
790fjtc get gbq i
791end
792fjstr
793else
794fynull
795end
796bryhd0
797fyk tbykhit
798tr = false
799if gt get gbql 0 0
800tr = brysrun
801else
802fputc 91
803fputc 93
804end
805fyk tbyktr
806fybool tr
807fyend
808ret 200
809end
810note brysrun: brsrun's git grep; the hits array; true when the BRHITS cap cut the list
811fn brysrun to bool
812fksource
813fka tbgrep
814fka tbgz
815fka tbgn
816fka tbgi
817fka tbgii
818fka tbgf
819fka tbnocol
820fka tbgmc
821fka tbge
822fkopen
823for i ulong get gbql 0 bound 256
824fkc get gbq i
825end
826fkopen
827brksha
828st = fexec BRTMO
829n = get fxol 0
830fputc 91
831if or ne st 0 eq n 0
832fputc 93
833ret false
834end
835tr = bryhits n
836fputc 93
837ret tr
838end
839note bryhits: brshits' grouping of "<sha>:<path>\0<line>\0<text>" lines by file, same cap
840fn bryhits n ulong to bool
841s = ulong 0
842c = ulong 0
843pa = ulong 0
844pl = ulong 0
845while lt s n bound BRBIG
846e = brxfind s n 10
847a = add s 41
848z = brxfind a e 0
849if and lt a z lt z e
850if brxsame pa pl a sub z a
851fputc 44
852else
853if gt pl 0
854fputc 93
855fputc 125
856fputc 44
857end
858bryshead a sub z a
859pa = a
860pl = sub z a
861end
862bryshit a sub z a add z 1 e
863c = add c 1
864end
865if ge c BRHITS
866if lt add e 1 n
867fputc 93
868fputc 125
869ret true
870end
871end
872s = add e 1
873end
874if gt pl 0
875fputc 93
876fputc 125
877end
878ret false
879end
880fn bryshref a ulong l ulong to void
881bruact tbblob
882fputc 47
883bruxo a l
884end
885fn bryshead a ulong l ulong to void
886fputc 123
887fyk0 tbykpa
888bryxs a l
889fyk tbykht
890fputc 34
891bryshref a l
892fputc 34
893fyk tbyklin
894fputc 91
895end
896fn bryshit a ulong l ulong b ulong e ulong to void
897y = brxfind b e 0
898t = add y 1
899m = ulong 0
900if lt t e
901m = sub e t
902end
903fputc 123
904fyk0 tbykln
905fnum brxdec b y
906fyk tbyktx
907bryxs t m
908fyk tbykht
909fputc 34
910bryshref a l
911fputc 35
912fputc 76
913fxput b sub y b
914fputc 34
915fputc 125
916end