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