nx_https_fetch.nx source
↩ module page · 80 lines · 3945 B
1// nx_https_fetch.nx -- sovereign authed / extra-header HTTPS GET, REUSABLE primitive (LangIntel oracle lever).
2// 100% our stack: OUR TLS 1.3 client + trust store + nx_https_req_complete send/recv core (the SAME path as
3// nx_https_get). The ONLY delta vs the plain fetch is caller-supplied extra request headers (Authorization,
4// Accept, ...), built via nx_http_client_build_request_xhdr. NO curl, NO graphql, NO libc, NO fetch shim.
5// nx_https_get_cli queued to adopt this to dedup its inline TLS setup (coordinated DRY consolidation rung).
6// license_tier: ORIGINAL
7import "nx_syscalls.nx"
8import "nx_csprng.nx"
9import "nx_x509_trust_store.nx"
10import "nx_trust_store_load_from_certdata.nx"
11import "nx_tls13_client_validate_certificate.nx"
12import "nx_tls13_client_session_run.nx"
13import "nx_https_url_for_fetch.nx"
14import "nx_https_url_connect.nx"
15import "nx_https_get_complete.nx"
16import "nx_tls_cert_cache.nx"
17const HF_MAGIC_4194304: i64 = 4194304
18const HF_MAGIC_1024: i64 = 1024
19
20const HF_CERTDATA: *u8 = "data/mozilla_certdata.txt" as *u8
21
22// Fetch `url` over sovereign TLS, injecting `xhdr` (CRLF-terminated header line(s), xhdr_len bytes; 0 = none)
23// into the GET. Response (status+headers+body) accumulated into out (cap out_cap). Returns bytes>0 on success
24// or a NEGATIVE verdict: -2 bad-url -3 trust/connect -4 tls-handshake (mirrors nx_https_get's own codes).
25func nx_https_fetch_hdr(url: *u8, xhdr: *u8, xhdr_len: i64, out: *u8, out_cap: i64) -> i64 {
26 let now: i64 = sys_now_realtime_sec()
27 let r: i64 = nx_trust_store_load_from_certdata(HF_CERTDATA, 512, HF_MAGIC_4194304)
28 if r <= 0 { return 0 - 3 }
29 let store: *TrustStore = r as *TrustStore
30 let cr: *u8 = sys_mmap(32)
31 nx_csprng_fill(cr, 32)
32 let priv: *u8 = sys_mmap(32)
33 nx_csprng_fill(priv, 32)
34 let url_p: *NxUrl = nx_url_new()
35 let target_raw: *u8 = sys_mmap(32)
36 let target: *NxHttpsTarget = target_raw as *NxHttpsTarget
37 target.url = url_p
38 target.port = 0
39 if nx_https_url_for_fetch(url, target) != NX_HTTPS_URL_OK { return 0 - 2 }
40 let fd_p: *i64 = sys_mmap(16) as *i64
41 if nx_https_url_connect(target, url, now, fd_p) != NX_HTTPS_CONNECT_OK { return 0 - 3 }
42 let fd: i64 = fd_p[0]
43 let val_raw: *u8 = sys_mmap(128)
44 let val_ctx: *TlsValidationContext = val_raw as *TlsValidationContext
45 val_ctx.store = store
46 val_ctx.sni_host = url + target.url.host_off
47 val_ctx.sni_host_len = target.url.host_len
48 val_ctx.now_epoch = now
49 let tcc_hit: i64 = tcc_load(url + target.url.host_off, target.url.host_len, now, val_ctx)
50 tcc_arm(val_ctx)
51 let sr: i64 = nx_tls13_client_session_run(fd, url + target.url.host_off, target.url.host_len, cr, priv, val_ctx)
52 if sr < 0 { sys_close(fd); return 0 - 4 }
53 let session: *Tls13ClientSession = sr as *Tls13ClientSession
54 tcc_save(url + target.url.host_off, target.url.host_len, now, val_ctx)
55 var path_ptr: *u8 = url + target.url.path_off
56 var path_len: i64 = target.url.path_len
57 if path_len == 0 {
58 let dp: *u8 = sys_mmap(2)
59 dp[0] = 47 as u8
60 path_ptr = dp
61 path_len = 1
62 }
63 if target.url.query_len > 0 {
64 let full: *u8 = sys_mmap(path_len + target.url.query_len + 4)
65 var fo: i64 = 0
66 var pci: i64 = 0
67 while pci < path_len { full[fo] = path_ptr[pci]; fo = fo + 1; pci = pci + 1 }
68 full[fo] = 63 as u8; fo = fo + 1
69 let qp: *u8 = url + target.url.query_off
70 var qci: i64 = 0
71 while qci < target.url.query_len { full[fo] = qp[qci]; fo = fo + 1; qci = qci + 1 }
72 path_ptr = full
73 path_len = fo
74 }
75 let req: *u8 = sys_mmap(path_len + xhdr_len + HF_MAGIC_1024)
76 let req_len: i64 = nx_http_client_build_request_xhdr(path_ptr, path_len, url + target.url.host_off, target.url.host_len, xhdr, xhdr_len, req)
77 let n: i64 = nx_https_req_complete(session, fd, req, req_len, out, out_cap)
78 sys_close(fd)
79 return n
80}