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 / http.om

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