interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / aienux / allen / http.om

495 lines · 6914 bytes Raw Blame History
1note SPDX-License-Identifier: MIT
2note HTTP/1.1 framing for one authenticated api.github.com connection, RFC 9110/9112.
3note Only TLS-authenticated plaintext enters Hchunk. No redirects, compression or pipelining.
4global Httpreq uchar 8192
5global Httpreqn ulong 1
6global Hchunk uchar 16384
7global Hstate ulong 8
8global Hline uchar 8192
9global Hp ulong 16
10text Hget "GET "
11text Htail " HTTP/1.1\nHost: api.github.com\nUser-Agent: Allen-Omega/1.0\nAccept-Encoding: identity\nX-GitHub-Api-Version: 2022-11-28\nConnection: close\nAccept: "
12text Hjson "application/vnd.github+json"
13text Hraw "application/vnd.github.raw+json"
14text Hver "HTTP/1.1 "
15text Hcl "content-length"
16text Hte "transfer-encoding"
17text Hce "content-encoding"
18text Hckword "chunked"
19text Hident "identity"
20text Htokens "!#$%&'*+-.^_`|~"
21note Hstate: state(0idle,1reading,2done,3error),status,body bytes,generation,kind,error.
22note Hp: phase,line bytes,CR pending,header bytes,CL present,CL,TE,remaining,maxbody,interim.
23fn hplower c uchar to uchar
24if and ge c 65 le c 90
25ret uchar add ulong c 32
26end
27ret c
28end
29fn hpkw kind ulong at ulong to uchar
30if eq kind 0
31ret get Hcl at
32end
33if eq kind 1
34ret get Hte at
35end
36if eq kind 2
37ret get Hce at
38end
39if eq kind 3
40ret get Hckword at
41end
42ret get Hident at
43end
44fn hpeq at ulong n ulong kind ulong size ulong to bool
45if ne n size
46ret false
47end
48for i ulong size
49if ne hplower get Hline add at i hpkw kind i
50ret false
51end
52end
53ret true
54end
55fn hpfail code ulong to void
56put Hstate 0 3
57put Hstate 5 code
58put Hstate 2 0
59put Httpreqn 0 0
60put Hp 0 10
61end
62fn hpemit c uchar to void
63n = get Httpreqn 0
64if ge n len Httpreq
65hpfail 1
66ret
67end
68put Httpreq n c
69put Httpreqn 0 add n 1
70end
71fn hpcrlf to void
72hpemit 13
73hpemit 10
74end
75fn httpstart kind ulong gen ulong mode ulong maxbody ulong pathlen ulong to bool
76for i ulong 16
77put Hp i 0
78end
79for i ulong 8
80put Hstate i 0
81end
82put Httpreqn 0 0
83if or or eq pathlen 0 ge pathlen len Ghpath gt maxbody len Ghbody
84hpfail 2
85ret false
86end
87if or gt mode 1 ne get Ghpath 0 47
88hpfail 2
89ret false
90end
91for i ulong pathlen
92c = get Ghpath i
93if or or le c 32 ge c 127 eq c 35
94hpfail 2
95ret false
96end
97end
98for i ulong len Hget
99hpemit get Hget i
100end
101for i ulong pathlen
102hpemit get Ghpath i
103end
104for i ulong len Htail
105c = get Htail i
106if eq c 10
107hpcrlf
108else
109hpemit c
110end
111end
112if eq mode 0
113for i ulong len Hjson
114hpemit get Hjson i
115end
116else
117for i ulong len Hraw
118hpemit get Hraw i
119end
120end
121hpcrlf
122hpcrlf
123if eq get Hstate 0 3
124ret false
125end
126put Hstate 0 1
127put Hstate 3 gen
128put Hstate 4 kind
129put Hp 8 maxbody
130ret true
131end
132fn hptoken c uchar to bool
133if or or and ge c 65 le c 90 and ge c 97 le c 122 and ge c 48 le c 57
134ret true
135end
136for i ulong len Htokens
137if eq c get Htokens i
138ret true
139end
140end
141ret false
142end
143fn hpfinish to void
144put Hp 0 9
145put Hstate 0 2
146end
147fn hpstatus to void
148n = get Hp 1
149if lt n 12
150hpfail 3
151ret
152end
153for i ulong len Hver
154if ne get Hline i get Hver i
155hpfail 3
156ret
157end
158end
159s = ulong 0
160for i ulong 3
161c = get Hline add i 9
162if or lt c 48 gt c 57
163hpfail 3
164ret
165end
166s = add mul s 10 sub ulong c 48
167end
168if or lt s 100 gt s 599
169hpfail 3
170ret
171end
172if gt n 12
173if ne get Hline 12 32
174hpfail 3
175ret
176end
177end
178put Hstate 1 s
179put Hp 0 1
180end
181fn hpheadersend to void
182s = get Hstate 1
183if lt s 200
184if or or eq s 101 gt get Hp 9 2 ne get Hp 6 0
185hpfail 4
186ret
187end
188if and ne get Hp 4 0 ne get Hp 5 0
189hpfail 4
190ret
191end
192put Hp 9 add get Hp 9 1
193put Hp 0 0
194put Hp 4 0
195put Hp 5 0
196put Hp 6 0
197ret
198end
199if and ne get Hp 4 0 ne get Hp 6 0
200hpfail 5
201ret
202end
203if or eq s 204 eq s 304
204if or ne get Hp 6 0 and ne get Hp 4 0 ne get Hp 5 0
205hpfail 5
206else
207hpfinish
208end
209ret
210end
211if ne get Hp 6 0
212put Hp 0 3
213ret
214end
215if ne get Hp 4 0
216put Hp 7 get Hp 5
217if eq get Hp 7 0
218hpfinish
219else
220put Hp 0 2
221end
222ret
223end
224note Close-delimited bodies complete only after authenticated TLS close_notify via httpend.
225put Hp 0 8
226end
227fn hpdecimal at ulong stop ulong to bool
228v = ulong 0
229for i ulong sub stop at
230c = get Hline add at i
231if or lt c 48 gt c 57
232ret false
233end
234d = sub ulong c 48
235if gt v div get Hp 8 10
236ret false
237end
238v = add mul v 10 d
239if gt v get Hp 8
240ret false
241end
242end
243put Hp 5 v
244ret true
245end
246fn hpheader trailer bool to void
247n = get Hp 1
248if eq n 0
249if trailer
250hpfinish
251else
252hpheadersend
253end
254ret
255end
256p = ulong 0
257while lt p n
258if eq get Hline p 58
259break
260end
261if not hptoken get Hline p
262hpfail 6
263ret
264end
265p = add p 1
266end
267if or eq p 0 eq p n
268hpfail 6
269ret
270end
271beg = add p 1
272stop = n
273while lt beg stop
274c = get Hline beg
275if and ne c 32 ne c 9
276break
277end
278beg = add beg 1
279end
280while gt stop beg
281c = get Hline sub stop 1
282if and ne c 32 ne c 9
283break
284end
285stop = sub stop 1
286end
287cl = hpeq 0 p 0 14
288te = hpeq 0 p 1 17
289ce = hpeq 0 p 2 16
290if trailer
291if or or cl te ce
292hpfail 7
293end
294ret
295end
296if cl
297if or ne get Hp 4 0 eq beg stop
298hpfail 5
299ret
300end
301if not hpdecimal beg stop
302hpfail 8
303ret
304end
305put Hp 4 1
306end
307if te
308if or ne get Hp 6 0 not hpeq beg sub stop beg 3 7
309hpfail 5
310ret
311end
312put Hp 6 1
313end
314if ce
315if not hpeq beg sub stop beg 4 8
316hpfail 9
317end
318end
319end
320fn hphex c uchar to ulong
321if and ge c 48 le c 57
322ret sub ulong c 48
323end
324x = hplower c
325if and ge x 97 le x 102
326ret sub ulong x 87
327end
328ret 16
329end
330fn hpchunkline to void
331n = get Hp 1
332v = ulong 0
333p = ulong 0
334while lt p n
335c = get Hline p
336if eq c 59
337break
338end
339d = hphex c
340if or ge d 16 gt v div get Hp 8 16
341hpfail 10
342ret
343end
344v = add mul v 16 d
345if gt v sub get Hp 8 get Hstate 2
346hpfail 8
347ret
348end
349p = add p 1
350end
351if eq p 0
352hpfail 10
353ret
354end
355if and lt p n eq add p 1 n
356hpfail 10
357ret
358end
359put Hp 7 v
360if eq v 0
361put Hp 0 7
362else
363put Hp 0 4
364end
365end
366fn hplineend to void
367p = get Hp 0
368if eq p 0
369hpstatus
370else
371if eq p 1
372hpheader false
373else
374if eq p 3
375hpchunkline
376else
377if eq p 7
378hpheader true
379else
380hpfail 11
381end
382end
383end
384end
385put Hp 1 0
386put Hp 2 0
387end
388fn hplinebyte c uchar to void
389put Hp 3 add get Hp 3 1
390if gt get Hp 3 65536
391hpfail 12
392ret
393end
394if ne get Hp 2 0
395if ne c 10
396hpfail 6
397else
398hplineend
399end
400ret
401end
402if eq c 13
403put Hp 2 1
404ret
405end
406if or or eq c 10 and lt c 32 ne c 9 eq c 127
407hpfail 6
408ret
409end
410n = get Hp 1
411if ge n len Hline
412hpfail 12
413ret
414end
415put Hline n c
416put Hp 1 add n 1
417end
418fn hpbody c uchar to void
419n = get Hstate 2
420if ge n get Hp 8
421hpfail 8
422ret
423end
424put Ghbody n c
425put Hstate 2 add n 1
426phase = get Hp 0
427if or eq phase 2 eq phase 4
428left = sub get Hp 7 1
429put Hp 7 left
430if eq left 0
431if eq phase 2
432hpfinish
433else
434put Hp 0 5
435end
436end
437end
438end
439fn httpfeed n ulong to bool
440if or gt n len Hchunk and eq get Hstate 0 2 ne n 0
441hpfail 13
442ret false
443end
444if ne get Hstate 0 1
445ret false
446end
447for i ulong n
448c = get Hchunk i
449phase = get Hp 0
450if or or eq phase 0 eq phase 1 or eq phase 3 eq phase 7
451hplinebyte c
452else
453if or or eq phase 2 eq phase 4 eq phase 8
454hpbody c
455else
456if eq phase 5
457if eq c 13
458put Hp 0 6
459else
460hpfail 10
461end
462else
463if eq phase 6
464if eq c 10
465put Hp 0 3
466else
467hpfail 10
468end
469else
470hpfail 13
471end
472end
473end
474end
475if eq get Hstate 0 3
476ret false
477end
478end
479ret true
480end
481fn httpend auth bool to bool
482if not auth
483hpfail 14
484ret false
485end
486if eq get Hstate 0 2
487ret true
488end
489if and eq get Hstate 0 1 eq get Hp 0 8
490hpfinish
491ret true
492end
493hpfail 14
494ret false
495end