nx_pages_vhost_debug.nx source
↩ module page · 61 lines · 2682 B
1// nx_pages_vhost_debug.nx -- isolate failures step-by-step via distinct exit codes.
2
3import "nx_pages_vhost.nx"
4import "nx_http.nx"
5const K_MAGIC_2048: i64 = 2048
6
7func main() -> i64 {
8 let arena: *u8 = sys_mmap(K_MAGIC_2048)
9 let vhosts: *i64 = sys_mmap(4 * NX_VHOST_STRIDE * 8) as *i64
10 let table: *NxVHostTable = sys_mmap(40) as *NxVHostTable
11 let cursor: *i64 = sys_mmap(8) as *i64
12 cursor[0] = 0
13 nx_vhost_table_init(table, arena, K_MAGIC_2048, vhosts)
14
15 // Step 1: parse a request.
16 let req: *HttpRequest = sys_mmap(K_MAGIC_2048) as *HttpRequest
17 let raw1: *u8 = "GET / HTTP/1.1\r\nHost: nishifamily.com\r\n\r\n" as *u8
18 var n: i64 = 0
19 while raw1[n] != 0 { n = n + 1 }
20 // Manually run the request-line parse to isolate where it fails.
21 http_request_init(req)
22 let after_m: i64 = http_scan_token_end(raw1, 0 as i64, n)
23 if after_m != 3 { return 50 + after_m } // expect 3 ("GET")
24 let parse_rc: i64 = http_parse(raw1, n, req)
25 if parse_rc != 0 { return 0 - parse_rc + 100 } // exit 101-105 = which HTTP_ERR
26
27 // Step 2: check n_headers after parse.
28 if req.n_headers < 1 { return 11 } // exit 11: no headers parsed
29
30 // Step 3: lookup Host header value.
31 let host_off: *i64 = sys_mmap(8) as *i64
32 let host_len: *i64 = sys_mmap(8) as *i64
33 let has_host: i64 = http_get_header(raw1, req, "Host" as *u8, host_off, host_len)
34 if has_host != 1 { return 12 } // exit 12: Host header not found
35
36 // Step 4: verify host len == 15 (length of "nishifamily.com").
37 if host_len[0] != 15 { return 13 } // exit 13: wrong host length
38
39 // Step 5: add a vhost; check the index.
40 let i_nf: i64 = nx_vhost_add(table, cursor,
41 "nishifamily.com" as *u8,
42 "/share/nishi-pages/dist" as *u8)
43 if i_nf != 0 { return 14 } // exit 14: add returned wrong index
44
45 // Step 6: check n_vhosts after add.
46 if table.n_vhosts != 1 { return 15 } // exit 15
47
48 // Step 7: verify the stored arena bytes match the input host name.
49 if table.vhosts[NX_VHOST_HNAME_LEN] != 15 { return 16 } // exit 16
50
51 // Step 8: dispatch.
52 let out_off: *i64 = sys_mmap(8) as *i64
53 let out_len: *i64 = sys_mmap(8) as *i64
54 let v: i64 = nx_vhost_dispatch(raw1, req, table, out_off, out_len)
55 if v != NX_VHOST_OK { return 20 + v } // exit 20-24 = which verdict
56
57 // Step 9: returned len matches doc root len = 23.
58 if out_len[0] != 23 { return 30 } // exit 30
59
60 return 0 // all steps OK
61}