Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/seed/net/examples/sse_server.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / seed / net / examples / sse_server.om` 196 lines ยท 4342 bytes Raw Blame History 1 note seed/net/examples/sse_server.om -- an HTTP/1.1 + server-sent events service embedding http.om. 2 note Compose: { printf 'om2\n'; cat seed/net/http.om seed/net/examples/sse_server.om; } | omc > sse_server 3 note Run: ./sse_server [maxconn] binds 127.0.0.1 on an ephemeral port (port 0), prints 4 note `PORT ` on stdout, serves at most maxconn connections (default 8, keep-alive inside each), 5 note then exits; it also exits after 30 s without a new connection. Routes: 6 note GET|HEAD /health 200 application/json {"ok":true} 7 note GET /events 200 text/event-stream, three events, then the stream ends 8 note POST /echo 200 application/octet-stream, the request body (at most 1 MiB) 9 note other paths 404, other methods on these paths 405; malformed requests get the status 10 note hreqrd reports (400, 413, 417, 431, 501, 505) and the connection closes. 11 text eok "{\"ok\":true}" 12 text ejson "application/json" 13 text eoct "application/octet-stream" 14 text ectype "Content-Type" 15 text eheal "/health" 16 text eevts "/events" 17 text eecho "/echo" 18 text eget "GET" 19 text epost "POST" 20 text ehead "HEAD" 21 text ehello "hello from Omega" 22 text emulti "line one\nline two" 23 text ebye "bye" 24 text emname "multi" 25 text eid1 "1" 26 text eid2 "2" 27 text eid3 "3" 28 text eport "PORT " 29 fn rhealth cs edit ulong 320 io edit uchar 131072 to long 30 r = hrspst cs io 200 31 r = hfe r hhname cs io ectype 32 r = hfe r hhval cs io ejson 33 r = hfe r hhend cs io 0 len eok 34 r = hfe r hput cs io eok 35 ret hfe r hwfin cs io 36 end 37 fn revents cs edit ulong 320 io edit uchar 131072 to long 38 r = hrspsse cs io 39 r = hfe r hsseid cs io eid1 40 r = hfe r hssedat cs io ehello 41 r = hfe r hsseend cs io 42 r = hfe r hsseevt cs io emname 43 r = hfe r hsseid cs io eid2 44 r = hfe r hssedat cs io emulti 45 r = hfe r hsseend cs io 46 r = hfe r hsseid cs io eid3 47 r = hfe r hssedat cs io ebye 48 r = hfe r hsseend cs io 49 ret hfe r hwfin cs io 50 end 51 fn recho cs edit ulong 320 io edit uchar 131072 body edit uchar `1048576` to long 52 put cs HBCAP `1048576` 53 n = hbodyall cs io body 54 if lt n 0 55 if eq n HEBIG 56 ret hrsperr cs io 413 57 end 58 ret hrsperr cs io 400 59 end 60 r = hrspst cs io 200 61 r = hfe r hhname cs io ectype 62 r = hfe r hhval cs io eoct 63 r = hfe r hhend cs io 0 ulong n 64 r = hfe r hput cs io view part body 0 ulong n 65 ret hfe r hwfin cs io 66 end 67 fn route cs edit ulong 320 io edit uchar 131072 body edit uchar `1048576` to long 68 if hispath cs io eheal 69 if or hismeth cs io eget hismeth cs io ehead 70 ret rhealth cs io 71 end 72 ret hrspempty cs io 405 73 end 74 if hispath cs io eevts 75 if hismeth cs io eget 76 ret revents cs io 77 end 78 ret hrspempty cs io 405 79 end 80 if hispath cs io eecho 81 if hismeth cs io epost 82 ret recho cs io body 83 end 84 ret hrspempty cs io 405 85 end 86 ret hrspempty cs io 404 87 end 88 note serve1: one connection, keep-alive, at most 100 requests; negative = no connection came 89 fn serve1 lf int cs edit ulong 320 io edit uchar 131072 body edit uchar `1048576` to long 90 c = haccept lf `30000000000` 91 if lt c 0 92 ret c 93 end 94 r = hinit cs io int c 1 95 if lt r 0 96 x = hclose cs 97 ret 0 98 end 99 hsettmo cs `10000000000` `10000000000` 100 note at most 20 s for one request body plus its response (a 1 MiB echo needs far less) 101 hsetbdl cs `20000000000` 102 for k ulong 100 103 r = hreqrd cs io 104 if le r 0 105 if lt r 0 106 x = hreject cs io r 107 end 108 break 109 end 110 r = route cs io body 111 if lt r 0 112 break 113 end 114 if eq hnext cs io 0 115 break 116 end 117 end 118 r = hclose cs 119 ret 0 120 end 121 fn wrout b view uchar * to long 122 p = ulong 0 123 for it ulong 4096 124 if ge p len b 125 ret 0 126 end 127 w = write 1 b p sub len b p 128 if le w 0 129 ret -5 130 end 131 p = add p ulong w 132 end 133 ret -5 134 end 135 fn argmax ab edit uchar 32 to ulong 136 if lt argcnt 2 137 ret 8 138 end 139 n = argread 1 ab 0 32 140 if le n 0 141 ret 8 142 end 143 if not hdigits view part ab 0 ulong n 144 ret 8 145 end 146 ret hdecv view part ab 0 ulong n 147 end 148 fn main to int 149 cs = new ulong 320 0 150 io = new uchar 131072 0 151 body = new uchar `1048576` 0 152 a = new uchar 16 0 153 ab = new uchar 32 0 154 mx = argmax ab 155 r = haddr4 a `2130706433` 0 156 lf = hlisten a 16 157 if lt lf 0 158 ret 2 159 end 160 for i ulong len eport 161 put ab i get eport i 162 end 163 k = pdec ab len eport hport a 164 put ab k 10 165 r = wrout view part ab 0 add k 1 166 if lt r 0 167 ret 3 168 end 169 for j ulong mx 170 r = serve1 int lf cs io body 171 if lt r 0 172 break 173 end 174 end 175 c = fclose int lf 176 ret 0 177 end 178 fn pdec b edit uchar 32 p ulong v ulong to ulong 179 d = new uchar 20 0 180 i = ulong 20 181 x = v 182 for it ulong 20 183 i = sub i 1 184 put d i uchar add rem x 10 48 185 x = div x 10 186 if eq x 0 187 break 188 end 189 end 190 q = p 191 for k ulong sub 20 i 192 put b q get d add i k 193 q = add q 1 194 end 195 ret q 196 end