interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / tools / bring / tests / standin.om

753 lines · 17230 bytes Raw Blame History
1om2
2note tools/bring/tests/standin.om: the stand-in Interplane hub for the tool's tests (Linux, pinned omc).
3note standin serve PORT DIR answers the contract's sign-in, /open/, /open/up chunk + finish, status,
4note shelf JSON and archive on 127.0.0.1:PORT, one connection at a time
5note standin count PORT DIR accepts and closes connections, appending one line per connection
6note Every request line is appended to DIR/log; every accepted connection to DIR/conns. Saved:
7note DIR/login.form, DIR/up.b64 (chunk data in seq order), DIR/finish.form. GET /login starts a new
8note upload (the injection and status state are reset). Injections (when the
9note file DIR/inject exists): the first try of seq 1 answers 429, and the first seq 2 answers
10note "409 next 1" (the client must resend chunk 1). The status JSON says "queued" once, then "stored"
11note (or the line in DIR/refuse as "refused: <line>" when that file exists). GET /open/<rid>.json
12note serves DIR/shelf.json and GET /open/<rid>.tar serves DIR/shelf.tar. Busy injections: DIR/busyj
13note (for the JSON) and DIR/busyt (for the archive) hold 3-digit codes, e.g. "503503"; each GET
14note answers the first code with an empty body and drops it; a "+" after the first code ("503+")
15note answers it every time. An empty or absent file serves normally. DIR/busyp does the same for chunk
16note POSTs and DIR/busyf for the finish POST (nothing is saved for a busy answer).
17note Withdraw: POST /open/ (origin, session, csrf as for /open/up; DIR/busyp applies) saves its form to
18note DIR/withdraw.form, answers 303 with Location: /open/ and writes DIR/gone = "1": the next shelf
19note JSON GET still serves DIR/shelf.json (the intake has not run yet) and sets it to "2"; from then on
20note the JSON is 404. When DIR/signout exists it answers 303 with Location: /login and saves nothing
21note (the hub when the session is gone).
22note Patch: POST /alice/demo/patches/new (session and csrf as for /open/up) is answered from DIR/busyq,
23note 3-digit codes as in DIR/busyp: a 429 with the forge's write-gate reason ("refused: too many
24note writes ..."), any other code with an empty body; no code saves the form to DIR/patch.form and
25note answers 201 with "path /alice/demo/patches/1". Every POST is one line of DIR/log.
26text tserve "serve"
27text tcount "count"
28text tlog "log"
29text tconns "conns"
30text tconn "conn\n"
31text tlogin "login.form"
32text tupb "up.b64"
33text tfin "finish.form"
34text tinject "inject"
35text trefuse "refuse"
36text tshelfj "shelf.json"
37text tshelft "shelf.tar"
38text tbusyj "busyj"
39text tbusyt "busyt"
40text tbusyp "busyp"
41text tbusyf "busyf"
42text tbusyq "busyq"
43text tptform "patch.form"
44text tpostpt "POST /alice/demo/patches/new "
45text tgetset "GET /settings "
46text tptpath "path /alice/demo/patches/1\n"
47text tptgate "refused: too many writes from this address or key; wait a minute\n"
48text tgone "gone"
49text twdform "withdraw.form"
50text tpostop "POST /open/ "
51text tone "1"
52text tsignout "signout"
53text tlocop "Location: /open/\n"
54text tloclg "Location: /login\n"
55text tgetlog "GET /login "
56text tpostlg "POST /login "
57text tgetop "GET /open/ "
58text tpostup "POST /open/up "
59text tgetup "GET /open/up/"
60text tgetopn "GET /open/"
61text tjs ".json "
62text ttar ".tar "
63text tpreck "__Host-forge_pre=0123456789abcdef0123456789abcdef"
64text tsessck "__Host-forge_session=feedfacefeedfacefeedfacefeedfacefeedfacefeedfacefeedfacefeedface"
65text tcsrf1 "csrf=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
66text tcsrf2 "csrf=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
67text torig "\norigin: http://127.0.0.1:"
68text tlpage "<form><input type=\"hidden\" name=\"csrf\" value=\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\">\n<input type=\"hidden\" name=\"nonce\" value=\"00112233445566778899aabbccddeeff\">\n<input type=\"hidden\" name=\"ts\" value=\"1762848000\"></form>\n"
69text topage "<form><input type=\"hidden\" name=\"csrf\" value=\"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"></form>\n"
70text tsc1 "Set-Cookie: __Host-forge_pre=0123456789abcdef0123456789abcdef; Max-Age=900; HttpOnly; Secure; SameSite=Lax; Path=/\n"
71text tsc2 "Set-Cookie: __Host-forge_session=feedfacefeedfacefeedfacefeedfacefeedfacefeedfacefeedfacefeedface; Max-Age=2592000; HttpOnly; Secure; SameSite=Lax; Path=/\nLocation: /\n"
72text tachunk "action=chunk"
73text tafin "action=finish"
74text tseq "&seq="
75text tdata "&data="
76text tsha "&sha="
77text tnext "next "
78text tqueued "queued "
79text tj1 "{\"schema\":\"interplane/open-upload/1\",\"state\":\"queued\",\"rid\":null,\"page\":null}\n"
80text tj2 "{\"schema\":\"interplane/open-upload/1\",\"state\":\"stored\",\"rid\":\"rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5\",\"page\":\"/open/z3gqcJUoA1n9HaHKufZs5FCSGazv5\"}\n"
81text tj3a "{\"schema\":\"interplane/open-upload/1\",\"state\":\"refused: "
82text tj3b "\",\"rid\":null,\"page\":null}\n"
83text th1 "HTTP/1.1 "
84text th2 "\nContent-Type: "
85text th3 "\nContent-Length: "
86text th4 "\nConnection: close\n"
87text tplain "text/plain"
88text thtml "text/html"
89text tjson "application/json"
90text tbin "application/x-tar"
91text tclen "content-length:"
92text tcookie "cookie:"
93text tusage "usage: standin serve|count PORT DIR\n"
94global gst ulong 8
95
96fn wall fd int b view uchar * to bool
97n = ulong 0
98while lt n len b bound 9223372036854775807
99r = write fd b n sub len b n
100if le r 0
101ret false
102end
103n = add n ulong r
104end
105ret true
106end
107note hput: text with each LF written as CR LF
108fn hput fd int t view uchar * to void
109cr = new uchar 2 0
110put cr 0 13
111put cr 1 10
112s = ulong 0
113for i ulong len t bound 9223372036854775807
114if eq get t i 10
115x = wall fd part t s sub i s
116x = wall fd cr
117s = add i 1
118end
119end
120x = wall fd part t s sub len t s
121end
122fn decw v ulong o edit uchar * n ulong to ulong
123d = new uchar 20 0
124k = ulong 0
125x = v
126while true bound 21
127put d k uchar add 48 rem x 10
128k = add k 1
129x = div x 10
130if eq x 0
131break
132end
133end
134for i ulong k
135put o add n i get d sub sub k i 1
136end
137ret add n k
138end
139fn bat b view uchar * i ulong t view uchar * to bool
140if gt add i len t len b
141ret false
142end
143for k ulong len t bound 9223372036854775807
144if ne get b add i k get t k
145ret false
146end
147end
148ret true
149end
150fn lcb c uchar to uchar
151if and ge c 65 le c 90
152ret add c 32
153end
154ret c
155end
156fn bati b view uchar * i ulong t view uchar * to bool
157if gt add i len t len b
158ret false
159end
160for k ulong len t bound 9223372036854775807
161if ne lcb get b add i k get t k
162ret false
163end
164end
165ret true
166end
167fn bfind b view uchar * t view uchar * to long
168i = ulong 0
169while le add i len t len b bound 9223372036854775807
170if bat b i t
171ret long i
172end
173i = add i 1
174end
175ret -1
176end
177fn bfindi b view uchar * t view uchar * to long
178i = ulong 0
179while le add i len t len b bound 9223372036854775807
180if bati b i t
181ret long i
182end
183i = add i 1
184end
185ret -1
186end
187note decat: the decimal number at b[i..] (stops at the first non-digit)
188fn decat b view uchar * i ulong to ulong
189v = ulong 0
190j = i
191while lt j len b bound 9223372036854775807
192c = get b j
193if or lt c 48 gt c 57
194break
195end
196v = add mul v 10 ulong sub c 48
197j = add j 1
198end
199ret v
200end
201fn appendf nm view uchar * b view uchar * to void
202fd = fopen nm 0 len nm 525377 384
203if lt fd 0
204exit 3
205end
206x = wall int fd b
207y = fclose int fd
208end
209fn exists nm view uchar * to bool
210fd = fopen nm 0 len nm 524288 0
211if lt fd 0
212ret false
213end
214x = fclose int fd
215ret true
216end
217note answer: status line, headers (extra holds whole header lines), body
218fn hct fd int ct ulong to void
219if eq ct 0
220hput fd tplain
221end
222if eq ct 1
223hput fd thtml
224end
225if eq ct 2
226hput fd tjson
227end
228if eq ct 3
229hput fd tbin
230end
231end
232fn answer fd int code ulong ct ulong ex ulong body view uchar * to void
233ahead fd code ct ex len body
234x = wall fd body
235end
236fn ahead fd int code ulong ct ulong ex ulong blen ulong to void
237d = new uchar 24 0
238k = decw code d 0
239hput fd th1
240x = wall fd part d 0 k
241hput fd th2
242hct fd ct
243hput fd th3
244k = decw blen d 0
245x = wall fd part d 0 k
246hput fd th4
247if eq ex 1
248hput fd tsc1
249end
250if eq ex 2
251hput fd tsc2
252end
253if eq ex 3
254hput fd tlocop
255end
256if eq ex 4
257hput fd tloclg
258end
259cr = new uchar 2 0
260put cr 0 13
261put cr 1 10
262x = wall fd cr
263end
264fn answerf fd int ct ulong nm view uchar * to void
265f = fopen nm 0 len nm 524288 0
266if lt f 0
267answer fd 404 0 0 part tlog 0 0
268ret
269end
270st = new uchar 128 0
271r = fstat int f st 0 128
272sz = ulong 0
273for i ulong 8
274sz = or sz shl ulong get st add 48 i mul i 8
275end
276ahead fd 200 ct 0 sz
277b = new uchar 65536 0
278while true bound 9223372036854775807
279n = read int f b 0 len b
280if le n 0
281break
282end
283y = wall fd part b 0 ulong n
284end
285x = fclose int f
286end
287note readreq: one request (headers and Content-Length body) into rq; returns its length or 0
288fn readreq rq edit uchar * fd int to ulong
289n = ulong 0
290hend = long -1
291while true bound 9223372036854775807
292if ge n len rq
293ret 0
294end
295r = read fd rq n sub len rq n
296if le r 0
297ret 0
298end
299n = add n ulong r
300hend = hdrend rq n
301if ge hend 0
302break
303end
304end
305want = add ulong hend bodylen rq ulong hend
306if gt want len rq
307ret 0
308end
309while lt n want bound 9223372036854775807
310r = read fd rq n sub want n
311if le r 0
312ret 0
313end
314n = add n ulong r
315end
316ret n
317end
318fn hdrend rq edit uchar * n ulong to long
319for i ulong n bound 9223372036854775807
320if gt add i 4 n
321ret -1
322end
323if and eq get rq i 13 eq get rq add i 1 10
324if and eq get rq add i 2 13 eq get rq add i 3 10
325ret long add i 4
326end
327end
328end
329ret -1
330end
331fn bodylen rq edit uchar * h ulong to ulong
332p = bfindi part rq 0 h tclen
333if lt p 0
334ret 0
335end
336i = add ulong p len tclen
337while eq get rq i 32 bound 9223372036854775807
338i = add i 1
339end
340ret decat part rq 0 h i
341end
342fn logline rq edit uchar * n ulong to void
343e = ulong 0
344while lt e n bound 9223372036854775807
345if eq get rq e 13
346break
347end
348e = add e 1
349end
350b = new uchar 8192 0
351if gt e 8000
352e = ulong 8000
353end
354for i ulong e bound 8000
355put b i get rq i
356end
357put b e 10
358appendf tlog part b 0 add e 1
359end
360note has: the request (headers or body) holds t
361fn has rq edit uchar * n ulong t view uchar * to bool
362ret ge bfind part rq 0 n t 0
363end
364fn hasi rq edit uchar * n ulong t view uchar * to bool
365ret ge bfindi part rq 0 n t 0
366end
367fn route rq edit uchar * fd int n ulong port ulong to void
368if bat part rq 0 n 0 tgetlog
369for i ulong 8
370put gst i 0
371end
372answer fd 200 1 1 tlpage
373ret
374end
375if bat part rq 0 n 0 tgetopn
376if not bat part rq 0 n 0 tgetop
377if not bat part rq 0 n 0 tgetup
378public rq fd n
379ret
380end
381end
382end
383if not origin rq n port
384if bat part rq 0 n 0 tpostlg
385answer fd 403 0 0 part tlog 0 0
386ret
387end
388if bat part rq 0 n 0 tpostup
389answer fd 403 0 0 part tlog 0 0
390ret
391end
392if bat part rq 0 n 0 tpostop
393answer fd 403 0 0 part tlog 0 0
394ret
395end
396end
397if bat part rq 0 n 0 tpostlg
398if or not has rq n tpreck not has rq n tcsrf1
399answer fd 403 0 0 part tlog 0 0
400ret
401end
402savebody rq n tlogin
403answer fd 303 0 2 part tlog 0 0
404ret
405end
406if not has rq n tsessck
407answer fd 403 0 0 part tlog 0 0
408ret
409end
410if bat part rq 0 n 0 tgetop
411answer fd 200 1 0 topage
412ret
413end
414if bat part rq 0 n 0 tpostup
415if not has rq n tcsrf2
416answer fd 403 0 0 part tlog 0 0
417ret
418end
419postup rq fd n
420ret
421end
422if bat part rq 0 n 0 tpostop
423if not has rq n tcsrf2
424answer fd 403 0 0 part tlog 0 0
425ret
426end
427if busyans fd tbusyp
428ret
429end
430if exists tsignout
431answer fd 303 0 4 part tlog 0 0
432ret
433end
434savebody rq n twdform
435g = fopen tgone 0 len tgone 524865 384
436x = wall int g tone
437y = fclose int g
438answer fd 303 0 3 part tlog 0 0
439ret
440end
441if bat part rq 0 n 0 tgetup
442status fd
443ret
444end
445if bat part rq 0 n 0 tgetset
446answer fd 200 1 0 topage
447ret
448end
449if bat part rq 0 n 0 tpostpt
450if not has rq n tcsrf2
451answer fd 403 0 0 part tlog 0 0
452ret
453end
454postpt rq fd n
455ret
456end
457answer fd 404 0 0 part tlog 0 0
458end
459note public: the shelf JSON and the archive need no session (as forged serves them)
460fn public rq edit uchar * fd int n ulong to void
461if has rq n tjs
462if busyans fd tbusyj
463ret
464end
465if gonejs fd
466ret
467end
468answerf fd 2 tshelfj
469ret
470end
471if has rq n ttar
472if busyans fd tbusyt
473ret
474end
475answerf fd 3 tshelft
476ret
477end
478answer fd 404 0 0 part tlog 0 0
479end
480note gonejs: after a withdraw, "1" in DIR/gone serves the shelf once more and becomes "2"; "2" is 404
481fn gonejs fd int to bool
482f = fopen tgone 0 len tgone 524290 0
483if lt f 0
484ret false
485end
486b = new uchar 1 0
487r = read int f b 0 1
488if and eq r 1 eq get b 0 49
489put b 0 50
490w = pwrite int f b 0 1 0
491x = fclose int f
492ret false
493end
494x = fclose int f
495answer fd 404 0 0 part tlog 0 0
496ret true
497end
498note busyans: the first 3-digit code in DIR/nm, answered with an empty body; true when one was sent
499fn busyans fd int nm view uchar * to bool
500c = busycode nm
501if eq c 0
502ret false
503end
504answer fd c 0 0 part tlog 0 0
505ret true
506end
507note busycode: the first 3-digit code in DIR/nm, dropped unless a "+" follows it; 0 when there is none
508fn busycode nm view uchar * to ulong
509f = fopen nm 0 len nm 524290 0
510if lt f 0
511ret 0
512end
513b = new uchar 4096 0
514r = read int f b 0 len b
515if lt r 3
516x = fclose int f
517ret 0
518end
519k = ulong r
520c = decat part b 0 3 0
521if not and gt k 3 eq get b 3 43
522w = pwrite int f b 3 sub k 3 0
523t = ftrunc int f long sub k 3
524end
525x = fclose int f
526ret c
527end
528note postpt: the patch upload; DIR/busyq first (a 429 carries the write gate's reason line)
529fn postpt rq edit uchar * fd int n ulong to void
530c = busycode tbusyq
531if eq c 429
532answer fd 429 0 0 tptgate
533ret
534end
535if ne c 0
536answer fd c 0 0 part tlog 0 0
537ret
538end
539savebody rq n tptform
540answer fd 201 0 0 tptpath
541end
542fn origin rq edit uchar * n ulong port ulong to bool
543o = new uchar 64 0
544k = ulong 0
545for i ulong len torig
546put o i get torig i
547end
548k = decw port o len torig
549cr = new uchar 1 0
550put o k 13
551ret hasi rq n part o 0 add k 1
552end
553fn savebody rq edit uchar * n ulong nm view uchar * to void
554h = hdrend rq n
555appendf nm part rq ulong h sub n ulong h
556end
557fn status fd int to void
558put gst 3 add get gst 3 1
559b = new uchar 4096 0
560if exists trefuse
561k = ulong 0
562for i ulong len tj3a
563put b i get tj3a i
564end
565k = len tj3a
566f = fopen trefuse 0 len trefuse 524288 0
567r = read int f b k 200
568x = fclose int f
569k = add k ulong r
570while and gt k 0 eq get b sub k 1 10 bound 4096
571k = sub k 1
572end
573for i ulong len tj3b
574put b add k i get tj3b i
575end
576answer fd 200 2 0 part b 0 add k len tj3b
577ret
578end
579if eq get gst 3 1
580answer fd 200 2 0 tj1
581ret
582end
583answer fd 200 2 0 tj2
584end
585note postup: gst 0 = the next seq, 1 = 429 injected, 2 = 409 injected
586fn postup rq edit uchar * fd int n ulong to void
587h = ulong hdrend rq n
588if has rq n tafin
589if busyans fd tbusyf
590ret
591end
592postfin rq fd n
593ret
594end
595if busyans fd tbusyp
596ret
597end
598p = bfind part rq h sub n h tseq
599if lt p 0
600answer fd 400 0 0 part tlog 0 0
601ret
602end
603postchk rq fd n ulong p
604end
605fn postfin rq edit uchar * fd int n ulong to void
606h = ulong hdrend rq n
607savebody rq n tfin
608p = bfind part rq h sub n h tsha
609b = new uchar 128 0
610k = ulong 0
611for i ulong len tqueued
612put b i get tqueued i
613end
614k = len tqueued
615for i ulong 64
616put b add k i get rq add add add h ulong p len tsha i
617end
618put b add k 64 10
619answer fd 200 0 0 part b 0 add k 65
620end
621fn postchk rq edit uchar * fd int n ulong p ulong to void
622h = ulong hdrend rq n
623seq = decat rq add add h p len tseq
624inj = exists tinject
625if and inj and eq seq 1 eq get gst 1 0
626put gst 1 1
627answer fd 429 0 0 part tlog 0 0
628ret
629end
630if and inj and eq seq 2 eq get gst 2 0
631put gst 2 1
632put gst 0 1
633nextans fd 409 1
634ret
635end
636if ne seq get gst 0
637nextans fd 409 get gst 0
638ret
639end
640chunkdata rq h n seq
641put gst 0 add seq 1
642nextans fd 200 add seq 1
643end
644note chunkdata: up.b64 holds the data of seq 0..seq-1 as one line each; a resent seq rewrites from it
645fn chunkdata rq edit uchar * h ulong n ulong seq ulong to void
646p = bfind part rq h sub n h tdata
647if lt p 0
648exit 5
649end
650s = add add h ulong p len tdata
651e = s
652while lt e n bound 9223372036854775807
653if eq get rq e 38
654break
655end
656e = add e 1
657end
658truncl seq
659appendf tupb part rq s sub e s
660nl = new uchar 1 0
661put nl 0 10
662appendf tupb nl
663end
664note truncl: cut up.b64 after its first k lines (a 409 rewinds the client)
665fn truncl k ulong to void
666f = fopen tupb 0 len tupb 524290 384
667if lt f 0
668ret
669end
670b = new uchar 65536 0
671seen = ulong 0
672pos = ulong 0
673while lt seen k bound 9223372036854775807
674r = read int f b 0 len b
675if le r 0
676break
677end
678for i ulong ulong r bound 65536
679if lt seen k
680pos = add pos 1
681if eq get b i 10
682seen = add seen 1
683end
684end
685end
686end
687x = ftrunc int f long pos
688x = fclose int f
689end
690fn nextans fd int code ulong v ulong to void
691b = new uchar 40 0
692k = ulong 0
693for i ulong len tnext
694put b i get tnext i
695end
696k = decw v b len tnext
697put b k 10
698answer fd code 0 0 part b 0 add k 1
699end
700fn main to int
701if ne argcnt 4
702x = write 2 tusage 0 len tusage
703ret 2
704end
705m = new uchar 16 0
706mn = argread 1 m 0 16
707pb = new uchar 16 0
708pn = argread 2 pb 0 16
709port = decat part pb 0 ulong pn 0
710d = new uchar 4096 0
711dn = argread 3 d 0 4096
712if lt chdir d 0 ulong dn 0
713ret 2
714end
715s = socknew 2 1 0
716one = new uchar 4 0
717put one 0 1
718x = sockopt int s 1 2 one 0 4
719sa = new uchar 16 0
720put sa 0 2
721put sa 2 uchar shr port 8
722put sa 3 uchar and port 255
723put sa 4 127
724put sa 7 1
725if lt bind int s sa 0 16 0
726ret 3
727end
728if lt listen int s 64 0
729ret 3
730end
731counting = and eq mn 5 bat m 0 tcount
732while true bound 9223372036854775807
733c = accept int s 0
734if lt c 0
735continue
736end
737appendf tconns tconn
738if not counting
739serve1 int c port
740end
741x = fclose int c
742end
743ret 0
744end
745note serve1: one request of at most 1 MiB (a chunk form is about 60 KB); larger ones are not read
746fn serve1 c int port ulong to void
747rq = new uchar 1048576 0
748n = readreq rq c
749if gt n 0
750logline rq n
751route rq c n port
752end
753end