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