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