nx_http_proxy_test.nx source
↩ module page · 53 lines · 2369 B
1// nx_http_proxy_test.nx -- gate the wired reverse-proxy: GET request-line
2// rewrite (strip /research prefix, force HTTP/1.0 close, preserve query) +
3// dotted-quad IP parse. The forward/relay is exercised live in the sites edge.
4import "nx_syscalls.nx"
5import "nx_http_proxy.nx"
6
7func pt_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
8func pt_has(hay: *u8, hayn: i64, needle: *u8) -> i64 {
9 let nn: i64 = pt_slen(needle)
10 if nn == 0 { return 1 }
11 if hayn < nn { return 0 }
12 let last: i64 = hayn - nn
13 var i: i64 = 0
14 while i <= last {
15 var j: i64 = 0; var st: i64 = 0
16 while st == 0 { if j >= nn { st = 2 } if st == 0 { if hay[i+j] != needle[j] { st = 1 } if st == 0 { j = j + 1 } } }
17 if st == 2 { return 1 }
18 i = i + 1
19 }
20 return 0
21}
22
23func main() -> i64 {
24 let out: *u8 = sys_mmap(8192)
25
26 let r1: *u8 = "GET /research/work/GBIF_1000003 HTTP/1.1\r\nHost: nishifamily.com\r\n\r\n" as *u8
27 let n1: i64 = nx_http_proxy_rewrite(r1, pt_slen(r1), "/research" as *u8, 9, out)
28 if pt_has(out, n1, "GET /work/GBIF_1000003 HTTP/1.0" as *u8) != 1 { return 1 }
29 if pt_has(out, n1, "Connection: close" as *u8) != 1 { return 2 }
30
31 let r2: *u8 = "GET /research/?q=Archaea HTTP/1.1\r\n\r\n" as *u8
32 let n2: i64 = nx_http_proxy_rewrite(r2, pt_slen(r2), "/research" as *u8, 9, out)
33 if pt_has(out, n2, "GET /?q=Archaea HTTP/1.0" as *u8) != 1 { return 3 }
34
35 let r3: *u8 = "GET /research HTTP/1.1\r\n\r\n" as *u8
36 let n3: i64 = nx_http_proxy_rewrite(r3, pt_slen(r3), "/research" as *u8, 9, out)
37 if pt_has(out, n3, "GET / HTTP/1.0" as *u8) != 1 { return 4 }
38
39 // non-matching prefix -> path passed through (just protocol downgrade)
40 let r4: *u8 = "GET /api/search?q=x HTTP/1.1\r\n\r\n" as *u8
41 let n4: i64 = nx_http_proxy_rewrite(r4, pt_slen(r4), "/research" as *u8, 9, out)
42 if pt_has(out, n4, "GET /api/search?q=x HTTP/1.0" as *u8) != 1 { return 5 }
43
44 let ip: *i64 = sys_mmap(64) as *i64
45 if nxp_parse_ip("127.0.0.1" as *u8, 9, ip) != 1 { return 6 }
46 if ip[0] != 127 { return 7 }
47 if ip[1] != 0 { return 8 }
48 if ip[3] != 1 { return 9 }
49
50 let msg: *u8 = "nx_http_proxy: 9/9 sovereign reverse-proxy PASS (GET rewrite + strip prefix + IP parse; nginx proxy_pass retired)\n" as *u8
51 sys_write(1, msg, pt_slen(msg))
52 return 0
53}