interplane.Get Omega
Read in your language

cortex-rs

Pure compiled native Rust memory engine with vector search and SQLite WAL

Brought by drakestapleton

branch main Branches Tags History

cortex-rs / om / http.om

793 lines · 13654 bytes Raw Blame History
1note http.om: HTTP/1.1 server side for Cortex. One request per connection (connection: close).
2note Request bytes land in rq; parsed fields in hv (indices below); the response head is built in hb,
3note the response body is the JSON output buffer job/jobl (json.om).
4global rq uchar 4194304
5global hv ulong 32
6global hb uchar 8192
7global hbl ulong 1
8global hnn ulong 1
9note per-connection deadlines in ns (srv.om sets them): 0 header complete, 1 whole request, 2 idle
10note between received bytes; all counted from the start of http_read
11global hto ulong 3
12note handler mode (hmir 1): every received byte is also kept, unparsed, in rqraw[0..hrawn)
13global rqraw uchar 4194304
14global hrawn ulong 1
15global hmir ulong 1
16note hprox 1: the peer is a local handler that already answered Expect: 100-continue
17global hprox ulong 1
18const H_METH ulong 0
19const H_PO ulong 1
20const H_PL ulong 2
21const H_QO ulong 3
22const H_QL ulong 4
23const H_BO ulong 5
24const H_BL ulong 6
25const H_AO ulong 7
26const H_AL ulong 8
27const H_CO ulong 9
28const H_CL ulong 10
29const H_OO ulong 11
30const H_OL ulong 12
31const H_ACRM ulong 13
32const H_EXP ulong 14
33const H_CLEN ulong 15
34const H_CHNK ulong 16
35const H_END ulong 17
36const H_N ulong 18
37const H_TE ulong 19
38const NONE ulong 18446744073709551615
39const BODYMAX ulong 2097152
40text tget "GET"
41text thead "HEAD"
42text tpost "POST"
43text topt "OPTIONS"
44text tauth "authorization"
45text tctype "content-type"
46text torigin "origin"
47text tacrm "access-control-request-method"
48text texpect "expect"
49text tclen "content-length"
50text tte "transfer-encoding"
51text t100c "100-continue"
52text tchunk "chunked"
53text thttp "HTTP/1.1 "
54text tvary "vary: origin, access-control-request-method, access-control-request-headers"
55text tctj "content-type: application/json"
56text tctt "content-type: text/plain; charset=utf-8"
57text tacao "access-control-allow-origin: "
58text tallow "allow: "
59text tclh "content-length: "
60text tdate "date: "
61text tconn "connection: close"
62text tacam "access-control-allow-methods: *"
63text tacah "access-control-allow-headers: *"
64text tcont "HTTP/1.1 100 Continue"
65text s200 "200 OK"
66text s201 "201 Created"
67text s400 "400 Bad Request"
68text s401 "401 Unauthorized"
69text s403 "403 Forbidden"
70text s404 "404 Not Found"
71text s405 "405 Method Not Allowed"
72text s408 "408 Request Timeout"
73text s409 "409 Conflict"
74text s413 "413 Payload Too Large"
75text s415 "415 Unsupported Media Type"
76text s422 "422 Unprocessable Entity"
77text s431 "431 Request Header Fields Too Large"
78text s500 "500 Internal Server Error"
79text s503 "503 Service Unavailable"
80text tdays "ThuFriSatSunMonTueWed"
81text tmons "JanFebMarAprMayJunJulAugSepOctNovDec"
82text orig1 "http://127.0.0.1:18095"
83text orig2 "http://localhost:18095"
84
85text tgmt " GMT"
86text tagh "GET,HEAD"
87text tap "POST"
88text tagp "GET,HEAD,POST"
89fn hb_byte c uchar to void
90n = get hbl 0
91if lt n len hb
92put hb n c
93put hbl 0 add n 1
94end
95end
96fn hb_txt s view uchar * to void
97for i ulong len s
98hb_byte get s i
99end
100end
101fn hb_crlf to void
102hb_byte 13
103hb_byte 10
104end
105fn hb_line s view uchar * to void
106hb_txt s
107hb_crlf
108end
109fn hb_dec v ulong to void
110if ge v 10
111hb_dec div v 10
112end
113hb_byte uchar add rem v 10 48
114end
115fn hb_dec2 v ulong to void
116hb_byte uchar add div v 10 48
117hb_byte uchar add rem v 10 48
118end
119fn hb_part s view uchar * k ulong to void
120o = mul k 3
121hb_byte get s o
122hb_byte get s add o 1
123hb_byte get s add o 2
124end
125fn hb_rq o ulong n ulong to void
126for i ulong n
127hb_byte get rq add o i
128end
129end
130
131note lower-case ASCII compare of rq[o..o+n) against a lower-case text
132fn rq_ieq o ulong n ulong t view uchar * to bool
133if ne n len t
134ret false
135end
136for i ulong n
137c = get rq add o i
138if and ge c 65 le c 90
139c = add c 32
140end
141if ne c get t i
142ret false
143end
144end
145ret true
146end
147fn rq_eq o ulong n ulong t view uchar * to bool
148if ne n len t
149ret false
150end
151for i ulong n
152if ne get rq add o i get t i
153ret false
154end
155end
156ret true
157end
158fn rq_isws c uchar to bool
159ret or eq c 32 eq c 9
160end
161
162note IMF-fixdate from realtime nanoseconds (RFC 9110 5.6.7), civil-from-days (Hinnant).
163global hdt ulong 3
164fn hb_civil days ulong to void
165z = add days 719468
166era = div z 146097
167doe = sub z mul era 146097
168yoe = div sub sub add doe div doe 36524 div doe 1460 div doe 146096 365
169doy = sub doe sub add mul 365 yoe div yoe 4 div yoe 100
170mp = div add mul 5 doy 2 153
171put hdt 2 add sub doy div add mul 153 mp 2 5 1
172m = add mp 3
173y = add yoe mul era 400
174if ge m 13
175m = sub m 12
176end
177if le m 2
178y = add y 1
179end
180put hdt 0 y
181put hdt 1 m
182end
183fn hb_date to void
184ns = realns
185s = ulong div ns 1000000000
186days = div s 86400
187sod = rem s 86400
188hb_part tdays rem days 7
189hb_byte 44
190hb_byte 32
191hb_civil days
192hb_dec2 get hdt 2
193hb_byte 32
194hb_part tmons sub get hdt 1 1
195hb_byte 32
196hb_dec get hdt 0
197hb_byte 32
198hb_dec2 div sod 3600
199hb_byte 58
200hb_dec2 rem div sod 60 60
201hb_byte 58
202hb_dec2 rem sod 60
203hb_txt tgmt
204end
205
206note find a header by lower-case name; returns its trimmed value in hv slots (off, len) or leaves NONE
207fn http_hdrs to bool
208p = get hv H_END
209e = get hv H_N
210while lt p e bound 999999
211ls = p
212while and lt p e ne get rq p 10 bound 999999
213p = add p 1
214end
215lend = p
216if and gt lend ls eq get rq sub lend 1 13
217lend = sub lend 1
218end
219p = add p 1
220if eq lend ls
221ret true
222end
223c = ls
224while and lt c lend ne get rq c 58 bound 999999
225c = add c 1
226end
227if ge c lend
228ret false
229end
230if eq c ls
231ret false
232end
233v = add c 1
234while and lt v lend rq_isws get rq v bound 999999
235v = add v 1
236end
237ve = lend
238while and gt ve v rq_isws get rq sub ve 1 bound 999999
239ve = sub ve 1
240end
241http_hdr1 ls sub c ls v sub ve v
242end
243ret false
244end
245fn http_hdr1 no ulong nl ulong v ulong vl ulong to void
246if rq_ieq no nl tauth
247if eq get hv H_AL NONE
248put hv H_AO v
249put hv H_AL vl
250end
251ret
252end
253if rq_ieq no nl tctype
254if eq get hv H_CL NONE
255put hv H_CO v
256put hv H_CL vl
257end
258ret
259end
260if rq_ieq no nl torigin
261if eq get hv H_OL NONE
262put hv H_OO v
263put hv H_OL vl
264end
265ret
266end
267if rq_ieq no nl tacrm
268put hv H_ACRM 1
269ret
270end
271if rq_ieq no nl texpect
272if rq_ieq v vl t100c
273put hv H_EXP 1
274end
275ret
276end
277if rq_ieq no nl tte
278put hv H_TE 1
279if rq_ieq v vl tchunk
280put hv H_CHNK 1
281end
282ret
283end
284if rq_ieq no nl tclen
285n = ulong 0
286if eq vl 0
287put hv H_CLEN sub NONE 1
288ret
289end
290for i ulong vl
291c = get rq add v i
292if or lt c 48 gt c 57
293put hv H_CLEN sub NONE 1
294ret
295end
296if gt n 100000000000
297put hv H_CLEN sub NONE 1
298ret
299end
300n = add mul n 10 ulong sub c 48
301end
302if and ne get hv H_CLEN NONE ne get hv H_CLEN n
303put hv H_CLEN sub NONE 1
304ret
305end
306put hv H_CLEN n
307end
308end
309
310note request line: METHOD SP target SP HTTP/1.x ; method code 1 GET 2 HEAD 3 POST 4 OPTIONS 9 other
311fn http_rline to bool
312e = get hv H_N
313p = ulong 0
314while and lt p e ne get rq p 32 bound 999999
315p = add p 1
316end
317if or ge p e eq p 0
318ret false
319end
320m = ulong 9
321if rq_eq 0 p tget
322m = 1
323end
324if rq_eq 0 p thead
325m = 2
326end
327if rq_eq 0 p tpost
328m = 3
329end
330if rq_eq 0 p topt
331m = 4
332end
333put hv H_METH m
334p = add p 1
335t = p
336while and lt p e ne get rq p 32 bound 999999
337if or eq get rq p 10 eq get rq p 13
338ret false
339end
340p = add p 1
341end
342if or ge p e eq p t
343ret false
344end
345q = t
346while and lt q p ne get rq q 63 bound 999999
347q = add q 1
348end
349put hv H_PO t
350put hv H_PL sub q t
351if lt q p
352put hv H_QO add q 1
353put hv H_QL sub sub p q 1
354end
355while and lt p e ne get rq p 10 bound 999999
356p = add p 1
357end
358put hv H_END add p 1
359ret true
360end
361
362note reset hv for a new request
363fn http_clear to void
364for i ulong len hv
365put hv i NONE
366end
367put hv H_ACRM 0
368put hv H_EXP 0
369put hv H_CHNK 0
370put hv H_TE 0
371end
372
373note wait for data then recv into rq at offset n; returns bytes read, 0 on close, -1 on timeout/error
374fn http_recv c int n ulong dl long to long
375now = monons
376if ge now dl
377ret -1
378end
379wt = sub dl now
380if gt wt long get hto 2
381wt = long get hto 2
382end
383w = waitio c 1 wt
384if le w 0
385ret -1
386end
387cap = sub len rq n
388if eq cap 0
389ret -1
390end
391r = recv c rq n cap 0
392if and gt r 0 eq get hmir 0 1
393http_mirror n ulong r
394end
395ret r
396end
397note handler mode: keep rq[n..n+r) raw in rqraw
398fn http_mirror n ulong r ulong to void
399for i ulong r
400put rqraw add n i get rq add n i
401end
402put hrawn 0 add n r
403end
404
405note find CRLFCRLF or LFLF end of head within rq[0..n); returns index after it or NONE
406fn http_headend n ulong to ulong
407i = ulong 0
408while lt i n bound 999999
409if eq get rq i 10
410j = add i 1
411if lt j n
412if eq get rq j 10
413ret add i 2
414end
415if lt add j 1 n
416if and eq get rq j 13 eq get rq add j 1 10
417ret add i 3
418end
419end
420end
421end
422i = add i 1
423end
424ret NONE
425end
426
427note read one request. returns 0 ok, 1 peer closed or timeout before any byte, else an HTTP status to answer
428fn http_read c int to ulong
429http_clear
430t0 = monons
431dl = add t0 long get hto 0
432n = ulong 0
433he = NONE
434while eq he NONE bound 4096
435r = http_recv c n dl
436if le r 0
437if eq n 0
438ret 1
439end
440ret 400
441end
442n = add n ulong r
443he = http_headend n
444if and eq he NONE gt n 65536
445ret 431
446end
447end
448put hv H_N he
449if not http_rline
450ret 400
451end
452if not http_hdrs
453ret 400
454end
455if eq get hv H_CLEN sub NONE 1
456ret 400
457end
458ret http_body c n add t0 long get hto 1
459end
460
461note body: content-length or chunked. Body bytes end up contiguous at hv H_BO
462fn http_body c int n0 ulong dl long to ulong
463n = n0
464he = get hv H_N
465put hv H_BO he
466if eq get hv H_CHNK 1
467ret http_chunked c n dl
468end
469cl = get hv H_CLEN
470if eq cl NONE
471if eq get hv H_TE 1
472ret 400
473end
474cl = 0
475end
476if gt cl BODYMAX
477put hv H_BL cl
478ret 413
479end
480if and eq get hv H_EXP 1 and gt cl 0 lt sub n he cl
481http_cont c
482end
483while lt sub n he cl bound 999999
484r = http_recv c n dl
485if le r 0
486ret 400
487end
488n = add n ulong r
489end
490put hv H_BL cl
491ret 0
492end
493fn http_cont c int to void
494if eq get hprox 0 1
495ret
496end
497put hbl 0 0
498hb_txt tcont
499hb_crlf
500hb_crlf
501s = send c hb 0 get hbl 0 0
502if lt s 0
503ret
504end
505end
506
507note chunked decode in place: data moves down to H_BO. hcs: 0 write pos, 1 read pos, 2 bytes held, 3 total
508global hcs ulong 4
509fn http_chunked c int n0 ulong dl long to ulong
510if eq get hv H_EXP 1
511http_cont c
512end
513put hcs 0 get hv H_N
514put hcs 1 get hv H_N
515put hcs 2 n0
516put hcs 3 0
517while true bound 999999
518st = http_chunk1 c dl
519if ne st 1
520ret st
521end
522end
523ret 400
524end
525note one chunk: returns 1 to continue, 0 done, else a status
526fn http_chunk1 c int dl long to ulong
527p = get hcs 1
528lnend = http_need_line c p get hcs 2 dl
529if eq lnend NONE
530ret 400
531end
532put hcs 2 get hnn 0
533sz = ulong 0
534dg = ulong 0
535while lt p lnend bound 999999
536v = http_hexv get rq p
537if eq v 99
538break
539end
540sz = add mul sz 16 v
541dg = add dg 1
542if gt sz BODYMAX
543ret 413
544end
545p = add p 1
546end
547if eq dg 0
548ret 400
549end
550p = add lnend 1
551if eq sz 0
552put hv H_BL get hcs 3
553ret 0
554end
555if gt add get hcs 3 sz BODYMAX
556ret 413
557end
558if not http_fill c add add p sz 2 dl
559ret 400
560end
561w = get hcs 0
562for i ulong sz
563put rq add w i get rq add p i
564end
565put hcs 0 add w sz
566put hcs 3 add get hcs 3 sz
567p = add p sz
568if eq get rq p 13
569p = add p 1
570end
571if ne get rq p 10
572ret 400
573end
574put hcs 1 add p 1
575ret 1
576end
577note receive until at least want bytes are held (hcs 2)
578fn http_fill c int want ulong dl long to bool
579while lt get hcs 2 want bound 999999
580n = get hcs 2
581if ge n len rq
582ret false
583end
584r = http_recv c n dl
585if le r 0
586ret false
587end
588put hcs 2 add n ulong r
589end
590ret true
591end
592fn http_hexv c uchar to ulong
593if and ge c 48 le c 57
594ret ulong sub c 48
595end
596if and ge c 97 le c 102
597ret ulong sub c 87
598end
599if and ge c 65 le c 70
600ret ulong sub c 55
601end
602ret 99
603end
604note make sure a full line starting at p is in rq; returns index of its LF (or NONE); stores n in hv H_TE
605fn http_need_line c int p ulong n0 ulong dl long to ulong
606n = n0
607while true bound 999999
608i = p
609while lt i n bound 999999
610if eq get rq i 10
611put hnn 0 n
612ret i
613end
614i = add i 1
615end
616if ge n len rq
617ret NONE
618end
619r = http_recv c n dl
620if le r 0
621ret NONE
622end
623n = add n ulong r
624end
625ret NONE
626end
627
628note status code to reason text
629fn hb_status st ulong to void
630hb_txt thttp
631if eq st 200
632hb_txt s200
633ret
634end
635if eq st 201
636hb_txt s201
637ret
638end
639if eq st 400
640hb_txt s400
641ret
642end
643if eq st 401
644hb_txt s401
645ret
646end
647if eq st 403
648hb_txt s403
649ret
650end
651if eq st 404
652hb_txt s404
653ret
654end
655if eq st 405
656hb_txt s405
657ret
658end
659if eq st 408
660hb_txt s408
661ret
662end
663if eq st 409
664hb_txt s409
665ret
666end
667if eq st 413
668hb_txt s413
669ret
670end
671if eq st 415
672hb_txt s415
673ret
674end
675if eq st 422
676hb_txt s422
677ret
678end
679if eq st 431
680hb_txt s431
681ret
682end
683if eq st 503
684hb_txt s503
685ret
686end
687hb_txt s500
688end
689
690note allowed CORS origin? 1 when the request Origin is one of the allow-list values
691fn http_origin_ok to bool
692o = get hv H_OO
693l = get hv H_OL
694if eq l NONE
695ret false
696end
697if rq_eq o l orig1
698ret true
699end
700ret rq_eq o l orig2
701end
702
703note send head + body. ctype 0 none, 1 json, 2 text. allow: 0 none, 1 "GET,HEAD", 2 "POST", 3 "GET,HEAD,POST"
704fn http_send c int st ulong ctype ulong allow ulong blen ulong to void
705put hbl 0 0
706hb_status st
707hb_crlf
708if eq ctype 1
709hb_line tctj
710end
711if eq ctype 2
712hb_line tctt
713end
714hb_line tvary
715if http_origin_ok
716hb_txt tacao
717hb_rq get hv H_OO get hv H_OL
718hb_crlf
719end
720http_allow allow
721hb_txt tclh
722hb_dec blen
723hb_crlf
724hb_txt tdate
725hb_date
726hb_crlf
727hb_line tconn
728hb_crlf
729s = send c hb 0 get hbl 0 0
730if or lt s 0 or eq blen 0 eq get hv H_METH 2
731ret
732end
733http_sendall c blen
734end
735fn http_allow allow ulong to void
736if eq allow 0
737ret
738end
739hb_txt tallow
740if eq allow 1
741hb_txt tagh
742end
743if eq allow 2
744hb_txt tap
745end
746if eq allow 3
747hb_txt tagp
748end
749hb_crlf
750end
751fn http_sendall c int blen ulong to void
752o = ulong 0
753dl = add monons 10000000000
754while lt o blen bound 999999
755w = waitio c 4 sub dl monons
756if le w 0
757ret
758end
759s = send c job o sub blen o 0
760if le s 0
761ret
762end
763o = add o ulong s
764end
765end
766
767note CORS preflight (tower-http CorsLayer): 200, allow-origin when listed, methods/headers *, allow per route
768fn http_preflight c int allow ulong to void
769put hbl 0 0
770hb_status 200
771hb_crlf
772if http_origin_ok
773hb_txt tacao
774hb_rq get hv H_OO get hv H_OL
775hb_crlf
776end
777hb_line tvary
778hb_line tacam
779hb_line tacah
780http_allow allow
781hb_txt tclh
782hb_dec 0
783hb_crlf
784hb_txt tdate
785hb_date
786hb_crlf
787hb_line tconn
788hb_crlf
789s = send c hb 0 get hbl 0 0
790if lt s 0
791ret
792end
793end