nx_research_fetch_wiki_test.nx source
↩ module page · 133 lines · 5302 B
1// nx_research_fetch_wiki_test.nx -- SOVEREIGN research-target fetch.
2//
3// B3 (research pipeline): re-point the loop off the Playwright benchmark
4// onto the team's OWN sovereign HTTPS stack. Fetches the SAME page the
5// Playwright benchmark used -- en.wikipedia.org/wiki/Web_development --
6// over nx_tls13_client_session_run + nx_https_get_complete (no browser,
7// no node, no curl), validated against the real Mozilla CA store, with
8// the team's identified User-Agent (NishiBot, already in the request
9// builder -- Wikimedia 403s anonymous clients).
10//
11// Saves the raw HTTP response (header+body) to
12// knowledge/fetched/wiki_web_dev_sovereign.raw (cwd = nxc2)
13// for the next rung (html->text -> ce_extract -> census -> pe14).
14//
15// expect_exit: 0 (200 OK)
16// license_tier: ORIGINAL
17
18import "nx_syscalls.nx"
19import "nx_x509_trust_store.nx"
20import "nx_trust_store_load_from_certdata.nx"
21import "nx_tls13_client_validate_certificate.nx"
22import "nx_tls13_client_session_run.nx"
23import "nx_https_url_for_fetch.nx"
24import "nx_https_url_connect.nx"
25import "nx_https_get.nx"
26import "nx_https_get_complete.nx"
27import "nx_http_response_parse.nx"
28
29func dump_dec(label0: i64, label1: i64, v: i64) -> i64 {
30 let lab: *u8 = sys_mmap(8)
31 lab[0] = label0 as u8; lab[1] = label1 as u8; lab[2] = 0x3D
32 sys_write(2, lab, 3)
33 var av: i64 = v
34 if av < 0 { let neg: *u8 = sys_mmap(8); neg[0]=0x2D; sys_write(2, neg, 1); av = 0 - av }
35 if av == 0 { let z: *u8 = sys_mmap(8); z[0]=0x30; sys_write(2, z, 1) }
36 else {
37 let buf: *u8 = sys_mmap(16)
38 var pos: i64 = 0
39 var x: i64 = av
40 while x > 0 { buf[pos] = (0x30 + (x % 10)) as u8; x = x / 10; pos = pos + 1 }
41 let out: *u8 = sys_mmap(16)
42 var oi: i64 = 0
43 while oi < pos { out[oi] = buf[pos - 1 - oi]; oi = oi + 1 }
44 sys_write(2, out, pos)
45 }
46 let nl: *u8 = sys_mmap(8); nl[0]=0x0A; sys_write(2, nl, 1)
47 return 0
48}
49
50func main() -> i64 {
51 // ---- Load real Mozilla certdata.txt into a TrustStore ----
52 let cpath: *u8 = "/tmp/mozilla_certdata.txt\x00"
53 let r: i64 = nx_trust_store_load_from_certdata(cpath, 512, 4194304)
54 dump_dec(0x4C, 0x4F, r) // LO=
55 if r <= 0 { return 1 }
56 let store: *TrustStore = r as *TrustStore
57 let n: i64 = trust_store_count(store)
58 dump_dec(0x43, 0x41, n) // CA=
59 if n < 50 { return 3 }
60
61 // ---- Target: https://en.wikipedia.org/wiki/Web_development ----
62 let url: *u8 = "https://en.wikipedia.org/wiki/Web_development\x00"
63
64 let cr: *u8 = sys_mmap(32)
65 var i: i64 = 0
66 while i < 32 { cr[i] = (0xC0 + i) as u8; i = i + 1 }
67 let priv: *u8 = sys_mmap(32)
68 i = 0
69 while i < 32 { priv[i] = (0xA0 + i) as u8; i = i + 1 }
70
71 let url_p: *NxUrl = nx_url_new()
72 let target_raw: *u8 = sys_mmap(32)
73 let target: *NxHttpsTarget = target_raw as *NxHttpsTarget
74 target.url = url_p
75 target.port = 0
76 if nx_https_url_for_fetch(url, target) != NX_HTTPS_URL_OK { return 41 }
77
78 let fd_p: *i64 = sys_mmap(16) as *i64
79 if nx_https_url_connect(target, url, sys_now_realtime_sec(), fd_p) != NX_HTTPS_CONNECT_OK { return 42 }
80 let fd: i64 = *fd_p
81
82 let val_ctx_raw: *u8 = sys_mmap(64)
83 let val_ctx: *TlsValidationContext = val_ctx_raw as *TlsValidationContext
84 val_ctx.store = store
85 val_ctx.sni_host = url + target.url.host_off
86 val_ctx.sni_host_len = target.url.host_len
87 val_ctx.now_epoch = sys_now_realtime_sec()
88
89 let sr: i64 = nx_tls13_client_session_run(
90 fd, url + target.url.host_off, target.url.host_len,
91 cr, priv, val_ctx
92 )
93 dump_dec(0x53, 0x52, sr) // SR=
94 if sr <= 0 { sys_close(fd); return 200 + (0 - sr) }
95
96 let session: *Tls13ClientSession = sr as *Tls13ClientSession
97 let buf: *u8 = sys_mmap(2097152) // 2MB -- wiki articles are large
98 let path: *u8 = "/wiki/Web_development\x00"
99 let gc: i64 = nx_https_get_complete(
100 session, fd, path, 21,
101 url + target.url.host_off, target.url.host_len,
102 buf, 2097152
103 )
104 sys_close(fd)
105 dump_dec(0x47, 0x43, gc) // GC= total response bytes
106 if gc < 0 { return 100 + (0 - gc) }
107
108 // ---- Parse HTTP status ----
109 let rs: *i64 = sys_mmap(128) as *i64
110 let pv: i64 = nx_http_response_parse(buf, gc, rs)
111 dump_dec(0x50, 0x56, pv) // PV=
112 let status: i64 = rs[1]
113 dump_dec(0x53, 0x54, status) // ST= HTTP status
114 let body_off: i64 = rs[6]
115 let body_kind: i64 = rs[8]
116 dump_dec(0x42, 0x4F, body_off) // BO=
117 dump_dec(0x42, 0x4B, body_kind) // BK= (2=chunked)
118
119 // ---- Save the raw response (header+body) for the next rung ----
120 let outpath: *u8 = "knowledge/fetched/wiki_web_dev_sovereign.raw\x00"
121 let ofd: i64 = sys_openat_wr(outpath, 0x1A4) // 0644
122 if ofd <= 0 { return 70 }
123 sys_write(ofd, buf, gc)
124 sys_close(ofd)
125
126 let banner: *u8 = "WIKI-SOVEREIGN-FETCH-OK\n\x00"
127 var bi: i64 = 0
128 while banner[bi] != (0 as u8) { bi = bi + 1 }
129 sys_write(1, banner, bi)
130
131 if status != 200 { return 51 }
132 return 0
133}