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 / protocol / synapse / board / src / 30-serve.om
| 1 | note synapse-board src/30-serve.om -- read-only GET server over seed/net/http.om, tailnet (100.64.0.0/10) only. |
| 2 | text tget "GET" |
| 3 | text thead "HEAD" |
| 4 | text pslash "/" |
| 5 | text pindex "/index.html" |
| 6 | text pboard "/BOARD.md" |
| 7 | text hct "Content-Type" |
| 8 | text vhtml "text/html; charset=utf-8" |
| 9 | text vmd "text/markdown; charset=utf-8" |
| 10 | text hcc "Cache-Control" |
| 11 | text vnost "no-store" |
| 12 | text hxct "X-Content-Type-Options" |
| 13 | text vnosn "nosniff" |
| 14 | text hcsp "Content-Security-Policy" |
| 15 | text vcsp "default-src 'none'; style-src 'unsafe-inline'" |
| 16 | text hallow "Allow" |
| 17 | text vallow "GET, HEAD" |
| 18 | text emaddr "serve needs --bind A.B.C.D:PORT with A.B.C.D inside 100.64.0.0/10 (tailnet); refusing" |
| 19 | text emlisten "listen failed" |
| 20 | text emdir "serve needs --dir DIR (at most 1900 bytes)" |
| 21 | text jserv1 "{\"ok\":true,\"serving\":\"" |
| 22 | text jserv2 "\"}\n" |
| 23 | note sbip: parse slot 7 as A.B.C.D:PORT; returns ip << 16 | port, or 1 << 48 when malformed |
| 24 | fn sbip to ulong |
| 25 | bad = shl ulong 1 48 |
| 26 | n = sbslotl 7 |
| 27 | ip = ulong 0 |
| 28 | oc = ulong 0 |
| 29 | dg = ulong 0 |
| 30 | np = ulong 0 |
| 31 | port = ulong 0 |
| 32 | inp = false |
| 33 | for i ulong n |
| 34 | c = sbslotc 7 i |
| 35 | if and ge c 48 le c 57 |
| 36 | if ge dg 5 |
| 37 | ret bad |
| 38 | end |
| 39 | if inp |
| 40 | port = add mul port 10 ulong sub c 48 |
| 41 | else |
| 42 | oc = add mul oc 10 ulong sub c 48 |
| 43 | end |
| 44 | dg = add dg 1 |
| 45 | else |
| 46 | if eq dg 0 |
| 47 | ret bad |
| 48 | end |
| 49 | if and eq c 46 not inp |
| 50 | if or gt oc 255 ge np 3 |
| 51 | ret bad |
| 52 | end |
| 53 | ip = or shl ip 8 oc |
| 54 | np = add np 1 |
| 55 | oc = 0 |
| 56 | dg = 0 |
| 57 | else |
| 58 | if and eq c 58 not inp |
| 59 | if or gt oc 255 ne np 3 |
| 60 | ret bad |
| 61 | end |
| 62 | ip = or shl ip 8 oc |
| 63 | inp = true |
| 64 | dg = 0 |
| 65 | else |
| 66 | ret bad |
| 67 | end |
| 68 | end |
| 69 | end |
| 70 | end |
| 71 | if or not inp eq dg 0 |
| 72 | ret bad |
| 73 | end |
| 74 | if or gt port 65535 eq port 0 |
| 75 | ret bad |
| 76 | end |
| 77 | ret or shl ip 16 port |
| 78 | end |
| 79 | note sbtail: the address is inside 100.64.0.0/10 |
| 80 | fn sbtail ip ulong to bool |
| 81 | ret and eq shr ip 24 100 eq and shr ip 22 3 1 |
| 82 | end |
| 83 | note sbfile: read DIR/name (w 0 BOARD.md, 1 index.html) into fb; byte count or negative |
| 84 | fn sbfile w ulong fb edit uchar 4194304 to long |
| 85 | n = sbfpath w |
| 86 | fd = fopen gtmp 0 n 524288 0 |
| 87 | if lt fd 0 |
| 88 | ret fd |
| 89 | end |
| 90 | p = ulong 0 |
| 91 | for it ulong 999999 |
| 92 | if ge p 4194304 |
| 93 | c = fclose int fd |
| 94 | ret -27 |
| 95 | end |
| 96 | r = read int fd fb p sub 4194304 p |
| 97 | if lt r 0 |
| 98 | if ne r -4 |
| 99 | c = fclose int fd |
| 100 | ret r |
| 101 | end |
| 102 | else |
| 103 | if eq r 0 |
| 104 | c = fclose int fd |
| 105 | ret long p |
| 106 | end |
| 107 | p = add p ulong r |
| 108 | end |
| 109 | end |
| 110 | c = fclose int fd |
| 111 | ret -27 |
| 112 | end |
| 113 | note sbfpath: gtmp = DIR "/" name; its length |
| 114 | fn sbfpath w ulong to ulong |
| 115 | n = sbslotl 6 |
| 116 | for i ulong n |
| 117 | put gtmp i sbslotc 6 i |
| 118 | end |
| 119 | put gtmp n 47 |
| 120 | n = add n 1 |
| 121 | if eq w 0 |
| 122 | for j ulong len tbmd |
| 123 | put gtmp add n j get tbmd j |
| 124 | end |
| 125 | ret add n len tbmd |
| 126 | end |
| 127 | for k ulong len tidx |
| 128 | put gtmp add n k get tidx k |
| 129 | end |
| 130 | ret add n len tidx |
| 131 | end |
| 132 | fn sbhdr cs edit ulong 320 io edit uchar 131072 nm view uchar * v view uchar * to long |
| 133 | r = hhname cs io nm |
| 134 | ret hfe r hhval cs io v |
| 135 | end |
| 136 | note sbanswer: one request already read into cs/io |
| 137 | fn sbanswer cs edit ulong 320 io edit uchar 131072 fb edit uchar 4194304 to long |
| 138 | g = hismeth cs io tget |
| 139 | hd = hismeth cs io thead |
| 140 | if not or g hd |
| 141 | r = hrspst cs io 405 |
| 142 | r = hfe r sbhdr cs io hallow vallow |
| 143 | r = hfe r hhend cs io 0 0 |
| 144 | ret hfe r hwfin cs io |
| 145 | end |
| 146 | w = ulong 2 |
| 147 | if or hispath cs io pslash hispath cs io pindex |
| 148 | w = 1 |
| 149 | end |
| 150 | if hispath cs io pboard |
| 151 | w = 0 |
| 152 | end |
| 153 | if eq w 2 |
| 154 | ret hrspempty cs io 404 |
| 155 | end |
| 156 | n = sbfile w fb |
| 157 | if lt n 0 |
| 158 | ret hrspempty cs io 503 |
| 159 | end |
| 160 | r = hrspst cs io 200 |
| 161 | if eq w 1 |
| 162 | r = hfe r sbhdr cs io hct vhtml |
| 163 | else |
| 164 | r = hfe r sbhdr cs io hct vmd |
| 165 | end |
| 166 | r = hfe r sbhdr cs io hcc vnost |
| 167 | r = hfe r sbhdr cs io hxct vnosn |
| 168 | r = hfe r sbhdr cs io hcsp vcsp |
| 169 | r = hfe r hhend cs io 0 ulong n |
| 170 | r = hfe r hput cs io part fb 0 ulong n |
| 171 | ret hfe r hwfin cs io |
| 172 | end |
| 173 | fn sbconn lf int cs edit ulong 320 io edit uchar 131072 fb edit uchar 4194304 to void |
| 174 | c = haccept lf -1 |
| 175 | if lt c 0 |
| 176 | ret |
| 177 | end |
| 178 | r = hinit cs io int c 1 |
| 179 | if lt r 0 |
| 180 | r = fclose int c |
| 181 | ret |
| 182 | end |
| 183 | hsettmo cs 10000000000 10000000000 |
| 184 | hsetbdl cs 20000000000 |
| 185 | for k ulong 100 |
| 186 | r = hreqrd cs io |
| 187 | if le r 0 |
| 188 | if lt r 0 |
| 189 | r = hreject cs io r |
| 190 | end |
| 191 | break |
| 192 | end |
| 193 | r = sbanswer cs io fb |
| 194 | if lt r 0 |
| 195 | break |
| 196 | end |
| 197 | if eq hnext cs io 0 |
| 198 | break |
| 199 | end |
| 200 | end |
| 201 | r = hclose cs |
| 202 | end |
| 203 | fn sbsvok to void |
| 204 | o = new uchar 128 0 |
| 205 | p = ulong 0 |
| 206 | for i ulong len jserv1 |
| 207 | put o p get jserv1 i |
| 208 | p = add p 1 |
| 209 | end |
| 210 | for j ulong sbslotl 7 |
| 211 | put o p sbslotc 7 j |
| 212 | p = add p 1 |
| 213 | end |
| 214 | for k ulong len jserv2 |
| 215 | put o p get jserv2 k |
| 216 | p = add p 1 |
| 217 | end |
| 218 | r = sbout o p |
| 219 | end |
| 220 | fn sbserve to long |
| 221 | fb = new uchar 4194304 0 |
| 222 | ret sbservebuf fb |
| 223 | end |
| 224 | fn sbservebuf fb edit uchar 4194304 to long |
| 225 | cs = new ulong 320 0 |
| 226 | io = new uchar 131072 0 |
| 227 | a = new uchar 16 0 |
| 228 | d = sbslotl 6 |
| 229 | if or eq d 0 gt d 1900 |
| 230 | sbsay emdir |
| 231 | ret 64 |
| 232 | end |
| 233 | v = sbip |
| 234 | if ne shr v 48 0 |
| 235 | sbsay emaddr |
| 236 | ret 64 |
| 237 | end |
| 238 | ip = shr v 16 |
| 239 | if not sbtail ip |
| 240 | sbsay emaddr |
| 241 | ret 64 |
| 242 | end |
| 243 | mx = ulong 999999 |
| 244 | if gt sbslotl 8 0 |
| 245 | mx = sbdecv 8 |
| 246 | if or eq mx 0 gt mx 999999 |
| 247 | sbsay emusage |
| 248 | ret 64 |
| 249 | end |
| 250 | end |
| 251 | r = haddr4 a uint ip and v 65535 |
| 252 | lf = hlisten a 16 |
| 253 | if lt lf 0 |
| 254 | sbsay emlisten |
| 255 | ret 75 |
| 256 | end |
| 257 | sbsvok |
| 258 | for it ulong mx |
| 259 | sbconn int lf cs io fb |
| 260 | end |
| 261 | r = fclose int lf |
| 262 | ret 0 |
| 263 | end |