spark-connector
aien-dev/spark-connector
Brought by drakestapleton
spark-connector / runner / 20net.om
| 1 | note runner/20net.om -- outbound HTTP(S) to the hub, and the ipcc1 messages (docs/PROTOCOL.md). |
| 2 | note The runner only ever dials out: it opens no listening socket. https:// URLs use the om/tls |
| 3 | note TLS 1.3 client with the system trust store; plain http:// is accepted only for 127.0.0.1 |
| 4 | note (a private test hub on the same machine). |
| 5 | |
| 6 | text rtget "GET" |
| 7 | text rtpost "POST" |
| 8 | text rthost "Host" |
| 9 | text rtuan "User-Agent" |
| 10 | text rtuav "interplane-runner/1" |
| 11 | text rtctn "Content-Type" |
| 12 | text rtctv "text/plain" |
| 13 | text rthttps "https://" |
| 14 | text rthttp "http://127.0.0.1:" |
| 15 | text rtlo "127.0.0.1" |
| 16 | text rtapi "/community/api" |
| 17 | text rtsalv "/salvage/" |
| 18 | text rtrecs "/record" |
| 19 | text rtipcc "ipcc1 " |
| 20 | text rtsig "sig" |
| 21 | text rtkeyn "key" |
| 22 | text rttime "time" |
| 23 | |
| 24 | note r_hubset: parse the hub URL u ("https://HOST" or "https://HOST:PORT", or for tests |
| 25 | note "http://127.0.0.1:PORT"); sets rhub, rhost, rport, rtls |
| 26 | fn r_hubset u view uchar * to bool |
| 27 | if or lt len u 9 gt len u 300 |
| 28 | ret false |
| 29 | end |
| 30 | tls = true |
| 31 | o = len rthttps |
| 32 | ht = false |
| 33 | if gt len u len rthttp |
| 34 | ht = r_eq part u 0 len rthttp rthttp |
| 35 | end |
| 36 | if ht |
| 37 | tls = false |
| 38 | o = sub len rthttp 1 |
| 39 | r_hostset rtlo |
| 40 | else |
| 41 | if not r_eq part u 0 len rthttps rthttps |
| 42 | ret false |
| 43 | end |
| 44 | end |
| 45 | e = len u |
| 46 | c = r_colon u o |
| 47 | if tls |
| 48 | if not r_hostok part u o sub c o |
| 49 | ret false |
| 50 | end |
| 51 | r_hostset part u o sub c o |
| 52 | end |
| 53 | port = ulong 443 |
| 54 | if lt c e |
| 55 | port = r_undec part u add c 1 sub sub e c 1 |
| 56 | if or eq port 0 gt port 65535 |
| 57 | ret false |
| 58 | end |
| 59 | else |
| 60 | if not tls |
| 61 | ret false |
| 62 | end |
| 63 | end |
| 64 | put rgv rport port |
| 65 | put rgv rtls 0 |
| 66 | if tls |
| 67 | put rgv rtls 1 |
| 68 | end |
| 69 | for i ulong e bound 512 |
| 70 | put rhub i get u i |
| 71 | end |
| 72 | put rgv rhubl e |
| 73 | ret true |
| 74 | end |
| 75 | |
| 76 | note r_colon: the index of the first ':' in u at or after o, or len u |
| 77 | fn r_colon u view uchar * o ulong to ulong |
| 78 | c = o |
| 79 | while lt c len u bound 512 |
| 80 | if eq get u c 58 |
| 81 | ret c |
| 82 | end |
| 83 | c = add c 1 |
| 84 | end |
| 85 | ret c |
| 86 | end |
| 87 | |
| 88 | note r_hostok: 1..253 bytes of a-z 0-9 . - |
| 89 | fn r_hostok s view uchar * to bool |
| 90 | if or eq len s 0 gt len s 253 |
| 91 | ret false |
| 92 | end |
| 93 | for i ulong len s bound 256 |
| 94 | ch = get s i |
| 95 | lo = and ge ch 97 le ch 122 |
| 96 | dg = and ge ch 48 le ch 57 |
| 97 | if not or or lo dg or eq ch 46 eq ch 45 |
| 98 | ret false |
| 99 | end |
| 100 | end |
| 101 | ret true |
| 102 | end |
| 103 | |
| 104 | fn r_hostset s view uchar * to void |
| 105 | for i ulong len s bound 256 |
| 106 | put rhost i get s i |
| 107 | end |
| 108 | put rgv rhstl len s |
| 109 | end |
| 110 | |
| 111 | note r_call: one request to the hub; GET when rgv rpost is 0, else POST of rmsg. The response body |
| 112 | note goes to rrsp. Returns the HTTP status, or negative: -1000 connect, -2000 trust store, |
| 113 | note -413 body larger than rrsp, else the http.om / TLS result. |
| 114 | fn r_call pth view uchar * to long |
| 115 | cs = new ulong 320 0 |
| 116 | io = new uchar 262144 0 |
| 117 | st = new uchar 262144 0 |
| 118 | hb = new uchar 256 0 |
| 119 | hl = get rgv rhstl |
| 120 | for i ulong hl bound 256 |
| 121 | put hb i get rhost i |
| 122 | end |
| 123 | tls = eq get rgv rtls 1 |
| 124 | if tls |
| 125 | e = tls_store st |
| 126 | if ne e 0 |
| 127 | ret -2000 |
| 128 | end |
| 129 | end |
| 130 | fd = tls_dial part hb 0 hl get rgv rport |
| 131 | if lt fd 0 |
| 132 | ret -1000 |
| 133 | end |
| 134 | r = hinit cs io int fd 0 |
| 135 | hsettmo cs 30000000000 30000000000 |
| 136 | htlsdl cs 120000000000 |
| 137 | if tls |
| 138 | r = hfe r htlsinit cs io part hb 0 hl |
| 139 | if ge r 0 |
| 140 | r = htlsconn cs io st |
| 141 | end |
| 142 | end |
| 143 | if ge r 0 |
| 144 | r = r_xchg cs io pth |
| 145 | end |
| 146 | if tls |
| 147 | x = htlsend cs io |
| 148 | else |
| 149 | x = hclose cs |
| 150 | end |
| 151 | ret r |
| 152 | end |
| 153 | |
| 154 | fn r_xchg cs edit ulong * io edit uchar * pth view uchar * to long |
| 155 | r = r_xhead cs io pth |
| 156 | if lt r 0 |
| 157 | ret r |
| 158 | end |
| 159 | ret r_xresp cs io |
| 160 | end |
| 161 | |
| 162 | note r_xhead: the request line, headers and (POST) body |
| 163 | fn r_xhead cs edit ulong * io edit uchar * pth view uchar * to long |
| 164 | hb = new uchar 256 0 |
| 165 | hl = get rgv rhstl |
| 166 | for i ulong hl bound 256 |
| 167 | put hb i get rhost i |
| 168 | end |
| 169 | post = eq get rgv rpost 1 |
| 170 | r = long 0 |
| 171 | if post |
| 172 | r = hreqst cs io rtpost |
| 173 | else |
| 174 | r = hreqst cs io rtget |
| 175 | end |
| 176 | r = hfe r hreqtg cs io pth |
| 177 | r = hfe r hhname cs io rthost |
| 178 | r = hfe r hhval cs io part hb 0 hl |
| 179 | r = hfe r hhname cs io rtuan |
| 180 | r = hfe r hhval cs io rtuav |
| 181 | if post |
| 182 | r = hfe r r_xbody cs io |
| 183 | else |
| 184 | r = hfe r hhend cs io 0 0 |
| 185 | end |
| 186 | ret hfe r hwfin cs io |
| 187 | end |
| 188 | |
| 189 | fn r_xbody cs edit ulong * io edit uchar * to long |
| 190 | mb = new uchar 8192 0 |
| 191 | n = get rgv rmsgl |
| 192 | for i ulong n bound 8192 |
| 193 | put mb i get rmsg i |
| 194 | end |
| 195 | r = hhname cs io rtctn |
| 196 | r = hfe r hhval cs io rtctv |
| 197 | r = hfe r hhend cs io 0 n |
| 198 | ret hfe r hput cs io part mb 0 n |
| 199 | end |
| 200 | |
| 201 | note r_xresp: the status, and the body into rrsp |
| 202 | fn r_xresp cs edit ulong * io edit uchar * to long |
| 203 | d = new uchar 16384 0 |
| 204 | s = hrsprd cs io |
| 205 | if lt s 0 |
| 206 | ret s |
| 207 | end |
| 208 | put rgv rrspl 0 |
| 209 | for it ulong 999999 |
| 210 | k = hbodyrd cs io d |
| 211 | if lt k 0 |
| 212 | ret k |
| 213 | end |
| 214 | if eq k 0 |
| 215 | ret s |
| 216 | end |
| 217 | if not r_rspapp part d 0 ulong k |
| 218 | ret -413 |
| 219 | end |
| 220 | end |
| 221 | ret -413 |
| 222 | end |
| 223 | |
| 224 | note r_rspapp: append d to rrsp; false when it would not fit |
| 225 | fn r_rspapp d view uchar * to bool |
| 226 | m = get rgv rrspl |
| 227 | if gt add m len d len rrsp |
| 228 | ret false |
| 229 | end |
| 230 | for j ulong len d bound 16384 |
| 231 | put rrsp add m j get d j |
| 232 | end |
| 233 | put rgv rrspl add m len d |
| 234 | ret true |
| 235 | end |
| 236 | |
| 237 | note ---- messages |
| 238 | fn r_madd s view uchar * to void |
| 239 | n = get rgv rmsgl |
| 240 | for i ulong len s bound 8192 |
| 241 | put rmsg add n i get s i |
| 242 | end |
| 243 | put rgv rmsgl add n len s |
| 244 | end |
| 245 | |
| 246 | fn r_mbeg k view uchar * to void |
| 247 | put rgv rmsgl 0 |
| 248 | r_madd rtipcc |
| 249 | r_madd k |
| 250 | r_madd rtnl |
| 251 | end |
| 252 | |
| 253 | fn r_mline nm view uchar * v view uchar * to void |
| 254 | r_madd nm |
| 255 | r_madd rtsp |
| 256 | r_madd v |
| 257 | r_madd rtnl |
| 258 | end |
| 259 | |
| 260 | fn r_mnum nm view uchar * v ulong to void |
| 261 | d = new uchar 20 0 |
| 262 | k = r_dec d 0 v |
| 263 | r_mline nm part d 0 k |
| 264 | end |
| 265 | |
| 266 | note r_mkey: "key <key id>" |
| 267 | fn r_mkey to void |
| 268 | k = new uchar 64 0 |
| 269 | for i ulong 64 |
| 270 | put k i get rkid i |
| 271 | end |
| 272 | r_mline rtkeyn k |
| 273 | end |
| 274 | |
| 275 | note r_mjf: "nm <rjf[o, o+n)>" |
| 276 | fn r_mjf nm view uchar * o ulong n ulong to void |
| 277 | d = new uchar 256 0 |
| 278 | for i ulong n bound 256 |
| 279 | put d i get rjf add o i |
| 280 | end |
| 281 | r_mline nm part d 0 n |
| 282 | end |
| 283 | |
| 284 | note r_mtime: "time <unix seconds>" |
| 285 | fn r_mtime to void |
| 286 | r_mnum rttime ulong div realns 1000000000 |
| 287 | end |
| 288 | |
| 289 | note r_msign: append "sig <r||s hex>" over every byte of rmsg so far |
| 290 | fn r_msign to bool |
| 291 | m = new uchar 8192 0 |
| 292 | sg = new uchar 128 0 |
| 293 | n = get rgv rmsgl |
| 294 | for i ulong n bound 8192 |
| 295 | put m i get rmsg i |
| 296 | end |
| 297 | if not r_ksign part m 0 n sg |
| 298 | ret false |
| 299 | end |
| 300 | r_mline rtsig sg |
| 301 | ret true |
| 302 | end |
| 303 | |
| 304 | note r_post: POST rmsg to the hub API; the HTTP status or a negative r_call result |
| 305 | fn r_post to long |
| 306 | put rgv rpost 1 |
| 307 | ret r_call rtapi |
| 308 | end |
| 309 | |
| 310 | note r_rspis: the response body starts with t |
| 311 | fn r_rspis t view uchar * to bool |
| 312 | if lt get rgv rrspl len t |
| 313 | ret false |
| 314 | end |
| 315 | for i ulong len t bound 4096 |
| 316 | if ne get rrsp i get t i |
| 317 | ret false |
| 318 | end |
| 319 | end |
| 320 | ret true |
| 321 | end |
| 322 | |
| 323 | note r_rspfld: the value of the response line "nm <value>" into d; its length must be len d |
| 324 | fn r_rspfld nm view uchar * d edit uchar * to bool |
| 325 | n = get rgv rrspl |
| 326 | s = ulong 0 |
| 327 | while lt s n bound 65536 |
| 328 | ok = le add add s len nm 1 n |
| 329 | if ok |
| 330 | for i ulong len nm bound 64 |
| 331 | if ne get rrsp add s i get nm i |
| 332 | ok = false |
| 333 | end |
| 334 | end |
| 335 | end |
| 336 | if ok |
| 337 | ok = eq get rrsp add s len nm 32 |
| 338 | end |
| 339 | if ok |
| 340 | v = add add s len nm 1 |
| 341 | e = v |
| 342 | while lt e n bound 65536 |
| 343 | if eq get rrsp e 10 |
| 344 | break |
| 345 | end |
| 346 | e = add e 1 |
| 347 | end |
| 348 | if or ge e n ne sub e v len d |
| 349 | ret false |
| 350 | end |
| 351 | for i ulong len d bound 65536 |
| 352 | put d i get rrsp add v i |
| 353 | end |
| 354 | ret true |
| 355 | end |
| 356 | while lt s n bound 65536 |
| 357 | if eq get rrsp s 10 |
| 358 | break |
| 359 | end |
| 360 | s = add s 1 |
| 361 | end |
| 362 | s = add s 1 |
| 363 | end |
| 364 | ret false |
| 365 | end |
| 366 | |
| 367 | note r_rspline: the first response line to stdout (the hub's answer word), for the log |
| 368 | fn r_rspline to void |
| 369 | n = get rgv rrspl |
| 370 | d = new uchar 256 0 |
| 371 | k = ulong 0 |
| 372 | while lt k n bound 256 |
| 373 | c = get rrsp k |
| 374 | if eq c 10 |
| 375 | break |
| 376 | end |
| 377 | if or lt c 32 gt c 126 |
| 378 | c = 63 |
| 379 | end |
| 380 | put d k c |
| 381 | k = add k 1 |
| 382 | end |
| 383 | w = write 1 d 0 k |
| 384 | end |