nx_uxf_fetch_avro.nx source
↩ module page · 136 lines · 5597 B
1// nx_uxf_fetch_avro.nx -- UXF arc R1a: SOVEREIGN external fetch of the DECOUPLING
2// exemplar (Apache Avro: reader-vs-writer schema resolution = data decoupled from
3// the code that reads it, the Law-4 keystone of the Unified Extensible Format family).
4//
5// Clones the PROVEN nx_research_fetch_wiki_test HTTPS path verbatim
6// (nx_trust_store_load_from_certdata -> nx_https_url_for_fetch -> nx_https_url_connect
7// -> nx_tls13_client_session_run [real Mozilla CA chain, NishiBot UA]
8// -> nx_https_get_complete). ONLY the URL / GET path / output filename differ.
9//
10// Saves the raw HTTP response (header+body) to
11// knowledge/fetched/avro_decoupling_sovereign.raw (cwd = nxc2)
12// for the R1b render(nx_html_to_text)+extract(nx_research_extract) rung.
13// Sovereign: no browser, no node, no curl.
14//
15// expect_exit: 0 (200 OK)
16// license_tier: ORIGINAL
17
18import "nx_syscalls.nx"
19import "nx_csprng.nx"
20import "nx_x509_trust_store.nx"
21import "nx_trust_store_load_from_certdata.nx"
22import "nx_tls13_client_validate_certificate.nx"
23import "nx_tls13_client_session_run.nx"
24import "nx_https_url_for_fetch.nx"
25import "nx_https_url_connect.nx"
26import "nx_https_get.nx"
27import "nx_https_get_complete.nx"
28import "nx_http_response_parse.nx"
29const K_MAGIC_4194304: i64 = 4194304
30const K_MAGIC_2097152: i64 = 2097152
31
32func dump_dec(label0: i64, label1: i64, v: i64) -> i64 {
33 let lab: *u8 = sys_mmap(8)
34 lab[0] = label0 as u8; lab[1] = label1 as u8; lab[2] = 0x3D
35 sys_write(2, lab, 3)
36 var av: i64 = v
37 if av < 0 { let neg: *u8 = sys_mmap(8); neg[0]=0x2D; sys_write(2, neg, 1); av = 0 - av }
38 if av == 0 { let z: *u8 = sys_mmap(8); z[0]=0x30; sys_write(2, z, 1) }
39 else {
40 let buf: *u8 = sys_mmap(16)
41 var pos: i64 = 0
42 var x: i64 = av
43 while x > 0 { buf[pos] = (0x30 + (x % 10)) as u8; x = x / 10; pos = pos + 1 }
44 let out: *u8 = sys_mmap(16)
45 var oi: i64 = 0
46 while oi < pos { out[oi] = buf[pos - 1 - oi]; oi = oi + 1 }
47 sys_write(2, out, pos)
48 }
49 let nl: *u8 = sys_mmap(8); nl[0]=0x0A; sys_write(2, nl, 1)
50 return 0
51}
52
53func main() -> i64 {
54 // ---- Load real Mozilla certdata.txt into a TrustStore ----
55 let cpath: *u8 = "/tmp/mozilla_certdata.txt\x00"
56 let r: i64 = nx_trust_store_load_from_certdata(cpath, 512, K_MAGIC_4194304)
57 dump_dec(0x4C, 0x4F, r) // LO=
58 if r <= 0 { return 1 }
59 let store: *TrustStore = r as *TrustStore
60 let n: i64 = trust_store_count(store)
61 dump_dec(0x43, 0x41, n) // CA=
62 if n < 50 { return 3 }
63
64 // ---- Target: https://en.wikipedia.org/wiki/Apache_Avro (the decoupling exemplar) ----
65 let url: *u8 = "https://en.wikipedia.org/wiki/Apache_Avro\x00"
66
67 let cr: *u8 = sys_mmap(32)
68 var i: i64 = 0
69 nx_csprng_fill(cr, 32) // CWE-330 (debt 1785970852): was the constant 0xC0..0xDF
70 let priv: *u8 = sys_mmap(32)
71 i = 0
72 nx_csprng_fill(priv, 32) // CWE-330: the X25519 scalar was the constant 0xA0..0xBF on EVERY session
73
74 let url_p: *NxUrl = nx_url_new()
75 let target_raw: *u8 = sys_mmap(32)
76 let target: *NxHttpsTarget = target_raw as *NxHttpsTarget
77 target.url = url_p
78 target.port = 0
79 if nx_https_url_for_fetch(url, target) != NX_HTTPS_URL_OK { return 41 }
80
81 let fd_p: *i64 = sys_mmap(16) as *i64
82 if nx_https_url_connect(target, url, sys_now_realtime_sec(), fd_p) != NX_HTTPS_CONNECT_OK { return 42 }
83 let fd: i64 = *fd_p
84
85 let val_ctx_raw: *u8 = sys_mmap(64)
86 let val_ctx: *TlsValidationContext = val_ctx_raw as *TlsValidationContext
87 val_ctx.store = store
88 val_ctx.sni_host = url + target.url.host_off
89 val_ctx.sni_host_len = target.url.host_len
90 val_ctx.now_epoch = sys_now_realtime_sec()
91
92 let sr: i64 = nx_tls13_client_session_run(
93 fd, url + target.url.host_off, target.url.host_len,
94 cr, priv, val_ctx
95 )
96 dump_dec(0x53, 0x52, sr) // SR=
97 if sr <= 0 { sys_close(fd); return 200 + (0 - sr) }
98
99 let session: *Tls13ClientSession = sr as *Tls13ClientSession
100 let buf: *u8 = sys_mmap(K_MAGIC_2097152) // 2MB -- wiki articles are large
101 let path: *u8 = "/wiki/Apache_Avro\x00"
102 let gc: i64 = nx_https_get_complete(
103 session, fd, path, 17,
104 url + target.url.host_off, target.url.host_len,
105 buf, K_MAGIC_2097152
106 )
107 sys_close(fd)
108 dump_dec(0x47, 0x43, gc) // GC= total response bytes
109 if gc < 0 { return 100 + (0 - gc) }
110
111 // ---- Parse HTTP status ----
112 let rs: *i64 = sys_mmap(128) as *i64
113 let pv: i64 = nx_http_response_parse(buf, gc, rs)
114 dump_dec(0x50, 0x56, pv) // PV=
115 let status: i64 = rs[1]
116 dump_dec(0x53, 0x54, status) // ST= HTTP status
117 let body_off: i64 = rs[6]
118 let body_kind: i64 = rs[8]
119 dump_dec(0x42, 0x4F, body_off) // BO=
120 dump_dec(0x42, 0x4B, body_kind) // BK= (2=chunked)
121
122 // ---- Save the raw response (header+body) for the R1b render+extract rung ----
123 let outpath: *u8 = "knowledge/fetched/avro_decoupling_sovereign.raw\x00"
124 let ofd: i64 = sys_openat_wr(outpath, 0x1A4) // 0644
125 if ofd <= 0 { return 70 }
126 sys_write(ofd, buf, gc)
127 sys_close(ofd)
128
129 let banner: *u8 = "AVRO-SOVEREIGN-FETCH-OK\n\x00"
130 var bi: i64 = 0
131 while banner[bi] != (0 as u8) { bi = bi + 1 }
132 sys_write(1, banner, bi)
133
134 if status != 200 { return 51 }
135 return 0
136}