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