interplane.Get Omega
Read in your language

aien-train

AIEN training programme: language-model history, GB10 constraints and the route to training on the project engine. Plans and runnable checks are distinguished.

Brought by drakestapleton

branch main Branches Tags History

aien-train / native / omegallm / net / http.om

2463 lines · 47422 bytes Raw Blame History
1note seed/net/http.om -- bounded HTTP/1.1 server and client in seeded Omega (omega-mv #788).
2note Library unit: it has no `om2` line, no `fn main` and no globals. An application composes
3note { printf 'om2\n'; cat seed/net/http.om app.om; } | omc
4note (JSON users add seed/json/json.om in the same cat). Names: functions h*, consts H*, texts h*.
5note Contract, framing rules, error values and ownership: seed/net/README.md.
6note
7note A connection is two caller-owned arrays:
8note cs = new ulong HCSLEN 0 state (fd, buffer cursors, deadlines, framing, header index)
9note io = new uchar N 0 one byte array split by hinit into a read region io[0, HRCAP),
10note a write region io[HWB, HWB+HWCAP) and a transport region
11note io[HTB, HTB+HTL) (empty for plain TCP; reserved for TLS)
12note Header and target offsets stored in cs are absolute offsets into io and stay valid until the
13note next hreqrd / hrsprd on the same cs.
14note
15note Results: a long. Nonnegative is success. Negative is minus a Linux errno (-11, -104, ...),
16note or one of the HE* protocol values below (-400 .. -505), or HETIME (-110) at a deadline.
17note
18note ---- state slots of cs
19const HFD ulong 0
20const HRLEN ulong 1
21const HRPOS ulong 2
22const HDL ulong 3
23const HIDLE ulong 4
24const HKEEP ulong 5
25const HBMODE ulong 6
26const HBREM ulong 7
27const HCST ulong 8
28const HMEM ulong 9
29const HBCAP ulong 10
30const HBGOT ulong 11
31const HVER ulong 12
32const HSTAT ulong 13
33const HMOFF ulong 14
34const HMLEN ulong 15
35const HTOFF ulong 16
36const HTLEN ulong 17
37const HHN ulong 18
38const HHMAX ulong 19
39const HHEADMX ulong 20
40const HWLEN ulong 21
41const HNMSG ulong 22
42const HMAXMSG ulong 23
43const HHEND ulong 24
44const HISHEAD ulong 25
45const HWMODE ulong 26
46const HEXPECT ulong 27
47const HFLAGS ulong 28
48const HCLV ulong 29
49const HOPEN ulong 30
50const HHDLV ulong 31
51const HSERVER ulong 32
52const HSTART ulong 33
53const HSCAN ulong 34
54const HRCAP ulong 35
55const HWB ulong 36
56const HWCAP ulong 37
57const HTB ulong 38
58const HTL ulong 39
59const HXPORT ulong 40
60const HWBODY ulong 41
61const HWDROP ulong 42
62const HWOUT ulong 43
63const HWCL ulong 44
64const HOMODE ulong 45
65const HSCODE ulong 46
66const HTRN ulong 47
67const HHOSTN ulong 48
68const HBDLV ulong 49
69const HFINAL ulong 50
70const HFRM ulong 51
71const HFRMAX ulong 52
72const HBSC ulong 53
73note header i (0 <= i < HHN): cs[HHX+4i] name offset, +1 name length, +2 value offset, +3 value length
74const HHX ulong 64
75const HHSLOTS ulong 64
76const HCSLEN ulong 320
77note ---- results
78const HEINVAL long -22
79const HENOSUP long -95
80const HETIME long -110
81const HEBAD long -400
82const HEEXP long -417
83const HEBIG long -413
84const HEHEAD long -431
85const HETRUNC long -499
86const HETE long -501
87const HEVER long -505
88note ---- SSE reader: ss = new ulong HSSLEN 0, lb = new uchar N 0 (N >= 256)
89note lb[0, 64) event type, lb[64, 128) last event id, lb[128, ...) data and the line being read
90const HSLINE ulong 0
91const HSDLEN ulong 1
92const HSELEN ulong 2
93const HSILEN ulong 3
94const HSRETRY ulong 4
95const HSRDY ulong 5
96const HSCR ulong 6
97const HSBOM ulong 7
98const HSNEV ulong 8
99note HSVO / HSVL: value offset and length of the field line being applied (scratch)
100const HSVO ulong 9
101const HSVL ulong 10
102const HSSLEN ulong 16
103const HSIDB ulong 64
104const HSDB ulong 128
105const HSFCAP ulong 64
106note ---- byte strings (lowercase ones are compared case-insensitively)
107text hvhttp "HTTP/"
108text hv11 "HTTP/1.1"
109text hnclen "content-length"
110text hnte "transfer-encoding"
111text hnconn "connection"
112text hnhost "host"
113text hnexp "expect"
114text hvchunk "chunked"
115text hvclose "close"
116text hvkeep "keep-alive"
117text hv100c "100-continue"
118text hmhead "HEAD"
119text hmconn "CONNECT"
120text hoclen "Content-Length: "
121text hote "Transfer-Encoding: chunked"
122text hocls "Connection: close"
123text ho100 "HTTP/1.1 100 Continue"
124text hoctype "Content-Type"
125text hocache "Cache-Control"
126text hvnoca "no-cache"
127text hvevst "text/event-stream"
128text hsdata "data: "
129text hsevent "event: "
130text hsid "id: "
131text hsretry "retry: "
132text hfdata "data"
133text hfevent "event"
134text hfid "id"
135text hfretry "retry"
136text hr100 "Continue"
137text hr101 "Switching Protocols"
138text hr200 "OK"
139text hr201 "Created"
140text hr202 "Accepted"
141text hr204 "No Content"
142text hr206 "Partial Content"
143text hr301 "Moved Permanently"
144text hr302 "Found"
145text hr304 "Not Modified"
146text hr400 "Bad Request"
147text hr401 "Unauthorized"
148text hr403 "Forbidden"
149text hr404 "Not Found"
150text hr405 "Method Not Allowed"
151text hr408 "Request Timeout"
152text hr409 "Conflict"
153text hr411 "Length Required"
154text hr413 "Content Too Large"
155text hr414 "URI Too Long"
156text hr415 "Unsupported Media Type"
157text hr417 "Expectation Failed"
158text hr422 "Unprocessable Content"
159text hr429 "Too Many Requests"
160text hr431 "Request Header Fields Too Large"
161text hr500 "Internal Server Error"
162text hr501 "Not Implemented"
163text hr502 "Bad Gateway"
164text hr503 "Service Unavailable"
165text hr504 "Gateway Timeout"
166text hr505 "HTTP Version Not Supported"
167note ======== small helpers
168fn hmin a ulong b ulong to ulong
169if lt a b
170ret a
171end
172ret b
173end
174fn hfe a long b long to long
175note first error: a when a is negative, else b
176if lt a 0
177ret a
178end
179ret b
180end
181fn hlow c uchar to uchar
182if and ge c 65 le c 90
183ret add c 32
184end
185ret c
186end
187fn hws c uchar to bool
188ret or eq c 32 eq c 9
189end
190note htchar: RFC 9110 tchar
191fn htchar c uchar to bool
192if and ge c 97 le c 122
193ret true
194end
195if and ge c 65 le c 90
196ret true
197end
198if and ge c 48 le c 57
199ret true
200end
201if or or or eq c 33 eq c 35 eq c 36 eq c 37
202ret true
203end
204if or or or eq c 38 eq c 39 eq c 42 eq c 43
205ret true
206end
207if or or or eq c 45 eq c 46 eq c 94 eq c 95
208ret true
209end
210ret or or eq c 96 eq c 124 eq c 126
211end
212note hvch: a field-value byte: HTAB, SP, VCHAR or obs-text (0x80-0xff); never CR, LF, NUL, DEL
213fn hvch c uchar to bool
214if eq c 9
215ret true
216end
217ret and ge c 32 ne c 127
218end
219fn hvall a view uchar * to bool
220for b ulong 65536
221s = mul b 65536
222if ge s len a
223ret true
224end
225m = hmin sub len a s 65536
226for i ulong m
227if not hvch get a add s i
228ret false
229end
230end
231end
232ret true
233end
234fn htok a view uchar * to bool
235n = len a
236if or eq n 0 gt n 64
237ret false
238end
239for i ulong n
240if not htchar get a i
241ret false
242end
243end
244ret true
245end
246fn htarg a view uchar * to bool
247n = len a
248if or eq n 0 gt n 8192
249ret false
250end
251for i ulong n
252c = get a i
253if or lt c 33 gt c 126
254ret false
255end
256end
257ret true
258end
259fn hdigits a view uchar * to bool
260n = len a
261if or eq n 0 gt n 19
262ret false
263end
264for i ulong n
265c = get a i
266if or lt c 48 gt c 57
267ret false
268end
269end
270ret true
271end
272fn hdecv a view uchar * to ulong
273note the value of 1..19 checked decimal digits (hdigits first)
274v = ulong 0
275for i ulong len a
276v = add mul v 10 ulong sub get a i 48
277end
278ret v
279end
280fn hieq a view uchar * t view uchar * to bool
281note ASCII case-insensitive equality; t is lowercase
282n = len a
283if ne n len t
284ret false
285end
286for i ulong n
287if ne hlow get a i get t i
288ret false
289end
290end
291ret true
292end
293fn heq a view uchar * t view uchar * to bool
294n = len a
295if ne n len t
296ret false
297end
298for i ulong n
299if ne get a i get t i
300ret false
301end
302end
303ret true
304end
305note hcopy: d[0, len s) = s; blocks of 65536 keep every loop under the 1000000-pass guard
306fn hcopy d edit uchar * s view uchar * to void
307n = len s
308for b ulong 65536
309st = mul b 65536
310if ge st n
311break
312end
313m = hmin sub n st 65536
314for i ulong m
315put d add st i get s add st i
316end
317end
318end
319note hmove: a[dt, dt+n) = a[sr, sr+n) with dt <= sr (forward copy)
320fn hmove a edit uchar * dt ulong sr ulong n ulong to void
321for b ulong 65536
322st = mul b 65536
323if ge st n
324break
325end
326m = hmin sub n st 65536
327for i ulong m
328put a add dt add st i get a add sr add st i
329end
330end
331end
332fn hhexv c uchar to ulong
333if and ge c 48 le c 57
334ret ulong sub c 48
335end
336if and ge c 97 le c 102
337ret ulong sub c 87
338end
339if and ge c 65 le c 70
340ret ulong sub c 55
341end
342ret 16
343end
344fn hhexd v ulong to uchar
345if lt v 10
346ret uchar add v 48
347end
348ret uchar add v 87
349end
350note ======== transport boundary: the only functions that call recv, send and waitio on a connection
351note hwait: wait for events ev on the connection; interval = min(idle, deadline - now)
352fn hwait cs edit ulong * ev int to long
353fd = int get cs HFD
354for it ulong 4096
355iv = long -1
356if ne get cs HIDLE 0
357iv = long get cs HIDLE
358end
359dl = get cs HDL
360if ne dl 0
361now = ulong monons
362if ge now dl
363ret HETIME
364end
365rm = long sub dl now
366if or lt iv 0 lt rm iv
367iv = rm
368end
369end
370r = waitio fd ev iv
371if eq r 0
372ret HETIME
373end
374if ne r -4
375ret r
376end
377end
378ret HETIME
379end
380note hrecvio: at most n bytes into io[off, off+n); count, 0 at end of stream, or negative
381fn hrecvio cs edit ulong * io edit uchar * off ulong n ulong to long
382if ne get cs HMEM 0
383ret 0
384end
385if ne get cs HXPORT 0
386ret HENOSUP
387end
388fd = int get cs HFD
389for it ulong 4096
390r = recv fd io off n 64
391if eq r -11
392w = hwait cs 1
393if lt w 0
394ret w
395end
396continue
397end
398if ne r -4
399ret r
400end
401end
402ret HETIME
403end
404note hrecvd: at most len d bytes straight into the caller's array d
405fn hrecvd cs edit ulong * io edit uchar * d edit uchar * to long
406if ne get cs HMEM 0
407ret 0
408end
409if ne get cs HXPORT 0
410ret HENOSUP
411end
412fd = int get cs HFD
413for it ulong 4096
414r = recv fd d 0 len d 64
415if eq r -11
416w = hwait cs 1
417if lt w 0
418ret w
419end
420continue
421end
422if ne r -4
423ret r
424end
425end
426ret HETIME
427end
428note hsendio: send all of io[off, off+n); 0 or negative
429fn hsendio cs edit ulong * io edit uchar * off ulong n ulong to long
430if ne get cs HMEM 0
431ret -9
432end
433if ne get cs HXPORT 0
434ret HENOSUP
435end
436fd = int get cs HFD
437e = add off n
438p = off
439for it ulong 4194304
440if ge p e
441ret 0
442end
443r = send fd io p sub e p 64
444if gt r 0
445p = add p ulong r
446continue
447end
448if eq r -11
449w = hwait cs 4
450if lt w 0
451ret w
452end
453continue
454end
455if eq r 0
456ret -32
457end
458if ne r -4
459ret r
460end
461end
462ret HETIME
463end
464note ======== connection setup
465note hinit: take ownership of fd (hclose closes it); fd < 0 selects memory mode (no I/O; tests).
466note role 1 = server (reads requests, writes responses), 0 = client.
467fn hinit cs edit ulong * io edit uchar * fd int role ulong to long
468if lt len cs HCSLEN
469ret HEINVAL
470end
471if or lt len io 512 gt len io 16777216
472ret HEINVAL
473end
474if gt role 1
475ret HEINVAL
476end
477for i ulong HCSLEN
478put cs i 0
479end
480if lt fd 0
481put cs HMEM 1
482else
483put cs HFD ulong fd
484put cs HOPEN 1
485end
486put cs HSERVER role
487put cs HBCAP 1048576
488put cs HHMAX HHSLOTS
489put cs HMAXMSG 1000
490put cs HIDLE 30000000000
491put cs HHDLV 30000000000
492put cs HBDLV 60000000000
493put cs HFRMAX 65536
494put cs HKEEP 1
495w = hmin div len io 4 65536
496if lt w 256
497w = 256
498end
499ret hlayout cs len io w
500end
501fn hlayout cs edit ulong * n ulong w ulong to long
502r = sub n w
503put cs HRCAP r
504put cs HWB r
505put cs HWCAP w
506put cs HTB n
507put cs HTL 0
508put cs HHEADMX hmin 16384 div r 2
509ret 0
510end
511note hsetw: change the write region capacity (256 .. 65536) before any I/O on cs
512fn hsetw cs edit ulong * io edit uchar * w ulong to long
513if or ne get cs HRLEN 0 ne get cs HWLEN 0
514ret HEINVAL
515end
516if or lt w 256 gt w 65536
517ret HEINVAL
518end
519if lt len io add w 512
520ret HEINVAL
521end
522ret hlayout cs len io w
523end
524note hsettmo: idle = longest single wait in ns (0 = none); hdl = deadline for reading one
525note message head, counted from the start of hreqrd / hrsprd (0 = none). Negative means 0.
526fn hsettmo cs edit ulong * idle long hdl long to void
527put cs HIDLE 0
528if gt idle 0
529put cs HIDLE ulong idle
530end
531put cs HHDLV 0
532if gt hdl 0
533put cs HHDLV ulong hdl
534end
535end
536note hsetbdl: interval armed as the deadline (HDL) for a message's body reads and the response
537note writes when its head has been read, and for a request's writes when hreqst starts it
538note (0 = none). The default is 60 s; a long-lived stream (SSE) sets its own with hsetdl.
539fn hsetbdl cs edit ulong * iv long to void
540put cs HBDLV 0
541if gt iv 0
542put cs HBDLV ulong iv
543end
544end
545fn hbdl cs edit ulong * to ulong
546if eq get cs HBDLV 0
547ret 0
548end
549ret add ulong monons get cs HBDLV
550end
551note hsetdl: absolute deadline now + iv for the following body reads and writes (0 clears)
552fn hsetdl cs edit ulong * iv long to void
553put cs HDL 0
554if gt iv 0
555put cs HDL add ulong monons ulong iv
556end
557end
558fn hclose cs edit ulong * to long
559if eq get cs HOPEN 1
560put cs HOPEN 0
561put cs HKEEP 0
562ret fclose int get cs HFD
563end
564ret 0
565end
566note hmemld: memory mode only; the read region holds exactly src (a whole test message)
567fn hmemld cs edit ulong * io edit uchar * src view uchar * to long
568if eq get cs HMEM 0
569ret HEINVAL
570end
571if gt len src get cs HRCAP
572ret HEBIG
573end
574hcopy part io 0 len src src
575put cs HRLEN len src
576put cs HRPOS 0
577ret 0
578end
579note ======== sockets (IPv4 or Unix); every descriptor is O_NONBLOCK and close-on-exec
580fn haddr4 a edit uchar * ip uint port ulong to long
581if lt len a 16
582ret HEINVAL
583end
584if gt port 65535
585ret HEINVAL
586end
587for i ulong 16
588put a i 0
589end
590put a 0 2
591put a 2 uchar shr port 8
592put a 3 uchar and port 255
593put a 4 uchar shr ip 24
594put a 5 uchar and shr ip 16 255
595put a 6 uchar and shr ip 8 255
596put a 7 uchar and ip 255
597ret 0
598end
599fn hport a view uchar * to ulong
600ret or shl ulong get a 2 8 ulong get a 3
601end
602fn hfam a view uchar * to int
603ret int or ulong get a 0 shl ulong get a 1 8
604end
605fn hnodel fd int to long
606one = new uchar 4 0
607put one 0 1
608ret sockopt fd 6 1 one 0 4
609end
610note hlisten: socket, SO_REUSEADDR, bind a (exactly the sockaddr bytes), listen, and write the
611note bound address back into a (so port 0 reports the ephemeral port). Returns the listening fd.
612fn hlisten a edit uchar * backlog int to long
613one = new uchar 4 0
614put one 0 1
615fd = socknew hfam a 526337 0
616if lt fd 0
617ret fd
618end
619r = sockopt int fd 1 2 one 0 4
620if eq r 0
621r = bind int fd a 0 len a
622end
623if eq r 0
624r = listen int fd backlog
625end
626if eq r 0
627k = sockname int fd a 0 len a
628if lt k 0
629r = k
630end
631end
632if ne r 0
633c = fclose int fd
634ret r
635end
636ret fd
637end
638note haccept: wait at most tmo ns (negative = forever) for a connection; returns its fd
639fn haccept lfd int tmo long to long
640w = waitio lfd 1 tmo
641if eq w 0
642ret HETIME
643end
644if lt w 0
645ret w
646end
647c = accept lfd 526336
648if lt c 0
649ret c
650end
651q = hnodel int c
652ret c
653end
654note hdial: nonblocking connect bounded by tmo ns; returns the connected fd or closes it
655fn hdial a view uchar * tmo long to long
656fd = socknew hfam a 526337 0
657if lt fd 0
658ret fd
659end
660r = connect int fd a 0 len a
661if eq r -115
662w = waitio int fd 4 tmo
663if le w 0
664c = fclose int fd
665if eq w 0
666ret HETIME
667end
668ret w
669end
670r = connect int fd a 0 len a
671if eq r -106
672r = 0
673end
674end
675if ne r 0
676c = fclose int fd
677ret r
678end
679q = hnodel int fd
680ret fd
681end
682note ======== reading a message head
683note hbegin: drop the consumed bytes of the previous message, reset per-message state, arm the
684note head deadline
685fn hbegin cs edit ulong * io edit uchar * to void
686p = get cs HRPOS
687n = get cs HRLEN
688if gt p 0
689hmove io 0 p sub n p
690put cs HRLEN sub n p
691put cs HRPOS 0
692end
693if gt get cs HHEADMX hmin 65536 div get cs HRCAP 2
694put cs HHEADMX hmin 65536 div get cs HRCAP 2
695end
696if gt get cs HHMAX HHSLOTS
697put cs HHMAX HHSLOTS
698end
699put cs HSTART 0
700put cs HSCAN 0
701put cs HHN 0
702put cs HFLAGS 0
703put cs HCLV 0
704put cs HBMODE 0
705put cs HBREM 0
706put cs HCST 0
707put cs HBGOT 0
708put cs HEXPECT 0
709put cs HHEND 0
710put cs HSTAT 0
711put cs HVER 0
712put cs HTRN 0
713put cs HHOSTN 0
714put cs HMOFF 0
715put cs HMLEN 0
716put cs HTOFF 0
717put cs HTLEN 0
718put cs HKEEP 1
719put cs HFINAL 0
720put cs HFRM 0
721put cs HBSC 0
722put cs HDL 0
723if ne get cs HHDLV 0
724put cs HDL add ulong monons get cs HHDLV
725end
726end
727note hskipcr: a server ignores empty lines (CRLF) before a request line (RFC 9112 2.2)
728fn hskipcr cs edit ulong * io edit uchar * to void
729for it ulong 999999
730st = get cs HSTART
731if gt add st 2 get cs HRLEN
732break
733end
734if ne get io st 13
735break
736end
737if ne get io add st 1 10
738break
739end
740put cs HSTART add st 2
741end
742end
743note hscan: look for CRLF CRLF in io[HSTART, min(HRLEN, HHEADMX)); end offset, 0 = need more,
744note HEBAD for a bare LF or a CR not followed by LF
745fn hscan cs edit ulong * io edit uchar * to long
746st = get cs HSTART
747lim = hmin get cs HRLEN get cs HHEADMX
748i = get cs HSCAN
749if lt i st
750i = st
751end
752while lt i lim bound 999999
753c = get io i
754if eq c 10
755if eq i st
756ret HEBAD
757end
758if ne get io sub i 1 13
759ret HEBAD
760end
761if ge i add st 3
762if and eq get io sub i 2 10 eq get io sub i 3 13
763put cs HSCAN add i 1
764ret long add i 1
765end
766end
767end
768if eq c 13
769if ge add i 1 get cs HRLEN
770put cs HSCAN i
771ret 0
772end
773if ne get io add i 1 10
774ret HEBAD
775end
776end
777i = add i 1
778end
779put cs HSCAN i
780ret 0
781end
782fn hhead cs edit ulong * io edit uchar * to long
783for it ulong 1000000
784if eq get cs HSERVER 1
785hskipcr cs io
786end
787e = hscan cs io
788if lt e 0
789ret e
790end
791if gt e 0
792ret hparse cs io ulong e
793end
794n = get cs HRLEN
795if ge n get cs HHEADMX
796ret HEHEAD
797end
798if ge n get cs HRCAP
799ret HEHEAD
800end
801k = hrecvio cs io n sub get cs HRCAP n
802if lt k 0
803ret k
804end
805if eq k 0
806if eq n get cs HSTART
807ret 0
808end
809ret HETRUNC
810end
811put cs HRLEN add n ulong k
812end
813ret HEHEAD
814end
815note hcr: the first CR in a[p, e) (the head scan guarantees CR LF pairs), or e
816fn hcr a view uchar * p ulong e ulong to ulong
817i = p
818while lt i e bound 999999
819if eq get a i 13
820ret i
821end
822i = add i 1
823end
824ret e
825end
826fn hparse cs edit ulong * io edit uchar * e ulong to long
827p = get cs HSTART
828x = hcr io p e
829r = long 0
830if eq get cs HSERVER 1
831r = hreql cs io p x
832else
833r = hstatl cs io p x
834end
835if lt r 0
836ret r
837end
838p = add x 2
839lim = sub e 2
840for it ulong 65536
841if ge p lim
842break
843end
844x = hcr io p lim
845r = hfield cs io p x
846if lt r 0
847ret r
848end
849p = add x 2
850end
851r = hsem cs io
852if lt r 0
853ret r
854end
855put cs HHEND e
856put cs HRPOS e
857put cs HDL hbdl cs
858ret 1
859end
860note hreqm: method = 1..32 tchar then SP; returns its length
861fn hreqm a view uchar * p ulong x ulong to long
862i = p
863while and lt i x htchar get a i bound 999999
864i = add i 1
865end
866ml = sub i p
867if or eq ml 0 gt ml 32
868ret HEBAD
869end
870if ne get a i 32
871ret HEBAD
872end
873ret long ml
874end
875note hreqt: request-target = 1..8192 bytes 0x21-0x7e then SP; returns its length
876fn hreqt a view uchar * t ulong x ulong to long
877j = t
878while and lt j x and ge get a j 33 le get a j 126 bound 999999
879j = add j 1
880end
881tl = sub j t
882if or eq tl 0 gt tl 8192
883ret HEBAD
884end
885if ne get a j 32
886ret HEBAD
887end
888ret long tl
889end
890note hver: exactly HTTP/1.1 (1) or HTTP/1.0 (0); another HTTP/d.d is HEVER, anything else HEBAD
891fn hver a view uchar * k ulong to long
892for i ulong 5
893if ne get a add k i get hvhttp i
894ret HEBAD
895end
896end
897mj = get a add k 5
898dt = get a add k 6
899mn = get a add k 7
900if or or lt mj 48 gt mj 57 ne dt 46
901ret HEBAD
902end
903if or lt mn 48 gt mn 57
904ret HEBAD
905end
906if ne mj 49
907ret HEVER
908end
909if eq mn 48
910ret 0
911end
912if eq mn 49
913ret 1
914end
915ret HEVER
916end
917fn hreql cs edit ulong * io edit uchar * p ulong x ulong to long
918ml = hreqm io p x
919if lt ml 0
920ret ml
921end
922t = add p add ulong ml 1
923tl = hreqt io t x
924if lt tl 0
925ret tl
926end
927k = add t add ulong tl 1
928if ne sub x k 8
929ret HEBAD
930end
931v = hver io k
932if lt v 0
933ret v
934end
935put cs HVER ulong v
936put cs HMOFF p
937put cs HMLEN ulong ml
938put cs HTOFF t
939put cs HTLEN ulong tl
940ret htform cs io
941end
942note htform: origin-form (/...), asterisk-form (*), authority-form (CONNECT only) or
943note absolute-form (scheme://...)
944fn htform cs edit ulong * io edit uchar * to long
945t = get cs HTOFF
946tl = get cs HTLEN
947c = get io t
948if eq c 47
949ret 0
950end
951if and eq tl 1 eq c 42
952ret 0
953end
954if heq view part io get cs HMOFF get cs HMLEN hmconn
955ret 0
956end
957ret hscheme view part io t tl
958end
959fn hscheme a view uchar * to long
960n = len a
961if lt n 4
962ret HEBAD
963end
964c = hlow get a 0
965if or lt c 97 gt c 122
966ret HEBAD
967end
968for i ulong n
969if gt i 0
970c = hlow get a i
971if eq c 58
972if gt add i 3 n
973ret HEBAD
974end
975if and eq get a add i 1 47 eq get a add i 2 47
976ret 0
977end
978ret HEBAD
979end
980if not hschc c
981ret HEBAD
982end
983end
984end
985ret HEBAD
986end
987note hschc: a scheme byte after the first (lowercased): ALPHA, DIGIT, + - .
988fn hschc c uchar to bool
989if and ge c 97 le c 122
990ret true
991end
992if and ge c 48 le c 57
993ret true
994end
995ret or or eq c 43 eq c 45 eq c 46
996end
997note hstatl: HTTP/1.x SP 3DIGIT SP reason; status 100..599
998fn hstatl cs edit ulong * io edit uchar * p ulong x ulong to long
999if lt sub x p 8
1000ret HEBAD
1001end
1002v = hver io p
1003if lt v 0
1004ret v
1005end
1006if lt sub x p 13
1007ret HEBAD
1008end
1009if or ne get io add p 8 32 ne get io add p 12 32
1010ret HEBAD
1011end
1012c = ulong 0
1013for i ulong 3
1014d = get io add p add 9 i
1015if or lt d 48 gt d 57
1016ret HEBAD
1017end
1018c = add mul c 10 ulong sub d 48
1019end
1020if or lt c 100 gt c 599
1021ret HEBAD
1022end
1023r = add p 13
1024if not hvall view part io r sub x r
1025ret HEBAD
1026end
1027put cs HVER ulong v
1028put cs HSTAT c
1029put cs HMOFF r
1030put cs HMLEN sub x r
1031ret 0
1032end
1033note hfvalid: a field line: tchar name, ':' with no space before it, field-value bytes;
1034note returns the colon index. A leading SP or HTAB (obs-fold) is HEBAD.
1035fn hfvalid a view uchar * to long
1036n = len a
1037if eq n 0
1038ret HEBAD
1039end
1040if hws get a 0
1041ret HEBAD
1042end
1043c = n
1044for i ulong n
1045if not htchar get a i
1046c = i
1047break
1048end
1049end
1050if or eq c 0 eq c n
1051ret HEBAD
1052end
1053if ne get a c 58
1054ret HEBAD
1055end
1056k = add c 1
1057if not hvall view part a k sub n k
1058ret HEBAD
1059end
1060ret long c
1061end
1062fn hfield cs edit ulong * io edit uchar * p ulong x ulong to long
1063co = hfvalid view part io p sub x p
1064if lt co 0
1065ret co
1066end
1067n = get cs HHN
1068if or ge n get cs HHMAX ge n HHSLOTS
1069ret HEHEAD
1070end
1071b = add HHX mul n 4
1072put cs b p
1073put cs add b 1 ulong co
1074put cs HHN add n 1
1075put cs add b 2 add add p ulong co 1
1076ret hftrim cs io b x
1077end
1078note hftrim: header slot b's value starts at get cs b+2 and ends before x; drop leading and
1079note trailing SP / HTAB
1080fn hftrim cs edit ulong * io edit uchar * b ulong x ulong to long
1081s = get cs add b 2
1082while and lt s x hws get io s bound 999999
1083s = add s 1
1084end
1085ve = x
1086while and gt ve s hws get io sub ve 1 bound 999999
1087ve = sub ve 1
1088end
1089put cs add b 2 s
1090put cs add b 3 sub ve s
1091ret 0
1092end
1093note ======== header semantics and message framing (RFC 9112 6.1-6.3)
1094fn hsem cs edit ulong * io edit uchar * to long
1095n = get cs HHN
1096for i ulong n
1097r = hsem1 cs io i
1098if lt r 0
1099ret r
1100end
1101end
1102ret hframe cs
1103end
1104fn hsem1 cs edit ulong * io edit uchar * i ulong to long
1105b = add HHX mul i 4
1106no = get cs b
1107nl = get cs add b 1
1108vo = get cs add b 2
1109vl = get cs add b 3
1110if hieq view part io no nl hnclen
1111ret hsemcl cs io vo vl
1112end
1113if hieq view part io no nl hnte
1114ret hsemte cs io vo vl
1115end
1116if hieq view part io no nl hnconn
1117ret hsemco cs io vo vl
1118end
1119if hieq view part io no nl hnhost
1120put cs HHOSTN add get cs HHOSTN 1
1121ret 0
1122end
1123if hieq view part io no nl hnexp
1124ret hsemex cs io vo vl
1125end
1126ret 0
1127end
1128note Content-Length: 1..19 digits, exactly one field line (a repeated or list value is ambiguous)
1129fn hsemcl cs edit ulong * io edit uchar * vo ulong vl ulong to long
1130if ne and get cs HFLAGS 1 0
1131ret HEBAD
1132end
1133put cs HFLAGS or get cs HFLAGS 1
1134if not hdigits view part io vo vl
1135ret HEBAD
1136end
1137put cs HCLV hdecv view part io vo vl
1138ret 0
1139end
1140note Transfer-Encoding: exactly one field line whose value is chunked; any other coding is HETE
1141fn hsemte cs edit ulong * io edit uchar * vo ulong vl ulong to long
1142if ne and get cs HFLAGS 2 0
1143ret HEBAD
1144end
1145put cs HFLAGS or get cs HFLAGS 2
1146if not hieq view part io vo vl hvchunk
1147ret HETE
1148end
1149ret 0
1150end
1151note Connection: comma list; close (flag 4) and keep-alive (flag 8) are recognised
1152fn hsemco cs edit ulong * io edit uchar * vo ulong vl ulong to long
1153p = vo
1154e = add vo vl
1155for it ulong 999999
1156if gt p e
1157break
1158end
1159q = p
1160while and lt q e ne get io q 44 bound 999999
1161q = add q 1
1162end
1163r = hcotok cs io p q
1164p = add q 1
1165end
1166ret 0
1167end
1168note hcotok: the list element io[a, q) without SP / HTAB; sets flag 4 (close) or 8 (keep-alive)
1169fn hcotok cs edit ulong * io edit uchar * a ulong q ulong to long
1170s = a
1171while and lt s q hws get io s bound 999999
1172s = add s 1
1173end
1174z = q
1175while and gt z s hws get io sub z 1 bound 999999
1176z = sub z 1
1177end
1178if hieq view part io s sub z s hvclose
1179put cs HFLAGS or get cs HFLAGS 4
1180end
1181if hieq view part io s sub z s hvkeep
1182put cs HFLAGS or get cs HFLAGS 8
1183end
1184ret 0
1185end
1186note Expect (server, HTTP/1.1 only): 100-continue is honoured on the first body read; others HEEXP
1187fn hsemex cs edit ulong * io edit uchar * vo ulong vl ulong to long
1188if or ne get cs HSERVER 1 ne get cs HVER 1
1189ret 0
1190end
1191if not hieq view part io vo vl hv100c
1192ret HEEXP
1193end
1194put cs HEXPECT 1
1195ret 0
1196end
1197fn hframe cs edit ulong * to long
1198f = get cs HFLAGS
1199cl = ne and f 1 0
1200te = ne and f 2 0
1201if and cl te
1202ret HEBAD
1203end
1204if and te eq get cs HVER 0
1205ret HEBAD
1206end
1207srv = eq get cs HSERVER 1
1208if and srv eq get cs HVER 1
1209if ne get cs HHOSTN 1
1210ret HEBAD
1211end
1212end
1213put cs HKEEP 0
1214if and eq get cs HVER 1 eq and f 4 0
1215put cs HKEEP 1
1216end
1217if srv
1218if ge add get cs HNMSG 1 get cs HMAXMSG
1219put cs HKEEP 0
1220end
1221ret hfrsrv cs
1222end
1223ret hfrcli cs
1224end
1225fn hfrsrv cs edit ulong * to long
1226f = get cs HFLAGS
1227if ne and f 2 0
1228put cs HBMODE 2
1229ret 0
1230end
1231if ne and f 1 0
1232v = get cs HCLV
1233if gt v get cs HBCAP
1234put cs HKEEP 0
1235ret HEBIG
1236end
1237if gt v 0
1238put cs HBMODE 1
1239put cs HBREM v
1240end
1241end
1242ret 0
1243end
1244fn hfrcli cs edit ulong * to long
1245s = get cs HSTAT
1246f = get cs HFLAGS
1247if eq s 101
1248put cs HKEEP 0
1249end
1250if or or eq get cs HISHEAD 1 lt s 200 or eq s 204 eq s 304
1251ret 0
1252end
1253if ne and f 2 0
1254put cs HBMODE 2
1255ret 0
1256end
1257if ne and f 1 0
1258v = get cs HCLV
1259if gt v get cs HBCAP
1260put cs HKEEP 0
1261ret HEBIG
1262end
1263if gt v 0
1264put cs HBMODE 1
1265put cs HBREM v
1266end
1267ret 0
1268end
1269put cs HBMODE 3
1270put cs HKEEP 0
1271ret 0
1272end
1273note ======== public head readers
1274note hreqrd (server): 1 = a request head was read, 0 = the peer closed between requests, negative =
1275note error (a value <= -400 is the status to answer with hrsperr before closing)
1276fn hreqrd cs edit ulong * io edit uchar * to long
1277if and eq get cs HOPEN 0 eq get cs HMEM 0
1278ret -9
1279end
1280hbegin cs io
1281put cs HISHEAD 0
1282r = hhead cs io
1283if le r 0
1284ret r
1285end
1286if heq view part io get cs HMOFF get cs HMLEN hmhead
1287put cs HISHEAD 1
1288end
1289ret 1
1290end
1291note hrsprd (client): the final status (101, or 200..599; 1xx interim responses are skipped)
1292fn hrsprd cs edit ulong * io edit uchar * to long
1293if and eq get cs HOPEN 0 eq get cs HMEM 0
1294ret -9
1295end
1296for it ulong 16
1297hbegin cs io
1298r = hhead cs io
1299if eq r 0
1300ret HETRUNC
1301end
1302if lt r 0
1303ret r
1304end
1305s = get cs HSTAT
1306if or ge s 200 eq s 101
1307ret long s
1308end
1309put cs HRPOS get cs HHEND
1310end
1311ret HEBAD
1312end
1313note header accessors: hhfind returns the index of the first field named nm (any case), or -1
1314fn hhfind cs edit ulong * io edit uchar * nm view uchar * to long
1315n = get cs HHN
1316for i ulong n
1317b = add HHX mul i 4
1318if hieqm view part io get cs b get cs add b 1 nm
1319ret long i
1320end
1321end
1322ret -1
1323end
1324note hieqm: case-insensitive equality of two arbitrary byte strings
1325fn hieqm a view uchar * t view uchar * to bool
1326n = len a
1327if ne n len t
1328ret false
1329end
1330for i ulong n
1331if ne hlow get a i hlow get t i
1332ret false
1333end
1334end
1335ret true
1336end
1337fn hhvoff cs edit ulong * i ulong to ulong
1338ret get cs add HHX add mul i 4 2
1339end
1340fn hhvlen cs edit ulong * i ulong to ulong
1341ret get cs add HHX add mul i 4 3
1342end
1343fn hhnoff cs edit ulong * i ulong to ulong
1344ret get cs add HHX mul i 4
1345end
1346fn hhnlen cs edit ulong * i ulong to ulong
1347ret get cs add HHX add mul i 4 1
1348end
1349note hismeth: the request method equals m exactly; hispath: the target before '?' equals pa
1350fn hismeth cs edit ulong * io edit uchar * m view uchar * to bool
1351ret heq view part io get cs HMOFF get cs HMLEN m
1352end
1353fn hplen cs edit ulong * io edit uchar * to ulong
1354t = get cs HTOFF
1355n = get cs HTLEN
1356for i ulong n
1357if eq get io add t i 63
1358ret i
1359end
1360end
1361ret n
1362end
1363fn hispath cs edit ulong * io edit uchar * pa view uchar * to bool
1364n = hplen cs io
1365ret heq view part io get cs HTOFF n pa
1366end
1367note ======== body reading
1368note hbodyrd: next decoded body bytes into d[0, r); 0 = end of body; negative = error.
1369note Content-Length, chunked and (client) close-delimited framing; HBCAP bounds the total.
1370fn hbodyrd cs edit ulong * io edit uchar * d edit uchar * to long
1371m = get cs HBMODE
1372if eq m 0
1373ret 0
1374end
1375if eq len d 0
1376ret HEINVAL
1377end
1378if eq get cs HEXPECT 1
1379put cs HEXPECT 2
1380r = hsend100 cs io
1381if lt r 0
1382ret r
1383end
1384end
1385if eq m 1
1386ret hbdlen cs io d
1387end
1388if eq m 3
1389ret hbdcls cs io d
1390end
1391ret hbdchk cs io d
1392end
1393fn hsend100 cs edit ulong * io edit uchar * to long
1394if or ne get cs HFINAL 0 ne get cs HMEM 0
1395ret 0
1396end
1397b = get cs HWB
1398n = len ho100
1399hcopy part io b n ho100
1400put io add b n 13
1401put io add b add n 1 10
1402put io add b add n 2 13
1403put io add b add n 3 10
1404ret hsendio cs io b add n 4
1405end
1406note hbcopy: move buffered bytes io[HRPOS, ...) into d (at most len d)
1407fn hbcopy cs edit ulong * io edit uchar * d edit uchar * to ulong
1408p = get cs HRPOS
1409n = hmin sub get cs HRLEN p len d
1410hcopy part d 0 n view part io p n
1411put cs HRPOS add p n
1412ret n
1413end
1414fn hbdlen cs edit ulong * io edit uchar * d edit uchar * to long
1415rm = get cs HBREM
1416if eq rm 0
1417ret 0
1418end
1419k = hmin rm len d
1420n = ulong 0
1421if gt get cs HRLEN get cs HRPOS
1422n = hbcopy cs io part d 0 k
1423else
1424r = hrecvd cs io part d 0 k
1425if lt r 0
1426ret r
1427end
1428if eq r 0
1429ret HETRUNC
1430end
1431n = ulong r
1432end
1433put cs HBREM sub rm n
1434put cs HBGOT add get cs HBGOT n
1435ret long n
1436end
1437fn hbdcls cs edit ulong * io edit uchar * d edit uchar * to long
1438if eq get cs HCST 4
1439ret 0
1440end
1441got = get cs HBGOT
1442cap = get cs HBCAP
1443k = hmin len d add sub cap got 1
1444n = ulong 0
1445if gt get cs HRLEN get cs HRPOS
1446n = hbcopy cs io part d 0 k
1447else
1448r = hrecvd cs io part d 0 k
1449if lt r 0
1450ret r
1451end
1452if eq r 0
1453put cs HCST 4
1454ret 0
1455end
1456n = ulong r
1457end
1458if gt add got n cap
1459ret HEBIG
1460end
1461put cs HBGOT add got n
1462ret long n
1463end
1464note chunked states (HCST): 0 size line, 1 data, 2 CRLF after data, 3 trailer lines, 4 done
1465fn hbdchk cs edit ulong * io edit uchar * d edit uchar * to long
1466for it ulong 1000000
1467st = get cs HCST
1468if eq st 4
1469ret 0
1470end
1471if eq st 1
1472ret hbdcdat cs io d
1473end
1474r = long 0
1475if eq st 0
1476r = hbdcsz cs io
1477end
1478if eq st 2
1479r = hbdccr cs io
1480end
1481if eq st 3
1482r = hbdctr cs io
1483end
1484if lt r 0
1485ret r
1486end
1487end
1488ret HEBAD
1489end
1490fn hbdcdat cs edit ulong * io edit uchar * d edit uchar * to long
1491rm = get cs HBREM
1492k = hmin rm len d
1493n = ulong 0
1494if gt get cs HRLEN get cs HRPOS
1495n = hbcopy cs io part d 0 k
1496else
1497r = hrecvd cs io part d 0 k
1498if lt r 0
1499ret r
1500end
1501if eq r 0
1502ret HETRUNC
1503end
1504n = ulong r
1505end
1506put cs HBREM sub rm n
1507put cs HBGOT add get cs HBGOT n
1508if eq rm n
1509put cs HCST 2
1510end
1511ret long n
1512end
1513note hfrchk: count n framing bytes (chunk-size lines, CRLF after chunk data, trailer lines and
1514note the final empty line); more than HFRMAX + 8 * decoded bytes is HEBIG
1515fn hfrchk cs edit ulong * n ulong to long
1516f = add get cs HFRM n
1517put cs HFRM f
1518if gt f add get cs HFRMAX mul get cs HBGOT 8
1519put cs HKEEP 0
1520ret HEBIG
1521end
1522ret 0
1523end
1524fn hbdcsz cs edit ulong * io edit uchar * to long
1525x = hbline cs io
1526if lt x 0
1527ret x
1528end
1529p = get cs HRPOS
1530v = hchsz view part io p sub ulong x p
1531if lt v 0
1532ret v
1533end
1534put cs HRPOS add ulong x 2
1535r = hfrchk cs sub add ulong x 2 p
1536if lt r 0
1537ret r
1538end
1539if eq v 0
1540put cs HCST 3
1541ret 0
1542end
1543if gt add get cs HBGOT ulong v get cs HBCAP
1544put cs HKEEP 0
1545ret HEBIG
1546end
1547put cs HBREM ulong v
1548put cs HCST 1
1549ret 0
1550end
1551fn hbdccr cs edit ulong * io edit uchar * to long
1552r = hneed cs io 2
1553if lt r 0
1554ret r
1555end
1556p = get cs HRPOS
1557if or ne get io p 13 ne get io add p 1 10
1558ret HEBAD
1559end
1560r = hfrchk cs 2
1561if lt r 0
1562ret r
1563end
1564put cs HRPOS add p 2
1565put cs HCST 0
1566ret 0
1567end
1568fn hbdctr cs edit ulong * io edit uchar * to long
1569x = hbline cs io
1570if lt x 0
1571ret x
1572end
1573p = get cs HRPOS
1574r = hfrchk cs sub add ulong x 2 p
1575if lt r 0
1576ret r
1577end
1578if eq ulong x p
1579put cs HRPOS add p 2
1580put cs HCST 4
1581ret 0
1582end
1583co = hfvalid view part io p sub ulong x p
1584if lt co 0
1585ret co
1586end
1587put cs HTRN add get cs HTRN 1
1588if gt get cs HTRN get cs HHMAX
1589ret HEHEAD
1590end
1591put cs HRPOS add ulong x 2
1592ret 0
1593end
1594note hchsz: chunk-size = 1..15 hex digits, then nothing or ';' and field-value bytes (extensions,
1595note ignored); no whitespace before ';'
1596fn hchsz a view uchar * to long
1597n = len a
1598v = ulong 0
1599c = n
1600for i ulong n
1601h = hhexv get a i
1602if eq h 16
1603c = i
1604break
1605end
1606if ge i 15
1607ret HEBAD
1608end
1609v = add mul v 16 h
1610end
1611if eq c 0
1612ret HEBAD
1613end
1614if lt c n
1615if ne get a c 59
1616ret HEBAD
1617end
1618if not hvall view part a c sub n c
1619ret HEBAD
1620end
1621end
1622ret long v
1623end
1624note hbline: the CR index of the CRLF line starting at HRPOS (at most HHEADMX bytes), reading more
1625note as needed
1626fn hbline cs edit ulong * io edit uchar * to long
1627for it ulong 1000000
1628p = get cs HRPOS
1629n = get cs HRLEN
1630lim = hmin n add p get cs HHEADMX
1631i = add p get cs HBSC
1632while lt i lim bound 999999
1633c = get io i
1634if eq c 10
1635ret HEBAD
1636end
1637if eq c 13
1638if ge add i 1 n
1639put cs HBSC sub i p
1640break
1641end
1642if ne get io add i 1 10
1643ret HEBAD
1644end
1645put cs HBSC 0
1646ret long i
1647end
1648i = add i 1
1649end
1650put cs HBSC sub i p
1651if ge sub n p get cs HHEADMX
1652ret HEHEAD
1653end
1654r = hmore cs io
1655if lt r 0
1656ret r
1657end
1658end
1659ret HEHEAD
1660end
1661note hcompact: move unread body bytes down to HHEND (the head stays in place)
1662fn hcompact cs edit ulong * io edit uchar * to void
1663b = get cs HHEND
1664p = get cs HRPOS
1665if gt p b
1666n = get cs HRLEN
1667hmove io b p sub n p
1668put cs HRLEN sub n sub p b
1669put cs HRPOS b
1670end
1671end
1672fn hmore cs edit ulong * io edit uchar * to long
1673hcompact cs io
1674n = get cs HRLEN
1675cap = get cs HRCAP
1676if ge n cap
1677ret HEHEAD
1678end
1679k = hrecvio cs io n sub cap n
1680if lt k 0
1681ret k
1682end
1683if eq k 0
1684ret HETRUNC
1685end
1686put cs HRLEN add n ulong k
1687ret k
1688end
1689fn hneed cs edit ulong * io edit uchar * k ulong to long
1690for it ulong 1000000
1691if ge sub get cs HRLEN get cs HRPOS k
1692ret 0
1693end
1694r = hmore cs io
1695if lt r 0
1696ret r
1697end
1698end
1699ret HETRUNC
1700end
1701note hbodyall: the whole body into d; the length, or HEBIG when it does not fit in d
1702fn hbodyall cs edit ulong * io edit uchar * d edit uchar * to long
1703pb = new uchar 1 0
1704got = ulong 0
1705for it ulong 16777216
1706if ge got len d
1707r = hbodyrd cs io pb
1708if gt r 0
1709put cs HKEEP 0
1710ret HEBIG
1711end
1712if lt r 0
1713ret r
1714end
1715ret long got
1716end
1717r = hbodyrd cs io part d got sub len d got
1718if lt r 0
1719ret r
1720end
1721if eq r 0
1722ret long got
1723end
1724got = add got ulong r
1725end
1726ret HEBIG
1727end
1728note ======== writing (one write region, flushed to the transport when full or on demand)
1729note HWMODE 0: raw bytes from HWB. HWMODE 1: chunked body; io[HWB, HWB+10) is reserved for the
1730note chunk-size line, so a flush sends one chunk without copying.
1731fn hwreset cs edit ulong * to void
1732put cs HWLEN 0
1733put cs HWMODE 0
1734put cs HWBODY 0
1735put cs HWDROP 0
1736put cs HWOUT 0
1737put cs HWCL 0
1738put cs HOMODE 0
1739end
1740fn hwcap cs edit ulong * to ulong
1741if eq get cs HWMODE 1
1742ret sub get cs HWCAP 2
1743end
1744ret get cs HWCAP
1745end
1746fn hhexpre io edit uchar * b ulong v ulong to ulong
1747put io add b 8 13
1748put io add b 9 10
1749i = ulong 8
1750x = v
1751for it ulong 8
1752i = sub i 1
1753put io add b i hhexd and x 15
1754x = shr x 4
1755if eq x 0
1756ret i
1757end
1758end
1759ret i
1760end
1761fn hflush cs edit ulong * io edit uchar * to long
1762n = get cs HWLEN
1763b = get cs HWB
1764if eq get cs HWMODE 1
1765if le n 10
1766ret 0
1767end
1768s = hhexpre io b sub n 10
1769put io add b n 13
1770put io add b add n 1 10
1771put cs HWLEN 10
1772ret hsendio cs io add b s sub add n 2 s
1773end
1774if eq n 0
1775ret 0
1776end
1777put cs HWLEN 0
1778ret hsendio cs io b n
1779end
1780note hovercl: n more body bytes would pass the declared Content-Length (mode 0 after hhend)
1781fn hovercl cs edit ulong * n ulong to bool
1782if or ne get cs HWBODY 1 ne get cs HOMODE 0
1783ret false
1784end
1785ret gt add get cs HWOUT n get cs HWCL
1786end
1787note hput: append s (body bytes after hhend are framed and counted; dropped for HEAD/204/304).
1788note A Content-Length body refuses bytes past clen before any is buffered (HEINVAL, closing).
1789fn hput cs edit ulong * io edit uchar * s view uchar * to long
1790if eq get cs HWBODY 1
1791if hovercl cs len s
1792put cs HKEEP 0
1793ret HEINVAL
1794end
1795put cs HWOUT add get cs HWOUT len s
1796if eq get cs HWDROP 1
1797ret 0
1798end
1799end
1800off = ulong 0
1801for it ulong 16777216
1802if ge off len s
1803ret 0
1804end
1805w = get cs HWLEN
1806if ge w hwcap cs
1807r = hflush cs io
1808if lt r 0
1809ret r
1810end
1811continue
1812end
1813k = hmin sub hwcap cs w sub len s off
1814w = add get cs HWB w
1815hcopy part io w k view part s off k
1816put cs HWLEN add get cs HWLEN k
1817off = add off k
1818end
1819ret HEINVAL
1820end
1821fn hputb cs edit ulong * io edit uchar * c uchar to long
1822if eq get cs HWBODY 1
1823if hovercl cs 1
1824put cs HKEEP 0
1825ret HEINVAL
1826end
1827put cs HWOUT add get cs HWOUT 1
1828if eq get cs HWDROP 1
1829ret 0
1830end
1831end
1832if ge get cs HWLEN hwcap cs
1833r = hflush cs io
1834if lt r 0
1835ret r
1836end
1837end
1838w = get cs HWLEN
1839put io add get cs HWB w c
1840put cs HWLEN add w 1
1841ret 0
1842end
1843fn hcrlf cs edit ulong * io edit uchar * to long
1844r = hputb cs io 13
1845ret hfe r hputb cs io 10
1846end
1847fn hdec cs edit ulong * io edit uchar * v ulong to long
1848d = new uchar 20 0
1849i = ulong 20
1850x = v
1851for it ulong 20
1852i = sub i 1
1853put d i uchar add rem x 10 48
1854x = div x 10
1855if eq x 0
1856break
1857end
1858end
1859ret hput cs io view part d i sub 20 i
1860end
1861note hrspst (server): start a response: status line with the standard reason phrase
1862fn hrspst cs edit ulong * io edit uchar * code ulong to long
1863if or lt code 100 gt code 599
1864ret HEINVAL
1865end
1866hwreset cs
1867put cs HSCODE code
1868if ge code 200
1869put cs HFINAL 1
1870end
1871r = hput cs io hv11
1872r = hfe r hputb cs io 32
1873r = hfe r hdec cs io code
1874r = hfe r hputb cs io 32
1875r = hfe r hreason cs io code
1876ret hfe r hcrlf cs io
1877end
1878fn hreason cs edit ulong * io edit uchar * code ulong to long
1879if lt code 300
1880ret hreas2 cs io code
1881end
1882if lt code 420
1883ret hreas4 cs io code
1884end
1885ret hreas5 cs io code
1886end
1887fn hreas2 cs edit ulong * io edit uchar * code ulong to long
1888if eq code 100
1889ret hput cs io hr100
1890end
1891if eq code 101
1892ret hput cs io hr101
1893end
1894if eq code 200
1895ret hput cs io hr200
1896end
1897if eq code 201
1898ret hput cs io hr201
1899end
1900if eq code 202
1901ret hput cs io hr202
1902end
1903if eq code 204
1904ret hput cs io hr204
1905end
1906if eq code 206
1907ret hput cs io hr206
1908end
1909ret 0
1910end
1911fn hreas4 cs edit ulong * io edit uchar * code ulong to long
1912if eq code 301
1913ret hput cs io hr301
1914end
1915if eq code 302
1916ret hput cs io hr302
1917end
1918if eq code 304
1919ret hput cs io hr304
1920end
1921if eq code 400
1922ret hput cs io hr400
1923end
1924if eq code 401
1925ret hput cs io hr401
1926end
1927if eq code 403
1928ret hput cs io hr403
1929end
1930if eq code 404
1931ret hput cs io hr404
1932end
1933if eq code 405
1934ret hput cs io hr405
1935end
1936if eq code 408
1937ret hput cs io hr408
1938end
1939if eq code 409
1940ret hput cs io hr409
1941end
1942if eq code 411
1943ret hput cs io hr411
1944end
1945if eq code 413
1946ret hput cs io hr413
1947end
1948if eq code 414
1949ret hput cs io hr414
1950end
1951if eq code 415
1952ret hput cs io hr415
1953end
1954if eq code 417
1955ret hput cs io hr417
1956end
1957ret 0
1958end
1959fn hreas5 cs edit ulong * io edit uchar * code ulong to long
1960if eq code 422
1961ret hput cs io hr422
1962end
1963if eq code 429
1964ret hput cs io hr429
1965end
1966if eq code 431
1967ret hput cs io hr431
1968end
1969if eq code 500
1970ret hput cs io hr500
1971end
1972if eq code 501
1973ret hput cs io hr501
1974end
1975if eq code 502
1976ret hput cs io hr502
1977end
1978if eq code 503
1979ret hput cs io hr503
1980end
1981if eq code 504
1982ret hput cs io hr504
1983end
1984if eq code 505
1985ret hput cs io hr505
1986end
1987ret 0
1988end
1989note hreqst + hreqtg (client): request line `m SP t SP HTTP/1.1`; HEAD is remembered for framing
1990fn hreqst cs edit ulong * io edit uchar * m view uchar * to long
1991if not htok m
1992ret HEINVAL
1993end
1994hwreset cs
1995hsetdl cs long get cs HBDLV
1996put cs HISHEAD 0
1997if heq m hmhead
1998put cs HISHEAD 1
1999end
2000put cs HNMSG add get cs HNMSG 1
2001r = hput cs io m
2002ret hfe r hputb cs io 32
2003end
2004fn hreqtg cs edit ulong * io edit uchar * t view uchar * to long
2005if not htarg t
2006ret HEINVAL
2007end
2008r = hput cs io t
2009r = hfe r hputb cs io 32
2010r = hfe r hput cs io hv11
2011ret hfe r hcrlf cs io
2012end
2013note hhname + hhval / hhnum: one header field. Framing fields (Content-Length,
2014note Transfer-Encoding, Connection) are refused: hhend writes them. Values must be field-value
2015note bytes without leading or trailing whitespace (no CR, LF or NUL: no header injection).
2016fn hhname cs edit ulong * io edit uchar * nm view uchar * to long
2017if not htok nm
2018ret HEINVAL
2019end
2020if or or hieqm nm hnclen hieqm nm hnte hieqm nm hnconn
2021ret HEINVAL
2022end
2023r = hput cs io nm
2024r = hfe r hputb cs io 58
2025ret hfe r hputb cs io 32
2026end
2027fn hhval cs edit ulong * io edit uchar * v view uchar * to long
2028n = len v
2029if not hvall v
2030ret HEINVAL
2031end
2032if gt n 0
2033if or hws get v 0 hws get v sub n 1
2034ret HEINVAL
2035end
2036end
2037r = hput cs io v
2038ret hfe r hcrlf cs io
2039end
2040fn hhnum cs edit ulong * io edit uchar * v ulong to long
2041r = hdec cs io v
2042ret hfe r hcrlf cs io
2043end
2044note hhend: framing and the end of the head. mode 0 = Content-Length clen, 1 = chunked,
2045note 2 = close-delimited (server only). A server answering HTTP/1.0 turns chunked into 2; 1xx,
2046note 204 and 304 get no framing field and no body; a HEAD response keeps its framing fields and
2047note drops the body bytes. put cs HKEEP 0 before hhend to close after this message.
2048fn hhend cs edit ulong * io edit uchar * mode ulong clen ulong to long
2049if gt mode 2
2050ret HEINVAL
2051end
2052srv = eq get cs HSERVER 1
2053m = mode
2054nob = false
2055if srv
2056s = get cs HSCODE
2057if or or lt s 200 eq s 204 eq s 304
2058nob = true
2059end
2060if and eq m 1 eq get cs HVER 0
2061m = 2
2062end
2063else
2064if eq m 2
2065ret HEINVAL
2066end
2067end
2068r = long 0
2069if nob
2070m = 0
2071else
2072r = hhfram cs io m clen
2073end
2074if eq m 2
2075put cs HKEEP 0
2076end
2077if eq get cs HKEEP 0
2078r = hfe r hput cs io hocls
2079r = hfe r hcrlf cs io
2080end
2081r = hfe r hcrlf cs io
2082if lt r 0
2083ret r
2084end
2085put cs HOMODE m
2086put cs HWCL clen
2087put cs HWBODY 1
2088put cs HWOUT 0
2089if or nob and srv eq get cs HISHEAD 1
2090put cs HWDROP 1
2091end
2092if and eq m 1 eq get cs HWDROP 0
2093r = hflush cs io
2094put cs HWMODE 1
2095put cs HWLEN 10
2096end
2097ret r
2098end
2099fn hhfram cs edit ulong * io edit uchar * m ulong clen ulong to long
2100if eq m 1
2101r = hput cs io hote
2102ret hfe r hcrlf cs io
2103end
2104if eq m 0
2105if or gt clen 0 eq get cs HSERVER 1
2106r = hput cs io hoclen
2107r = hfe r hdec cs io clen
2108ret hfe r hcrlf cs io
2109end
2110end
2111ret 0
2112end
2113note hwfin: flush, end the chunked body, check a Content-Length body was exactly clen bytes
2114fn hwfin cs edit ulong * io edit uchar * to long
2115r = hflush cs io
2116if lt r 0
2117put cs HKEEP 0
2118ret r
2119end
2120drop = eq get cs HWDROP 1
2121m = get cs HOMODE
2122put cs HWBODY 0
2123put cs HWMODE 0
2124put cs HWLEN 0
2125if and eq m 1 not drop
2126b = get cs HWB
2127put io b 48
2128put io add b 1 13
2129put io add b 2 10
2130put io add b 3 13
2131put io add b 4 10
2132r = hsendio cs io b 5
2133end
2134if and eq m 0 not drop
2135if ne get cs HWOUT get cs HWCL
2136put cs HKEEP 0
2137ret HEINVAL
2138end
2139end
2140if eq get cs HSERVER 1
2141put cs HNMSG add get cs HNMSG 1
2142end
2143if lt r 0
2144put cs HKEEP 0
2145end
2146ret r
2147end
2148note hreject: answer a failed hreqrd result r. HEBAD .. HEVER answer with that status, HETIME
2149note with 408; any other value (peer gone, socket error) sends nothing. The connection must
2150note then be closed (HKEEP is 0). Returns the send result or 0.
2151fn hreject cs edit ulong * io edit uchar * r long to long
2152put cs HKEEP 0
2153if eq r HETIME
2154ret hrsperr cs io 408
2155end
2156if or eq r HEBAD eq r HEBIG
2157ret hrsperr cs io ulong neg r
2158end
2159if or or eq r HEHEAD eq r HEEXP or eq r HETE eq r HEVER
2160ret hrsperr cs io ulong neg r
2161end
2162ret 0
2163end
2164note hrsperr: a complete empty error response that closes the connection
2165fn hrsperr cs edit ulong * io edit uchar * code ulong to long
2166put cs HKEEP 0
2167ret hrspempty cs io code
2168end
2169note hrspempty: a complete response with an empty body; the connection stays reusable when
2170note the request allowed it (hnext still closes when the request body was left unread)
2171fn hrspempty cs edit ulong * io edit uchar * code ulong to long
2172r = hrspst cs io code
2173r = hfe r hhend cs io 0 0
2174ret hfe r hwfin cs io
2175end
2176note hnext: 1 when the connection may carry another message (keep-alive, body fully read,
2177note response finished, under HMAXMSG), else 0: the caller closes it
2178fn hnext cs edit ulong * io edit uchar * to long
2179if eq get cs HKEEP 0
2180ret 0
2181end
2182if eq get cs HWBODY 1
2183ret 0
2184end
2185m = get cs HBMODE
2186if and eq m 1 ne get cs HBREM 0
2187ret 0
2188end
2189if and eq m 2 ne get cs HCST 4
2190ret 0
2191end
2192if eq m 3
2193ret 0
2194end
2195if ge get cs HNMSG get cs HMAXMSG
2196ret 0
2197end
2198ret 1
2199end
2200note ======== server-sent events: writing (one event per chunk)
2201fn hrspsse cs edit ulong * io edit uchar * to long
2202r = hrspst cs io 200
2203r = hfe r hhname cs io hoctype
2204r = hfe r hhval cs io hvevst
2205r = hfe r hhname cs io hocache
2206r = hfe r hhval cs io hvnoca
2207ret hfe r hhend cs io 1 0
2208end
2209fn hsseok a view uchar * to bool
2210n = len a
2211for i ulong n
2212c = get a i
2213if or or eq c 10 eq c 13 eq c 0
2214ret false
2215end
2216end
2217ret true
2218end
2219fn hsseln1 cs edit ulong * io edit uchar * v view uchar * to long
2220r = hput cs io v
2221ret hfe r hputb cs io 10
2222end
2223fn hsseevt cs edit ulong * io edit uchar * v view uchar * to long
2224if not hsseok v
2225ret HEINVAL
2226end
2227r = hput cs io hsevent
2228ret hfe r hsseln1 cs io v
2229end
2230fn hsseid cs edit ulong * io edit uchar * v view uchar * to long
2231if not hsseok v
2232ret HEINVAL
2233end
2234r = hput cs io hsid
2235ret hfe r hsseln1 cs io v
2236end
2237fn hsseret cs edit ulong * io edit uchar * v view uchar * to long
2238if not hdigits v
2239ret HEINVAL
2240end
2241r = hput cs io hsretry
2242ret hfe r hsseln1 cs io v
2243end
2244fn hssecmt cs edit ulong * io edit uchar * v view uchar * to long
2245if not hsseok v
2246ret HEINVAL
2247end
2248r = hputb cs io 58
2249r = hfe r hputb cs io 32
2250ret hfe r hsseln1 cs io v
2251end
2252note hlnend: index of the first CR or LF in a at or after p, or len a
2253fn hlnend a view uchar * p ulong to ulong
2254n = len a
2255for b ulong 65536
2256s = add p mul b 65536
2257if ge s n
2258ret n
2259end
2260m = hmin sub n s 65536
2261for i ulong m
2262c = get a add s i
2263if or eq c 10 eq c 13
2264ret add s i
2265end
2266end
2267end
2268ret n
2269end
2270note hssedat: one `data: ` line per line of v (CR, LF and CRLF all end a line)
2271fn hssedat cs edit ulong * io edit uchar * v view uchar * to long
2272n = len v
2273p = ulong 0
2274r = long 0
2275for it ulong 16777216
2276q = hlnend v p
2277r = hfe r hput cs io hsdata
2278r = hfe r hput cs io view part v p sub q p
2279r = hfe r hputb cs io 10
2280if ge q n
2281ret r
2282end
2283p = add q 1
2284if eq get v q 13
2285if lt p n
2286if eq get v p 10
2287p = add p 1
2288end
2289end
2290end
2291end
2292ret r
2293end
2294fn hsseend cs edit ulong * io edit uchar * to long
2295r = hputb cs io 10
2296ret hfe r hflush cs io
2297end
2298note ======== server-sent events: incremental reader (WHATWG HTML 9.2.6)
2299fn hsseinit ss edit ulong * to long
2300if lt len ss HSSLEN
2301ret HEINVAL
2302end
2303for i ulong HSSLEN
2304put ss i 0
2305end
2306ret 0
2307end
2308note hssefeed: consume src; returns the bytes consumed. When an event completes it stops right
2309note after the dispatching line with get ss HSRDY = 1: type lb[0, HSELEN) (empty = message),
2310note id lb[64, 64+HSILEN), data lb[128, 128+HSDLEN). Call hssenext, then feed the rest.
2311fn hssefeed ss edit ulong * lb edit uchar * src view uchar * to long
2312if ne get ss HSRDY 0
2313ret HEINVAL
2314end
2315if lt len lb 256
2316ret HEINVAL
2317end
2318n = len src
2319i = ulong 0
2320for it ulong 4194304
2321if ge i n
2322ret long n
2323end
2324r = hssebyte ss lb get src i
2325i = add i 1
2326if lt r 0
2327ret r
2328end
2329if gt r 0
2330ret long i
2331end
2332end
2333ret long i
2334end
2335note hssebyte: one stream byte; 1 = an event is ready, 0 = continue, negative = error
2336fn hssebyte ss edit ulong * lb edit uchar * c uchar to long
2337if eq c 10
2338if eq get ss HSCR 1
2339put ss HSCR 0
2340ret 0
2341end
2342ret hssleol ss lb
2343end
2344put ss HSCR 0
2345if eq c 13
2346put ss HSCR 1
2347ret hssleol ss lb
2348end
2349w = add add HSDB get ss HSDLEN get ss HSLINE
2350if ge w len lb
2351ret HEBIG
2352end
2353put lb w c
2354put ss HSLINE add get ss HSLINE 1
2355ret 0
2356end
2357fn hssleol ss edit ulong * lb edit uchar * to long
2358r = hsseline ss lb
2359if lt r 0
2360ret r
2361end
2362if ne get ss HSRDY 0
2363ret 1
2364end
2365ret 0
2366end
2367fn hssenext ss edit ulong * to void
2368put ss HSRDY 0
2369put ss HSDLEN 0
2370put ss HSELEN 0
2371end
2372fn hssedis ss edit ulong * to long
2373n = get ss HSDLEN
2374if eq n 0
2375put ss HSELEN 0
2376ret 0
2377end
2378put ss HSDLEN sub n 1
2379put ss HSRDY 1
2380put ss HSNEV add get ss HSNEV 1
2381ret 0
2382end
2383note hssebom: strip one U+FEFF (EF BB BF) from the first line of the stream; the new length
2384fn hssebom ss edit ulong * lb edit uchar * s ulong l ulong to ulong
2385if ne get ss HSBOM 0
2386ret l
2387end
2388put ss HSBOM 1
2389if lt l 3
2390ret l
2391end
2392if and and eq get lb s 239 eq get lb add s 1 187 eq get lb add s 2 191
2393hmove lb s add s 3 sub l 3
2394ret sub l 3
2395end
2396ret l
2397end
2398fn hsseline ss edit ulong * lb edit uchar * to long
2399s = add HSDB get ss HSDLEN
2400l = hssebom ss lb s get ss HSLINE
2401put ss HSLINE 0
2402if eq l 0
2403ret hssedis ss
2404end
2405c = l
2406for i ulong l
2407if eq get lb add s i 58
2408c = i
2409break
2410end
2411end
2412if eq c 0
2413ret 0
2414end
2415vo = c
2416if lt c l
2417vo = add c 1
2418if lt vo l
2419if eq get lb add s vo 32
2420vo = add vo 1
2421end
2422end
2423end
2424put ss HSVO vo
2425put ss HSVL sub l vo
2426ret hssefv ss lb s c
2427end
2428note hssefv: apply field lb[s, s+c) with value lb[s+vo, s+vo+vl), vo and vl from ss
2429fn hssefv ss edit ulong * lb edit uchar * s ulong c ulong to long
2430vo = get ss HSVO
2431vl = get ss HSVL
2432if heq view part lb s c hfdata
2433hmove lb s add s vo vl
2434put lb add s vl 10
2435put ss HSDLEN add get ss HSDLEN add vl 1
2436ret 0
2437end
2438if heq view part lb s c hfevent
2439if gt vl HSFCAP
2440ret HEHEAD
2441end
2442hmove lb 0 add s vo vl
2443put ss HSELEN vl
2444ret 0
2445end
2446if heq view part lb s c hfid
2447if not hsseok view part lb add s vo vl
2448ret 0
2449end
2450if gt vl HSFCAP
2451ret HEHEAD
2452end
2453hmove lb HSIDB add s vo vl
2454put ss HSILEN vl
2455ret 0
2456end
2457if heq view part lb s c hfretry
2458if hdigits view part lb add s vo vl
2459put ss HSRETRY hdecv view part lb add s vo vl
2460end
2461end
2462ret 0
2463end