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 / seed / net / tests / https.om

224 lines · 5296 bytes Raw Blame History
1note seed/net/tests/https.om -- HTTPS GET through http.om + https.om (om/tls) for tests/check.py.
2note Composed after the om/tls units, http.om and https.om (see https.om). Program mode.
3note usage: https HOST PORT STOREFILE PATH [CONNECTHOST [IDLEMS [TOTALMS [IOSPARE]]]]
4note HOST is the TLS name (SNI, certificate check) and the Host header; CONNECTHOST (default
5note HOST) is what is resolved and dialled; STOREFILE "-" is the system trust store; IDLEMS
6note (default 30000) is the longest single wait and the head deadline in ms (hsettmo);
7note TOTALMS (default 0 = none) is one deadline over handshake, request and response (htlsdl);
8note IOSPARE (default 0 = all 262144 bytes) gives the connection an io of TLCSZ + IOSPARE bytes.
9note Writes the decoded body bytes to stdout and "status N" (the final status) to stderr, then
10note closes with close_notify. On any negative http.om result r it prints "TLSDETAIL d" (htlsdet,
11note when not 0) and "HTTPSERR r" to stderr and exits 4 (HETLS - e for a TLS code e, HETIME at a
12note deadline, HETRUNC on a short body). Exit 2 usage, 3 DNS or connect failure.
13text uusage "usage: https HOST PORT STOREFILE PATH [CONNECTHOST [IDLEMS [TOTALMS [IOSPARE]]]]\n"
14text udet "TLSDETAIL "
15text uerr "HTTPSERR "
16text ustat "status "
17text unl "\n"
18text uget "GET"
19text uhost "Host"
20text uuan "User-Agent"
21text uuav "omega-http-check/1"
22
23note decimal number from b, or 0
24fn unum b view uchar * to ulong
25v = ulong 0
26for i ulong len b
27ch = ulong get b i
28if or lt ch 48 gt ch 57
29ret 0
30end
31v = add mul v 10 sub ch 48
32if gt v 65535
33ret 0
34end
35end
36ret v
37end
38
39note signed decimal of v after the text t on stderr
40fn umsg t view uchar * v long to void
41b = new uchar 24 0
42x = write 2 t 0 len t
43k = ulong 23
44n = v
45sg = lt v 0
46if sg
47n = neg v
48end
49u = ulong n
50put b k 10
51while or gt u 0 eq k 23 bound 22
52k = sub k 1
53put b k uchar add 48 rem u 10
54u = div u 10
55end
56if sg
57k = sub k 1
58put b k 45
59end
60x = write 2 b k sub 24 k
61end
62
63note ubody: the decoded body to stdout; 0 at its end or a negative result
64fn ubody cs edit ulong * io edit uchar * to long
65d = new uchar 16384 0
66for it ulong 999999
67n = hbodyrd cs io d
68if le n 0
69ret n
70end
71x = write 1 d 0 ulong n
72end
73ret HEBIG
74end
75
76note ureq: the request and the response; hp = host length << 16 | path length; 0 or negative
77fn ureq cs edit ulong * io edit uchar * w view uchar 4096 hp ulong to long
78r = hreqst cs io uget
79r = hfe r hreqtg cs io part w 1344 and hp 65535
80r = hfe r hhname cs io uhost
81r = hfe r hhval cs io part w 0 shr hp 16
82r = hfe r hhname cs io uuan
83r = hfe r hhval cs io uuav
84r = hfe r hhend cs io 0 0
85r = hfe r hwfin cs io
86if lt r 0
87ret r
88end
89r = hrsprd cs io
90if lt r 0
91ret r
92end
93umsg ustat r
94ret ubody cs io
95end
96
97note w: host 0..256, port 256..320, store 320..1344, path 1344..2368, connect host 2368..2624
98note v: 0 host length, 1 path length, 2 store path length, 3 port, 4 connect host length, 5 idle ns
99fn uargs w edit uchar 4096 v edit ulong 8 to int
100if lt argcnt 5
101x = write 2 uusage 0 len uusage
102ret 2
103end
104hl = argread 1 w 0 255
105pl = argread 4 w 1344 1023
106sl = argread 3 w 320 1023
107pn = argread 2 w 256 6
108if or or or le hl 0 le pl 0 le sl 0 le pn 0
109ret 2
110end
111po = unum part w 256 ulong pn
112if eq po 0
113ret 2
114end
115put v 0 ulong hl
116put v 1 ulong pl
117put v 2 ulong sl
118put v 3 po
119tls_mov w 2368 0 ulong hl
120if ge argcnt 6
121hl = argread 5 w 2368 255
122if le hl 0
123ret 2
124end
125end
126put v 4 ulong hl
127put v 5 30000000000
128if ne uopt w v 6 5 1000000 0
129ret 2
130end
131if ne uopt w v 7 6 1000000 0
132ret 2
133end
134if ne uopt w v 8 7 1 0
135ret 2
136end
137ret 0
138end
139
140note uopt: optional argument i, a decimal, times m into v[j]; 0, or 2 when it is malformed
141fn uopt w edit uchar 4096 v edit ulong 8 i ulong j ulong m ulong to int
142if le argcnt long i
143ret 0
144end
145hl = argread i w 2624 8
146if le hl 0
147ret 2
148end
149put v j mul unum part w 2624 ulong hl m
150ret 0
151end
152
153note ustore: the trust store from STOREFILE ("-" = system); 0 or HETLS - e
154fn ustore st edit uchar 262144 w view uchar 4096 sl ulong to long
155e = ulong 0
156if and eq sl 1 eq get w 320 45
157e = tls_store st
158else
159e = tls_storef st part w 320 sl
160end
161if ne e 0
162ret sub HETLS long e
163end
164ret 0
165end
166
167note uconn: dial and the TLS setup (hinit, htlsinit); 0, -1000 (connect) or a negative result
168fn uconn cs edit ulong * io edit uchar * w view uchar 4096 v view ulong 8 to long
169fd = tls_dial part w 2368 get v 4 get v 3
170if lt fd 0
171ret -1000
172end
173r = hinit cs io int fd 0
174hsettmo cs long get v 5 long get v 5
175htlsdl cs long get v 6
176ret hfe r htlsinit cs io part w 0 get v 0
177end
178
179note urun: connect, handshake, request and response on io, then the TLS detail of a failure and
180note close_notify; 0, -1000 (connect) or a negative result
181fn urun cs edit ulong 320 io edit uchar * st view uchar 262144 w view uchar 4096 v view ulong 8 to long
182r = uconn cs io w v
183if ge r 0
184r = htlsconn cs io st
185end
186if ge r 0
187r = ureq cs io w or shl get v 0 16 get v 1
188end
189dt = htlsdet cs io
190if and lt r 0 ne dt 0
191umsg udet long dt
192end
193x = htlsend cs io
194ret r
195end
196
197fn main to int
198cs = new ulong 320 0
199io = new uchar 262144 0
200st = new uchar 262144 0
201w = new uchar 4096 0
202v = new ulong 8 0
203e = uargs w v
204if ne e 0
205ret e
206end
207r = ustore st w get v 2
208if ge r 0
209k = get v 7
210if eq k 0
211r = urun cs io st w v
212else
213r = urun cs part io 0 add TLCSZ k st w v
214end
215end
216if eq r -1000
217ret 3
218end
219if lt r 0
220umsg uerr r
221ret 4
222end
223ret 0
224end