code wiki / _hdl_build / nx_lib_research_alias_gate.nx
nx_lib_research_alias_gate.nx source
↩ module page · 68 lines · 3444 B
1// nx_lib_research_alias_gate.nx -- gate for the /research edge-prefix alias in nx_lib_http.
2// The sites edge conf-routes `nishifamily.com /research 8095 stream`; STREAM mode passes the
3// RAW request through (no prefix strip), so lhd_response now accepts the /research prefix by
4// delegating to the identical stripped route. This gate PROVES the alias byte-identical:
5// T1 /research/ == / (home)
6// T2 /research/api/works == /api/works (JSON API)
7// T3 /research?q=cat == /?q=cat (re-rooted query form)
8// T4 /research == / (bare prefix -> home)
9// NEG T5 /researchfoo -> 404 (prefix must be followed by / or ? -- no greedy match)
10// NEG T6 /bogus -> 404 (untouched routes still 404)
11// license_tier: ORIGINAL
12import "nx_lib_http.nx"
13
14func ga_eq(a: *u8, an: i64, b: *u8, bn: i64) -> i64 {
15 if an != bn { return 0 }
16 var i: i64 = 0
17 while i < an { if a[i] != b[i] { return 0 } i = i + 1 }
18 return 1
19}
20
21func ga_has404(a: *u8, an: i64) -> i64 {
22 // look for "404" in the status line
23 var i: i64 = 0
24 while i + 2 < an {
25 if a[i] == 52 as u8 { if a[i+1] == 48 as u8 { if a[i+2] == 52 as u8 { return 1 } } }
26 i = i + 1
27 }
28 return 0
29}
30
31func ga_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
32func ga_puts(s: *u8) -> i64 { sys_write(1, s, ga_len(s)); return 0 }
33
34// run one equality case: alias path vs canonical path must be byte-identical and non-empty.
35func ga_case(name: *u8, ap: *u8, cp: *u8, oa: *u8, ob: *u8) -> i64 {
36 let an: i64 = lhd_response(ap, ga_len(ap), oa)
37 let bn: i64 = lhd_response(cp, ga_len(cp), ob)
38 if an <= 0 { ga_puts(" [FAIL] " as *u8); ga_puts(name); ga_puts(" (empty alias response)\n" as *u8); return 0 }
39 if ga_eq(oa, an, ob, bn) == 0 { ga_puts(" [FAIL] " as *u8); ga_puts(name); ga_puts(" (alias != canonical)\n" as *u8); return 0 }
40 ga_puts(" [OK] " as *u8); ga_puts(name); ga_puts("\n" as *u8)
41 return 1
42}
43
44func main() -> i64 {
45 ga_puts("=== nx_lib_research_alias_gate: /research prefix alias byte-identical ===\n" as *u8)
46 let oa: *u8 = sys_mmap(1300000)
47 let ob: *u8 = sys_mmap(1300000)
48 var pass: i64 = 0
49
50 pass = pass + ga_case("T1 /research/ == /" as *u8, "/research/" as *u8, "/" as *u8, oa, ob)
51 pass = pass + ga_case("T2 /research/api/works == /api/works" as *u8, "/research/api/works" as *u8, "/api/works" as *u8, oa, ob)
52 pass = pass + ga_case("T3 /research?q=cat == /?q=cat" as *u8, "/research?q=cat" as *u8, "/?q=cat" as *u8, oa, ob)
53 pass = pass + ga_case("T4 /research == /" as *u8, "/research" as *u8, "/" as *u8, oa, ob)
54
55 // NEG T5: /researchfoo must NOT alias (no greedy prefix) -> 404
56 let n5: i64 = lhd_response("/researchfoo" as *u8, 12, oa)
57 if ga_has404(oa, n5) == 1 { ga_puts(" [OK] NEG T5 /researchfoo -> 404\n" as *u8); pass = pass + 1 }
58 else { ga_puts(" [FAIL] NEG T5 /researchfoo did not 404\n" as *u8) }
59
60 // NEG T6: unrelated path still 404s
61 let n6: i64 = lhd_response("/bogus" as *u8, 6, oa)
62 if ga_has404(oa, n6) == 1 { ga_puts(" [OK] NEG T6 /bogus -> 404\n" as *u8); pass = pass + 1 }
63 else { ga_puts(" [FAIL] NEG T6 /bogus did not 404\n" as *u8) }
64
65 if pass == 6 { ga_puts("=== LIB-ALIAS-GATE verdict=GREEN 6/6 ===\n" as *u8); return 0 }
66 ga_puts("=== LIB-ALIAS-GATE verdict=RED ===\n" as *u8)
67 return 1
68}