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