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
omega-mv / apps / edge / lib.om
| 1 | note aien-edge: pure Omega static edge HTTP router, path sanitizer, security header builder, and HTML renderer. |
| 2 | note Ported from upstream aien-edge (src/site.rs, src/handler.rs, src/config.rs). |
| 3 | note Compose with si/directives/om2.om before this file. |
| 4 | |
| 5 | const EDGE_METHOD_BAD ulong 0 |
| 6 | const EDGE_METHOD_GET ulong 1 |
| 7 | const EDGE_METHOD_HEAD ulong 2 |
| 8 | const EDGE_METHOD_OTHER ulong 3 |
| 9 | |
| 10 | const EDGE_SCHEME_HTTP ulong 0 |
| 11 | const EDGE_SCHEME_REDIRECT ulong 1 |
| 12 | const EDGE_SCHEME_HTTPS ulong 2 |
| 13 | |
| 14 | const EDGE_ENC_IDENTITY ulong 0 |
| 15 | const EDGE_ENC_GZIP ulong 1 |
| 16 | const EDGE_ENC_BR ulong 2 |
| 17 | |
| 18 | struct EdgeReq method ulong path_off ulong path_len ulong ok ulong end |
| 19 | struct EdgeResp status ulong body_len ulong no_script ulong is_html ulong is_asset ulong end |
| 20 | |
| 21 | text edge_t_get "GET" |
| 22 | text edge_t_head "HEAD" |
| 23 | text edge_t_post "POST" |
| 24 | text edge_t_put "PUT" |
| 25 | text edge_t_del "DELETE" |
| 26 | text edge_t_http11 "HTTP/1.1" |
| 27 | text edge_t_http10 "HTTP/1.0" |
| 28 | text edge_t_wellknown ".well-known" |
| 29 | text edge_t_map ".map" |
| 30 | text edge_t_assets "assets/" |
| 31 | text edge_t_hive_board "hive/board" |
| 32 | text edge_t_hive_board_idx "hive/board/index.html" |
| 33 | text edge_t_index "index.html" |
| 34 | text edge_t_health "health" |
| 35 | text edge_t_app_css "assets/app.css" |
| 36 | text edge_t_sec_txt ".well-known/security.txt" |
| 37 | |
| 38 | text edge_t_amp "&" |
| 39 | text edge_t_lt "<" |
| 40 | text edge_t_gt ">" |
| 41 | text edge_t_quot """ |
| 42 | text edge_t_apos "'" |
| 43 | |
| 44 | text edge_h_200 "HTTP/1.1 200 OK" |
| 45 | text edge_h_400 "HTTP/1.1 400 Bad Request" |
| 46 | text edge_h_403 "HTTP/1.1 403 Forbidden" |
| 47 | text edge_h_404 "HTTP/1.1 404 Not Found" |
| 48 | text edge_h_405 "HTTP/1.1 405 Method Not Allowed" |
| 49 | text edge_h_allow "Allow: GET, HEAD" |
| 50 | |
| 51 | text edge_h_srv "Server: aien-edge" |
| 52 | text edge_h_nosniff "X-Content-Type-Options: nosniff" |
| 53 | text edge_h_ref "Referrer-Policy: strict-origin-when-cross-origin" |
| 54 | text edge_h_xfo "X-Frame-Options: DENY" |
| 55 | text edge_h_perm "Permissions-Policy: camera=(), microphone=(), geolocation=()" |
| 56 | text edge_h_vary "Vary: Accept-Encoding" |
| 57 | text edge_h_hsts "Strict-Transport-Security: max-age=31536000" |
| 58 | text edge_h_csp_default "Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; base-uri 'self'; frame-ancestors 'none'" |
| 59 | text edge_h_csp_noscript "Content-Security-Policy: default-src 'none'; style-src 'self' 'unsafe-inline'; img-src 'self'; script-src 'none'; base-uri 'none'; frame-ancestors 'none'" |
| 60 | text edge_h_cc_asset "Cache-Control: public, max-age=31536000, immutable" |
| 61 | text edge_h_cc_html "Cache-Control: public, max-age=0, must-revalidate" |
| 62 | text edge_h_cc_other "Cache-Control: public, max-age=3600" |
| 63 | text edge_h_ct_html "Content-Type: text/html; charset=utf-8" |
| 64 | text edge_h_ct_text "Content-Type: text/plain; charset=utf-8" |
| 65 | text edge_h_ct_css "Content-Type: text/css; charset=utf-8" |
| 66 | text edge_h_cl "Content-Length: " |
| 67 | |
| 68 | text edge_p_head1 "<!doctype html>\n<html lang=\"en\">\n<head><meta charset=\"utf-8\"><title>" |
| 69 | text edge_p_head2 "</title></head>\n<body><h1>" |
| 70 | text edge_p_mid "</h1><p>" |
| 71 | text edge_p_foot "</p></body>\n</html>\n" |
| 72 | |
| 73 | text edge_t_home_title "AIEN Edge" |
| 74 | text edge_t_home_body "Deterministic pure-Omega static edge server." |
| 75 | text edge_t_board_title "Hive Board" |
| 76 | text edge_t_board_body "Read-only Hive Board surface served under strict no-script CSP." |
| 77 | text edge_t_400_title "400 Bad Request" |
| 78 | text edge_t_400_body "Request rejected by strict edge parser or path sanitizer." |
| 79 | text edge_t_403_title "403 Forbidden" |
| 80 | text edge_t_403_body "Access to traversal, hidden dotfile, or sourcemap path refused." |
| 81 | text edge_t_404_title "404 Not Found" |
| 82 | text edge_t_404_prefix "Resource not found: " |
| 83 | text edge_t_405_title "405 Method Not Allowed" |
| 84 | text edge_t_405_body "Only GET and HEAD methods are permitted." |
| 85 | text edge_t_health_body "ok\n" |
| 86 | text edge_t_css_body "body{font:16px/1.5 system-ui,sans-serif;margin:2rem auto;max-width:44rem}\n" |
| 87 | text edge_t_sec_body "Contact: mailto:security@aienos.com\nPreferred-Languages: en\n" |
| 88 | |
| 89 | text edge_t_br "br" |
| 90 | text edge_t_gzip "gzip" |
| 91 | text edge_t_q0 "q=0" |
| 92 | text edge_t_q00 "q=0.0" |
| 93 | text edge_t_q000 "q=0.00" |
| 94 | text edge_t_q0000 "q=0.000" |
| 95 | |
| 96 | fn edge_to_lower c uchar to uchar |
| 97 | if ge c 65 |
| 98 | if le c 90 |
| 99 | ret add c 32 |
| 100 | end |
| 101 | end |
| 102 | ret c |
| 103 | end |
| 104 | |
| 105 | fn edge_hex_val c uchar to int |
| 106 | if ge c 48 |
| 107 | if le c 57 |
| 108 | ret int sub c 48 |
| 109 | end |
| 110 | end |
| 111 | if ge c 97 |
| 112 | if le c 102 |
| 113 | ret int add sub c 97 10 |
| 114 | end |
| 115 | end |
| 116 | if ge c 65 |
| 117 | if le c 70 |
| 118 | ret int add sub c 65 10 |
| 119 | end |
| 120 | end |
| 121 | ret -1 |
| 122 | end |
| 123 | |
| 124 | fn edge_eq_bytes a view uchar * b view uchar * to bool |
| 125 | n = len a |
| 126 | if ne n len b |
| 127 | ret false |
| 128 | end |
| 129 | i = ulong 0 |
| 130 | while lt i n bound 65536 |
| 131 | if ne get a i get b i |
| 132 | ret false |
| 133 | end |
| 134 | i = add i 1 |
| 135 | end |
| 136 | ret true |
| 137 | end |
| 138 | |
| 139 | fn edge_eq_ci a view uchar * b view uchar * to bool |
| 140 | n = len a |
| 141 | if ne n len b |
| 142 | ret false |
| 143 | end |
| 144 | i = ulong 0 |
| 145 | while lt i n bound 65536 |
| 146 | if ne edge_to_lower get a i edge_to_lower get b i |
| 147 | ret false |
| 148 | end |
| 149 | i = add i 1 |
| 150 | end |
| 151 | ret true |
| 152 | end |
| 153 | |
| 154 | fn edge_starts_with a view uchar * pfx view uchar * to bool |
| 155 | pn = len pfx |
| 156 | if lt len a pn |
| 157 | ret false |
| 158 | end |
| 159 | i = ulong 0 |
| 160 | while lt i pn bound 65536 |
| 161 | if ne get a i get pfx i |
| 162 | ret false |
| 163 | end |
| 164 | i = add i 1 |
| 165 | end |
| 166 | ret true |
| 167 | end |
| 168 | |
| 169 | fn edge_ends_with a view uchar * sfx view uchar * to bool |
| 170 | sn = len sfx |
| 171 | an = len a |
| 172 | if lt an sn |
| 173 | ret false |
| 174 | end |
| 175 | base = sub an sn |
| 176 | i = ulong 0 |
| 177 | while lt i sn bound 65536 |
| 178 | if ne get a add base i get sfx i |
| 179 | ret false |
| 180 | end |
| 181 | i = add i 1 |
| 182 | end |
| 183 | ret true |
| 184 | end |
| 185 | |
| 186 | fn edge_append dst edit uchar * pos ulong src view uchar * to long |
| 187 | n = len src |
| 188 | if gt add pos n len dst |
| 189 | ret -1 |
| 190 | end |
| 191 | i = ulong 0 |
| 192 | while lt i n bound 65536 |
| 193 | put dst add pos i get src i |
| 194 | i = add i 1 |
| 195 | end |
| 196 | ret long add pos n |
| 197 | end |
| 198 | |
| 199 | fn edge_append_u64 dst edit uchar * pos ulong val ulong to long |
| 200 | tmp = new uchar 24 0 |
| 201 | cnt = ulong 0 |
| 202 | v = val |
| 203 | if eq v 0 |
| 204 | put tmp 0 uchar 48 |
| 205 | cnt = ulong 1 |
| 206 | else |
| 207 | while gt v 0 bound 24 |
| 208 | d = rem v 10 |
| 209 | put tmp cnt uchar add d 48 |
| 210 | v = div v 10 |
| 211 | cnt = add cnt 1 |
| 212 | end |
| 213 | end |
| 214 | if gt add pos cnt len dst |
| 215 | ret -1 |
| 216 | end |
| 217 | i = ulong 0 |
| 218 | while lt i cnt bound 24 |
| 219 | put dst add pos i get tmp sub sub cnt 1 i |
| 220 | i = add i 1 |
| 221 | end |
| 222 | ret long add pos cnt |
| 223 | end |
| 224 | |
| 225 | fn edge_escape_html dst edit uchar * pos ulong src view uchar * to long |
| 226 | n = len src |
| 227 | p = pos |
| 228 | i = ulong 0 |
| 229 | while lt i n bound 65536 |
| 230 | c = get src i |
| 231 | if eq c 38 |
| 232 | np = edge_append dst p edge_t_amp |
| 233 | if lt np 0 |
| 234 | ret -1 |
| 235 | end |
| 236 | p = ulong np |
| 237 | else |
| 238 | if eq c 60 |
| 239 | np = edge_append dst p edge_t_lt |
| 240 | if lt np 0 |
| 241 | ret -1 |
| 242 | end |
| 243 | p = ulong np |
| 244 | else |
| 245 | if eq c 62 |
| 246 | np = edge_append dst p edge_t_gt |
| 247 | if lt np 0 |
| 248 | ret -1 |
| 249 | end |
| 250 | p = ulong np |
| 251 | else |
| 252 | if eq c 34 |
| 253 | np = edge_append dst p edge_t_quot |
| 254 | if lt np 0 |
| 255 | ret -1 |
| 256 | end |
| 257 | p = ulong np |
| 258 | else |
| 259 | if eq c 39 |
| 260 | np = edge_append dst p edge_t_apos |
| 261 | if lt np 0 |
| 262 | ret -1 |
| 263 | end |
| 264 | p = ulong np |
| 265 | else |
| 266 | if ge p len dst |
| 267 | ret -1 |
| 268 | end |
| 269 | put dst p c |
| 270 | p = add p 1 |
| 271 | end |
| 272 | end |
| 273 | end |
| 274 | end |
| 275 | end |
| 276 | i = add i 1 |
| 277 | end |
| 278 | ret long p |
| 279 | end |
| 280 | |
| 281 | fn edge_bad_path_byte b uchar to bool |
| 282 | if lt b 32 |
| 283 | ret true |
| 284 | end |
| 285 | if eq b 127 |
| 286 | ret true |
| 287 | end |
| 288 | if eq b 92 |
| 289 | ret true |
| 290 | end |
| 291 | ret false |
| 292 | end |
| 293 | |
| 294 | fn edge_decode_pct raw view uchar * i ulong to int |
| 295 | if gt add i 3 len raw |
| 296 | ret -1 |
| 297 | end |
| 298 | hi = edge_hex_val get raw add i 1 |
| 299 | if lt hi 0 |
| 300 | ret -1 |
| 301 | end |
| 302 | lo = edge_hex_val get raw add i 2 |
| 303 | if lt lo 0 |
| 304 | ret -1 |
| 305 | end |
| 306 | ret add mul hi 16 lo |
| 307 | end |
| 308 | |
| 309 | note Percent-decode raw URI path up to '?' or '#'. |
| 310 | note Returns decoded length >= 0, -1 malformed %XX, -2 NUL/control/backslash, -4 overflow. |
| 311 | fn edge_percent_decode raw view uchar * dst edit uchar * to long |
| 312 | n = len raw |
| 313 | i = ulong 0 |
| 314 | w = ulong 0 |
| 315 | while lt i n bound 8192 |
| 316 | c = get raw i |
| 317 | if eq c 63 |
| 318 | break |
| 319 | end |
| 320 | if eq c 35 |
| 321 | break |
| 322 | end |
| 323 | b = c |
| 324 | if eq c 37 |
| 325 | v = edge_decode_pct raw i |
| 326 | if lt v 0 |
| 327 | ret -1 |
| 328 | end |
| 329 | b = uchar v |
| 330 | i = add i 3 |
| 331 | else |
| 332 | i = add i 1 |
| 333 | end |
| 334 | if edge_bad_path_byte b |
| 335 | ret -2 |
| 336 | end |
| 337 | if ge w len dst |
| 338 | ret -4 |
| 339 | end |
| 340 | put dst w b |
| 341 | w = add w 1 |
| 342 | end |
| 343 | ret long w |
| 344 | end |
| 345 | |
| 346 | note Check one decoded path segment: 0=skip, 1=keep, -3=refuse (.. or hidden dotfile except .well-known). |
| 347 | fn edge_check_segment seg view uchar * to int |
| 348 | n = len seg |
| 349 | if eq n 0 |
| 350 | ret 0 |
| 351 | end |
| 352 | if eq n 1 |
| 353 | if eq get seg 0 46 |
| 354 | ret 0 |
| 355 | end |
| 356 | ret 1 |
| 357 | end |
| 358 | if eq n 2 |
| 359 | if eq get seg 0 46 |
| 360 | if eq get seg 1 46 |
| 361 | ret -3 |
| 362 | end |
| 363 | end |
| 364 | end |
| 365 | if eq get seg 0 46 |
| 366 | if edge_eq_bytes seg edge_t_wellknown |
| 367 | ret 1 |
| 368 | end |
| 369 | ret -3 |
| 370 | end |
| 371 | ret 1 |
| 372 | end |
| 373 | |
| 374 | note Normalize URL path: percent-decode, reject NUL/control/backslash/traversal/hidden/.map. |
| 375 | note Returns normalized length >= 0, -1 bad hex (400), -2 NUL/ctrl/backslash (400), -3 traversal/hidden/map (403), -4 overflow. |
| 376 | fn edge_normalize_path raw view uchar * dst edit uchar * to long |
| 377 | dec = new uchar 2048 0 |
| 378 | dlen = edge_percent_decode raw dec |
| 379 | if lt dlen 0 |
| 380 | ret dlen |
| 381 | end |
| 382 | dn = ulong dlen |
| 383 | w = ulong 0 |
| 384 | i = ulong 0 |
| 385 | while lt i dn bound 2048 |
| 386 | start = i |
| 387 | while lt i dn bound 2048 |
| 388 | if eq get dec i 47 |
| 389 | break |
| 390 | end |
| 391 | i = add i 1 |
| 392 | end |
| 393 | slen = sub i start |
| 394 | if lt i dn |
| 395 | i = add i 1 |
| 396 | end |
| 397 | verdict = edge_check_segment part dec start slen |
| 398 | if lt verdict 0 |
| 399 | ret long verdict |
| 400 | end |
| 401 | if eq verdict 1 |
| 402 | if gt w 0 |
| 403 | if ge w len dst |
| 404 | ret -4 |
| 405 | end |
| 406 | put dst w uchar 47 |
| 407 | w = add w 1 |
| 408 | end |
| 409 | nw = edge_append dst w part dec start slen |
| 410 | if lt nw 0 |
| 411 | ret -4 |
| 412 | end |
| 413 | w = ulong nw |
| 414 | end |
| 415 | end |
| 416 | if edge_ends_with part dst 0 w edge_t_map |
| 417 | ret -3 |
| 418 | end |
| 419 | ret long w |
| 420 | end |
| 421 | |
| 422 | note Normalize Host header: strip :port, strip trailing '.', lowercase ASCII. |
| 423 | note Returns length >= 0, or -1 if invalid. |
| 424 | fn edge_normalize_host raw view uchar * dst edit uchar * to long |
| 425 | n = len raw |
| 426 | if eq n 0 |
| 427 | ret -1 |
| 428 | end |
| 429 | colon = n |
| 430 | i = ulong 0 |
| 431 | while lt i n bound 512 |
| 432 | c = get raw i |
| 433 | if le c 32 |
| 434 | ret -1 |
| 435 | end |
| 436 | if eq c 127 |
| 437 | ret -1 |
| 438 | end |
| 439 | if eq c 47 |
| 440 | ret -1 |
| 441 | end |
| 442 | if eq c 58 |
| 443 | colon = i |
| 444 | end |
| 445 | i = add i 1 |
| 446 | end |
| 447 | endp = n |
| 448 | if lt colon n |
| 449 | all_dig = true |
| 450 | j = add colon 1 |
| 451 | if eq j n |
| 452 | all_dig = false |
| 453 | end |
| 454 | while lt j n bound 16 |
| 455 | dc = get raw j |
| 456 | if lt dc 48 |
| 457 | all_dig = false |
| 458 | end |
| 459 | if gt dc 57 |
| 460 | all_dig = false |
| 461 | end |
| 462 | j = add j 1 |
| 463 | end |
| 464 | if all_dig |
| 465 | endp = colon |
| 466 | end |
| 467 | end |
| 468 | while gt endp 0 bound 512 |
| 469 | if ne get raw sub endp 1 46 |
| 470 | break |
| 471 | end |
| 472 | endp = sub endp 1 |
| 473 | end |
| 474 | if eq endp 0 |
| 475 | ret -1 |
| 476 | end |
| 477 | if gt endp len dst |
| 478 | ret -1 |
| 479 | end |
| 480 | k = ulong 0 |
| 481 | while lt k endp bound 512 |
| 482 | put dst k edge_to_lower get raw k |
| 483 | k = add k 1 |
| 484 | end |
| 485 | ret long endp |
| 486 | end |
| 487 | |
| 488 | note Check if Accept-Encoding header allows encoding `name` without q=0. |
| 489 | fn edge_tok_allows tok view uchar * name view uchar * to bool |
| 490 | tn = len tok |
| 491 | start = ulong 0 |
| 492 | while lt start tn bound 256 |
| 493 | if ne get tok start 32 |
| 494 | if ne get tok start 9 |
| 495 | break |
| 496 | end |
| 497 | end |
| 498 | start = add start 1 |
| 499 | end |
| 500 | semi = start |
| 501 | while lt semi tn bound 256 |
| 502 | if eq get tok semi 59 |
| 503 | break |
| 504 | end |
| 505 | semi = add semi 1 |
| 506 | end |
| 507 | cend = semi |
| 508 | while gt cend start bound 256 |
| 509 | c = get tok sub cend 1 |
| 510 | if ne c 32 |
| 511 | if ne c 9 |
| 512 | break |
| 513 | end |
| 514 | end |
| 515 | cend = sub cend 1 |
| 516 | end |
| 517 | if not edge_eq_ci part tok start sub cend start name |
| 518 | ret false |
| 519 | end |
| 520 | if ge semi tn |
| 521 | ret true |
| 522 | end |
| 523 | p = add semi 1 |
| 524 | while lt p tn bound 256 |
| 525 | while lt p tn bound 256 |
| 526 | if ne get tok p 32 |
| 527 | if ne get tok p 9 |
| 528 | break |
| 529 | end |
| 530 | end |
| 531 | p = add p 1 |
| 532 | end |
| 533 | qend = p |
| 534 | while lt qend tn bound 256 |
| 535 | if eq get tok qend 59 |
| 536 | break |
| 537 | end |
| 538 | qend = add qend 1 |
| 539 | end |
| 540 | te = qend |
| 541 | while gt te p bound 256 |
| 542 | c = get tok sub te 1 |
| 543 | if ne c 32 |
| 544 | if ne c 9 |
| 545 | break |
| 546 | end |
| 547 | end |
| 548 | te = sub te 1 |
| 549 | end |
| 550 | if edge_eq_ci part tok p sub te p edge_t_q0 |
| 551 | ret false |
| 552 | end |
| 553 | if edge_eq_ci part tok p sub te p edge_t_q00 |
| 554 | ret false |
| 555 | end |
| 556 | if edge_eq_ci part tok p sub te p edge_t_q000 |
| 557 | ret false |
| 558 | end |
| 559 | if edge_eq_ci part tok p sub te p edge_t_q0000 |
| 560 | ret false |
| 561 | end |
| 562 | p = add qend 1 |
| 563 | end |
| 564 | ret true |
| 565 | end |
| 566 | |
| 567 | fn edge_accept_allows hdr view uchar * name view uchar * to bool |
| 568 | n = len hdr |
| 569 | i = ulong 0 |
| 570 | while lt i n bound 1024 |
| 571 | start = i |
| 572 | while lt i n bound 1024 |
| 573 | if eq get hdr i 44 |
| 574 | break |
| 575 | end |
| 576 | i = add i 1 |
| 577 | end |
| 578 | if edge_tok_allows part hdr start sub i start name |
| 579 | ret true |
| 580 | end |
| 581 | if lt i n |
| 582 | i = add i 1 |
| 583 | end |
| 584 | end |
| 585 | ret false |
| 586 | end |
| 587 | |
| 588 | fn edge_pick_encoding hdr view uchar * has_br bool has_gzip bool to ulong |
| 589 | if has_br |
| 590 | if edge_accept_allows hdr edge_t_br |
| 591 | ret EDGE_ENC_BR |
| 592 | end |
| 593 | end |
| 594 | if has_gzip |
| 595 | if edge_accept_allows hdr edge_t_gzip |
| 596 | ret EDGE_ENC_GZIP |
| 597 | end |
| 598 | end |
| 599 | ret EDGE_ENC_IDENTITY |
| 600 | end |
| 601 | |
| 602 | fn edge_find_sp line view uchar * start ulong n ulong to long |
| 603 | i = start |
| 604 | while lt i n bound 2048 |
| 605 | c = get line i |
| 606 | if lt c 32 |
| 607 | ret -1 |
| 608 | end |
| 609 | if eq c 127 |
| 610 | ret -1 |
| 611 | end |
| 612 | if eq c 32 |
| 613 | ret long i |
| 614 | end |
| 615 | i = add i 1 |
| 616 | end |
| 617 | ret -1 |
| 618 | end |
| 619 | |
| 620 | fn edge_classify_method m view uchar * to ulong |
| 621 | if edge_eq_bytes m edge_t_get |
| 622 | ret EDGE_METHOD_GET |
| 623 | end |
| 624 | if edge_eq_bytes m edge_t_head |
| 625 | ret EDGE_METHOD_HEAD |
| 626 | end |
| 627 | if edge_eq_bytes m edge_t_post |
| 628 | ret EDGE_METHOD_OTHER |
| 629 | end |
| 630 | if edge_eq_bytes m edge_t_put |
| 631 | ret EDGE_METHOD_OTHER |
| 632 | end |
| 633 | if edge_eq_bytes m edge_t_del |
| 634 | ret EDGE_METHOD_OTHER |
| 635 | end |
| 636 | ret EDGE_METHOD_BAD |
| 637 | end |
| 638 | |
| 639 | fn edge_valid_http_ver v view uchar * to bool |
| 640 | if edge_eq_bytes v edge_t_http11 |
| 641 | ret true |
| 642 | end |
| 643 | ret edge_eq_bytes v edge_t_http10 |
| 644 | end |
| 645 | |
| 646 | note Parse HTTP request line: "METHOD TARGET HTTP/1.1" |
| 647 | fn edge_parse_req_line line view uchar * req edit EdgeReq to bool |
| 648 | put req ok ulong 0 |
| 649 | put req method EDGE_METHOD_BAD |
| 650 | n = len line |
| 651 | if gt n 0 |
| 652 | if eq get line sub n 1 10 |
| 653 | n = sub n 1 |
| 654 | end |
| 655 | end |
| 656 | if gt n 0 |
| 657 | if eq get line sub n 1 13 |
| 658 | n = sub n 1 |
| 659 | end |
| 660 | end |
| 661 | if lt n 12 |
| 662 | ret false |
| 663 | end |
| 664 | s1 = edge_find_sp line 0 n |
| 665 | if le s1 0 |
| 666 | ret false |
| 667 | end |
| 668 | s2 = edge_find_sp line add ulong s1 1 n |
| 669 | if le s2 add s1 1 |
| 670 | ret false |
| 671 | end |
| 672 | voff = add ulong s2 1 |
| 673 | if not edge_valid_http_ver part line voff sub n voff |
| 674 | ret false |
| 675 | end |
| 676 | m = edge_classify_method part line 0 ulong s1 |
| 677 | if eq m EDGE_METHOD_BAD |
| 678 | ret false |
| 679 | end |
| 680 | put req method m |
| 681 | put req path_off add ulong s1 1 |
| 682 | put req path_len sub ulong s2 add ulong s1 1 |
| 683 | put req ok ulong 1 |
| 684 | ret true |
| 685 | end |
| 686 | |
| 687 | note Deterministic HTML page renderer with strict escaping of title and body. |
| 688 | fn edge_render_html dst edit uchar * title view uchar * body view uchar * to long |
| 689 | p = edge_append dst 0 edge_p_head1 |
| 690 | if lt p 0 |
| 691 | ret -1 |
| 692 | end |
| 693 | p = edge_escape_html dst ulong p title |
| 694 | if lt p 0 |
| 695 | ret -1 |
| 696 | end |
| 697 | p = edge_append dst ulong p edge_p_head2 |
| 698 | if lt p 0 |
| 699 | ret -1 |
| 700 | end |
| 701 | p = edge_escape_html dst ulong p title |
| 702 | if lt p 0 |
| 703 | ret -1 |
| 704 | end |
| 705 | p = edge_append dst ulong p edge_p_mid |
| 706 | if lt p 0 |
| 707 | ret -1 |
| 708 | end |
| 709 | p = edge_escape_html dst ulong p body |
| 710 | if lt p 0 |
| 711 | ret -1 |
| 712 | end |
| 713 | ret edge_append dst ulong p edge_p_foot |
| 714 | end |
| 715 | |
| 716 | note Check if normalized path is under /hive/board/ (requires strict no-script CSP). |
| 717 | fn edge_is_hive_board norm view uchar * to bool |
| 718 | if edge_eq_bytes norm edge_t_hive_board |
| 719 | ret true |
| 720 | end |
| 721 | if edge_starts_with norm edge_t_hive_board |
| 722 | if gt len norm 10 |
| 723 | if eq get norm 10 47 |
| 724 | ret true |
| 725 | end |
| 726 | end |
| 727 | end |
| 728 | ret false |
| 729 | end |
| 730 | |
| 731 | fn edge_append_crlf dst edit uchar * pos ulong to long |
| 732 | if gt add pos 2 len dst |
| 733 | ret -1 |
| 734 | end |
| 735 | put dst pos uchar 13 |
| 736 | put dst add pos 1 uchar 10 |
| 737 | ret long add pos 2 |
| 738 | end |
| 739 | |
| 740 | fn edge_append_line dst edit uchar * pos ulong src view uchar * to long |
| 741 | p = edge_append dst pos src |
| 742 | if lt p 0 |
| 743 | ret -1 |
| 744 | end |
| 745 | ret edge_append_crlf dst ulong p |
| 746 | end |
| 747 | |
| 748 | note Emit HTTP response headers including security headers, CSP, Cache-Control, Content-Type, Content-Length. |
| 749 | fn edge_emit_sec_lines dst edit uchar * pos ulong to long |
| 750 | p = edge_append_line dst pos edge_h_srv |
| 751 | if lt p 0 |
| 752 | ret -1 |
| 753 | end |
| 754 | p = edge_append_line dst ulong p edge_h_nosniff |
| 755 | if lt p 0 |
| 756 | ret -1 |
| 757 | end |
| 758 | p = edge_append_line dst ulong p edge_h_ref |
| 759 | if lt p 0 |
| 760 | ret -1 |
| 761 | end |
| 762 | p = edge_append_line dst ulong p edge_h_xfo |
| 763 | if lt p 0 |
| 764 | ret -1 |
| 765 | end |
| 766 | p = edge_append_line dst ulong p edge_h_perm |
| 767 | if lt p 0 |
| 768 | ret -1 |
| 769 | end |
| 770 | ret edge_append_line dst ulong p edge_h_vary |
| 771 | end |
| 772 | |
| 773 | fn edge_emit_headers dst edit uchar * meta view EdgeResp scheme ulong to long |
| 774 | st = get meta status |
| 775 | p = long 0 |
| 776 | if eq st 200 |
| 777 | p = edge_append_line dst 0 edge_h_200 |
| 778 | else |
| 779 | if eq st 400 |
| 780 | p = edge_append_line dst 0 edge_h_400 |
| 781 | else |
| 782 | if eq st 403 |
| 783 | p = edge_append_line dst 0 edge_h_403 |
| 784 | else |
| 785 | if eq st 404 |
| 786 | p = edge_append_line dst 0 edge_h_404 |
| 787 | else |
| 788 | p = edge_append_line dst 0 edge_h_405 |
| 789 | if lt p 0 |
| 790 | ret -1 |
| 791 | end |
| 792 | p = edge_append_line dst ulong p edge_h_allow |
| 793 | end |
| 794 | end |
| 795 | end |
| 796 | end |
| 797 | if lt p 0 |
| 798 | ret -1 |
| 799 | end |
| 800 | p = edge_emit_sec_lines dst ulong p |
| 801 | if lt p 0 |
| 802 | ret -1 |
| 803 | end |
| 804 | if eq scheme EDGE_SCHEME_HTTPS |
| 805 | p = edge_append_line dst ulong p edge_h_hsts |
| 806 | if lt p 0 |
| 807 | ret -1 |
| 808 | end |
| 809 | end |
| 810 | if eq get meta no_script 1 |
| 811 | p = edge_append_line dst ulong p edge_h_csp_noscript |
| 812 | else |
| 813 | p = edge_append_line dst ulong p edge_h_csp_default |
| 814 | end |
| 815 | if lt p 0 |
| 816 | ret -1 |
| 817 | end |
| 818 | if eq get meta is_asset 1 |
| 819 | p = edge_append_line dst ulong p edge_h_cc_asset |
| 820 | else |
| 821 | if eq get meta is_html 1 |
| 822 | p = edge_append_line dst ulong p edge_h_cc_html |
| 823 | else |
| 824 | p = edge_append_line dst ulong p edge_h_cc_other |
| 825 | end |
| 826 | end |
| 827 | if lt p 0 |
| 828 | ret -1 |
| 829 | end |
| 830 | if eq get meta is_html 1 |
| 831 | p = edge_append_line dst ulong p edge_h_ct_html |
| 832 | else |
| 833 | if eq get meta is_asset 1 |
| 834 | p = edge_append_line dst ulong p edge_h_ct_css |
| 835 | else |
| 836 | p = edge_append_line dst ulong p edge_h_ct_text |
| 837 | end |
| 838 | end |
| 839 | if lt p 0 |
| 840 | ret -1 |
| 841 | end |
| 842 | p = edge_append dst ulong p edge_h_cl |
| 843 | if lt p 0 |
| 844 | ret -1 |
| 845 | end |
| 846 | p = edge_append_u64 dst ulong p get meta body_len |
| 847 | if lt p 0 |
| 848 | ret -1 |
| 849 | end |
| 850 | ret edge_append_crlf dst ulong p |
| 851 | end |
| 852 | |
| 853 | note Build 404 body with strict HTML escaping of the requested path. |
| 854 | fn edge_build_404_body dst edit uchar * norm view uchar * to long |
| 855 | msg = new uchar 2200 0 |
| 856 | m = edge_append msg 0 edge_t_404_prefix |
| 857 | if lt m 0 |
| 858 | ret -1 |
| 859 | end |
| 860 | m = edge_append msg ulong m norm |
| 861 | if lt m 0 |
| 862 | ret -1 |
| 863 | end |
| 864 | ret edge_render_html dst edge_t_404_title part msg 0 ulong m |
| 865 | end |
| 866 | |
| 867 | fn edge_dispatch_norm norm view uchar * body edit uchar * meta edit EdgeResp to long |
| 868 | if edge_is_hive_board norm |
| 869 | put meta no_script ulong 1 |
| 870 | end |
| 871 | if eq len norm 0 |
| 872 | put meta status ulong 200 |
| 873 | ret edge_render_html body edge_t_home_title edge_t_home_body |
| 874 | end |
| 875 | if edge_eq_bytes norm edge_t_index |
| 876 | put meta status ulong 200 |
| 877 | ret edge_render_html body edge_t_home_title edge_t_home_body |
| 878 | end |
| 879 | if edge_eq_bytes norm edge_t_hive_board |
| 880 | put meta status ulong 200 |
| 881 | ret edge_render_html body edge_t_board_title edge_t_board_body |
| 882 | end |
| 883 | if edge_eq_bytes norm edge_t_hive_board_idx |
| 884 | put meta status ulong 200 |
| 885 | ret edge_render_html body edge_t_board_title edge_t_board_body |
| 886 | end |
| 887 | if edge_eq_bytes norm edge_t_health |
| 888 | put meta status ulong 200 |
| 889 | put meta is_html ulong 0 |
| 890 | ret edge_append body 0 edge_t_health_body |
| 891 | end |
| 892 | if edge_eq_bytes norm edge_t_app_css |
| 893 | put meta status ulong 200 |
| 894 | put meta is_html ulong 0 |
| 895 | put meta is_asset ulong 1 |
| 896 | ret edge_append body 0 edge_t_css_body |
| 897 | end |
| 898 | if edge_eq_bytes norm edge_t_sec_txt |
| 899 | put meta status ulong 200 |
| 900 | put meta is_html ulong 0 |
| 901 | ret edge_append body 0 edge_t_sec_body |
| 902 | end |
| 903 | put meta status ulong 404 |
| 904 | ret edge_build_404_body body norm |
| 905 | end |
| 906 | |
| 907 | fn edge_route_path raw_path view uchar * body edit uchar * meta edit EdgeResp to long |
| 908 | norm = new uchar 2048 0 |
| 909 | nlen = edge_normalize_path raw_path norm |
| 910 | if lt nlen 0 |
| 911 | if eq nlen -3 |
| 912 | put meta status ulong 403 |
| 913 | ret edge_render_html body edge_t_403_title edge_t_403_body |
| 914 | end |
| 915 | put meta status ulong 400 |
| 916 | ret edge_render_html body edge_t_400_title edge_t_400_body |
| 917 | end |
| 918 | ret edge_dispatch_norm part norm 0 ulong nlen body meta |
| 919 | end |
| 920 | |
| 921 | note Route an HTTP request line and emit complete deterministic HTTP/1.1 response. |
| 922 | note Returns response byte length >= 0 (and populates meta.status), or -1 on buffer overflow. |
| 923 | fn edge_handle_request req_line view uchar * scheme ulong resp edit uchar * meta edit EdgeResp to long |
| 924 | put meta status ulong 400 |
| 925 | put meta body_len ulong 0 |
| 926 | put meta no_script ulong 0 |
| 927 | put meta is_html ulong 1 |
| 928 | put meta is_asset ulong 0 |
| 929 | req = new EdgeReq 0 0 0 0 |
| 930 | body = new uchar 4096 0 |
| 931 | blen = long 0 |
| 932 | is_head = false |
| 933 | if not edge_parse_req_line req_line req |
| 934 | put meta status ulong 400 |
| 935 | blen = edge_render_html body edge_t_400_title edge_t_400_body |
| 936 | else |
| 937 | m = get req method |
| 938 | if eq m EDGE_METHOD_OTHER |
| 939 | put meta status ulong 405 |
| 940 | blen = edge_render_html body edge_t_405_title edge_t_405_body |
| 941 | else |
| 942 | if eq m EDGE_METHOD_HEAD |
| 943 | is_head = true |
| 944 | end |
| 945 | blen = edge_route_path part req_line get req path_off get req path_len body meta |
| 946 | end |
| 947 | end |
| 948 | if lt blen 0 |
| 949 | ret -1 |
| 950 | end |
| 951 | put meta body_len ulong blen |
| 952 | hp = edge_emit_headers resp meta scheme |
| 953 | if lt hp 0 |
| 954 | ret -1 |
| 955 | end |
| 956 | hp = edge_append_crlf resp ulong hp |
| 957 | if lt hp 0 |
| 958 | ret -1 |
| 959 | end |
| 960 | if is_head |
| 961 | ret hp |
| 962 | end |
| 963 | ret edge_append resp ulong hp part body 0 ulong blen |
| 964 | end |