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_client.om

158 lines · 3483 bytes Raw Blame History
1note seed/net/examples/sse_client.om -- incremental server-sent events client embedding http.om.
2note Compose: { printf 'om2\n'; cat seed/net/http.om seed/net/examples/sse_client.om; } | omc > sse_client
3note Run: ./sse_client PORT PATH connects to 127.0.0.1:PORT, sends GET PATH with
4note Accept: text/event-stream, reads the body (Content-Length, chunked or close-delimited) in
5note 13-byte steps to exercise the incremental reader, and prints one line per event:
6note event=TYPE id=ID data=DATA (DATA with each LF written as the two bytes \n)
7note Exit 0 at the end of the stream; 2 bad arguments, 3 connect, 4 status not 200, 5 read
8note error, 6 event reader error, 7 stdout. Idle wait 10 s per read; no overall deadline (a
9note stream may run for long). Never point it at a production port in tests.
10text caccept "Accept"
11text cevst "text/event-stream"
12text chost "Host"
13text cloop "127.0.0.1"
14text cget "GET"
15text cpev "event="
16text cpid " id="
17text cpdat " data="
18fn wout b view uchar * to long
19p = ulong 0
20for it ulong 65536
21if ge p len b
22ret 0
23end
24w = write 1 b p sub len b p
25if le w 0
26ret -5
27end
28p = add p ulong w
29end
30ret -5
31end
32fn appc o edit uchar 16640 p ulong t view uchar * to ulong
33for i ulong len t
34put o add p i get t i
35end
36ret add p len t
37end
38note pevent: format the ready event of ss/lb into o and write it
39fn pevent ss edit ulong 16 lb edit uchar 8192 o edit uchar 16640 to long
40p = appc o 0 cpev
41p = appc o p view part lb 0 get ss HSELEN
42p = appc o p cpid
43p = appc o p view part lb 64 get ss HSILEN
44p = appc o p cpdat
45for i ulong get ss HSDLEN
46c = get lb add 128 i
47if eq c 10
48put o p 92
49put o add p 1 110
50p = add p 2
51else
52put o p c
53p = add p 1
54end
55end
56put o p 10
57ret wout view part o 0 add p 1
58end
59fn feed ss edit ulong 16 lb edit uchar 8192 o edit uchar 16640 src view uchar 13 n ulong to long
60off = ulong 0
61for j ulong 64
62if ge off n
63ret 0
64end
65c = hssefeed ss lb view part src off sub n off
66if lt c 0
67ret -6
68end
69off = add off ulong c
70if ne get ss HSRDY 0
71r = pevent ss lb o
72if lt r 0
73ret -7
74end
75hssenext ss
76end
77end
78ret -6
79end
80fn request cs edit ulong 320 io edit uchar 65536 pa view uchar 1024 pl ulong to long
81r = hreqst cs io cget
82r = hfe r hreqtg cs io view part pa 0 pl
83r = hfe r hhname cs io chost
84r = hfe r hhval cs io cloop
85r = hfe r hhname cs io caccept
86r = hfe r hhval cs io cevst
87r = hfe r hhend cs io 0 0
88r = hfe r hwfin cs io
89if lt r 0
90ret r
91end
92ret hrsprd cs io
93end
94fn stream cs edit ulong 320 io edit uchar 65536 ss edit ulong 16 lb edit uchar 8192 o edit uchar 16640 to int
95src = new uchar 13 0
96for it ulong 16777216
97n = hbodyrd cs io src
98if lt n 0
99ret 5
100end
101if eq n 0
102ret 0
103end
104r = feed ss lb o src ulong n
105if eq r -6
106ret 6
107end
108if lt r 0
109ret 7
110end
111end
112ret 5
113end
114fn main to int
115cs = new ulong 320 0
116io = new uchar 65536 0
117ss = new ulong 16 0
118lb = new uchar 8192 0
119o = new uchar 16640 0
120pa = new uchar 1024 0
121a = new uchar 16 0
122if ne argcnt 3
123ret 2
124end
125pn = argread 1 pa 0 1024
126if le pn 0
127ret 2
128end
129if not hdigits view part pa 0 ulong pn
130ret 2
131end
132port = hdecv view part pa 0 ulong pn
133if or eq port 0 gt port 65535
134ret 2
135end
136pl = argread 2 pa 0 1024
137if le pl 0
138ret 2
139end
140r = haddr4 a 2130706433 port
141fd = hdial a 5000000000
142if lt fd 0
143ret 3
144end
145r = hinit cs io int fd 0
146hsettmo cs 10000000000 10000000000
147note a stream may run for long: no body deadline, only the 10 s idle limit per read
148hsetbdl cs 0
149r = hsseinit ss
150st = request cs io pa ulong pl
151if ne st 200
152r = hclose cs
153ret 4
154end
155e = stream cs io ss lb o
156r = hclose cs
157ret e
158end