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 / examples / sse_server.om

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